LibreChat/api/server/middleware/validateMessageReq.js
Danny Avila a2fd975cd5
🚤 refactor: Optimize Request Lifecycle Speeds (#3222)
* refactor: optimize backend operations for client requests

* fix: message styling

* refactor: Improve handleKeyUp logic in StreamRunManager.js and handleText.js

* refactor: Improve handleKeyUp logic in StreamRunManager.js and handleText.js

* fix: clear new convo messages on clear all convos

* fix: forgot to pass userId to getConvo

* refactor: update getPartialText to send basePayload.text
2024-06-28 08:44:47 -04:00

28 lines
774 B
JavaScript

const { getConvo } = require('~/models');
// Middleware to validate conversationId and user relationship
const validateMessageReq = async (req, res, next) => {
let conversationId = req.params.conversationId || req.body.conversationId;
if (conversationId === 'new') {
return res.status(200).send([]);
}
if (!conversationId && req.body.message) {
conversationId = req.body.message.conversationId;
}
const conversation = await getConvo(req.user.id, conversationId);
if (!conversation) {
return res.status(404).json({ error: 'Conversation not found' });
}
if (conversation.user !== req.user.id) {
return res.status(403).json({ error: 'User not authorized for this conversation' });
}
next();
};
module.exports = validateMessageReq;