mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
Update Message.js (#191)
Fixed Error handling, Code duplication and Naming conventions. Contact me for more information at: DavidTheDev#0166 Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
This commit is contained in:
parent
5dd9c11326
commit
4b94af0429
1 changed files with 37 additions and 19 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
const Message = require('./schema/messageSchema');
|
const Message = require('./schema/messageSchema');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Message,
|
Message,
|
||||||
saveMessage: async ({
|
|
||||||
|
async saveMessage({
|
||||||
messageId,
|
messageId,
|
||||||
newMessageId,
|
newMessageId,
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
@ -12,7 +14,7 @@ module.exports = {
|
||||||
error,
|
error,
|
||||||
unfinished,
|
unfinished,
|
||||||
cancelled
|
cancelled
|
||||||
}) => {
|
}) {
|
||||||
try {
|
try {
|
||||||
// may also need to update the conversation here
|
// may also need to update the conversation here
|
||||||
await Message.findOneAndUpdate(
|
await Message.findOneAndUpdate(
|
||||||
|
|
@ -30,39 +32,55 @@ module.exports = {
|
||||||
},
|
},
|
||||||
{ upsert: true, new: true }
|
{ upsert: true, new: true }
|
||||||
);
|
);
|
||||||
return { messageId, conversationId, parentMessageId, sender, text, isCreatedByUser };
|
|
||||||
} catch (error) {
|
return {
|
||||||
console.error(error);
|
messageId,
|
||||||
return { message: 'Error saving message' };
|
conversationId,
|
||||||
|
parentMessageId,
|
||||||
|
sender,
|
||||||
|
text,
|
||||||
|
isCreatedByUser
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Error saving message: ${err}`);
|
||||||
|
throw new Error('Failed to save message.');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
deleteMessagesSince: async ({ messageId, conversationId }) => {
|
|
||||||
|
async deleteMessagesSince({ messageId, conversationId }) {
|
||||||
try {
|
try {
|
||||||
const message = await Message.findOne({ messageId }).exec();
|
const message = await Message.findOne({ messageId }).exec();
|
||||||
|
|
||||||
if (message)
|
if (message) {
|
||||||
return await Message.find({ conversationId })
|
return await Message.find({ conversationId })
|
||||||
.deleteMany({ createdAt: { $gt: message.createdAt } })
|
.deleteMany({ createdAt: { $gt: message.createdAt } })
|
||||||
.exec();
|
.exec();
|
||||||
} catch (error) {
|
}
|
||||||
console.error(error);
|
|
||||||
return { message: 'Error deleting messages' };
|
} catch (err) {
|
||||||
|
console.error(`Error deleting messages: ${err}`);
|
||||||
|
throw new Error('Failed to delete messages.');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getMessages: async (filter) => {
|
|
||||||
|
async getMessages(filter) {
|
||||||
try {
|
try {
|
||||||
return await Message.find(filter).sort({ createdAt: 1 }).exec();
|
return await Message.find(filter).sort({ createdAt: 1 }).exec();
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
} catch (err) {
|
||||||
return { message: 'Error getting messages' };
|
console.error(`Error getting messages: ${err}`);
|
||||||
|
throw new Error('Failed to get messages.');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
deleteMessages: async (filter) => {
|
|
||||||
|
async deleteMessages(filter) {
|
||||||
try {
|
try {
|
||||||
return await Message.deleteMany(filter).exec();
|
return await Message.deleteMany(filter).exec();
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
} catch (err) {
|
||||||
return { message: 'Error deleting messages' };
|
console.error(`Error deleting messages: ${err}`);
|
||||||
|
throw new Error('Failed to delete messages.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue