2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-05-31 23:10:25 +02:00
|
|
|
import { readFile, unlink, writeFile } from "node:fs/promises";
|
2025-06-23 00:17:22 +02:00
|
|
|
import type { ApiSchedule, ApiSubscription, ApiUserType } from "~/shared/types/api";
|
|
|
|
import type { Id } from "~/shared/types/common";
|
2025-03-05 18:41:47 +01:00
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
export interface ServerSession {
|
2025-07-07 22:42:49 +02:00
|
|
|
id: Id,
|
|
|
|
access: ApiUserType,
|
|
|
|
accountId?: number,
|
2025-07-09 15:21:39 +02:00
|
|
|
authenticationProvider?: "telegram",
|
|
|
|
authenticationSlug?: string,
|
|
|
|
authenticationName?: string,
|
2025-07-09 14:54:54 +02:00
|
|
|
rotatesAtMs: number,
|
|
|
|
expiresAtMs?: number,
|
2025-07-07 22:42:49 +02:00
|
|
|
discardAtMs: number,
|
|
|
|
successor?: Id,
|
2025-06-09 16:51:05 +02:00
|
|
|
};
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
export interface ServerUser {
|
|
|
|
id: Id,
|
|
|
|
updatedAt: string,
|
|
|
|
deleted?: boolean,
|
|
|
|
type: ApiUserType,
|
|
|
|
name?: string,
|
|
|
|
interestedEventIds?: number[],
|
|
|
|
interestedEventSlotIds?: number[],
|
|
|
|
timezone?: string,
|
|
|
|
locale?: string,
|
|
|
|
}
|
|
|
|
|
2025-07-09 15:21:39 +02:00
|
|
|
export interface ServerAuthenticationMethod {
|
|
|
|
id: Id,
|
|
|
|
provider: "telegram",
|
|
|
|
slug: string,
|
|
|
|
name: string,
|
|
|
|
userId: Id,
|
|
|
|
}
|
|
|
|
|
2025-03-05 18:41:47 +01:00
|
|
|
// For this demo I'm just storing the runtime data in JSON files. When making
|
|
|
|
// this into proper application this should be replaced with an actual database.
|
|
|
|
|
2025-03-07 12:25:10 +01:00
|
|
|
const schedulePath = "data/schedule.json";
|
|
|
|
const subscriptionsPath = "data/subscriptions.json";
|
2025-06-23 00:17:22 +02:00
|
|
|
const usersPath = "data/users.json";
|
|
|
|
const nextUserIdPath = "data/next-user-id.json";
|
2025-03-07 12:41:57 +01:00
|
|
|
const sessionsPath = "data/sessions.json";
|
|
|
|
const nextSessionIdPath = "data/next-session-id.json";
|
2025-07-09 15:21:39 +02:00
|
|
|
const authMethodPath = "data/auth-method.json";
|
|
|
|
const nextAuthenticationMethodIdPath = "data/auth-method-id.json"
|
2025-03-05 18:41:47 +01:00
|
|
|
|
2025-05-31 23:10:25 +02:00
|
|
|
async function remove(path: string) {
|
|
|
|
try {
|
|
|
|
await unlink(path);
|
|
|
|
} catch (err: any) {
|
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-23 12:48:34 +02:00
|
|
|
export async function deleteDatabase() {
|
2025-05-31 23:10:25 +02:00
|
|
|
await remove(schedulePath);
|
|
|
|
await remove(subscriptionsPath);
|
2025-06-23 00:17:22 +02:00
|
|
|
await remove(usersPath);
|
2025-05-31 23:10:25 +02:00
|
|
|
await remove(sessionsPath);
|
|
|
|
}
|
|
|
|
|
2025-03-07 12:25:10 +01:00
|
|
|
async function readJson<T>(filePath: string, fallback: T) {
|
|
|
|
let data: T extends () => infer R ? R : T;
|
2025-03-05 18:41:47 +01:00
|
|
|
try {
|
2025-03-07 12:25:10 +01:00
|
|
|
data = JSON.parse(await readFile(filePath, "utf-8"));
|
2025-03-05 18:41:47 +01:00
|
|
|
} catch (err: any) {
|
|
|
|
if (err.code !== "ENOENT")
|
|
|
|
throw err;
|
2025-03-07 12:25:10 +01:00
|
|
|
data = typeof fallback === "function" ? fallback() : fallback;
|
2025-03-05 18:41:47 +01:00
|
|
|
}
|
2025-03-07 12:25:10 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function readSchedule() {
|
2025-06-28 01:23:52 +02:00
|
|
|
return readJson(schedulePath, (): ApiSchedule => ({
|
|
|
|
id: 111,
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
}));
|
2025-03-05 18:41:47 +01:00
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
export async function writeSchedule(schedule: ApiSchedule) {
|
2025-03-05 18:41:47 +01:00
|
|
|
await writeFile(schedulePath, JSON.stringify(schedule, undefined, "\t") + "\n", "utf-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function readSubscriptions() {
|
2025-06-11 21:05:17 +02:00
|
|
|
let subscriptions = await readJson<ApiSubscription[]>(subscriptionsPath, []);
|
2025-03-07 12:30:39 +01:00
|
|
|
if (subscriptions.length && "keys" in subscriptions[0]) {
|
|
|
|
// Discard old format
|
|
|
|
subscriptions = [];
|
|
|
|
}
|
2025-03-07 12:25:10 +01:00
|
|
|
return subscriptions;
|
2025-03-05 18:41:47 +01:00
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
export async function writeSubscriptions(subscriptions: ApiSubscription[]) {
|
2025-03-05 18:41:47 +01:00
|
|
|
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
|
|
|
|
}
|
2025-03-07 12:41:57 +01:00
|
|
|
|
2025-06-28 01:23:52 +02:00
|
|
|
export async function readNextUserId() {
|
|
|
|
return await readJson(nextUserIdPath, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeNextUserId(nextId: number) {
|
|
|
|
await writeFile(nextUserIdPath, String(nextId), "utf-8");
|
|
|
|
}
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
export async function nextUserId() {
|
|
|
|
let nextId = await readJson(nextUserIdPath, 0);
|
2025-03-07 23:53:57 +01:00
|
|
|
if (nextId === 0) {
|
2025-06-23 00:17:22 +02:00
|
|
|
nextId = Math.max(...(await readUsers()).map(user => user.id), -1) + 1;
|
2025-03-07 23:53:57 +01:00
|
|
|
}
|
2025-06-23 00:17:22 +02:00
|
|
|
await writeFile(nextUserIdPath, String(nextId + 1), "utf-8");
|
2025-03-07 23:53:57 +01:00
|
|
|
return nextId;
|
|
|
|
}
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
export async function readUsers() {
|
2025-06-28 01:23:52 +02:00
|
|
|
return await readJson(usersPath, (): ServerUser[] => []);
|
2025-03-07 12:41:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
export async function writeUsers(users: ServerUser[]) {
|
|
|
|
await writeFile(usersPath, JSON.stringify(users, undefined, "\t") + "\n", "utf-8");
|
2025-03-07 12:41:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-28 01:23:52 +02:00
|
|
|
export async function readNextSessionId() {
|
|
|
|
return await readJson(nextSessionIdPath, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeNextSessionId(nextId: number) {
|
|
|
|
await writeFile(nextSessionIdPath, String(nextId), "utf-8");
|
|
|
|
}
|
|
|
|
|
2025-03-07 12:41:57 +01:00
|
|
|
export async function nextSessionId() {
|
|
|
|
const nextId = await readJson(nextSessionIdPath, 0);
|
|
|
|
await writeFile(nextSessionIdPath, String(nextId + 1), "utf-8");
|
|
|
|
return nextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function readSessions() {
|
2025-07-07 22:42:49 +02:00
|
|
|
return readJson<ServerSession[]>(sessionsPath, [])
|
2025-03-07 12:41:57 +01:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:51:05 +02:00
|
|
|
export async function writeSessions(sessions: ServerSession[]) {
|
2025-07-07 22:42:49 +02:00
|
|
|
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
|
2025-03-07 12:41:57 +01:00
|
|
|
}
|
2025-07-09 15:21:39 +02:00
|
|
|
|
|
|
|
export async function nextAuthenticationMethodId() {
|
|
|
|
const nextId = await readJson(nextAuthenticationMethodIdPath, 0);
|
|
|
|
await writeFile(nextAuthenticationMethodIdPath, String(nextId + 1), "utf-8");
|
|
|
|
return nextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function readAuthenticationMethods() {
|
|
|
|
return readJson<ServerAuthenticationMethod[]>(authMethodPath, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function writeAuthenticationMethods(authMethods: ServerAuthenticationMethod[]) {
|
|
|
|
await writeFile(authMethodPath, JSON.stringify(authMethods, undefined, "\t") + "\n", "utf-8");
|
|
|
|
}
|