67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
/**
|
|
* Checks the rows `settings-service.ts` reads from `user_settings`, and turns
|
|
* them into the {@link UserSettings} the settings endpoints answer.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { SQLOutputValue } from "node:sqlite";
|
|
import type { UserSettings } from "../../shared/settings.ts";
|
|
import { isColorScheme, isStyleName } from "../../shared/theme.ts";
|
|
|
|
/**
|
|
* Stored by `updateSettings` and read back by `getSettings`. A user without a
|
|
* row has the defaults.
|
|
*/
|
|
export interface UserSettingsRow {
|
|
user_id: string;
|
|
/** A style name, or null for the instance's. */
|
|
style: string | null;
|
|
color_scheme: string | null;
|
|
/** 0 or 1. */
|
|
digest_weekly: number;
|
|
/** 0 or 1. */
|
|
digest_monthly: number;
|
|
updated_at: string;
|
|
[key: string]: SQLOutputValue;
|
|
}
|
|
|
|
/**
|
|
* Names the column of each setting, for the queries that pick users by one,
|
|
* as `digest-service.ts` does by its digest's switch.
|
|
*/
|
|
export const SETTINGS_COLUMNS: Record<keyof UserSettings, string> = {
|
|
style: "style",
|
|
colorScheme: "color_scheme",
|
|
digestWeekly: "digest_weekly",
|
|
digestMonthly: "digest_monthly",
|
|
};
|
|
|
|
/**
|
|
* Checks whether `obj`, a row `getSettings` selected, is a
|
|
* {@link UserSettingsRow}.
|
|
*/
|
|
export function isUserSettingsRow(obj: unknown): obj is UserSettingsRow {
|
|
return !!obj && typeof obj === "object" &&
|
|
"user_id" in obj && typeof obj.user_id === "string" &&
|
|
"style" in obj && (obj.style === null || typeof obj.style === "string") &&
|
|
"color_scheme" in obj &&
|
|
(obj.color_scheme === null || typeof obj.color_scheme === "string") &&
|
|
"digest_weekly" in obj && typeof obj.digest_weekly === "number" &&
|
|
"digest_monthly" in obj && typeof obj.digest_monthly === "number" &&
|
|
"updated_at" in obj && typeof obj.updated_at === "string";
|
|
}
|
|
|
|
/**
|
|
* Converts a row to the {@link UserSettings} the client gets. A style or
|
|
* scheme the code no longer knows, as after one is removed, reads as unset.
|
|
*/
|
|
export function settingsRowToApi(row: UserSettingsRow): UserSettings {
|
|
return {
|
|
style: isStyleName(row.style) ? row.style : null,
|
|
colorScheme: isColorScheme(row.color_scheme) ? row.color_scheme : null,
|
|
digestWeekly: row.digest_weekly === 1,
|
|
digestMonthly: row.digest_monthly === 1,
|
|
};
|
|
}
|