Mise à jour des channels lors de l'ajout d'un nouveau message.

This commit is contained in:
Bilal Dieumegard 2024-12-09 16:42:21 +01:00
parent 872573d6c5
commit 3125a11f85
6 changed files with 41 additions and 25 deletions

4
.idea/vcs.xml generated
View file

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View file

@ -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;
}

12
src/lib/utils/sort.ts Normal file
View file

@ -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);
});
}

View file

@ -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);
});
}

View file

@ -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) {

View file

@ -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