playing wiith database Blocked Users

This commit is contained in:
NigeParis 2025-12-01 18:46:38 +01:00
parent d1496e1697
commit b35c462648
13 changed files with 291 additions and 168 deletions

View file

@ -20,8 +20,8 @@
"fastify-plugin": "^5.1.0",
"joi": "^18.0.2",
"otp": "^1.1.2",
"typebox": "^1.0.56",
"uuidv7": "^1.0.2"
"typebox": "^1.0.59",
"uuidv7": "^1.1.0"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.13",

View file

@ -2,7 +2,6 @@ import { isNullish } from '@shared/utils';
import type { Database } from './_base';
import { UserId } from './user';
// never use this directly
// describe every function in the object
export interface IBlockedDb extends Database {
@ -10,6 +9,8 @@ export interface IBlockedDb extends Database {
addBlockedUserFor(id: UserId, blocked: UserId): void,
removeBlockedUserFor(id: UserId, blocked: UserId): void,
unblockAllUserFor(id: UserId): void,
getAllBlockedUsers(this: IBlockedDb): BlockedData[] | undefined,
};
export const BlockedImpl: Omit<IBlockedDb, keyof Database> = {
@ -28,6 +29,23 @@ export const BlockedImpl: Omit<IBlockedDb, keyof Database> = {
removeBlockedUserFor(this: IBlockedDb, id: UserId, blocked: UserId): void {
this.prepare('DELETE FROM blocked WHERE user = @id AND blocked = @blocked').run({ id, blocked });
},
/**
* Get all blocked user
*
* @param
*
* @returns The list of users if it exists, undefined otherwise
*/
getAllBlockedUsers(this: IBlockedDb): BlockedData[] {
const rows = this.prepare('SELECT * FROM blocked').all() as Partial<BlockedData>[];
return rows
.map(row => blockedFromRow(row))
.filter((u): u is BlockedData => u !== undefined);
},
};
export type BlockedId = number & { readonly __brand: unique symbol };
@ -46,6 +64,7 @@ export type BlockedData = {
* @returns The blocked if it exists, undefined otherwise
*/
export function blockedFromRow(row?: Partial<BlockedData>): BlockedData | undefined {
console.log('HELLO ?????', row);
if (isNullish(row)) return undefined;
if (isNullish(row.id)) return undefined;
if (isNullish(row.user)) return undefined;

View file

@ -18,6 +18,8 @@ export interface IUserDb extends Database {
ensureUserOtpSecret(id: UserId): string | undefined,
deleteUserOtpSecret(id: UserId): void,
getAllUserFromProvider(provider: string): User[] | undefined,
getAllUsers(this: IUserDb): User[] | undefined,
};
export const UserImpl: Omit<IUserDb, keyof Database> = {
@ -36,6 +38,15 @@ export const UserImpl: Omit<IUserDb, keyof Database> = {
);
},
getAllUsers(this: IUserDb): User[] {
const rows = this.prepare('SELECT * FROM user').all() as Partial<User>[];
return rows
.map(row => userFromRow(row))
.filter((u): u is User => u !== undefined);
},
/**
* Get a user from a raw [UserId]
*