diff --git a/src/app.ts b/src/app.ts index d7b3965..374e879 100644 --- a/src/app.ts +++ b/src/app.ts @@ -32,28 +32,59 @@ import './ws.js'; import { getMCPService } from './mcp.js'; // Middlewares -app.use(cors(config.corsOptions)); +const corsOptionsDelegate = function (req: Request, callback: any) { + const origin = req.header('Origin'); + const allowedOrigins = config.corsOptions.origin; + let corsOptions; + + if (allowedOrigins == '*') + { + corsOptions = { + origin: origin, + credentials: true, + methods: config.corsOptions.methods, + allowedHeaders: config.corsOptions.allowedHeaders, + }; + } + else + { + if (allowedOrigins.includes(origin)) { + corsOptions = { + origin: origin, + credentials: true, + methods: config.corsOptions.methods, + allowedHeaders: config.corsOptions.allowedHeaders, + }; + } else { + corsOptions = { origin: false }; + } + } + + callback(null, corsOptions); +}; + +app.use(cors(corsOptionsDelegate)); + app.use(cookieParser(process.env.COOKIE_SIGN_KEY)); app.use(morgan('dev')); -app.use(AllowedOriginCheck); +// app.use(AllowedOriginCheck); app.use(SilverIssueMiddleware); -app.use(clerkMiddleware()); - app.use(express.json({ limit: "1000mb" })); app.use(express.urlencoded({ extended: true })); app.use(express.static(path.join(__dirname, 'public'))); +app.use(clerkMiddleware()); + // Routes -app.use('/api', api); //requireAuth(), -app.use('/api/ai', api_ai); //requireAuth(), -app.use('/api/db', api_db); //requireAuth(), -app.use('/user', user); //requireAuth(), -app.use('/admin', admin); //requireAuth(), -app.use('/money', money); //requireAuth(), - +app.use('/api', api); +app.use('/api/ai', api_ai); //requireAuth() +app.use('/user', user); +app.use('/admin', admin); +app.use('/money', money); +app.use('/api/db', api_db); //requireAuth() app.get('/version', (req, res) => { diff --git a/src/assets/ts/tags.ts b/src/assets/ts/tags.ts index 3e4c9e4..9aa68ec 100644 --- a/src/assets/ts/tags.ts +++ b/src/assets/ts/tags.ts @@ -48,6 +48,7 @@ import { randomUUID } from "crypto"; if (!tag.user_id) return { error: true, message: "user_id requis" }; tag.uuid = tag.uuid || randomUUID(); + tag._id = tag.uuid || randomUUID(); tag.created_at = tag.created_at || Date.now(); const res = await this.fetch('/push', { diff --git a/src/assets/ts/types.ts b/src/assets/ts/types.ts index e3b0a1e..a6e6a3c 100644 --- a/src/assets/ts/types.ts +++ b/src/assets/ts/types.ts @@ -39,6 +39,7 @@ export interface Note { export interface Tag { + _id: string; uuid: string; user_id?: string; id: number; diff --git a/src/assets/ts/utils.ts b/src/assets/ts/utils.ts index bc74b87..007f9e2 100644 --- a/src/assets/ts/utils.ts +++ b/src/assets/ts/utils.ts @@ -1,4 +1,4 @@ -import { randomUUID } from 'crypto' +import crypto, { randomUUID } from 'crypto' class utils { diff --git a/src/config.json b/src/config.json index 506d3ac..e7501e8 100644 --- a/src/config.json +++ b/src/config.json @@ -3,7 +3,7 @@ "PORT": 3000, "corsOptions": { - "origin": [ "https://www.silvernote.fr", "https://app.silvernote.fr", "http://localhost:5173", "http://localhost:4173" ], + "origin": [ "https://www.silvernote.fr", "https://app.silvernote.fr", "http://localhost:5173", "http://localhost:4173" ], "methods": ["GET", "POST"], "allowedHeaders": ["Content-Type", "authorization"], "credentials": true @@ -12,7 +12,7 @@ "news": { - "active": false, + "active": true, "message": "Simplifier vous la tache avec silvernote.", "title": "Bienvenue sur SilverNote", diff --git a/src/mcp.ts b/src/mcp.ts index e63c054..d5b9855 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -8,7 +8,7 @@ const MCP_CONFIG = { args: [ '-y', 'tsx', - process.env.MCP_SERVER_PATH || './mcp-server/index.js' + process.env.MCP_SERVER_PATH || './src/mcp-server/index.js' ] }; diff --git a/src/routes/api.db.ts b/src/routes/api.db.ts index 582236a..901529f 100644 --- a/src/routes/api.db.ts +++ b/src/routes/api.db.ts @@ -2,37 +2,18 @@ import express, { Request, Response } from "express"; import note_db from "../assets/ts/notes.js"; import tag_db from "../assets/ts/tags.js"; +import utils from "../assets/ts/utils.js"; +import { Note, Tag } from "../assets/ts/types.js"; const router = express.Router(); -function areArraysEqualIgnoreOrder(a: T[], b: T[]): boolean { - if (!Array.isArray(a) || !Array.isArray(b)) return false; - if (a.length !== b.length) return false; - - const normalize = (arr: T[]) => - [...arr] - .map(item => typeof item === 'object' - ? JSON.stringify(Object.keys(item as object).sort().reduce((acc, key) => { - (acc as Record)[key] = (item as any)[key]; - return acc; - }, {} as Record)) - : String(item) - ) - .sort(); - - const normA = normalize(a); - const normB = normalize(b); - - return normA.every((val, idx) => val === normB[idx]); -} - router.post('/verify/data', async (req: Request, res: Response) => { try { - const { notes, tags } = req.body as { notes: any[]; tags: any[] }; + const { notes, tags } = req.body as { notes: string; tags: string }; // hash of notes and tags const user_id: string | undefined = req.cookies?.user_id; if (!notes || !tags || !user_id) { @@ -40,14 +21,17 @@ router.post('/verify/data', async (req: Request, res: Response) => { return; } - const db_notes = (await note_db.getNoteByUserId(user_id)).notes; - const db_tags = (await tag_db.getTagsByUserId(user_id)).tags; + const db_notes: Note[] = (await note_db.getNoteByUserId(user_id)).notes.filter(note => note.title !== '' && note.content !== ''); + const db_tags: Tag[] = (await tag_db.getTagsByUserId(user_id)).tags; + + const db_notes_hash: string = await utils.hash(db_notes); + const db_tags_hash: string = await utils.hash(db_tags); - const notesMatch = areArraysEqualIgnoreOrder(db_notes, notes); - const tagsMatch = areArraysEqualIgnoreOrder(db_tags, tags); // pk ça renvoi false ?? + const notesMatch = notes === db_notes_hash; + const tagsMatch = tags === db_tags_hash; res.json({ - ok: notesMatch, //&& tagsMatch, + ok: notesMatch && tagsMatch, notes: notesMatch, notes_length: db_notes.length, tags: tagsMatch, @@ -86,7 +70,11 @@ router.post('/delete/a/note', async (req: Request, res: Response) => { }); router.get('/get/user/notes', async (req: Request, res: Response) => { - res.json(await note_db.getNoteByUserId(req.query.user_id as string)); + const db_res = await note_db.getNoteByUserId(req.query.user_id as string); + res.json({ + ...db_res, + notes: db_res.notes.filter(note => note.title !== '' && note.content !== '') + }); }); router.post('/delete/notes', async (req: Request, res: Response) => { diff --git a/src/routes/api.ts b/src/routes/api.ts index c757ba3..6777f24 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -13,7 +13,7 @@ const router = Router(); router.get('/get_news', async (req: Request, res: Response) => { - const data = await fs.promises.readFile('./config.json', 'utf-8'); + const data = await fs.promises.readFile('./dist/config.json', 'utf-8'); const news: Promise = JSON.parse(data).news; res.json( (await news).active ? news : false );