feat(user): added description and global guest user mute

This commit is contained in:
Maieul BOYER 2025-12-18 14:46:02 +01:00 committed by Maix0
parent bcba86ed8a
commit 556138d624
24 changed files with 1856 additions and 1460 deletions

View file

@ -20,7 +20,7 @@
"fastify-plugin": "^5.1.0",
"joi": "^18.0.2",
"otp": "^1.1.2",
"typebox": "^1.0.63",
"typebox": "^1.0.64",
"uuidv7": "^1.1.0"
},
"devDependencies": {

View file

@ -5,7 +5,9 @@ CREATE TABLE IF NOT EXISTS user (
password TEXT,
otp TEXT,
guest INTEGER NOT NULL DEFAULT 0,
oauth2 TEXT DEFAULT NULL
oauth2 TEXT DEFAULT NULL,
desc TEXT NOT NULL DEFAULT "What a good day to be reviewing this project :D",
allow_guest_message INTEGER NOT NULL DEFAULT 1
);

View file

@ -19,12 +19,19 @@ export interface IUserDb extends Database {
ensureUserOtpSecret(id: UserId): string | undefined,
deleteUserOtpSecret(id: UserId): void,
getAllUserFromProvider(provider: string): User[] | undefined,
getAllUsers(this: IUserDb): User[] | undefined,
getAllUsers(this: IUserDb): User[] | undefined,
updateDisplayName(id: UserId, new_name: string): boolean,
getUserFromDisplayName(name: string): User | undefined,
setUserDescription(id: UserId, newDescription: string): void,
getUserDescription(id: UserId): string | undefined,
allowGuestMessage(id: UserId): void,
denyGuestMessage(id: UserId): void,
getGuestMessage(id: UserId): boolean | undefined,
};
export const UserImpl: Omit<IUserDb, keyof Database> = {
@ -44,11 +51,11 @@ export const UserImpl: Omit<IUserDb, keyof Database> = {
},
getAllUsers(this: IUserDb): User[] {
const rows = this.prepare('SELECT * FROM user').all() as Partial<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);
return rows
.map(row => userFromRow(row))
.filter((u): u is User => u !== undefined);
},
@ -182,6 +189,28 @@ export const UserImpl: Omit<IUserDb, keyof Database> = {
const res = this.prepare('SELECT * FROM user WHERE name = @name LIMIT 1').get({ name }) as User | undefined;
return userFromRow(res);
},
setUserDescription(this: IUserDb, id: UserId, desc: string): void {
this.prepare('UPDATE OR IGNORE user SET desc = @desc WHERE id = @id').run({ id, desc });
},
getUserDescription(this: IUserDb, id: UserId): string | undefined {
return this.prepare('SELECT desc FROM user WHERE id = @id').get({ id }) as string | undefined;
},
allowGuestMessage(this: IUserDb, id: UserId): void {
this.prepare('UPDATE OR IGNORE user SET allow_guest_message = @allow_guest_message WHERE id = @id').run({ id, allow_guest_message: 1 });
},
denyGuestMessage(this: IUserDb, id: UserId): void {
this.prepare('UPDATE OR IGNORE user SET allow_guest_message = @allow_guest_message WHERE id = @id').run({ id, allow_guest_message: 0 });
},
getGuestMessage(this: IUserDb, id: UserId): boolean | undefined {
return this.prepare('SELECT allow_guest_message FROM user WHERE id = @id').get({ id }) as boolean | undefined;
},
};
export type UserId = UUID;
@ -196,6 +225,9 @@ export type User = {
// will be split/merged from the `oauth2` column
readonly provider_name?: string;
readonly provider_unique?: string;
readonly allow_guest_message: boolean,
readonly desc: string,
};
export async function verifyUserPassword(
@ -235,6 +267,8 @@ export function userFromRow(row?: Partial<Omit<User, 'provider_name' | 'provider
if (isNullish(row.id)) return undefined;
if (isNullish(row.name)) return undefined;
if (isNullish(row.guest)) return undefined;
if (isNullish(row.desc)) return undefined;
if (isNullish(row.allow_guest_message)) return undefined;
let provider_name = undefined;
let provider_unique = undefined;
@ -254,5 +288,7 @@ export function userFromRow(row?: Partial<Omit<User, 'provider_name' | 'provider
otp: row.otp ?? undefined,
guest: !!(row.guest ?? true),
provider_name, provider_unique,
allow_guest_message: !!(row.allow_guest_message ?? true),
desc: row.desc ?? 'NO DESC ????',
};
}

View file

@ -142,3 +142,10 @@ export function typeResponse<K extends string, T extends TProperties>(
export function isNullish<T>(v: T | undefined | null): v is null | undefined {
return v === null || v === undefined;
}
export function escape(s: string): string {
return s.replace(
/[^0-9A-Za-z ]/g,
c => '&#' + c.charCodeAt(0) + ';',
);
}

View file

@ -27,8 +27,7 @@
"fastify": "^5.6.2",
"fastify-cli": "^7.4.1",
"fastify-plugin": "^5.1.0",
"socket.io-client": "^4.8.1",
"typebox": "^1.0.63"
"typebox": "^1.0.64"
},
"devDependencies": {
"@types/node": "^22.19.3",

View file

@ -27,7 +27,7 @@
"fastify": "^5.6.2",
"fastify-plugin": "^5.1.0",
"socket.io": "^4.8.1",
"typebox": "^1.0.63"
"typebox": "^1.0.64"
},
"devDependencies": {
"@types/node": "^22.19.3",

View file

@ -3,6 +3,7 @@ import type { ClientProfil } from './chat_types';
import type { User } from '@shared/database/mixin/user';
import { getUserByName } from './getUserByName';
import { Socket } from 'socket.io';
import { escape } from '@shared/utils';
/**
* function makeProfil - translates the Users[] to a one user looking by name
@ -13,7 +14,7 @@ import { Socket } from 'socket.io';
* @returns
*/
export async function makeProfil(fastify: FastifyInstance, user: string, socket: Socket): Promise <ClientProfil> {
export async function makeProfil(fastify: FastifyInstance, user: string, socket: Socket): Promise<ClientProfil> {
let clientProfil!: ClientProfil;
const users: User[] = fastify.db.getAllUsers() ?? [];
@ -29,7 +30,7 @@ export async function makeProfil(fastify: FastifyInstance, user: string, socket:
user: `${allUsers.name}`,
loginName: `${allUsers?.login ?? 'Guest'}`,
userID: `${allUsers?.id ?? ''}`,
text: '',
text: escape(allUsers.desc),
timestamp: Date.now(),
SenderWindowID: socket.id,
SenderName: '',
@ -38,4 +39,4 @@ export async function makeProfil(fastify: FastifyInstance, user: string, socket:
};
}
return clientProfil;
};
};

View file

@ -1234,6 +1234,375 @@
]
}
},
"/api/user/allowGuestMessage": {
"post": {
"operationId": "allowGuestMessage",
"responses": {
"200": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"success"
]
},
"msg": {
"enum": [
"guestMessage.success"
]
}
}
}
}
}
},
"401": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"anyOf": [
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
},
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
}
]
}
}
}
},
"403": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"guestMessage.failure.notLoggedIn"
]
}
}
}
}
}
}
},
"tags": [
"openapi_other"
]
}
},
"/api/user/denyGuestMessage": {
"post": {
"operationId": "denyGuestMessage",
"responses": {
"200": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"success"
]
},
"msg": {
"enum": [
"guestMessage.success"
]
}
}
}
}
}
},
"401": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"anyOf": [
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
},
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
}
]
}
}
}
},
"403": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"guestMessage.failure.notLoggedIn"
]
}
}
}
}
}
}
},
"tags": [
"openapi_other"
]
}
},
"/api/user/changeDesc": {
"post": {
"operationId": "changeDesc",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"desc"
],
"properties": {
"desc": {
"type": "string"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"success"
]
},
"msg": {
"enum": [
"changedesc.success"
]
}
}
}
}
}
},
"400": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"changedesc.failure.descTooLong"
]
}
}
}
}
}
},
"401": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
}
}
}
},
"403": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"changedesc.failure.notLoggedIn"
]
}
}
}
}
}
}
},
"tags": [
"openapi_other"
]
}
},
"/api/user/changeDisplayName": {
"put": {
"operationId": "changeDisplayName",

View file

@ -23,7 +23,6 @@
},
"devDependencies": {
"@eslint/js": "^9.39.2",
"@openapitools/openapi-generator-cli": "^2.25.2",
"@typescript-eslint/eslint-plugin": "^8.50.0",
"@typescript-eslint/parser": "^8.50.0",
"eslint": "^9.39.2",
@ -34,7 +33,7 @@
"vite": "^7.3.0"
},
"dependencies": {
"@redocly/cli": "^2.12.7",
"@redocly/cli": "^2.13.0",
"bindings": "^1.5.0"
}
}

1467
src/pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,366 @@
"schemas": {}
},
"paths": {
"/api/user/allowGuestMessage": {
"post": {
"operationId": "allowGuestMessage",
"responses": {
"200": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"success"
]
},
"msg": {
"enum": [
"guestMessage.success"
]
}
}
}
}
}
},
"401": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"anyOf": [
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
},
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
}
]
}
}
}
},
"403": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"guestMessage.failure.notLoggedIn"
]
}
}
}
}
}
}
}
}
},
"/api/user/denyGuestMessage": {
"post": {
"operationId": "denyGuestMessage",
"responses": {
"200": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"success"
]
},
"msg": {
"enum": [
"guestMessage.success"
]
}
}
}
}
}
},
"401": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"anyOf": [
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
},
{
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
}
]
}
}
}
},
"403": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"guestMessage.failure.notLoggedIn"
]
}
}
}
}
}
}
}
}
},
"/api/user/changeDesc": {
"post": {
"operationId": "changeDesc",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"desc"
],
"properties": {
"desc": {
"type": "string"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"success"
]
},
"msg": {
"enum": [
"changedesc.success"
]
}
}
}
}
}
},
"400": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"changedesc.failure.descTooLong"
]
}
}
}
}
}
},
"401": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"notLoggedIn"
]
},
"msg": {
"enum": [
"auth.noCookie",
"auth.invalidKind",
"auth.noUser",
"auth.invalid"
]
}
}
}
}
}
},
"403": {
"description": "Default Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"kind",
"msg"
],
"properties": {
"kind": {
"enum": [
"failure"
]
},
"msg": {
"enum": [
"changedesc.failure.notLoggedIn"
]
}
}
}
}
}
}
}
}
},
"/api/user/changeDisplayName": {
"put": {
"operationId": "changeDisplayName",

View file

@ -26,7 +26,7 @@
"fastify": "^5.6.2",
"fastify-cli": "^7.4.1",
"fastify-plugin": "^5.1.0",
"typebox": "^1.0.63"
"typebox": "^1.0.64"
},
"devDependencies": {
"@types/node": "^22.19.3",

View file

@ -0,0 +1,47 @@
import { FastifyPluginAsync } from 'fastify';
import { isNullish, MakeStaticResponse, typeResponse } from '@shared/utils';
export const GuestMessageRes = {
'200': typeResponse('success', 'guestMessage.success'),
'403': typeResponse('failure', 'guestMessage.failure.notLoggedIn'),
};
export type GuestMessageRes = MakeStaticResponse<typeof GuestMessageRes>;
const allowRoute: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post(
'/api/user/allowGuestMessage',
{ schema: { response: GuestMessageRes, operationId: 'allowGuestMessage' }, config: { requireAuth: true } },
async function(req, res) {
if (isNullish(req.authUser)) { return res.makeResponse(403, 'failure', 'guestMessage.failure.notLoggedIn'); }
this.db.allowGuestMessage(req.authUser.id);
return res.makeResponse(200, 'success', 'guestMessage.success');
},
);
};
const denyRoute: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post(
'/api/user/denyGuestMessage',
{ schema: { response: GuestMessageRes, operationId: 'denyGuestMessage' }, config: { requireAuth: true } },
async function(req, res) {
if (isNullish(req.authUser)) { return res.makeResponse(403, 'failure', 'guestMessage.failure.notLoggedIn'); }
this.db.denyGuestMessage(req.authUser.id);
return res.makeResponse(200, 'success', 'guestMessage.success');
},
);
};
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.register(allowRoute);
fastify.register(denyRoute);
};
export default route;

View file

@ -0,0 +1,36 @@
import { FastifyPluginAsync } from 'fastify';
import { Static, Type } from 'typebox';
import { isNullish, MakeStaticResponse, typeResponse } from '@shared/utils';
export const ChangeDescRes = {
'200': typeResponse('success', 'changedesc.success'),
'400': typeResponse('failure', 'changedesc.failure.descTooLong'),
'403': typeResponse('failure', 'changedesc.failure.notLoggedIn'),
};
export type ChangeDescRes = MakeStaticResponse<typeof ChangeDescRes>;
export const ChangeDescBody = Type.Object({ desc: Type.String() });
export type ChangeDescBody = Static<typeof ChangeDescBody>;
const route: FastifyPluginAsync = async (fastify, _opts): Promise<void> => {
void _opts;
fastify.post<{ Body: ChangeDescBody }>(
'/api/user/changeDesc',
{ schema: { body: ChangeDescBody, response: ChangeDescRes, operationId: 'changeDesc' }, config: { requireAuth: true } },
async function(req, res) {
if (isNullish(req.authUser)) { return res.makeResponse(403, 'failure', 'changedesc.failure.notLoggedIn'); }
if (req.body.desc.length > 75) {
return res.makeResponse(400, 'failure', 'changedesc.failure.descTooLong');
}
this.db.setUserDescription(req.authUser.id, req.body.desc);
return res.makeResponse(200, 'success', 'changedesc.success');
},
);
};
export default route;