From cbf953a25ba612822df6793fc0ecb7331777ee1d Mon Sep 17 00:00:00 2001 From: Bilal Dieumegard Date: Mon, 2 Dec 2024 17:48:22 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20r=C3=A9activit=C3=A9=20pour?= =?UTF-8?q?=20les=20messages=20et=20pour=20les=20channels.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/components/ui/CreateChat.svelte | 2 +- src/lib/components/ui/Search.svelte | 5 +- src/routes/api/channels/+server.ts | 91 +++++++++++++------ .../api/channels/[id]/messages/+server.ts | 4 +- src/routes/chats/+page.svelte | 16 +++- src/routes/chats/[id]/+page.server.ts | 19 ++++ src/routes/chats/[id]/+page.svelte | 6 +- 7 files changed, 107 insertions(+), 36 deletions(-) create mode 100644 src/routes/chats/[id]/+page.server.ts diff --git a/src/lib/components/ui/CreateChat.svelte b/src/lib/components/ui/CreateChat.svelte index 0f62da7..c1e6dd8 100644 --- a/src/lib/components/ui/CreateChat.svelte +++ b/src/lib/components/ui/CreateChat.svelte @@ -15,7 +15,7 @@ if (chatName.trim()) { try { // Appel API pour créer le chat - const response = await fetch('/api/canals', { + const response = await fetch('/api/channels', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/lib/components/ui/Search.svelte b/src/lib/components/ui/Search.svelte index 749d684..cec4c25 100644 --- a/src/lib/components/ui/Search.svelte +++ b/src/lib/components/ui/Search.svelte @@ -2,15 +2,18 @@ import { Search } from "lucide-svelte"; // Icône de recherche depuis Lucide Svelte export let placeholder: string = "Rechercher..."; // Texte par défaut pour l'input + + export let onChange: (value: string) => void = () => {}; // Callback pour la recherche
- + onChange(event.target.value)} />
diff --git a/src/routes/api/channels/+server.ts b/src/routes/api/channels/+server.ts index 5e21e42..1947d03 100644 --- a/src/routes/api/channels/+server.ts +++ b/src/routes/api/channels/+server.ts @@ -4,40 +4,75 @@ import redisClient from '$lib/redisClient'; import logger from '$lib/logger'; // GET: Liste tous les canaux avec leur premier message -export async function GET() { - try { - const cachedChannels = await redisClient.get('channels'); - - if (cachedChannels != null) { - logger.debug('Cache entry found, fetching channels from cache'); - return json(JSON.parse(cachedChannels)); - } - - logger.debug('No cache entry was found, fetching channels from database'); - let canaux = await prisma.channel.findMany({ - include: { - messages: { - take: 1, // Récupère le dernier message - orderBy: { createdAt: 'desc' }, // Trie par date décroissante +export async function GET({ params, url }) { + if(url.searchParams.get("name") != null && url.searchParams.get("name") != ""){ + const name = url.searchParams.get("name"); + try { + let canaux = await prisma.channel.findMany({ + where: { + name: { + contains: name, + mode: 'insensitive', + }, }, - }, - }); + include: { + messages: { + take: 1, // Récupère le dernier message + orderBy: { createdAt: 'desc' }, // Trie par date décroissante + }, + }, + }); - canaux = canaux.sort((a, b) => { - const lastMessageA = a.messages[0]?.createdAt || a.createdAt ? a.createdAt : new Date(); - const lastMessageB = b.messages[0]?.createdAt || b.createdAt ? b.createdAt : new Date(); - return new Date(lastMessageB).getTime() - new Date(lastMessageA).getTime(); - }); + canaux = canaux.sort((a, b) => { + const lastMessageA = a.messages[0]?.createdAt || a.createdAt; + const lastMessageB = b.messages[0]?.createdAt || b.createdAt; - logger.debug('Caching channels with EX of 3600 secs'); - await redisClient.set('channels', JSON.stringify(canaux), { EX: 3600 }); + return new Date(lastMessageB).getTime() - new Date(lastMessageA).getTime(); + }); - return json(canaux); + return json(canaux); - } catch (err) { - logger.error(err) - return json({ error: 'Erreur serveur' }, { status: 500 }); + } catch (err) { + logger.error(err); + return json({ error: 'Erreur serveur' }, { status: 500 }); + } + }else{ + try { + const cachedChannels = await redisClient.get('channels'); + + if (cachedChannels != null) { + logger.debug('Cache entry found, fetching channels from cache'); + return json(JSON.parse(cachedChannels)); + } + + logger.debug('No cache entry was found, fetching channels from database'); + let canaux = await prisma.channel.findMany({ + include: { + messages: { + take: 1, // Récupère le dernier message + orderBy: { createdAt: 'desc' }, // Trie par date décroissante + }, + }, + }); + + 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(); + }); + + logger.debug('Caching channels with EX of 3600 secs'); + await redisClient.set('channels', JSON.stringify(canaux), { EX: 3600 }); + + return json(canaux); + + } catch (err) { + logger.error(err) + return json({ error: 'Erreur serveur' }, { status: 500 }); + } } + } export async function POST({ request }) { diff --git a/src/routes/api/channels/[id]/messages/+server.ts b/src/routes/api/channels/[id]/messages/+server.ts index 029b222..a294ccf 100644 --- a/src/routes/api/channels/[id]/messages/+server.ts +++ b/src/routes/api/channels/[id]/messages/+server.ts @@ -8,11 +8,11 @@ export async function GET({ params, url }) { // @ts-ignore const page = url.searchParams.get('page') != null ? parseInt(url.searchParams.get('page')) : 1; // @ts-ignore - const limit = url.searchParams.get('limit') != null ? parseInt(url.searchParams.get('limit')) : 10; + const limit = 10; const offset = (page - 1) * limit; // Générer une clé cache Redis unique en fonction du canal et des paramètres de pagination - const cacheKey = `channel:${channelId}:messages:page:${page}:limit:${limit}`; + const cacheKey = `channel:${channelId}:messages:page:${page}`; try { const cachedMessages = await redisClient.get(cacheKey); diff --git a/src/routes/chats/+page.svelte b/src/routes/chats/+page.svelte index 26fe869..f0f031d 100644 --- a/src/routes/chats/+page.svelte +++ b/src/routes/chats/+page.svelte @@ -38,10 +38,20 @@ showCreateChat = false; // Fermer le composant CreateChat } + async function loadNewChannels(name : string) { + console.log('loadNewChannels'); + const res = await fetch(`/api/channels?name=${name}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + } + }); + channels = await res.json(); + console.log(channels); + } + export let data; export let channels = data.channels;// Assurez-vous que 'lastMessage' est facultatif si nécessaire - console.log(channels); -
@@ -58,7 +68,7 @@
- +
diff --git a/src/routes/chats/[id]/+page.server.ts b/src/routes/chats/[id]/+page.server.ts new file mode 100644 index 0000000..8fa2c3d --- /dev/null +++ b/src/routes/chats/[id]/+page.server.ts @@ -0,0 +1,19 @@ +export async function load({ fetch, params }) { + try { + const res = await fetch(`/api/channels/${params.id}/messages?page=1`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + } + const messages = await res.json(); + return { + messages + } + }catch (error) { + console.error('Erreur lors du chargement des messages:', error); + return { + messages: [] + }; + } +} \ No newline at end of file diff --git a/src/routes/chats/[id]/+page.svelte b/src/routes/chats/[id]/+page.svelte index d57418a..f5f6507 100644 --- a/src/routes/chats/[id]/+page.svelte +++ b/src/routes/chats/[id]/+page.svelte @@ -2,7 +2,11 @@ import Textarea from "../../../lib/components/ui/textarea/textarea.svelte"; import { Button } from "$lib/components/ui/button"; import PaperPlane from "svelte-radix/PaperPlane.svelte"; - import Message from "$lib/components/Message.svelte"; + import Message from "$lib/components/Message.svelte"; + + export let data; + export let messages = data.messages; +