reset drizzle, get DMs working (partly, usernames aren't resolved)

This commit is contained in:
Soph :3 2026-01-04 23:13:39 +02:00
parent 3a0f096ade
commit bf679f9ee0
32 changed files with 1150 additions and 2867 deletions

View file

@ -1,17 +1,18 @@
import { Status } from '$lib';
import { kvStore } from '$lib/server/db/index.js';
import { json } from '@sveltejs/kit';
interface SubscribedTo {
subscribed: string[];
subscribed: string[]; // any ID (including channels, dms, et cetera can be added in here, to send SSE data to any group of users)
userId: string;
controller: ReadableStreamDefaultController;
}
export const _clients = new Map<string, SubscribedTo>();
export function _sendToSubscribers(userId: string, payload: unknown) {
export function _sendToSubscribers(id: string, payload: unknown) {
for (const [key, client] of _clients) {
if (client.subscribed.includes(userId)) {
if (client.subscribed.includes(id)) {
try {
client.controller.enqueue(`data: ${JSON.stringify(payload)}\n\n`);
} catch {
@ -30,7 +31,7 @@ export function _isUserConnected(userId: string): boolean {
export async function GET({ locals, request }) {
if (!locals.user) {
return new Response('No authentication', { status: 401 });
return json({ error: 'Unauthorized' }, { status: 401 });
}
const userId = locals.user.id;
@ -38,14 +39,14 @@ export async function GET({ locals, request }) {
const subscribed = locals.user.friends.map((f) => f.id);
const overwrite = locals.user.statusOverwrite;
const reqId = crypto.randomUUID();
const sessionId = crypto.randomUUID();
const stream = new ReadableStream({
start(controller) {
_clients.set(reqId, { subscribed, userId, controller });
_clients.set(sessionId, { subscribed, userId, controller });
console.log(`SSE Client opened. total: ${_clients.size}`);
controller.enqueue(`data: ${JSON.stringify({ type: 'connected' })}\n\n`);
controller.enqueue(`data: ${JSON.stringify({ type: 'connected', sessionId })}\n\n`);
if (overwrite === Status.DND) {
kvStore.set(`user-${userId}-state`, Status.DND);
@ -56,7 +57,7 @@ export async function GET({ locals, request }) {
}
request.signal.addEventListener('abort', () => {
_clients.delete(reqId);
_clients.delete(sessionId);
console.log(`SSE Client aborted. total: ${_clients.size}`);
if (_isUserConnected(userId)) return;

View file

@ -0,0 +1,111 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { _clients } from '../+server';
import { DirectMessageID } from '$lib';
import { db } from '$lib/server/db';
import * as table from '$lib/server/db/schema';
import { or } from 'drizzle-orm';
import { eq } from 'drizzle-orm';
export const POST: RequestHandler = async ({ params, request, locals }) => {
if (!locals.user) {
return json({ error: 'Unauthorized' }, { status: 401 });
}
const { sessionId } = params;
if (!sessionId) {
return json({ error: 'Missing sessionId' }, { status: 400 });
}
const client = _clients.get(sessionId);
if (!client) {
return json({ error: 'Client not found' }, { status: 404 });
}
if (client.userId !== locals.user.id) {
return json({ error: 'Unauthorized sessionID access' }, { status: 401 });
}
const body = await request.json();
const { subscribeTo } = body;
if (typeof subscribeTo !== 'string' || !subscribeTo.trim()) {
return json({ error: 'Invalid subscribeTo value' }, { status: 400 });
}
let isValidDmid = false;
if (DirectMessageID.is(subscribeTo)) {
const find = await db
.select()
.from(table.directMessage)
.where(
or(
eq(table.directMessage.firstMember, locals.user.id),
eq(table.directMessage.secondMember, locals.user.id)
)
);
if (find?.length != 0) {
isValidDmid = true;
}
}
if (
!(
isValidDmid ||
locals.user.groups.find((z) => z.id === subscribeTo) ||
locals.user.servers.find((z) => z.id === subscribeTo)
)
) {
return json({ error: 'Invalid subscription' }, { status: 401 });
}
if (!client.subscribed.includes(subscribeTo)) {
client.subscribed.push(subscribeTo);
}
return json({
message: `Added ${subscribeTo} to client subscriptions.`,
subscribed: client.subscribed
});
};
export const DELETE: RequestHandler = async ({ params, request, locals }) => {
if (!locals.user) {
return json({ error: 'Unauthorized' }, { status: 401 });
}
const { sessionId } = params;
if (!sessionId) {
return json({ error: 'Missing sessionId' }, { status: 400 });
}
const client = _clients.get(sessionId);
if (!client) {
return json({ error: 'Client not found' }, { status: 404 });
}
if (client.userId !== locals.user.id) {
return json({ error: 'Unauthorized sessionID access' }, { status: 401 });
}
const body = await request.json();
const { subscribeTo } = body;
if (typeof subscribeTo !== 'string' || !subscribeTo.trim()) {
return json({ error: 'Invalid subscribeTo value' }, { status: 400 });
}
const index = client.subscribed.indexOf(subscribeTo);
if (index !== -1) {
client.subscribed.splice(index, 1);
} else {
return json({ error: 'Subscription not found' }, { status: 404 });
}
return json({
message: `Removed ${subscribeTo} from client subscriptions.`,
subscribed: client.subscribed
});
};