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}
-
-
+
+
+
+
+
+
+
+
+ {username}
+
+
+
+
+ {formatDistanceToNow(createdAt)}
+
+
+
+
+
+
{messageContent}
-
-
\ No newline at end of file
+
+
+
+
+
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/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 @@
+
+
+
+
+
+
{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 5e21e42..a38ed3d 100644
--- a/src/routes/api/channels/+server.ts
+++ b/src/routes/api/channels/+server.ts
@@ -4,40 +4,65 @@ 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 = sortChannels(canaux);
- logger.debug('Caching channels with EX of 3600 secs');
- await redisClient.set('channels', JSON.stringify(canaux), { EX: 3600 });
+ return json(canaux);
- return json(canaux);
+ } catch (err) {
+ logger.error(err);
+ return json({ error: 'Erreur serveur' }, { status: 500 });
+ }
+ }else{
+ try {
+ const cachedChannels = await redisClient.get('channels');
- } catch (err) {
- logger.error(err)
- return json({ error: 'Erreur serveur' }, { status: 500 });
+ 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 = sortChannels(canaux);
+
+ 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 }) {
@@ -46,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 029b222..b777297 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);
@@ -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/+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..346867e
--- /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..e3f47a3 100644
--- a/src/routes/chats/[id]/+page.svelte
+++ b/src/routes/chats/[id]/+page.svelte
@@ -1,19 +1,60 @@
-
-
-
-
+
+
+
+
Utilisateurs
+
+ {#each users as user}
+
+ {/each}
+
-
-
-
+
+
+
+
+
+
+ {#if messages.length > 0}
+ {#each messages as message}
+
+ {/each}
+ {:else}
+
Sélectionnez un message le chat est vide.
+ {/if}
+
+
+
+
-
\ No newline at end of file
+
+
+