Browse Source

finished with forward

unknown 1 year ago
parent
commit
9583aef021
4 changed files with 37 additions and 12 deletions
  1. 2 4
      controllers/chats.js
  2. 25 8
      controllers/messages.js
  3. 8 0
      model/schemas/message.js
  4. 2 0
      validation/message.js

+ 2 - 4
controllers/chats.js

@@ -1,8 +1,6 @@
 const ChatModel = require('../model/chat');
 const UserModel = require('../model/user');
 const MessageModel = require('../model/message');
-const fs = require('fs').promises;
-const path = require('path');
 const s3 = require('../helpers/aws');
 const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
 require('dotenv').config();
@@ -36,14 +34,14 @@ const startChat = async (req, res, next) => {
 		const userId = user.id;
 		const companion = await UserModel.findById(id);
 		const userChat = await ChatModel.getByField(id, userId);
-		if (userChat) {
+		if (userChat && userId !== id) {
 			return res.json({
 				status: 'success',
 				code: 200,
 				data: userChat,
 			});
 		}
-		if (!userChat) {
+		if (!userChat && userId !== id) {
 			const {
 				name,
 				lastName,

+ 25 - 8
controllers/messages.js

@@ -440,7 +440,7 @@ const sentMessageReply = async (req, res, next) => {
 
 const sentMessageForward = async (req, res, next) => {
 	try {
-		const { id, companionIdForwardToAndFrom } = req.body;
+		const { id, companionIdForwardToAndFrom, message, caption } = req.body;
 		const idTime = Math.round(Date.now() / 1000);
 		const user = req.user;
 		const userId = user.id;
@@ -449,6 +449,11 @@ const sentMessageForward = async (req, res, next) => {
 			companionIdForwardToAndFrom,
 			userId
 		);
+		const companionMessage = await MessageModel.findByFields(
+			userId,
+			userMessage.idTime,
+			userMessage.companionId
+		);
 		const isCompanionUser = await UserModel.findById(userMessage.companionId);
 		const isForwardHasChatWithCompanion = await ChatModel.getByField(
 			userMessage.companionId,
@@ -458,10 +463,18 @@ const sentMessageForward = async (req, res, next) => {
 			userId,
 			companionIdForwardToAndFrom
 		);
-		if (userMessage && isCompanionUser && isChat && isForwardChat) {
+		if (
+			userMessage &&
+			isCompanionUser &&
+			isChat &&
+			isForwardChat &&
+			companionMessage
+		) {
 			const newMessage = await MessageModel.add({
-				message: userMessage.message,
-				caption: userMessage.caption,
+				message,
+				caption,
+				forwardMessage: userMessage.message,
+				forwardCaption: userMessage.caption,
 				name: user.name,
 				lastName: user.lastName,
 				avatarUrl: user.avatarUrl,
@@ -471,6 +484,7 @@ const sentMessageForward = async (req, res, next) => {
 				fullType: userMessage.fullType,
 				forwardName: userMessage.name,
 				forwardLastName: userMessage.lastName,
+				oldId: userMessage._id,
 				idTime,
 				companionIdFlow: userId,
 				companionIdForwardToAndFrom: userMessage.companionId,
@@ -478,8 +492,10 @@ const sentMessageForward = async (req, res, next) => {
 				owner: userId,
 			});
 			await MessageModel.add({
-				message: userMessage.message,
-				caption: userMessage.caption,
+				message,
+				caption,
+				forwardMessage: userMessage.message,
+				forwardCaption: userMessage.caption,
 				name: isForwardChat.name,
 				lastName: isForwardChat.lastName,
 				avatarUrl: user.avatarUrl,
@@ -493,6 +509,7 @@ const sentMessageForward = async (req, res, next) => {
 				forwardLastName: isForwardHasChatWithCompanion
 					? isForwardHasChatWithCompanion.lastName
 					: isCompanionUser.lastName,
+				oldId: companionMessage._id,
 				idTime,
 				companionIdFlow: userId,
 				companionId: userId,
@@ -507,7 +524,7 @@ const sentMessageForward = async (req, res, next) => {
 				total,
 				seen: total,
 				watched: false,
-				lastMessage: userMessage.message,
+				lastMessage: message,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			const { total: Total } = await MessageModel.getList(
@@ -516,7 +533,7 @@ const sentMessageForward = async (req, res, next) => {
 			);
 			await ChatModel.update(isForwardChat._id, companionIdForwardToAndFrom, {
 				total: Total,
-				lastMessage: userMessage.message,
+				lastMessage: message,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({

+ 8 - 0
model/schemas/message.js

@@ -14,6 +14,10 @@ const messageSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		forwardMessage: {
+			type: String,
+			default: null,
+		},
 		companionId: {
 			type: String,
 			default: null,
@@ -86,6 +90,10 @@ const messageSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		forwardCaption: {
+			type: String,
+			default: null,
+		},
 		emoji: {
 			type: String,
 			default: null,

+ 2 - 0
validation/message.js

@@ -11,6 +11,8 @@ const schemaSentMessage = Joi.object({
 const schemaSentForwardMessage = Joi.object({
 	id: Joi.string().required(),
 	companionIdForwardToAndFrom: Joi.any().required(),
+	message: Joi.string().min(1).max(1400).required(),
+	caption: Joi.any().optional(),
 });
 
 const schemaRemoveSelected = Joi.object({