diff --git a/.idea/vcs.xml b/.idea/vcs.xml index d843f34..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,4 +1,6 @@ - + + + \ No newline at end of file diff --git a/src/lib/stores/socket.ts b/src/lib/stores/socket.ts index ac1fa92..c8766d4 100644 --- a/src/lib/stores/socket.ts +++ b/src/lib/stores/socket.ts @@ -4,14 +4,5 @@ import { io } from "socket.io-client"; export const initSocket = () => { const socketInstance = io("http://localhost:5173"); - // Événements globaux de connexion - socketInstance.on("connect", () => { - console.log("Connected to Socket.IO server:", socketInstance.id); - }); - - socketInstance.on("disconnect", () => { - console.log("Disconnected from Socket.IO server"); - }); - return socketInstance; } \ No newline at end of file diff --git a/src/lib/utils/sort.ts b/src/lib/utils/sort.ts new file mode 100644 index 0000000..5b31700 --- /dev/null +++ b/src/lib/utils/sort.ts @@ -0,0 +1,12 @@ +export function sortChannels(channels) { + channels = channels.map((channel) => { + return { + ...channel, + lastUpdate : channel.lastMessage != null ? channel.lastMessage.createdAt : channel.createdAt + }; + }); + + return channels.sort((a, b) => { + return new Date(b.lastUpdate) - new Date(a.lastUpdate); + }); +} \ No newline at end of file diff --git a/src/routes/api/channels/+server.ts b/src/routes/api/channels/+server.ts index 02ea2b5..691e5cd 100644 --- a/src/routes/api/channels/+server.ts +++ b/src/routes/api/channels/+server.ts @@ -2,6 +2,7 @@ import { json } from '@sveltejs/kit'; import prisma from '$lib/prismaClient'; import redisClient from '$lib/redisClient'; import logger from '$lib/logger'; +import { sortChannels } from '$lib/utils/sort.ts'; // GET: Liste tous les canaux avec leur premier message export async function GET({ params, url }) { @@ -124,17 +125,4 @@ export async function POST({ request }) { } } -function sortChannels(channels) { - channels = channels.map((channel) => { - return { - ...channel, - lastUpdate : channel.lastMessage != null ? channel.lastMessage.createdAt : channel.createdAt - }; - }); - - return channels.sort((a, b) => { - return new Date(b.lastUpdate) - new Date(a.lastUpdate); - }); -} - diff --git a/src/routes/api/channels/[id]/messages/+server.ts b/src/routes/api/channels/[id]/messages/+server.ts index ebd637e..9dcebac 100644 --- a/src/routes/api/channels/[id]/messages/+server.ts +++ b/src/routes/api/channels/[id]/messages/+server.ts @@ -2,6 +2,7 @@ import { json } from '@sveltejs/kit'; import prisma from '$lib/prismaClient'; import redisClient from '$lib/redisClient'; import logger from '$lib/logger'; +import { sortChannels } from '$lib/utils/sort.ts'; export async function GET({ params, url }) { const channelId = params.id; @@ -93,6 +94,12 @@ export async function POST({ params, request }) { profilePicture: true, }, }, + channel: { + select: { + id: true, + name: true, + }, + } }, }); @@ -103,6 +110,15 @@ export async function POST({ params, request }) { value: `message:${newMessage.id}`, }); + //update the channels cache with the new message + const cachedChannels = await redisClient.get('channels'); + let channels = JSON.parse(cachedChannels); + const channel = channels.find((c) => c.id === channelId); + channel.lastMessage = newMessage; + channel.lastUpdate = newMessage.createdAt; + channels = sortChannels(channels); + await redisClient.set('channels', JSON.stringify(channels), { EX: 600 }); + logger.debug(`Nouveau message ajouté pour le channel : ${channelId}`); return json(newMessage, { status: 201 }); } catch (err) { diff --git a/src/routes/chats/+page.svelte b/src/routes/chats/+page.svelte index 04dcaf2..5df12c4 100644 --- a/src/routes/chats/+page.svelte +++ b/src/routes/chats/+page.svelte @@ -15,11 +15,18 @@ let socket = initSocket(); // Initialiser le socket socket.on("new-channel", (channel) => { - console.log(channel); - console.log(channels); channels = [channel, ...channels]; }); + socket.on("new-message", (message) => { + const channel = channels.find((channel) => channel.id === message.channel.id); + if (channel) { + channel.lastMessage = message; + channel.lastUpdate = message.createdAt; + channels = [channel, ...channels.filter((c) => c.id !== channel.id)]; + } + }); + function openProfileCard() { console.log('openProfileCard'); showProfileCard = true; // Inverser l'état pour afficher/masquer le ProfilCard