unknown пре 2 година
родитељ
комит
dd2d038d96
4 измењених фајлова са 128 додато и 80 уклоњено
  1. 4 0
      controllers/chats.js
  2. 77 44
      controllers/messages.js
  3. 43 36
      controllers/user.js
  4. 4 0
      model/schemas/chat.js

+ 4 - 0
controllers/chats.js

@@ -53,6 +53,7 @@ const startChat = async (req, res, next) => {
 				color,
 				online,
 				number,
+				country,
 			} = companion;
 			const {
 				name: Name,
@@ -64,6 +65,7 @@ const startChat = async (req, res, next) => {
 				color: Color,
 				online: Online,
 				number: Number,
+				country: Country,
 			} = user;
 			const newChat = await ChatModel.add({
 				name,
@@ -75,6 +77,7 @@ const startChat = async (req, res, next) => {
 				color,
 				online,
 				number,
+				country,
 				companionId: id,
 				owner: userId,
 			});
@@ -88,6 +91,7 @@ const startChat = async (req, res, next) => {
 				color: Color,
 				online: Online,
 				number: Number,
+				country: Country,
 				companionId: userId,
 				owner: id,
 			});

+ 77 - 44
controllers/messages.js

@@ -2,9 +2,8 @@ const MessageModel = require('../model/message');
 const UserModel = require('../model/user');
 const ChatModel = require('../model/chat');
 const fs = require('fs').promises;
-const path = require('path');
-const createFolderIsExist = require('../helpers/create-directory');
-const DIR_STATIC = process.env.DIR_STATIC;
+const s3 = require('../helpers/aws');
+const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
 require('dotenv').config();
 
 const listMessages = async (req, res, next) => {
@@ -21,6 +20,24 @@ const listMessages = async (req, res, next) => {
 	}
 };
 
+const listMessagesById = async (req, res, next) => {
+	try {
+		const userId = req.user.id;
+		const companionId = req.params.companionId;
+		const messages = await MessageModel.getList(
+			{ owner: userId, companionId },
+			{}
+		);
+		return res.json({
+			status: 'success',
+			code: 200,
+			data: messages,
+		});
+	} catch (e) {
+		next(e);
+	}
+};
+
 const removeMessage = async (req, res, next) => {
 	try {
 		const id = req.params.id;
@@ -31,8 +48,15 @@ const removeMessage = async (req, res, next) => {
 			userMessage.idTime,
 			userMessage.companionId
 		);
-		if (userMessage.type !== 'text')
-			await fs.unlink(path.join(DIR_STATIC, userMessage.message));
+		if (userMessage.type !== 'text') {
+			const params = {
+				Bucket: AWS_BUCKET_NAME,
+				Key: `${userMessage.message}`,
+			};
+			s3.deleteObject(params, async function (err, _data) {
+				if (err) throw err;
+			});
+		}
 
 		const isChat = await ChatModel.getByField(userMessage.companionId, userId);
 		const isCompanionChat = await ChatModel.getByField(
@@ -67,24 +91,6 @@ const removeMessage = async (req, res, next) => {
 	}
 };
 
-const listMessagesById = async (req, res, next) => {
-	try {
-		const userId = req.user.id;
-		const companionId = req.params.companionId;
-		const messages = await MessageModel.getList(
-			{ owner: userId, companionId },
-			{}
-		);
-		return res.json({
-			status: 'success',
-			code: 200,
-			data: messages,
-		});
-	} catch (e) {
-		next(e);
-	}
-};
-
 const sentMessage = async (req, res, next) => {
 	try {
 		const { id, message } = req.body;
@@ -163,12 +169,19 @@ const imageMessage = async (req, res, next) => {
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const originalName = req.file.originalname;
 		const pathToFile = req.file.path;
-		const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await createFolderIsExist(path.join(DIR_STATIC, userId));
-		await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameImg));
-		const imgUrl = path.normalize(path.join(userId, newNameImg));
-		if (isChat && isCompanionChat && imgUrl) {
+		const imgUrl = `${Math.round(Date.now() / 1000)}${userId}${originalName}`;
+		const fileContent = await fs.readFile(pathToFile);
+		const params = {
+			Bucket: AWS_BUCKET_NAME,
+			Key: `${imgUrl}`,
+			Body: fileContent,
+		};
+		s3.upload(params, async (err, _data) => {
+			if (err) throw err;
+			fs.unlink(pathToFile);
+		});
+		if (isChat && isCompanionChat) {
 			const { name, lastName, avatarUrl, color, number } = req.user;
 			const newMessage = await MessageModel.add({
 				message: imgUrl,
@@ -238,12 +251,19 @@ const audioMessage = async (req, res, next) => {
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const originalName = req.file.originalname;
 		const pathToFile = req.file.path;
-		const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await createFolderIsExist(path.join(DIR_STATIC, userId));
-		await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameAudio));
-		const audioUrl = path.normalize(path.join(userId, newNameAudio));
-		if (isChat && isCompanionChat && audioUrl) {
+		const audioUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
+		const fileContent = await fs.readFile(pathToFile);
+		const params = {
+			Bucket: AWS_BUCKET_NAME,
+			Key: `${audioUrl}`,
+			Body: fileContent,
+		};
+		s3.upload(params, async (err, _data) => {
+			if (err) throw err;
+			fs.unlink(pathToFile);
+		});
+		if (isChat && isCompanionChat) {
 			const { name, lastName, avatarUrl, color, number } = req.user;
 			const newMessage = await MessageModel.add({
 				message: audioUrl,
@@ -313,12 +333,19 @@ const videoMessage = async (req, res, next) => {
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		const originalName = req.file.originalname;
 		const pathToFile = req.file.path;
-		const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
+		const videoUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
 		const fullType = req.file.mimetype;
-		await createFolderIsExist(path.join(DIR_STATIC, userId));
-		await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameVideo));
-		const videoUrl = path.normalize(path.join(userId, newNameVideo));
-		if (isChat && isCompanionChat && videoUrl) {
+		const fileContent = await fs.readFile(pathToFile);
+		const params = {
+			Bucket: AWS_BUCKET_NAME,
+			Key: `${videoUrl}`,
+			Body: fileContent,
+		};
+		s3.upload(params, async (err, _data) => {
+			if (err) throw err;
+			fs.unlink(pathToFile);
+		});
+		if (isChat && isCompanionChat) {
 			const { name, lastName, avatarUrl, color, number } = req.user;
 			const newMessage = await MessageModel.add({
 				message: videoUrl,
@@ -386,7 +413,6 @@ const fileMessage = async (req, res, next) => {
 		const idTime = Math.round(Date.now() / 1000);
 		const isChat = await ChatModel.getByField(id, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, id);
-		const originalName = req.file.originalname;
 		const pathToFile = req.file.path;
 		const fullType = req.file.mimetype;
 		let type;
@@ -403,11 +429,18 @@ const fileMessage = async (req, res, next) => {
 			default:
 				break;
 		}
-		const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
-		await createFolderIsExist(path.join(DIR_STATIC, userId));
-		await fs.rename(pathToFile, path.join(DIR_STATIC, userId, newNameFile));
-		const fileUrl = path.normalize(path.join(userId, newNameFile));
-		if (isChat && isCompanionChat && fileUrl) {
+		const fileUrl = `${Math.round(Date.now() / 1000)}file.${type}`;
+		const fileContent = await fs.readFile(pathToFile);
+		const params = {
+			Bucket: AWS_BUCKET_NAME,
+			Key: `${fileUrl}`,
+			Body: fileContent,
+		};
+		s3.upload(params, async (err, _data) => {
+			if (err) throw err;
+			fs.unlink(pathToFile);
+		});
+		if (isChat && isCompanionChat) {
 			const { name, lastName, avatarUrl, color, number } = req.user;
 			const newMessage = await MessageModel.add({
 				message: fileUrl,

+ 43 - 36
controllers/user.js

@@ -184,32 +184,39 @@ const removeAvatar = async (req, res, next) => {
 		const { id, avatarsArr, number } = req.user;
 		const index = req.params.index;
 		const toDelete = avatarsArr[index];
-		const filteredAvatars = [...avatarsArr].filter(
-			({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
-		);
-		const avatarUrl = filteredAvatars[0].avatarUrl;
+		let filteredAvatars;
+		let avatarUrl;
+		if (Number(index) === 0) {
+			filteredAvatars = [];
+			avatarUrl = null;
+		} else {
+			filteredAvatars = [...avatarsArr].filter(
+				({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
+			);
+			avatarUrl = filteredAvatars[0].avatarUrl;
+		}
 		const params = {
 			Bucket: AWS_BUCKET_NAME,
 			Key: `${toDelete.avatarUrl}`,
 		};
 		s3.deleteObject(params, async function (err, _data) {
 			if (err) throw err;
-			await UserModel.updateUser(id, {
-				avatarUrl,
-				avatarsArr: filteredAvatars,
-			});
-			await ChatModel.updateCompanionsChat(id, {
-				avatarUrl,
-				avatarsArr: filteredAvatars,
-			});
-			await ContactModel.updateCompanionsContact(number, { avatarUrl });
-			await MessageModel.updateOwnerMessages(
-				{ companionIdFlow: { $eq: id } },
-				{ avatarUrl }
-			);
-			return res.status(200).json({
-				data: {},
-			});
+		});
+		await UserModel.updateUser(id, {
+			avatarUrl,
+			avatarsArr: filteredAvatars,
+		});
+		await ChatModel.updateCompanionsChat(id, {
+			avatarUrl,
+			avatarsArr: filteredAvatars,
+		});
+		await ContactModel.updateCompanionsContact(number, { avatarUrl });
+		await MessageModel.updateOwnerMessages(
+			{ companionIdFlow: { $eq: id } },
+			{ avatarUrl }
+		);
+		return res.status(200).json({
+			data: {},
 		});
 	} catch (e) {
 		next(e);
@@ -243,22 +250,22 @@ const updateAvatar = async (req, res, next) => {
 		s3.upload(params, async (err, _data) => {
 			if (err) throw err;
 			fs.unlink(pathToFile);
-			const avatarsArr = [
-				{ avatarUrl, updatedAt: new Date() },
-				...req.user.avatarsArr,
-			];
-			await UserModel.updateUser(userId, { avatarUrl, avatarsArr });
-			await ChatModel.updateCompanionsChat(userId, { avatarUrl, avatarsArr });
-			await ContactModel.updateCompanionsContact(userNumber, { avatarUrl });
-			await MessageModel.updateOwnerMessages(
-				{ companionIdFlow: { $eq: userId } },
-				{ avatarUrl }
-			);
-			return res.status(200).json({
-				status: 'success',
-				code: 200,
-				data: {},
-			});
+		});
+		const avatarsArr = [
+			{ avatarUrl, updatedAt: new Date() },
+			...req.user.avatarsArr,
+		];
+		await UserModel.updateUser(userId, { avatarUrl, avatarsArr });
+		await ChatModel.updateCompanionsChat(userId, { avatarUrl, avatarsArr });
+		await ContactModel.updateCompanionsContact(userNumber, { avatarUrl });
+		await MessageModel.updateOwnerMessages(
+			{ companionIdFlow: { $eq: userId } },
+			{ avatarUrl }
+		);
+		return res.status(200).json({
+			status: 'success',
+			code: 200,
+			data: {},
 		});
 	} catch (e) {
 		next(e);

+ 4 - 0
model/schemas/chat.js

@@ -79,6 +79,10 @@ const chatSchema = new Schema(
 			min: 8,
 			max: 14,
 		},
+		country: {
+			type: String,
+			default: false,
+		},
 		owner: {
 			type: SchemaTypes.ObjectId,
 			ref: 'user',