|
@@ -1,6 +1,11 @@
|
|
|
const MessageModel = require('../model/message');
|
|
|
const UserModel = require('../model/user');
|
|
|
const ChatModel = require('../model/chat');
|
|
|
+const Jimp = require('jimp');
|
|
|
+const fs = require('fs').promises;
|
|
|
+const path = require('path');
|
|
|
+const createFolderIsExist = require('../helpers/create-directory');
|
|
|
+require('dotenv').config();
|
|
|
|
|
|
const listMessages = async (req, res, next) => {
|
|
|
try {
|
|
@@ -51,6 +56,7 @@ const sentMessage = async (req, res, next) => {
|
|
|
avatarUrl,
|
|
|
color,
|
|
|
number,
|
|
|
+ type: 'text',
|
|
|
companionId: id,
|
|
|
owner: userId,
|
|
|
});
|
|
@@ -61,6 +67,59 @@ const sentMessage = async (req, res, next) => {
|
|
|
avatarUrl,
|
|
|
color,
|
|
|
number,
|
|
|
+ type: 'text',
|
|
|
+ companionId: userId,
|
|
|
+ owner: id,
|
|
|
+ });
|
|
|
+ const { total } = await MessageModel.getList(userId, {});
|
|
|
+ await ChatModel.update(isChat._id, { total, seen: total });
|
|
|
+ const { total: Total } = await MessageModel.getList(id, {});
|
|
|
+ await ChatModel.update(isCompanionChat._id, { total: Total });
|
|
|
+ return res.status(201).json({
|
|
|
+ status: 'success',
|
|
|
+ code: 201,
|
|
|
+ data: newMessage,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const imgMessage = async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const userId = req.user.id;
|
|
|
+ const id = req.params.companionId;
|
|
|
+ const isChat = await ChatModel.getByField(id, userId);
|
|
|
+ const isCompanionChat = await ChatModel.getByField(userId, id);
|
|
|
+ const DIR_IMAGES = process.env.DIR_IMAGES;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
+ const newNameImg = req.file.originalname;
|
|
|
+ await Jimp.read(pathToFile);
|
|
|
+ await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
|
|
|
+ const imgUrl = path.normalize(path.join(userId, newNameImg));
|
|
|
+ if (isChat && isCompanionChat && imgUrl) {
|
|
|
+ const { name, lastName, avatarUrl, color, number } = req.user;
|
|
|
+ const newMessage = await MessageModel.add({
|
|
|
+ message: imgUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'image',
|
|
|
+ companionId: id,
|
|
|
+ owner: userId,
|
|
|
+ });
|
|
|
+ await MessageModel.add({
|
|
|
+ message: imgUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'image',
|
|
|
companionId: userId,
|
|
|
owner: id,
|
|
|
});
|
|
@@ -81,6 +140,7 @@ const sentMessage = async (req, res, next) => {
|
|
|
|
|
|
module.exports = {
|
|
|
listMessages,
|
|
|
- sentMessage,
|
|
|
listMessagesById,
|
|
|
+ sentMessage,
|
|
|
+ imgMessage,
|
|
|
};
|