diff --git a/back/api/channels.js b/back/api/channels.js index ebafd8c..1f1c17e 100644 --- a/back/api/channels.js +++ b/back/api/channels.js @@ -4,17 +4,13 @@ const { getChannels, getChannel, addChannel, - getMessage, getMessages, addMessage, - deleteMessage, addMention, getMentions, getUserByUsername, deleteChannelMessages, deleteChannel, - setHasReplies, - addReply, getMessageReplies } = require('../libs/mysql'); const rateLimit = require("express-rate-limit"); @@ -130,13 +126,14 @@ async function getReplies(message, connection) { router.get('/:name/messages', async (req, res) => { const name = req.params.name; + const limit = Number(req.query.limit) || 10; const connection = await getConnection(); const channel = await getChannel(connection, name); if (!channel[0]) { connection.end(); return res.send('No channel found'); } - const messages = await getMessages(connection, channel[0].id); + const messages = await getMessages(connection, channel[0].id, limit); for (const message of messages) { if (message.content.includes('@')) { diff --git a/back/libs/mysql.js b/back/libs/mysql.js index ef277e9..61ed50a 100644 --- a/back/libs/mysql.js +++ b/back/libs/mysql.js @@ -335,7 +335,7 @@ function deleteChannelMessages(connection, channel_id) { }); } -function getMessages(connection, channel_id) { +function getMessages(connection, channel_id, limit) { return new Promise((resolve, reject) => { connection.query( `SELECT messages.id, user_id, username, content, date, channels.name AS channel_name, has_replies @@ -343,8 +343,9 @@ function getMessages(connection, channel_id) { JOIN users ON messages.user_id = users.id JOIN channels ON messages.channel_id = channels.id WHERE channel_id = ? AND reply_to_id IS NULL - ORDER BY date DESC`, - [channel_id], + ORDER BY date DESC + LIMIT ?`, + [channel_id, limit], (error, result) => { if (error) { reject(new Error(error)); diff --git a/front/src/pages/ChannelPage.tsx b/front/src/pages/ChannelPage.tsx index 374199c..2324e4a 100644 --- a/front/src/pages/ChannelPage.tsx +++ b/front/src/pages/ChannelPage.tsx @@ -139,6 +139,17 @@ export default function ChannelPage({socket}: {socket: WebSocket}) { setSearchedEmojis([]); } }, [message]); + + useEffect(() => { + axios + .get(`/api/channels/${name}/messages?limit=${maxMessageToShown}`) + .then((res) => { + setMessages(res.data); + }) + .catch((err) => { + console.error(err.response.data); + }); + }, [maxMessageToShown]); if (noChannel) { return ( @@ -247,7 +258,7 @@ export default function ChannelPage({socket}: {socket: WebSocket}) { {messages ? (
No messages yet.
} - {messages.slice(0, maxMessageToShown).map((message) => ( + {messages.map((message) => (