This commit is contained in:
Sofía Aritz 2024-06-30 18:13:29 +02:00
parent ed4ea1db40
commit b9f36b5c2c
Signed by: sofia
GPG key ID: 90B5116E3542B28F
12 changed files with 160 additions and 131 deletions

View file

@ -1,4 +1,8 @@
import globals from "globals";
import pluginJs from "@eslint/js";
export default [{ languageOptions: { globals: globals.node } }, pluginJs.configs.recommended];
export default [
{ ignores: ["**/dist/*"] },
{ languageOptions: { globals: globals.node } },
pluginJs.configs.recommended,
];

View file

@ -19,8 +19,8 @@
},
"scripts": {
"start": "tsc && node dist/index.js",
"lint:fix": "eslint --fix && prettier . --write",
"lint": "eslint && prettier . --check"
"lint:fix": "eslint . --fix && prettier . --write",
"lint": "eslint . && prettier . --check"
},
"devDependencies": {
"@eslint/js": "^9.5.0",

View file

@ -19,7 +19,7 @@ import Fastify from "fastify";
let app = Fastify({
logger: true,
}).withTypeProvider<TypeBoxTypeProvider>()
}).withTypeProvider<TypeBoxTypeProvider>();
export type AppInterface = typeof app;
export default app;

View file

@ -20,36 +20,35 @@ import * as Jose from "jose";
import { JWT_ALG, JWT_SECRET } from "./consts.js";
import { DatabaseInterface, toDBList } from "./database.js";
export type AuthInterface = Awaited<ReturnType<typeof startAuth>>;
export type User = {
id: string,
createdAt: number,
lastConnected: number,
name: string,
email: string,
password: string,
limitID: string,
}
id: string;
createdAt: number;
lastConnected: number;
name: string;
email: string;
password: string;
limitID: string;
};
export type NewUser = {
name: string,
email: string,
password: string,
}
name: string;
email: string;
password: string;
};
export type UpdateUser = {
name?: string,
email?: string,
password?: string,
assets?: string[],
}
name?: string;
email?: string;
password?: string;
assets?: string[];
};
export async function startAuth(database: DatabaseInterface) {
let funcs = {
user: async (uid: string) => await database.user(uid) satisfies User,
findUserByEmail: async (email: string) => await database.findUserByEmail(email) satisfies User,
user: async (uid: string) => (await database.user(uid)) satisfies User,
findUserByEmail: async (email: string) => (await database.findUserByEmail(email)) satisfies User,
findUserBySessionKey: async (sessionKey: string) => {
let key = await database.sessionKey(sessionKey);
return await database.user(key.userID);
@ -61,7 +60,8 @@ export async function startAuth(database: DatabaseInterface) {
return await database.updateUser(uid, user);
},
addUser: async (user: NewUser) => {
let result = await database.insertUser({
let result = await database.insertUser(
{
id: randomUUID(),
createdAt: Date.now(),
lastConnected: Date.now(),
@ -71,11 +71,13 @@ export async function startAuth(database: DatabaseInterface) {
assets: toDBList([]),
// FIXME: This shouldn't be required, the DB interface overwrites it.
limitID: "",
}, {
},
{
id: randomUUID(),
currentAssetCount: 0,
maxAssetCount: 5,
});
},
);
return result satisfies User;
},
@ -103,9 +105,9 @@ export async function startAuth(database: DatabaseInterface) {
},
verifyJwt: async (jwt) => {
return await Jose.jwtVerify<{
uid: string,
email: string,
name: string,
uid: string;
email: string;
name: string;
}>(jwt, JWT_SECRET);
},
cleanUser: (user: User) => {
@ -114,7 +116,7 @@ export async function startAuth(database: DatabaseInterface) {
clean.limitID = undefined;
return clean;
}
},
};
return funcs;

View file

@ -21,14 +21,14 @@ import { sqliteTable } from "drizzle-orm/sqlite-core";
import { text, integer } from "drizzle-orm/sqlite-core";
import { asc, desc, eq, sql } from "drizzle-orm";
export type DatabaseInterface = Awaited<ReturnType<typeof startDatabase>>
export type DatabaseInterface = Awaited<ReturnType<typeof startDatabase>>;
export function toDBList(input: any[]): string {
return JSON.stringify(input);
}
export function fromDBList<T>(input: string): Array<T> {
return JSON.parse(input)
return JSON.parse(input);
}
export async function startDatabase() {
@ -134,11 +134,14 @@ export async function startDatabase() {
let result = await database.select().from(limits).where(eq(limits.id, limitsID));
return result[0];
},
updateUser: async (userID: string, newUser: {
name?: string,
email?: string,
password?: string,
}) => {
updateUser: async (
userID: string,
newUser: {
name?: string;
email?: string;
password?: string;
},
) => {
let result = await database.update(users).set(newUser).where(eq(users.id, userID)).returning();
return result[0];
},
@ -157,7 +160,12 @@ export async function startDatabase() {
findSessionKeyByUserID: async (userID: string) => {
return await database.select().from(session_keys).where(eq(session_keys.userID, userID));
},
insertEntry: async (entry: typeof entries.$inferInsert, musicEntry?: typeof musicEntries.$inferInsert, locationEntry?: typeof locationEntries.$inferInsert, dateEntry?: typeof dateEntries.$inferInsert) => {
insertEntry: async (
entry: typeof entries.$inferInsert,
musicEntry?: typeof musicEntries.$inferInsert,
locationEntry?: typeof locationEntries.$inferInsert,
dateEntry?: typeof dateEntries.$inferInsert,
) => {
if (entry.kind === "album" || entry.kind === "song") {
let result = await database.insert(musicEntries).values(musicEntry).returning({ id: musicEntries.id });
entry.musicEntry = result[0].id;
@ -188,12 +196,16 @@ export async function startDatabase() {
.orderBy(desc(entries.createdAt));
for (let key in result) {
if (!result.hasOwnProperty(key)) { continue; }
if (!result.hasOwnProperty(key)) {
continue;
}
let entry = structuredClone(result[key]);
let base = {};
if (entry.musicEntry != null) {
let musicDetails = (await database.select().from(musicEntries).where(eq(musicEntries.id, entry.musicEntry)))[0];
let musicDetails = (
await database.select().from(musicEntries).where(eq(musicEntries.id, entry.musicEntry))
)[0];
(musicDetails["link"] as any) = fromDBList(musicDetails.links);
(musicDetails["id"] as any) = fromDBList(musicDetails.universalIDs);
@ -201,22 +213,26 @@ export async function startDatabase() {
musicDetails["universalIDs"] = undefined;
base = musicDetails;
} else if (entry.locationEntry != null) {
let locationDetails = (await database.select().from(locationEntries).where(eq(locationEntries.id, entry.locationEntry)))[0];
let locationDetails = (
await database.select().from(locationEntries).where(eq(locationEntries.id, entry.locationEntry))
)[0];
if (locationDetails.locationCoordinates != null) {
base = { location: JSON.parse(locationDetails.locationCoordinates) }
base = { location: JSON.parse(locationDetails.locationCoordinates) };
} else if (locationDetails.locationText != null) {
base = { location: locationDetails.locationText }
base = { location: locationDetails.locationText };
}
} else if (entry.dateEntry != null) {
let dateDetails = (await database.select().from(dateEntries).where(eq(dateEntries.id, entry.dateEntry)))[0];
let dateDetails = (
await database.select().from(dateEntries).where(eq(dateEntries.id, entry.dateEntry))
)[0];
base = {
referencedDate: new Date(dateDetails["referencedDate"]).toISOString()
referencedDate: new Date(dateDetails["referencedDate"]).toISOString(),
};
}
base["kind"] = entry.kind
base["kind"] = entry.kind;
result[key]["creationDate"] = new Date(entry.createdAt).toISOString();
(result[key] as any)["feelings"] = fromDBList(entry.feelings);

View file

@ -24,7 +24,7 @@ const Body = Type.Object({
contactMethod: Type.String(),
name: Type.String(),
value: Type.String(),
})
});
type BodyType = Static<typeof Body>;
@ -53,9 +53,9 @@ export default function register(app: AppInterface, auth: AuthInterface, databas
});
return (await database.listHeirs(payload.uid))
.map(v => v["contactMethod"] = "email")
.map(v => v["value"] = v["email"])
.map(v => v["email"] = undefined);
.map((v) => (v["contactMethod"] = "email"))
.map((v) => (v["value"] = v["email"]))
.map((v) => (v["email"] = undefined));
},
schema: {
headers: { $ref: "schema://identity/authorization" },
@ -76,9 +76,9 @@ export default function register(app: AppInterface, auth: AuthInterface, databas
await database.removeHeir(request.body);
return (await database.listHeirs(payload.uid))
.map(v => v["contactMethod"] = "email")
.map(v => v["value"] = v["email"])
.map(v => v["email"] = undefined);
.map((v) => (v["contactMethod"] = "email"))
.map((v) => (v["value"] = v["email"]))
.map((v) => (v["email"] = undefined));
},
schema: {
headers: { $ref: "schema://identity/authorization" },

View file

@ -24,7 +24,7 @@ import { Static, Type } from "@sinclair/typebox";
const EntryIDQuery = Type.Object({
entry_id: Type.String(),
})
});
type EntryIDQueryType = Static<typeof EntryIDQuery>;
@ -43,22 +43,23 @@ const PutEntryBody = Type.Object({
backgroundColor: Type.String(),
textColor: Type.String(),
}),
])
]),
),
base: Type.Union([
Type.Object({
kind: Type.String(),
}),
Type.Object({
kind: Type.String(),
artist: Type.String(),
title: Type.String(),
link: Type.Array(Type.String()),
id: Type.Array(Type.Object({
id: Type.Array(
Type.Object({
provider: Type.String(),
id: Type.String(),
})),
}),
),
}),
Type.Object({
kind: Type.String(),
@ -75,7 +76,7 @@ const PutEntryBody = Type.Object({
referencedDate: Type.String(),
}),
]),
})
}),
});
type PutEntryBodyType = Static<typeof PutEntryBody>;
@ -105,28 +106,30 @@ export default function registerRoutes(app: AppInterface, auth: AuthInterface, d
let entry = request.body.entry;
let musicEntry, locationEntry, dateEntry;
if ((entry.base.kind === "album" || entry.base.kind === "song") && 'artist' in entry.base) {
if ((entry.base.kind === "album" || entry.base.kind === "song") && "artist" in entry.base) {
musicEntry = {
id: randomUUID(),
title: entry.base.title,
artist: entry.base.artist,
links: toDBList(entry.base.link),
universalIDs: toDBList(entry.base.id),
}
} else if (entry.base.kind === "environment" && 'location' in entry.base) {
};
} else if (entry.base.kind === "environment" && "location" in entry.base) {
locationEntry = {
id: randomUUID(),
locationText: typeof entry.base.location === "string" ? entry.base.location : undefined,
locationCoordinates: typeof entry.base.location === "string" ? undefined : JSON.stringify(entry.base.location),
}
} else if (entry.base.kind === "date" && 'referencedDate' in entry.base) {
locationCoordinates:
typeof entry.base.location === "string" ? undefined : JSON.stringify(entry.base.location),
};
} else if (entry.base.kind === "date" && "referencedDate" in entry.base) {
dateEntry = {
id: randomUUID(),
referencedDate: Date.parse(entry.base.referencedDate),
}
};
}
await database.insertEntry({
await database.insertEntry(
{
id: randomUUID(),
userID: payload.uid,
feelings: toDBList(entry.feelings),
@ -135,7 +138,11 @@ export default function registerRoutes(app: AppInterface, auth: AuthInterface, d
description: entry.description,
kind: entry.base.kind,
createdAt: Date.now(),
}, musicEntry, locationEntry, dateEntry);
},
musicEntry,
locationEntry,
dateEntry,
);
},
schema: {
headers: { $ref: "schema://identity/authorization" },

View file

@ -22,7 +22,7 @@ import type { DatabaseInterface } from "../../database.js";
const Query = Type.Object({
limit: Type.Number(),
offset: Type.Number(),
})
});
type QueryType = Static<typeof Query>;
@ -37,7 +37,7 @@ export default function register(app: AppInterface, auth: AuthInterface, databas
let jwt = request.headers["authorization"].replace("Bearer", "").trim();
let { payload } = await auth.verifyJwt(jwt);
return await database.entryPage(payload.uid, request.query.offset, request.query.limit)
return await database.entryPage(payload.uid, request.query.offset, request.query.limit);
},
schema: {
headers: { $ref: "schema://identity/authorization" },