generated from lucien/api-template
add: added limit param to message get url to send fewer messages
This commit is contained in:
parent
9052a99972
commit
bd3cbc528c
3 changed files with 19 additions and 10 deletions
|
@ -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('@')) {
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue