From 45eba3eb1d526d11f1dfc2134c07eb2253b92cb6 Mon Sep 17 00:00:00 2001 From: Bilal Dieumegard Date: Mon, 2 Dec 2024 19:53:40 +0100 Subject: [PATCH] =?UTF-8?q?Modification=20de=20la=20page=20de=20chat=20et?= =?UTF-8?q?=20des=20requetes=20de=20cr=C3=A9ation=20de=20channel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/components/Message.svelte | 52 +++++++++++--- src/lib/components/ui/UserChat.svelte | 58 ++++++++++++++++ src/routes/api/channels/+server.ts | 36 +++++----- .../api/channels/[id]/messages/+server.ts | 8 +-- src/routes/chats/[id]/+page.server.ts | 2 +- src/routes/chats/[id]/+page.svelte | 69 +++++++++++++++---- 6 files changed, 180 insertions(+), 45 deletions(-) create mode 100644 src/lib/components/ui/UserChat.svelte diff --git a/src/lib/components/Message.svelte b/src/lib/components/Message.svelte index 4d86257..7351fb7 100644 --- a/src/lib/components/Message.svelte +++ b/src/lib/components/Message.svelte @@ -1,15 +1,47 @@ - - - {username} - - + + + +
+ Profile Picture + +
+ + {username} + +
+
+ + {formatDistanceToNow(createdAt)} + +
+ + + +

{messageContent} - - \ No newline at end of file +

+
+
+ + diff --git a/src/lib/components/ui/UserChat.svelte b/src/lib/components/ui/UserChat.svelte new file mode 100644 index 0000000..d119dfe --- /dev/null +++ b/src/lib/components/ui/UserChat.svelte @@ -0,0 +1,58 @@ + + +
+ Profile +
+ {username} +
+ {status} + {#if status === "En ligne"} + + + + {:else} + + + + {/if} +
+
+
+ + diff --git a/src/routes/api/channels/+server.ts b/src/routes/api/channels/+server.ts index 1947d03..a38ed3d 100644 --- a/src/routes/api/channels/+server.ts +++ b/src/routes/api/channels/+server.ts @@ -23,12 +23,7 @@ export async function GET({ params, url }) { }, }); - canaux = canaux.sort((a, b) => { - const lastMessageA = a.messages[0]?.createdAt || a.createdAt; - const lastMessageB = b.messages[0]?.createdAt || b.createdAt; - - return new Date(lastMessageB).getTime() - new Date(lastMessageA).getTime(); - }); + canaux = sortChannels(canaux); return json(canaux); @@ -55,12 +50,7 @@ export async function GET({ params, url }) { }, }); - canaux = canaux.sort((a, b) => { - const lastMessageA = a.messages[0]?.createdAt || a.createdAt; - const lastMessageB = b.messages[0]?.createdAt || b.createdAt; - - return new Date(lastMessageB).getTime() - new Date(lastMessageA).getTime(); - }); + canaux = sortChannels(canaux); logger.debug('Caching channels with EX of 3600 secs'); await redisClient.set('channels', JSON.stringify(canaux), { EX: 3600 }); @@ -81,28 +71,40 @@ export async function POST({ request }) { try { const canal = await prisma.channel.create({ data: { - name, - createdAt: new Date(), + name }, }); logger.debug('Creating a new channel in database with id ' + canal.id); const cachedChanels = await redisClient.get('channels'); - const channels = cachedChanels != null ? JSON.parse(cachedChanels) : []; + + let channels = cachedChanels != null ? JSON.parse(cachedChanels) : []; + console.log(channels); + console.log(canal); channels.push(canal); + channels = sortChannels(channels); + console.log(channels); + logger.debug(`Added channel (${canal.id}) to channels cache.`); await redisClient.set('channels', JSON.stringify(channels), { EX: 600 }); return json(canal, { status: 201 }); } catch (err) { + console.log(err); logger.error(err); return json({ error: 'Erreur lors de la création du canal' }, { status: 500 }); } } +function sortChannels(channels) { + return channels.sort((a, b) => { + // Vérifie si 'a.messages' existe et est un tableau, sinon utilise la date de création du canal + const lastMessageA = Array.isArray(a.messages) && a.messages.length > 0 ? a.messages[0]?.createdAt : a.createdAt; + const lastMessageB = Array.isArray(b.messages) && b.messages.length > 0 ? b.messages[0]?.createdAt : b.createdAt; - - + return new Date(lastMessageB).getTime() - new Date(lastMessageA).getTime(); + }); +} diff --git a/src/routes/api/channels/[id]/messages/+server.ts b/src/routes/api/channels/[id]/messages/+server.ts index a294ccf..b777297 100644 --- a/src/routes/api/channels/[id]/messages/+server.ts +++ b/src/routes/api/channels/[id]/messages/+server.ts @@ -104,21 +104,21 @@ export async function DELETE({ params }) { } // Fonction pour mettre à jour tous les caches des messages -function updateCaches(channelId: string) { +async function updateCaches(channelId: string) { let page : number = 1; let limit : number = 10; let offset : number = (page - 1) * limit; while (true) { - const cacheKey = `channel:${channelId}:messages:page:${page}:limit:${limit}`; + const cacheKey = `channel:${channelId}:messages:page:${page}`; const cachedMessages = await redisClient.get(cacheKey); if (!cachedMessages) { break; } const totalMessages = await prisma.message.count({ - where: { canalId }, + where: { channelId }, }); const messages = await prisma.message.findMany({ - where: { canalId }, + where: { channelId }, include: { user: { select: { id: true, pseudo: true }, diff --git a/src/routes/chats/[id]/+page.server.ts b/src/routes/chats/[id]/+page.server.ts index 8fa2c3d..346867e 100644 --- a/src/routes/chats/[id]/+page.server.ts +++ b/src/routes/chats/[id]/+page.server.ts @@ -5,7 +5,7 @@ export async function load({ fetch, params }) { headers: { 'Content-Type': 'application/json' } - } + }); const messages = await res.json(); return { messages diff --git a/src/routes/chats/[id]/+page.svelte b/src/routes/chats/[id]/+page.svelte index f5f6507..e63c15c 100644 --- a/src/routes/chats/[id]/+page.svelte +++ b/src/routes/chats/[id]/+page.svelte @@ -1,23 +1,66 @@ -
-
- - +
+ +
+

Utilisateurs

+
+ + {#each users as user} + + {/each} +
-
-