Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions src/assets/ts/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
1 change: 1 addition & 0 deletions src/assets/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface Note {


export interface Tag {
_id: string;
uuid: string;
user_id?: string;
id: number;
Expand Down
2 changes: 1 addition & 1 deletion src/assets/ts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { randomUUID } from 'crypto'
import crypto, { randomUUID } from 'crypto'

class utils {

Expand Down
4 changes: 2 additions & 2 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,7 +12,7 @@

"news": {

"active": false,
"active": true,

"message": "Simplifier vous la tache avec silvernote.",
"title": "Bienvenue sur SilverNote",
Expand Down
2 changes: 1 addition & 1 deletion src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
};

Expand Down
44 changes: 16 additions & 28 deletions src/routes/api.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,36 @@ 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<T>(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<string, any>)[key] = (item as any)[key];
return acc;
}, {} as Record<string, any>))
: 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) {
res.json({ error: true, message: 'Missing parameters.' });
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,
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<News> = JSON.parse(data).news;

res.json( (await news).active ? news : false );
Expand Down