You've already forked obsidian-visualiser
First reworks
This commit is contained in:
@@ -1,89 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { schema } from '~/schemas/login';
|
||||
import { User, UserExtendedData, UserRawData, UserSession, UserSessionRequired } from '~/types/auth';
|
||||
import type { Database } from "bun:sqlite";
|
||||
import { ZodError } from 'zod';
|
||||
import { checkSession, logSession } from '~/server/utils/user';
|
||||
|
||||
interface SuccessHandler
|
||||
{
|
||||
success: true;
|
||||
session: UserSession;
|
||||
}
|
||||
interface ErrorHandler
|
||||
{
|
||||
success: false;
|
||||
error: Error | ZodError<{
|
||||
usernameOrEmail: string;
|
||||
password: string;
|
||||
}>;
|
||||
}
|
||||
type Return = SuccessHandler | ErrorHandler;
|
||||
|
||||
export default defineEventHandler(async (e): Promise<Return> => {
|
||||
try
|
||||
{
|
||||
const session = await getUserSession(e);
|
||||
const db = useDatabase();
|
||||
|
||||
const checkedSession = await checkSession(e, session);
|
||||
|
||||
if(checkedSession !== undefined)
|
||||
return checkedSession;
|
||||
|
||||
const body = await readValidatedBody(e, schema.safeParse);
|
||||
|
||||
if (!body.success)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: body.error };
|
||||
}
|
||||
|
||||
const hash = await Bun.password.hash(body.data.password);
|
||||
const checkID = db.query(`SELECT id, hash FROM users WHERE (username = ?1 or email = ?1)`);
|
||||
const id = checkID.get(body.data.usernameOrEmail) as { id: number, hash: string };
|
||||
|
||||
if(!id || !id.id || !id.hash)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 401);
|
||||
return { success: false, error: new ZodError([{ code: 'custom', path: ['username'], message: 'Identifiant inconnu' }]) };
|
||||
}
|
||||
|
||||
const valid = await Bun.password.verify(body.data.password, id.hash);
|
||||
|
||||
if(!valid)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 401);
|
||||
return { success: false, error: new ZodError([{ code: 'custom', path: ['password'], message: 'Mot de passe incorrect' }]) };
|
||||
}
|
||||
|
||||
const data = await logSession(e, await setUserSession(e, { user: getData(db, id.id) }) as UserSessionRequired);
|
||||
|
||||
setResponseStatus(e, 201);
|
||||
return { success: true, session: data };
|
||||
}
|
||||
catch(err: any)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
console.error(err);
|
||||
return { success: false, error: err as Error };
|
||||
}
|
||||
});
|
||||
|
||||
function getData(db: Database, id: number): User
|
||||
{
|
||||
const userQuery = db.query(`SELECT id, username, email, state FROM users WHERE id = ?1`);
|
||||
const user = userQuery.get(id) as UserRawData;
|
||||
|
||||
const userDataQuery = db.query(`SELECT * FROM users_data WHERE user_id = ?1`);
|
||||
const userData = userDataQuery.get(id) as UserExtendedData;
|
||||
|
||||
return { ...user, ...userData };
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
import { ZodError, ZodIssue } from 'zod';
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { schema } from '~/schemas/registration';
|
||||
import { checkSession, logSession } from '~/server/utils/user';
|
||||
import { UserSession, UserSessionRequired } from '~/types/auth';
|
||||
|
||||
interface SuccessHandler
|
||||
{
|
||||
success: true;
|
||||
session: UserSession;
|
||||
}
|
||||
interface ErrorHandler
|
||||
{
|
||||
success: false;
|
||||
error: Error | ZodError<{
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}>;
|
||||
}
|
||||
type Return = SuccessHandler | ErrorHandler;
|
||||
|
||||
export default defineEventHandler(async (e): Promise<Return> => {
|
||||
try
|
||||
{
|
||||
const session = await getUserSession(e);
|
||||
const db = useDatabase();
|
||||
|
||||
const checkedSession = await checkSession(e, session);
|
||||
|
||||
if(checkedSession !== undefined)
|
||||
return checkedSession;
|
||||
|
||||
const body = await readValidatedBody(e, schema.safeParse);
|
||||
|
||||
if (!body.success)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: body.error };
|
||||
}
|
||||
|
||||
const usernameQuery = db.query(`SELECT COUNT(*) as count FROM users WHERE username = ?1`);
|
||||
const checkUsername = usernameQuery.get(body.data.username) as any;
|
||||
|
||||
const emailQuery = db.query(`SELECT COUNT(*) as count FROM users WHERE email = ?1`);
|
||||
const checkEmail = emailQuery.get(body.data.email) as any;
|
||||
|
||||
const errors: ZodIssue[] = [];
|
||||
if(checkUsername.count !== 0)
|
||||
errors.push({ code: 'custom', path: ['username'], message: "Ce nom d'utilisateur est déjà utilisé" });
|
||||
if(checkEmail.count !== 0)
|
||||
errors.push({ code: 'custom', path: ['email'], message: "Cette adresse mail est déjà utilisée" });
|
||||
|
||||
if(errors.length > 0)
|
||||
{
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: new ZodError(errors) };
|
||||
}
|
||||
else
|
||||
{
|
||||
const hash = await Bun.password.hash(body.data.password);
|
||||
const registration = db.query(`INSERT INTO users(username, email, hash, state) VALUES(?1, ?2, ?3, 0)`);
|
||||
registration.run(body.data.username, body.data.email, hash);
|
||||
|
||||
const userIdQuery = db.query(`SELECT id FROM users WHERE username = ?1`);
|
||||
const id = (userIdQuery.get(body.data.username) as any).id;
|
||||
|
||||
const registeringData = db.query(`INSERT INTO users_data(user_id, signin_timestamp) VALUES(?1, ?2)`);
|
||||
registeringData.run(id, Date.now());
|
||||
|
||||
logSession(e, await setUserSession(e, { user: { id: id, username: body.data.username, email: body.data.email, state: 0 } }) as UserSessionRequired);
|
||||
|
||||
setResponseStatus(e, 201);
|
||||
return { success: true, session };
|
||||
}
|
||||
}
|
||||
catch(err: any)
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
console.error(err);
|
||||
return { success: false, error: err as Error };
|
||||
}
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
import { eventHandler } from 'h3';
|
||||
import { clearUserSession } from '~/server/utils/session';
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
await clearUserSession(event);
|
||||
|
||||
return { loggedOut: true };
|
||||
})
|
||||
@@ -1,13 +0,0 @@
|
||||
import { eventHandler } from 'h3'
|
||||
import { getUserSession, sessionHooks } from '~/server/utils/session'
|
||||
import type { UserSessionRequired } from '~/types/auth'
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const session = await getUserSession(event)
|
||||
|
||||
if (session.user) {
|
||||
await sessionHooks.callHookParallel('fetch', session as UserSessionRequired, event)
|
||||
}
|
||||
|
||||
return session
|
||||
})
|
||||
@@ -1,31 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { ProjectSearch } from '~/types/api';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const query = getQuery(e);
|
||||
|
||||
const where = ["f.type != $type"];
|
||||
const criteria: Record<string, any> = { $type: "Folder" };
|
||||
|
||||
if(query && query.owner !== undefined)
|
||||
{
|
||||
where.push("owner = $owner");
|
||||
criteria["$owner"] = query.owner;
|
||||
}
|
||||
if(query && query.name !== undefined)
|
||||
{
|
||||
where.push("name = $name");
|
||||
criteria["$name"] = query.name;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT p.*, u.username, COUNT(f.path) as pages FROM explorer_projects p LEFT JOIN users u ON p.owner = u.id LEFT JOIN explorer_files f ON f.project = p.id WHERE ${where.join(" AND ")} GROUP BY p.id`).all(criteria) as ProjectSearch[];
|
||||
|
||||
if(content.length > 0)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const project = getRouterParam(e, "projectId");
|
||||
|
||||
const where = ["id = $id"];
|
||||
const criteria: Record<string, any> = { $id: project };
|
||||
|
||||
if (!!project) {
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).get(criteria) as Project;
|
||||
|
||||
if (content) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const project = getRouterParam(e, "projectId");
|
||||
|
||||
const where = ["project = $project"];
|
||||
const criteria: Record<string, any> = { $project: project };
|
||||
|
||||
if (!!project) {
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).all(criteria) as Project[];
|
||||
|
||||
if (content.length > 0) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const project = getRouterParam(e, "projectId");
|
||||
|
||||
const where = ["project = $project"];
|
||||
const criteria: Record<string, any> = { $project: project };
|
||||
|
||||
if (!!project) {
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT * FROM explorer_projects WHERE ${where.join(" and ")}`).all(criteria) as Project[];
|
||||
|
||||
if (content.length > 0) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
@@ -1,54 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import type { File } from '~/types/api';
|
||||
|
||||
export default defineCachedEventHandler(async (e) => {
|
||||
const project = getRouterParam(e, "projectId");
|
||||
const query = getQuery(e);
|
||||
|
||||
if(!project)
|
||||
{
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}
|
||||
|
||||
const where = ["project = $project"];
|
||||
const criteria: Record<string, any> = { $project: project };
|
||||
|
||||
if(query && query.path !== undefined)
|
||||
{
|
||||
where.push("path = $path");
|
||||
criteria["$path"] = query.path;
|
||||
}
|
||||
if(query && query.title !== undefined)
|
||||
{
|
||||
where.push("title = $title");
|
||||
criteria["$title"] = query.title;
|
||||
}
|
||||
if(query && query.type !== undefined)
|
||||
{
|
||||
where.push("type = $type");
|
||||
criteria["$type"] = query.type;
|
||||
}
|
||||
if (query && query.search !== undefined)
|
||||
{
|
||||
where.push("path LIKE $search");
|
||||
criteria["$search"] = query.search;
|
||||
}
|
||||
|
||||
if(where.length > 1)
|
||||
{
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT * FROM explorer_files WHERE ${where.join(" and ")}`).all(criteria) as File[];
|
||||
|
||||
if(content.length > 0)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
}, {
|
||||
maxAge: 60*60*24,
|
||||
getKey: (e) => `${getRouterParam(e, "projectId")}-${JSON.stringify(getQuery(e))}`
|
||||
});
|
||||
@@ -1,41 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import type { CommentedFile, CommentSearch, File } from '~/types/api';
|
||||
|
||||
export default defineCachedEventHandler(async (e) => {
|
||||
const project = getRouterParam(e, "projectId");
|
||||
const path = decodeURIComponent(getRouterParam(e, "path") ?? '');
|
||||
|
||||
if(!project)
|
||||
{
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}
|
||||
if(!path)
|
||||
{
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}
|
||||
|
||||
const where = ["project = $project", "path = $path"];
|
||||
const criteria: Record<string, any> = { $project: project, $path: path };
|
||||
|
||||
if(where.length > 1)
|
||||
{
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT * FROM explorer_files WHERE ${where.join(" and ")}`).get(criteria) as File;
|
||||
|
||||
if(content !== undefined)
|
||||
{
|
||||
const comments = db.query(`SELECT comment.*, user.username FROM explorer_comments comment LEFT JOIN users user ON comment.user_id = user.id WHERE ${where.join(" and ")}`).all(criteria) as CommentSearch[];
|
||||
|
||||
return { ...content, comments } as CommentedFile;
|
||||
}
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}, {
|
||||
maxAge: 60*60*24,
|
||||
getKey: (e) => `file-${getRouterParam(e, "projectId")}-${getRouterParam(e, "path")}`
|
||||
});
|
||||
@@ -1,72 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { Navigation } from '~/types/api';
|
||||
|
||||
type NavigatioNExtension = Navigation & { owner: number, navigable: boolean };
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const project = getRouterParam(e, "projectId");
|
||||
const { user } = await getUserSession(e);
|
||||
|
||||
if(!project)
|
||||
{
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
const content = db.query(`SELECT "path", "title", "type", "order", "private", "navigable", "owner" FROM explorer_files WHERE project = ?1`).all(project!).sort((a: any, b: any) => a.path.length - b.path.length) as NavigatioNExtension[];
|
||||
|
||||
if(content.length > 0)
|
||||
{
|
||||
const navigation: Navigation[] = [];
|
||||
|
||||
for(const idx in content)
|
||||
{
|
||||
const item = content[idx];
|
||||
if(!!item.private && (user?.id ?? -1) !== item.owner || !item.navigable)
|
||||
{
|
||||
delete content[idx];
|
||||
continue;
|
||||
}
|
||||
|
||||
const parent = item.path.includes('/') ? item.path.substring(0, item.path.lastIndexOf('/')) : undefined;
|
||||
|
||||
if(parent && !content.find(e => e && e.path === parent))
|
||||
{
|
||||
delete content[idx];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
for(const item of content.filter(e => !!e))
|
||||
{
|
||||
addChild(navigation, item);
|
||||
}
|
||||
|
||||
return navigation;
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
|
||||
function addChild(arr: Navigation[], e: Navigation): void
|
||||
{
|
||||
const parent = arr.find(f => e.path.startsWith(f.path));
|
||||
|
||||
if(parent)
|
||||
{
|
||||
if(!parent.children)
|
||||
parent.children = [];
|
||||
|
||||
addChild(parent.children, e);
|
||||
}
|
||||
else
|
||||
{
|
||||
arr.push({ title: e.title, path: e.path, type: e.type, order: e.order, private: e.private });
|
||||
arr.sort((a, b) => {
|
||||
if(a.order && b.order)
|
||||
return a.order - b.order;
|
||||
return a.title.localeCompare(b.title);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import type { Tag } from '~/types/api';
|
||||
|
||||
export default defineCachedEventHandler(async (e) => {
|
||||
try
|
||||
{
|
||||
const project = getRouterParam(e, "projectId");
|
||||
const tag = decodeURIComponent(getRouterParam(e, "tag") ?? '');
|
||||
|
||||
if(!project)
|
||||
{
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}
|
||||
if(!tag)
|
||||
{
|
||||
setResponseStatus(e, 404);
|
||||
return;
|
||||
}
|
||||
|
||||
const where = ["project = $project", "tag = $tag"];
|
||||
const criteria: Record<string, any> = { $project: project, $tag: tag };
|
||||
|
||||
const db = useDatabase();
|
||||
const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag;
|
||||
|
||||
if(content !== undefined)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
console.error(err);
|
||||
setResponseStatus(e, 500);
|
||||
}
|
||||
}, {
|
||||
maxAge: 60*60*24,
|
||||
getKey: (e) => `tag-${getRouterParam(e, "projectId")}-${getRouterParam(e, "tag")}`
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const query = getQuery(e);
|
||||
|
||||
if (query.search) {
|
||||
const db = useDatabase();
|
||||
|
||||
const projects = db.query(`SELECT p.*, u.username, COUNT(f.path) as pages FROM explorer_projects p LEFT JOIN users u ON p.owner = u.id LEFT JOIN explorer_files f ON f.project = p.id WHERE name LIKE ?1 AND f.type != "Folder" GROUP BY p.id`).all(query.search) as ProjectSearch[];
|
||||
const files = db.query(`SELECT f.*, u.username, count(c.path) as comments FROM explorer_files f LEFT JOIN users u ON f.owner = u.id LEFT JOIN explorer_comments c ON c.project = f.project AND c.path = f.path WHERE title LIKE ?1 AND private = 0 AND type != "Folder" GROUP BY f.project, f.path`).all(query.search) as FileSearch[];
|
||||
const users = db.query(`SELECT id, username FROM users WHERE username LIKE ?1`).all(query.search) as UserSearch[];
|
||||
|
||||
return {
|
||||
projects,
|
||||
files,
|
||||
users
|
||||
} as Search;
|
||||
}
|
||||
|
||||
setResponseStatus(e, 404);
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import type { User } from "~/types/auth";
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const id = getRouterParam(e, 'id');
|
||||
|
||||
if(!id)
|
||||
{
|
||||
setResponseStatus(e, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
return db.query(`SELECT id, username, email, state, d.* FROM users u LEFT JOIN users_data d ON u.id = d.user_id WHERE u.id = ?1`).get(id) as User;
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import type { CommentSearch } from "~/types/api";
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const id = getRouterParam(e, 'id');
|
||||
|
||||
if(!id)
|
||||
{
|
||||
setResponseStatus(e, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
return db.query(`SELECT * FROM explorer_comments WHERE user_id = ?1`).all(id) as CommentSearch[];
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import type { ProjectSearch } from "~/types/api";
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
const id = getRouterParam(e, 'id');
|
||||
|
||||
if(!id)
|
||||
{
|
||||
setResponseStatus(e, 400);
|
||||
return;
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
return db.query(`SELECT p.*, count(f.path) as pages FROM explorer_projects p LEFT JOIN explorer_files f ON p.id = f.project WHERE p.owner = ?1`).all(id) as Omit<ProjectSearch, 'username'>[];
|
||||
});
|
||||
@@ -1,38 +0,0 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
|
||||
const monthAsMs = 60 * 60 * 24 * 30;
|
||||
|
||||
export default defineNitroPlugin(() => {
|
||||
const db = useDatabase();
|
||||
|
||||
sessionHooks.hook('fetch', async (session, event) => {
|
||||
const query = db.prepare('SELECT last_refresh FROM user_sessions WHERE id = ?1 AND user_id = ?2');
|
||||
const result = query.get(session.id, session.user.id) as { last_refresh: number };
|
||||
|
||||
if(!result)
|
||||
{
|
||||
clearUserSession(event);
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' });
|
||||
}
|
||||
else if(result && result.last_refresh && result.last_refresh < Date.now() - monthAsMs)
|
||||
{
|
||||
clearUserSession(event);
|
||||
throw createError({ statusCode: 401, message: 'Session has expired' });
|
||||
}
|
||||
else
|
||||
{
|
||||
db.prepare('UPDATE user_sessions SET last_refresh = ?1 WHERE id = ?2 AND user_id = ?3').run(Date.now(), session.id, session.user.id);
|
||||
}
|
||||
});
|
||||
sessionHooks.hook('clear', async (session, event) => {
|
||||
if(session.id && session.user)
|
||||
{
|
||||
try
|
||||
{
|
||||
const query = db.prepare('DELETE FROM user_sessions WHERE id = ?1 AND user_id = ?2');
|
||||
query.run(session.id, session.user.id);
|
||||
}
|
||||
catch(e) { }
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,165 +0,0 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import { extname, basename } from 'node:path';
|
||||
import type { File, FileType, Tag } from '~/types/api';
|
||||
import { CanvasColor, CanvasContent } from "~/types/canvas";
|
||||
import useMarkdown from "~/composables/useMarkdown";
|
||||
|
||||
const typeMapping: Record<string, FileType> = {
|
||||
".md": "Markdown",
|
||||
".canvas": "Canvas"
|
||||
};
|
||||
|
||||
export default defineTask({
|
||||
meta: {
|
||||
name: 'sync',
|
||||
description: 'Synchronise the project 1 with Obsidian',
|
||||
},
|
||||
async run(event) {
|
||||
try {
|
||||
const tree = await $fetch('https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/git/trees/master', {
|
||||
method: 'get',
|
||||
headers: {
|
||||
accept: 'application/json',
|
||||
},
|
||||
params: {
|
||||
recursive: true,
|
||||
per_page: 1000,
|
||||
}
|
||||
}) as any;
|
||||
|
||||
const files: File[] = await Promise.all(tree.tree.filter((e: any) => !e.path.startsWith(".")).map(async (e: any) => {
|
||||
if(e.type === 'tree')
|
||||
{
|
||||
const title = basename(e.path);
|
||||
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
|
||||
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
|
||||
return {
|
||||
path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
|
||||
order: order && order[1] ? order[1] : 50,
|
||||
title: order && order[2] ? order[2] : title,
|
||||
type: 'Folder',
|
||||
content: null
|
||||
}
|
||||
}
|
||||
|
||||
const extension = extname(e.path);
|
||||
const title = basename(e.path, extension);
|
||||
const order = /(\d+)\. ?(.+)/gsmi.exec(title);
|
||||
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
|
||||
const content = (await $fetch(`https://git.peaceultime.com/api/v1/repos/peaceultime/system-aspect/raw/${encodeURIComponent(e.path)}`));
|
||||
|
||||
return {
|
||||
path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
|
||||
order: order && order[1] ? order[1] : 50,
|
||||
title: order && order[2] ? order[2] : title,
|
||||
type: (typeMapping[extension] ?? 'File'),
|
||||
content: reshapeContent(content as string, typeMapping[extension] ?? 'File')
|
||||
}
|
||||
}));
|
||||
|
||||
let tags: Tag[] = [];
|
||||
const tagFile = files.find(e => e.path === "tags");
|
||||
|
||||
if(tagFile)
|
||||
{
|
||||
const parsed = useMarkdown()(tagFile.content);
|
||||
const titles = parsed.children.filter(e => e.type === 'element' && e.tagName.match(/h\d/));
|
||||
for(let i = 0; i < titles.length; i++)
|
||||
{
|
||||
const start = titles[i].position?.start.offset ?? 0;
|
||||
const end = titles.length === i + 1 ? tagFile.content.length : titles[i + 1].position.start.offset - 1;
|
||||
tags.push({ tag: titles[i].properties.id, description: tagFile.content.substring(titles[i].position?.end.offset + 1, end) });
|
||||
}
|
||||
}
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[];
|
||||
const removeFiles = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`);
|
||||
db.transaction((data: File[]) => data.forEach(e => removeFiles.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path)));
|
||||
removeFiles.finalize();
|
||||
|
||||
const oldTags = db.prepare(`SELECT * FROM explorer_tags WHERE project = ?1`).all('1') as Tag[];
|
||||
const removeTags = db.prepare(`DELETE FROM explorer_tags WHERE project = ?1 AND tag = ?2`);
|
||||
db.transaction((data: Tag[]) => data.forEach(e => removeTags.run('1', e.tag)))(oldTags.filter(e => !tags.find(f => f.tag = e.tag)));
|
||||
removeTags.finalize();
|
||||
|
||||
const insertFiles = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`);
|
||||
const updateFiles = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`);
|
||||
db.transaction((content) => {
|
||||
for (const item of content) {
|
||||
let order = item.order;
|
||||
|
||||
if (typeof order === 'string')
|
||||
order = parseInt(item.order, 10);
|
||||
|
||||
if (isNaN(order))
|
||||
order = 999;
|
||||
|
||||
if(oldFiles.find(e => item.path === e.path))
|
||||
updateFiles.run({ $path: item.path, $content: item.content });
|
||||
else
|
||||
insertFiles.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order });
|
||||
}
|
||||
})(files);
|
||||
|
||||
insertFiles.finalize();
|
||||
updateFiles.finalize();
|
||||
|
||||
const insertTags = db.prepare(`INSERT INTO explorer_tags("project", "tag", "description") VALUES (1, $tag, $description)`);
|
||||
const updateTags = db.prepare(`UPDATE explorer_tags SET description = $description WHERE project = 1 AND tag = $tag`);
|
||||
db.transaction((content) => {
|
||||
for (const item of content) {
|
||||
if (oldTags.find(e => item.tag === e.tag))
|
||||
updateTags.run({ $tag: item.tag, $description: item.description });
|
||||
else
|
||||
insertTags.run({ $tag: item.tag, $description: item.description });
|
||||
}
|
||||
})(tags);
|
||||
|
||||
insertTags.finalize();
|
||||
updateTags.finalize();
|
||||
|
||||
useStorage('cache').clear();
|
||||
|
||||
return { result: true };
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
return { result: false };
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
function reshapeContent(content: string, type: FileType): string | null
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case "Markdown":
|
||||
case "File":
|
||||
return content;
|
||||
case "Canvas":
|
||||
const data = JSON.parse(content) as CanvasContent;
|
||||
data.edges.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
|
||||
data.nodes.forEach(e => e.color = typeof e.color === 'string' ? getColor(e.color) : undefined);
|
||||
return JSON.stringify(data);
|
||||
default:
|
||||
case 'Folder':
|
||||
return null;
|
||||
}
|
||||
}
|
||||
function getColor(color: string): CanvasColor
|
||||
{
|
||||
const colors: Record<string, string> = {
|
||||
'1': 'red',
|
||||
'2': 'orange',
|
||||
'3': 'yellow',
|
||||
'4': 'green',
|
||||
'5': 'cyan',
|
||||
'6': 'purple',
|
||||
};
|
||||
if(colors.hasOwnProperty(color))
|
||||
return { class: colors[color] };
|
||||
else
|
||||
return { hex: color };
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
import type { H3Event, SessionConfig } from 'h3'
|
||||
import { useSession, createError } from 'h3'
|
||||
import { defu } from 'defu'
|
||||
import { createHooks } from 'hookable'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
import type { UserSession, UserSessionRequired } from '~/types/auth'
|
||||
|
||||
export interface SessionHooks {
|
||||
/**
|
||||
* Called when fetching the session from the API
|
||||
* - Add extra properties to the session
|
||||
* - Throw an error if the session could not be verified (with a database for example)
|
||||
*/
|
||||
fetch: (session: UserSessionRequired, event: H3Event) => void | Promise<void>
|
||||
/**
|
||||
* Called before clearing the session
|
||||
*/
|
||||
clear: (session: UserSession, event: H3Event) => void | Promise<void>
|
||||
}
|
||||
|
||||
export const sessionHooks = createHooks<SessionHooks>()
|
||||
|
||||
/**
|
||||
* Get the user session from the current request
|
||||
* @param event The Request (h3) event
|
||||
* @returns The user session
|
||||
*/
|
||||
export async function getUserSession(event: H3Event) {
|
||||
const session = await _useSession(event);
|
||||
|
||||
if(!session.data || !session.data.id)
|
||||
{
|
||||
await session.update(defu({ id: session.id }, session.data));
|
||||
}
|
||||
|
||||
return session.data;
|
||||
}
|
||||
/**
|
||||
* Set a user session
|
||||
* @param event The Request (h3) event
|
||||
* @param data User session data, please only store public information since it can be decoded with API calls
|
||||
* @see https://github.com/atinux/nuxt-auth-utils
|
||||
*/
|
||||
export async function setUserSession(event: H3Event, data: UserSession) {
|
||||
const session = await _useSession(event)
|
||||
|
||||
await session.update(defu(data, session.data))
|
||||
|
||||
return session.data
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a user session
|
||||
* @param event The Request (h3) event
|
||||
* @param data User session data, please only store public information since it can be decoded with API calls
|
||||
*/
|
||||
export async function replaceUserSession(event: H3Event, data: UserSession) {
|
||||
const session = await _useSession(event)
|
||||
|
||||
await session.clear()
|
||||
await session.update(data)
|
||||
|
||||
return session.data
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the user session and removing the session cookie
|
||||
* @param event The Request (h3) event
|
||||
* @returns true if the session was cleared
|
||||
*/
|
||||
export async function clearUserSession(event: H3Event) {
|
||||
const session = await _useSession(event)
|
||||
|
||||
await sessionHooks.callHookParallel('clear', session.data, event)
|
||||
await session.clear()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Require a user session, throw a 401 error if the user is not logged in
|
||||
* @param event
|
||||
* @param opts Options to customize the error message and status code
|
||||
* @param opts.statusCode The status code to use for the error (defaults to 401)
|
||||
* @param opts.message The message to use for the error (defaults to Unauthorized)
|
||||
* @see https://github.com/atinux/nuxt-auth-utils
|
||||
*/
|
||||
export async function requireUserSession(event: H3Event, opts: { statusCode?: number, message?: string } = {}): Promise<UserSessionRequired> {
|
||||
const userSession = await getUserSession(event)
|
||||
|
||||
if (!userSession.user) {
|
||||
throw createError({
|
||||
statusCode: opts.statusCode || 401,
|
||||
message: opts.message || 'Unauthorized',
|
||||
})
|
||||
}
|
||||
|
||||
return userSession as UserSessionRequired
|
||||
}
|
||||
|
||||
let sessionConfig: SessionConfig
|
||||
|
||||
function _useSession(event: H3Event) {
|
||||
if (!sessionConfig) {
|
||||
const runtimeConfig = useRuntimeConfig(event)
|
||||
|
||||
sessionConfig = runtimeConfig.session;
|
||||
}
|
||||
return useSession<UserSession>(event, sessionConfig)
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import useDatabase from "~/composables/useDatabase";
|
||||
import { Return } from "~/types/api";
|
||||
import type { UserSession, UserSessionRequired } from "~/types/auth";
|
||||
|
||||
export async function checkSession(e: H3Event<EventRequestHandler>, session: UserSession): Promise<Return | undefined>
|
||||
{
|
||||
const db = useDatabase();
|
||||
|
||||
if(session.id && session.user?.id)
|
||||
{
|
||||
const checkSession = db.query("SELECT user_id FROM user_sessions WHERE id = ?1");
|
||||
const sessionId = checkSession.get(session.id) as any;
|
||||
|
||||
if(sessionId && sessionId.user_id === session.user?.id)
|
||||
{
|
||||
return { success: true, session };
|
||||
}
|
||||
else
|
||||
{
|
||||
await clearUserSession(e);
|
||||
|
||||
setResponseStatus(e, 406);
|
||||
return { success: false, error: new Error('Vous êtes déjà connecté') };
|
||||
}
|
||||
}
|
||||
}
|
||||
export async function logSession(e: H3Event<EventRequestHandler>, session: UserSessionRequired): Promise<UserSessionRequired>
|
||||
{
|
||||
const db = useDatabase();
|
||||
|
||||
const loggingIn = db.query(`INSERT INTO user_sessions(id, user_id, ip, agent, last_refresh) VALUES(?1, ?2, ?3, ?4, ?5)`);
|
||||
loggingIn.get(session.id, session.user.id, getRequestIP(e) ?? null, getRequestHeader(e, 'User-Agent') ?? null, Date.now());
|
||||
|
||||
return session;
|
||||
}
|
||||
Reference in New Issue
Block a user