소스 검색

done litle bit left bar

unknown 2 년 전
부모
커밋
2051916e2b

BIN
audios/62275dbc1f60894d30cce0c9/audioMessage1649422367049.mp3


+ 4 - 64
controllers/chats.js

@@ -9,40 +9,6 @@ const listChats = async (req, res, next) => {
 			userId,
 			req.query
 		);
-
-		// const messages = await MessageModel.getLastMessagesList(userId);
-
-		// const lastOnline = await chats.reduce(async (acc, { companionId }) => {
-		// 	const { _id, online, updatedAt } = await UserModel.findById(companionId);
-		// 	acc.push({ _id, online, updatedAt });
-		// 	return acc;
-		// }, []);
-
-		// const lastMessages = await messages.reduce(
-		// 	(acc, { message, companionId, createdAt, updatedAt }) => {
-		// 		const obj = {
-		// 			message,
-		// 			companionId,
-		// 			createdAt,
-		// 			updatedAt,
-		// 		};
-		// 		if (acc.length === 0) {
-		// 			acc.push(obj);
-		// 			return acc;
-		// 		}
-		// 		if (acc.some((el) => el.companionId === companionId)) {
-		// 			const i = acc.findIndex((el) => {
-		// 				return el.companionId === companionId;
-		// 			});
-		// 			acc[i] = obj;
-		// 			return acc;
-		// 		}
-		// 		acc.push(obj);
-		// 		return acc;
-		// 	},
-		// 	[]
-		// );
-
 		return res.json({
 			status: 'success',
 			code: 200,
@@ -51,8 +17,6 @@ const listChats = async (req, res, next) => {
 				limit,
 				page,
 				chats,
-				lastMessages: [{ message: 'fff' }, { message: 'ddd' }],
-				lastOnline: [new Date(), new Date()],
 			},
 		});
 	} catch (e) {
@@ -140,8 +104,8 @@ const removeChatForBoth = async (req, res, next) => {
 		const isUserChat = await ChatModel.getByField(companionId, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, companionId);
 		if (isUserChat || isCompanionChat) {
-			isUserChat && (await MessageModel.removeAll(userId));
-			isCompanionChat && (await MessageModel.removeAll(companionId));
+			isUserChat && (await MessageModel.removeAll(companionId, userId));
+			isCompanionChat && (await MessageModel.removeAll(userId, companionId));
 			isUserChat && (await ChatModel.remove(isUserChat._id, userId));
 			isCompanionChat &&
 				(await ChatModel.remove(isCompanionChat._id, companionId));
@@ -162,31 +126,6 @@ const removeChatForBoth = async (req, res, next) => {
 	}
 };
 
-const removeChatForMe = async (req, res, next) => {
-	try {
-		const companionId = req.params.id;
-		const userId = req.user.id;
-		const isUserChat = await ChatModel.getByField(companionId, userId);
-		if (isUserChat) {
-			await MessageModel.removeAll(userId);
-			await ChatModel.remove(isUserChat._id, userId);
-			return res.json({
-				status: 'success',
-				code: 200,
-				data: {},
-			});
-		} else {
-			return res.status(404).json({
-				status: 'error',
-				code: 404,
-				data: 'Not Found',
-			});
-		}
-	} catch (e) {
-		next(e);
-	}
-};
-
 const muteChat = async (req, res, next) => {
 	try {
 		const id = req.body.id;
@@ -277,6 +216,7 @@ const chatById = async (req, res, next) => {
 				lastName,
 				avatarUrl,
 				color,
+				lastMessage,
 				mute,
 				sort,
 				seen,
@@ -300,6 +240,7 @@ const chatById = async (req, res, next) => {
 					avatarUrl,
 					color,
 					online,
+					lastMessage,
 					mute,
 					sort,
 					seen,
@@ -325,7 +266,6 @@ module.exports = {
 	listChats,
 	startChat,
 	removeChatForBoth,
-	removeChatForMe,
 	muteChat,
 	sortChat,
 	seenChat,

+ 3 - 2
controllers/contacts.js

@@ -78,8 +78,9 @@ const addContact = async (req, res, next) => {
 const updateContact = async (req, res, next) => {
 	try {
 		const { id, _id, name, lastName } = req.body;
-		const contact = await ContactModel.update(id, { name, lastName });
-		const chat = await ChatModel.update(_id, { name, lastName });
+		const userId = req.user.id;
+		const contact = await ContactModel.update(id, userId, { name, lastName });
+		const chat = await ChatModel.update(_id, userId, { name, lastName });
 		if (contact && chat) {
 			return res.status(200).json({
 				data: { _id },

+ 45 - 10
controllers/messages.js

@@ -72,9 +72,16 @@ const sentMessage = async (req, res, next) => {
 				owner: id,
 			});
 			const { total } = await MessageModel.getList(userId, {});
-			await ChatModel.update(isChat._id, { total, seen: total });
+			await ChatModel.update(isChat._id, {
+				total,
+				seen: total,
+				lastMessage: message,
+			});
 			const { total: Total } = await MessageModel.getList(id, {});
-			await ChatModel.update(isCompanionChat._id, { total: Total });
+			await ChatModel.update(isCompanionChat._id, {
+				total: Total,
+				lastMessage: message,
+			});
 			return res.status(201).json({
 				status: 'success',
 				code: 201,
@@ -127,9 +134,16 @@ const imageMessage = async (req, res, next) => {
 				owner: id,
 			});
 			const { total } = await MessageModel.getList(userId, {});
-			await ChatModel.update(isChat._id, { total, seen: total });
+			await ChatModel.update(isChat._id, {
+				total,
+				seen: total,
+				lastMessage: imgUrl,
+			});
 			const { total: Total } = await MessageModel.getList(id, {});
-			await ChatModel.update(isCompanionChat._id, { total: Total });
+			await ChatModel.update(isCompanionChat._id, {
+				total: Total,
+				lastMessage: imgUrl,
+			});
 			return res.status(201).json({
 				status: 'success',
 				code: 201,
@@ -181,9 +195,16 @@ const audioMessage = async (req, res, next) => {
 				owner: id,
 			});
 			const { total } = await MessageModel.getList(userId, {});
-			await ChatModel.update(isChat._id, { total, seen: total });
+			await ChatModel.update(isChat._id, {
+				total,
+				seen: total,
+				lastMessage: audioUrl,
+			});
 			const { total: Total } = await MessageModel.getList(id, {});
-			await ChatModel.update(isCompanionChat._id, { total: Total });
+			await ChatModel.update(isCompanionChat._id, {
+				total: Total,
+				lastMessage: audioUrl,
+			});
 			return res.status(201).json({
 				status: 'success',
 				code: 201,
@@ -235,9 +256,16 @@ const videoMessage = async (req, res, next) => {
 				owner: id,
 			});
 			const { total } = await MessageModel.getList(userId, {});
-			await ChatModel.update(isChat._id, { total, seen: total });
+			await ChatModel.update(isChat._id, {
+				total,
+				seen: total,
+				lastMessage: videoUrl,
+			});
 			const { total: Total } = await MessageModel.getList(id, {});
-			await ChatModel.update(isCompanionChat._id, { total: Total });
+			await ChatModel.update(isCompanionChat._id, {
+				total: Total,
+				lastMessage: videoUrl,
+			});
 			return res.status(201).json({
 				status: 'success',
 				code: 201,
@@ -303,9 +331,16 @@ const fileMessage = async (req, res, next) => {
 				owner: id,
 			});
 			const { total } = await MessageModel.getList(userId, {});
-			await ChatModel.update(isChat._id, { total, seen: total });
+			await ChatModel.update(isChat._id, {
+				total,
+				seen: total,
+				lastMessage: fileUrl,
+			});
 			const { total: Total } = await MessageModel.getList(id, {});
-			await ChatModel.update(isCompanionChat._id, { total: Total });
+			await ChatModel.update(isCompanionChat._id, {
+				total: Total,
+				lastMessage: fileUrl,
+			});
 			return res.status(201).json({
 				status: 'success',
 				code: 201,

BIN
images/62275dbc1f60894d30cce0c9/download(2).png


BIN
images/62275dbc1f60894d30cce0c9/passport.jpg


+ 2 - 2
model/contact.js

@@ -52,9 +52,9 @@ const remove = async (id, userId) => {
 	return removedContact;
 };
 
-const update = async (id, body) => {
+const update = async (id, userId, body) => {
 	return await Contact.findByIdAndUpdate(
-		{ _id: id },
+		{ _id: id, owner: userId },
 		{ ...body },
 		{ new: true }
 	);

+ 2 - 7
model/message.js

@@ -39,11 +39,6 @@ const getById = async (id, userId) => {
 	return foundMessage;
 };
 
-const getLastMessagesList = async (userId) => {
-	const messages = await Message.find({ owner: userId });
-	return messages;
-};
-
 const getByField = async (number, userId) => {
 	const message = await Message.findOne({
 		number,
@@ -65,8 +60,9 @@ const remove = async (id, userId) => {
 	return removedMessage;
 };
 
-const removeAll = async (userId) => {
+const removeAll = async (companionId, userId) => {
 	const removedAllMessages = await Message.remove({
+		companionId,
 		owner: userId,
 	});
 	return removedAllMessages;
@@ -77,7 +73,6 @@ module.exports = {
 	getListById,
 	getByField,
 	getById,
-	getLastMessagesList,
 	add,
 	remove,
 	removeAll,

+ 4 - 0
model/schemas/chat.js

@@ -30,6 +30,10 @@ const chatSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		lastMessage: {
+			type: String,
+			default: null,
+		},
 		mute: {
 			type: Boolean,
 			default: false,

+ 1 - 2
routes/chats.js

@@ -12,7 +12,6 @@ router
 	.patch('/sort/', guard, controllers.sortChat)
 	.patch('/seen/', guard, controllers.seenChat)
 	.patch('/typing/', guard, validation.typingChat, controllers.typingChat)
-	.delete('/both/:id', guard, controllers.removeChatForBoth)
-	.delete('/me/:id', guard, controllers.removeChatForMe);
+	.delete('/both/:id', guard, controllers.removeChatForBoth);
 
 module.exports = router;

BIN
videos/62275dbc1f60894d30cce0c9/audio_mpeg(2).ogg


BIN
videos/62275dbc1f60894d30cce0c9/videoMessage1649422358604.mp4


BIN
videos/62275dbc1f60894d30cce0c9/video_mp4(1).mp4