unknown 2 lat temu
rodzic
commit
5efbfd9d24
4 zmienionych plików z 100 dodań i 36 usunięć
  1. 89 32
      controllers/messages.js
  2. 0 4
      model/schemas/message.js
  3. 1 0
      routes/messages.js
  4. 10 0
      validation/message.js

+ 89 - 32
controllers/messages.js

@@ -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) => {
 	try {
 		const id = req.params.id;
@@ -126,7 +202,6 @@ const removeMessage = async (req, res, next) => {
 				oldId: userMessage._id,
 			},
 			{
-				deleted: true,
 				replyMessage: null,
 				replyName: null,
 				replyLastName: null,
@@ -151,7 +226,6 @@ const removeMessage = async (req, res, next) => {
 				oldId: companionMessage._id,
 			},
 			{
-				deleted: true,
 				replyMessage: null,
 				replyName: null,
 				replyLastName: null,
@@ -187,7 +261,7 @@ const removeMessage = async (req, res, next) => {
 			userId,
 			userMessage.companionId
 		);
-		const { total, messages } = await MessageModel.getList(
+		const { total } = await MessageModel.getList(
 			{ owner: userId, companionId: userMessage.companionId },
 			{}
 		);
@@ -202,28 +276,20 @@ const removeMessage = async (req, res, next) => {
 				return total;
 			}
 		};
-		const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
+		const updatedAt = new Date();
 		await ChatModel.update(isChat._id, userId, {
 			total: total,
 			seen: seenCalculate(isChat.seen, 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, {
 			total: Total,
 			seen: seenCalculate(isCompanionChat.seen, 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({
 			status: 'success',
@@ -246,7 +312,6 @@ const removeSelected = async (req, res, next) => {
 					oldId: userMessage._id,
 				},
 				{
-					deleted: true,
 					replyMessage: null,
 					replyName: null,
 					replyLastName: null,
@@ -271,7 +336,6 @@ const removeSelected = async (req, res, next) => {
 					oldId: companionMessage._id,
 				},
 				{
-					deleted: true,
 					replyMessage: null,
 					replyName: null,
 					replyLastName: null,
@@ -304,7 +368,7 @@ const removeSelected = async (req, res, next) => {
 		await selectedArr.forEach(async (id) => await toDeleteMessage(id));
 		const isChat = await ChatModel.getByField(companionId, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, companionId);
-		const { total, messages } = await MessageModel.getList(
+		const { total } = await MessageModel.getList(
 			{ owner: userId, companionId },
 			{}
 		);
@@ -319,28 +383,20 @@ const removeSelected = async (req, res, next) => {
 				return total;
 			}
 		};
-		const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
+		const updatedAt = new Date();
 		await ChatModel.update(isChat._id, userId, {
 			total: total,
 			seen: seenCalculate(isChat.seen, 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, {
 			total: Total,
 			seen: seenCalculate(isCompanionChat.seen, 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({
 			status: 'success',
@@ -988,6 +1044,7 @@ module.exports = {
 	listMessages,
 	removeMessage,
 	removeSelected,
+	editMessage,
 	updateMessageEmoji,
 	updateMessagePin,
 	unpinAllMessage,

+ 0 - 4
model/schemas/message.js

@@ -106,10 +106,6 @@ const messageSchema = new Schema(
 			type: Boolean,
 			default: false,
 		},
-		deleted: {
-			type: Boolean,
-			default: false,
-		},
 		edited: {
 			type: Boolean,
 			default: false,

+ 1 - 0
routes/messages.js

@@ -9,6 +9,7 @@ router
 	.get('/', guard, controllers.listMessages)
 	.post('/', guard, validation.sentMessage, controllers.sentMessage)
 	.post('/reply/', guard, validation.sentMessage, controllers.sentMessageReply)
+	.post('/edit/', guard, validation.editMessage, controllers.editMessage)
 	.post(
 		'/forward/',
 		guard,

+ 10 - 0
validation/message.js

@@ -8,6 +8,12 @@ const schemaSentMessage = Joi.object({
 	caption: Joi.any().optional(),
 });
 
+const schemaEditMessage = Joi.object({
+	id: Joi.string().required(),
+	message: Joi.any().optional(),
+	caption: Joi.any().optional(),
+});
+
 const schemaSentForwardMessage = Joi.object({
 	id: Joi.string().required(),
 	companionIdForwardToAndFrom: Joi.any().required(),
@@ -28,6 +34,10 @@ module.exports.sentMessage = (req, _res, next) => {
 	return validate(schemaSentMessage, req.body, next);
 };
 
+module.exports.editMessage = (req, _res, next) => {
+	return validate(schemaEditMessage, req.body, next);
+};
+
 module.exports.sentForwardMessage = (req, _res, next) => {
 	return validate(schemaSentForwardMessage, req.body, next);
 };