add message rendering, fix sse cancling, add user id copying
This commit is contained in:
parent
f1539bdffa
commit
3a0f096ade
6 changed files with 155 additions and 42 deletions
|
|
@ -1,24 +0,0 @@
|
|||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const GET: RequestHandler = async ({ params }) => {
|
||||
const { groupOrServerId, channelId } = params;
|
||||
|
||||
const isGroup = !channelId;
|
||||
|
||||
// fake messages
|
||||
const messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: `user_${Math.floor(Math.random() * 10)}`,
|
||||
content: isGroup ? `Group message #${i + 1}` : `Server message #${i + 1}`,
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
|
||||
return json({
|
||||
type: isGroup ? 'group' : 'server',
|
||||
groupId: isGroup ? groupOrServerId : null,
|
||||
serverId: isGroup ? null : groupOrServerId,
|
||||
channelId: channelId ?? null,
|
||||
messages
|
||||
});
|
||||
};
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
import { fail, json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { GroupID, ServerID, UserID } from '$lib';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, locals }) => {
|
||||
if (!locals.user) {
|
||||
return new Response('No authentication', { status: 401 });
|
||||
}
|
||||
|
||||
const { grp_srv_dm, channelId } = params;
|
||||
if (!grp_srv_dm) {
|
||||
return new Response('Missing group, server, or DM ID.', { status: 400 });
|
||||
}
|
||||
let messages = [];
|
||||
let type = '';
|
||||
|
||||
if (GroupID.is(grp_srv_dm)) {
|
||||
type = 'group';
|
||||
messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: `user_${Math.floor(Math.random() * 10)}`,
|
||||
content: 'group message ' + (i + 1),
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
} else if (ServerID.is(grp_srv_dm)) {
|
||||
type = 'server';
|
||||
|
||||
if (!channelId) {
|
||||
return new Response('Missing channel ID.', { status: 400 });
|
||||
}
|
||||
|
||||
messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: `user_${Math.floor(Math.random() * 10)}`,
|
||||
content: 'server message ' + (i + 1),
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
} else if (UserID.is(grp_srv_dm)) {
|
||||
type = 'dms';
|
||||
messages = Array.from({ length: 5 }, (_, i) => ({
|
||||
id: crypto.randomUUID(),
|
||||
authorId: Math.random() > 0.5 ? locals.user.id : grp_srv_dm,
|
||||
content: 'dm message ' + (i + 1),
|
||||
timestamp: Date.now() - Math.floor(Math.random() * 100000)
|
||||
}));
|
||||
}
|
||||
|
||||
return json({
|
||||
type,
|
||||
id: grp_srv_dm,
|
||||
messages
|
||||
});
|
||||
};
|
||||
|
|
@ -66,6 +66,16 @@ export async function GET({ locals, request }) {
|
|||
kvStore.set(`user-${userId}-state`, Status.OFFLINE);
|
||||
_sendToSubscribers(userId, { type: 'status', id: userId, status: Status.OFFLINE });
|
||||
});
|
||||
},
|
||||
cancel() {
|
||||
console.log(`SSE Client cancelled. total: ${_clients.size}`);
|
||||
|
||||
if (_isUserConnected(userId)) return;
|
||||
|
||||
if (overwrite === Status.OFFLINE) return;
|
||||
|
||||
kvStore.set(`user-${userId}-state`, Status.OFFLINE);
|
||||
_sendToSubscribers(userId, { type: 'status', id: userId, status: Status.OFFLINE });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue