|
@@ -86,7 +86,7 @@ const sentMessage = async (req, res, next) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const imgMessage = async (req, res, next) => {
|
|
|
+const imageMessage = async (req, res, next) => {
|
|
|
try {
|
|
|
const userId = req.user.id;
|
|
|
const id = req.params.companionId;
|
|
@@ -138,9 +138,165 @@ const imgMessage = async (req, res, next) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+const audioMessage = 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_AUDIOS = process.env.DIR_AUDIOS;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
+ const newNameAudio = req.file.originalname;
|
|
|
+ await createFolderIsExist(path.join(DIR_AUDIOS, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
|
|
|
+ const audioUrl = path.normalize(path.join(userId, newNameAudio));
|
|
|
+ if (isChat && isCompanionChat && audioUrl) {
|
|
|
+ const { name, lastName, avatarUrl, color, number } = req.user;
|
|
|
+ const newMessage = await MessageModel.add({
|
|
|
+ message: audioUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'audio',
|
|
|
+ companionId: id,
|
|
|
+ owner: userId,
|
|
|
+ });
|
|
|
+ await MessageModel.add({
|
|
|
+ message: audioUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'audio',
|
|
|
+ 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 videoMessage = 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_VIDEOS = process.env.DIR_VIDEOS;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
+ const newNameVideo = req.file.originalname;
|
|
|
+ await createFolderIsExist(path.join(DIR_VIDEOS, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
|
|
|
+ const videoUrl = path.normalize(path.join(userId, newNameVideo));
|
|
|
+ if (isChat && isCompanionChat && videoUrl) {
|
|
|
+ const { name, lastName, avatarUrl, color, number } = req.user;
|
|
|
+ const newMessage = await MessageModel.add({
|
|
|
+ message: videoUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'video',
|
|
|
+ companionId: id,
|
|
|
+ owner: userId,
|
|
|
+ });
|
|
|
+ await MessageModel.add({
|
|
|
+ message: videoUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'video',
|
|
|
+ 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 fileMessage = 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_FILES = process.env.DIR_FILES;
|
|
|
+ const pathToFile = req.file.path;
|
|
|
+ const newNameFile = req.file.originalname;
|
|
|
+ await createFolderIsExist(path.join(DIR_FILES, userId));
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
|
|
|
+ const fileUrl = path.normalize(path.join(userId, newNameFile));
|
|
|
+ if (isChat && isCompanionChat && fileUrl) {
|
|
|
+ const { name, lastName, avatarUrl, color, number } = req.user;
|
|
|
+ const newMessage = await MessageModel.add({
|
|
|
+ message: fileUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'file',
|
|
|
+ companionId: id,
|
|
|
+ owner: userId,
|
|
|
+ });
|
|
|
+ await MessageModel.add({
|
|
|
+ message: fileUrl,
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ number,
|
|
|
+ type: 'file',
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
module.exports = {
|
|
|
listMessages,
|
|
|
listMessagesById,
|
|
|
sentMessage,
|
|
|
- imgMessage,
|
|
|
+ imageMessage,
|
|
|
+ audioMessage,
|
|
|
+ videoMessage,
|
|
|
+ fileMessage,
|
|
|
};
|