mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
29 lines
778 B
JavaScript
29 lines
778 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;
|