|
@@ -38,6 +38,82 @@ const listMessagesById = async (req, res, next) => {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+const editMessage = async (req, res, next) => {
|
|
|
|
+ try {
|
|
|
|
+ const userId = req.user.id;
|
|
|
|
+ const { id, message, caption } = req.body;
|
|
|
|
+ const userMessage = await MessageModel.getById(id, userId);
|
|
|
|
+ await MessageModel.updateMessage(id, userId, {
|
|
|
|
+ message: userMessage.fullType === null ? message : userMessage.message,
|
|
|
|
+ caption,
|
|
|
|
+ edited: true,
|
|
|
|
+ });
|
|
|
|
+ await MessageModel.findByFieldsAndUpdateMany(
|
|
|
|
+ {
|
|
|
|
+ oldId: userMessage._id,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ forwardMessage:
|
|
|
|
+ userMessage.fullType === null ? message : userMessage.forwardMessage,
|
|
|
|
+ forwardCaption: caption,
|
|
|
|
+ replyMessage:
|
|
|
|
+ userMessage.fullType === null ? message : userMessage.replyMessage,
|
|
|
|
+ replyCaption: caption,
|
|
|
|
+ edited: true,
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+ const companionMessage = await MessageModel.findByFields(
|
|
|
|
+ userId,
|
|
|
|
+ userMessage.idTime,
|
|
|
|
+ userMessage.companionId
|
|
|
|
+ );
|
|
|
|
+ await MessageModel.findByFieldsAndUpdate(
|
|
|
|
+ userId,
|
|
|
|
+ userMessage.idTime,
|
|
|
|
+ userMessage.companionId,
|
|
|
|
+ {
|
|
|
|
+ message: userMessage.fullType === null ? message : userMessage.message,
|
|
|
|
+ caption,
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ await MessageModel.findByFieldsAndUpdateMany(
|
|
|
|
+ {
|
|
|
|
+ oldId: companionMessage._id,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ forwardMessage:
|
|
|
|
+ userMessage.fullType === null ? message : userMessage.forwardMessage,
|
|
|
|
+ forwardCaption: caption,
|
|
|
|
+ replyMessage:
|
|
|
|
+ userMessage.fullType === null ? message : userMessage.replyMessage,
|
|
|
|
+ replyCaption: caption,
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+ const isChat = await ChatModel.getByField(userMessage.companionId, userId);
|
|
|
|
+ const isCompanionChat = await ChatModel.getByField(
|
|
|
|
+ userId,
|
|
|
|
+ userMessage.companionId
|
|
|
|
+ );
|
|
|
|
+ const updatedAt = new Date();
|
|
|
|
+ await ChatModel.update(isChat._id, userId, {
|
|
|
|
+ lastMessage: 'The Message was Edited',
|
|
|
|
+ lastMessageCreatedAt: updatedAt,
|
|
|
|
+ });
|
|
|
|
+ await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
|
|
|
|
+ lastMessage: 'The Message was Edited',
|
|
|
|
+ lastMessageCreatedAt: updatedAt,
|
|
|
|
+ });
|
|
|
|
+ return res.status(200).json({
|
|
|
|
+ status: 'success',
|
|
|
|
+ code: 200,
|
|
|
|
+ data: {},
|
|
|
|
+ });
|
|
|
|
+ } catch (e) {
|
|
|
|
+ next(e);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
const updateMessageEmoji = async (req, res, next) => {
|
|
const updateMessageEmoji = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const id = req.params.id;
|
|
const id = req.params.id;
|
|
@@ -126,7 +202,6 @@ const removeMessage = async (req, res, next) => {
|
|
oldId: userMessage._id,
|
|
oldId: userMessage._id,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- deleted: true,
|
|
|
|
replyMessage: null,
|
|
replyMessage: null,
|
|
replyName: null,
|
|
replyName: null,
|
|
replyLastName: null,
|
|
replyLastName: null,
|
|
@@ -151,7 +226,6 @@ const removeMessage = async (req, res, next) => {
|
|
oldId: companionMessage._id,
|
|
oldId: companionMessage._id,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- deleted: true,
|
|
|
|
replyMessage: null,
|
|
replyMessage: null,
|
|
replyName: null,
|
|
replyName: null,
|
|
replyLastName: null,
|
|
replyLastName: null,
|
|
@@ -187,7 +261,7 @@ const removeMessage = async (req, res, next) => {
|
|
userId,
|
|
userId,
|
|
userMessage.companionId
|
|
userMessage.companionId
|
|
);
|
|
);
|
|
- const { total, messages } = await MessageModel.getList(
|
|
|
|
|
|
+ const { total } = await MessageModel.getList(
|
|
{ owner: userId, companionId: userMessage.companionId },
|
|
{ owner: userId, companionId: userMessage.companionId },
|
|
{}
|
|
{}
|
|
);
|
|
);
|
|
@@ -202,28 +276,20 @@ const removeMessage = async (req, res, next) => {
|
|
return total;
|
|
return total;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
|
|
|
|
|
|
+ const updatedAt = new Date();
|
|
await ChatModel.update(isChat._id, userId, {
|
|
await ChatModel.update(isChat._id, userId, {
|
|
total: total,
|
|
total: total,
|
|
seen: seenCalculate(isChat.seen, total),
|
|
seen: seenCalculate(isChat.seen, total),
|
|
seenCompanion: seenCalculate(isChat.seenCompanion, total),
|
|
seenCompanion: seenCalculate(isChat.seenCompanion, total),
|
|
- lastMessage: isLastMessages
|
|
|
|
- ? isLastMessages.type === 'text'
|
|
|
|
- ? isLastMessages.message
|
|
|
|
- : isLastMessages.fullType
|
|
|
|
- : null,
|
|
|
|
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
|
|
|
|
|
|
+ lastMessage: 'The Message was Deleted',
|
|
|
|
+ lastMessageCreatedAt: updatedAt,
|
|
});
|
|
});
|
|
await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
|
|
await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
|
|
total: Total,
|
|
total: Total,
|
|
seen: seenCalculate(isCompanionChat.seen, Total),
|
|
seen: seenCalculate(isCompanionChat.seen, Total),
|
|
seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
|
|
seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
|
|
- lastMessage: isLastMessages
|
|
|
|
- ? isLastMessages.type === 'text'
|
|
|
|
- ? isLastMessages.message
|
|
|
|
- : isLastMessages.fullType
|
|
|
|
- : null,
|
|
|
|
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
|
|
|
|
|
|
+ lastMessage: 'The Message was Deleted',
|
|
|
|
+ lastMessageCreatedAt: updatedAt,
|
|
});
|
|
});
|
|
return res.json({
|
|
return res.json({
|
|
status: 'success',
|
|
status: 'success',
|
|
@@ -246,7 +312,6 @@ const removeSelected = async (req, res, next) => {
|
|
oldId: userMessage._id,
|
|
oldId: userMessage._id,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- deleted: true,
|
|
|
|
replyMessage: null,
|
|
replyMessage: null,
|
|
replyName: null,
|
|
replyName: null,
|
|
replyLastName: null,
|
|
replyLastName: null,
|
|
@@ -271,7 +336,6 @@ const removeSelected = async (req, res, next) => {
|
|
oldId: companionMessage._id,
|
|
oldId: companionMessage._id,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- deleted: true,
|
|
|
|
replyMessage: null,
|
|
replyMessage: null,
|
|
replyName: null,
|
|
replyName: null,
|
|
replyLastName: null,
|
|
replyLastName: null,
|
|
@@ -304,7 +368,7 @@ const removeSelected = async (req, res, next) => {
|
|
await selectedArr.forEach(async (id) => await toDeleteMessage(id));
|
|
await selectedArr.forEach(async (id) => await toDeleteMessage(id));
|
|
const isChat = await ChatModel.getByField(companionId, userId);
|
|
const isChat = await ChatModel.getByField(companionId, userId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, companionId);
|
|
const isCompanionChat = await ChatModel.getByField(userId, companionId);
|
|
- const { total, messages } = await MessageModel.getList(
|
|
|
|
|
|
+ const { total } = await MessageModel.getList(
|
|
{ owner: userId, companionId },
|
|
{ owner: userId, companionId },
|
|
{}
|
|
{}
|
|
);
|
|
);
|
|
@@ -319,28 +383,20 @@ const removeSelected = async (req, res, next) => {
|
|
return total;
|
|
return total;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
|
|
|
|
|
|
+ const updatedAt = new Date();
|
|
await ChatModel.update(isChat._id, userId, {
|
|
await ChatModel.update(isChat._id, userId, {
|
|
total: total,
|
|
total: total,
|
|
seen: seenCalculate(isChat.seen, total),
|
|
seen: seenCalculate(isChat.seen, total),
|
|
seenCompanion: seenCalculate(isChat.seenCompanion, total),
|
|
seenCompanion: seenCalculate(isChat.seenCompanion, total),
|
|
- lastMessage: isLastMessages
|
|
|
|
- ? isLastMessages.type === 'text'
|
|
|
|
- ? isLastMessages.message
|
|
|
|
- : isLastMessages.fullType
|
|
|
|
- : null,
|
|
|
|
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
|
|
|
|
|
|
+ lastMessage: 'Some Messages were Deleted',
|
|
|
|
+ lastMessageCreatedAt: updatedAt,
|
|
});
|
|
});
|
|
await ChatModel.update(isCompanionChat._id, companionId, {
|
|
await ChatModel.update(isCompanionChat._id, companionId, {
|
|
total: Total,
|
|
total: Total,
|
|
seen: seenCalculate(isCompanionChat.seen, Total),
|
|
seen: seenCalculate(isCompanionChat.seen, Total),
|
|
seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
|
|
seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
|
|
- lastMessage: isLastMessages
|
|
|
|
- ? isLastMessages.type === 'text'
|
|
|
|
- ? isLastMessages.message
|
|
|
|
- : isLastMessages.fullType
|
|
|
|
- : null,
|
|
|
|
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
|
|
|
|
|
|
+ lastMessage: 'Some Messages were Deleted',
|
|
|
|
+ lastMessageCreatedAt: updatedAt,
|
|
});
|
|
});
|
|
return res.json({
|
|
return res.json({
|
|
status: 'success',
|
|
status: 'success',
|
|
@@ -988,6 +1044,7 @@ module.exports = {
|
|
listMessages,
|
|
listMessages,
|
|
removeMessage,
|
|
removeMessage,
|
|
removeSelected,
|
|
removeSelected,
|
|
|
|
+ editMessage,
|
|
updateMessageEmoji,
|
|
updateMessageEmoji,
|
|
updateMessagePin,
|
|
updateMessagePin,
|
|
unpinAllMessage,
|
|
unpinAllMessage,
|