123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- 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 {
- const userId = req.user.id;
- const messages = await MessageModel.getList({ owner: userId }, req.query);
- 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;
- const userId = req.user.id;
- const DIR_IMAGES = process.env.DIR_IMAGES;
- const DIR_AUDIOS = process.env.DIR_AUDIOS;
- const DIR_VIDEOS = process.env.DIR_VIDEOS;
- const DIR_FILES = process.env.DIR_FILES;
- const userMessage = await MessageModel.remove(id, userId);
- const companionMessage = await MessageModel.removeByFields(
- userId,
- userMessage.idTime,
- userMessage.companionId
- );
- if (userMessage.type !== 'text') {
- switch (userMessage.type) {
- case 'image':
- await fs.unlink(path.join(DIR_IMAGES, userMessage.message));
- break;
- case 'audio':
- await fs.unlink(path.join(DIR_AUDIOS, userMessage.message));
- break;
- case 'video':
- await fs.unlink(path.join(DIR_VIDEOS, userMessage.message));
- break;
- default:
- await fs.unlink(path.join(DIR_FILES, userMessage.message));
- break;
- }
- }
- if (userMessage && companionMessage) {
- 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 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;
- const idTime = Math.round(Date.now() / 1000);
- const user = req.user;
- const userId = user.id;
- const companion = await UserModel.findById(id);
- const isChat = await ChatModel.getByField(id, userId);
- const isCompanionChat = await ChatModel.getByField(userId, id);
- const { name, lastName, avatarUrl, color, number } = user;
- if (companion && isChat && isCompanionChat) {
- const newMessage = await MessageModel.add({
- message,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'text',
- idTime,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'text',
- idTime,
- companionId: userId,
- owner: id,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: id },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: message,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- lastMessage: message,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newMessage,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const imageMessage = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const id = req.params.companionId;
- const idTime = Math.round(Date.now() / 1000);
- 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 originalName = req.file.originalname;
- const newNameImg = `${Math.round(Date.now() / 1000)}${originalName}`;
- const fullType = req.file.mimetype;
- 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',
- fullType,
- idTime,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: imgUrl,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'image',
- fullType,
- idTime,
- companionId: userId,
- owner: id,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: id },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: imgUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- lastMessage: imgUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newMessage,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const audioMessage = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const id = req.params.companionId;
- const idTime = Math.round(Date.now() / 1000);
- 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 originalName = req.file.originalname;
- const newNameAudio = `${Math.round(Date.now() / 1000)}${originalName}`;
- const fullType = req.file.mimetype;
- 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',
- fullType,
- idTime,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: audioUrl,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'audio',
- fullType,
- idTime,
- companionId: userId,
- owner: id,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: id },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: audioUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- lastMessage: audioUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- 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 idTime = Math.round(Date.now() / 1000);
- 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 originalName = req.file.originalname;
- const newNameVideo = `${Math.round(Date.now() / 1000)}${originalName}`;
- const fullType = req.file.mimetype;
- 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',
- fullType,
- idTime,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: videoUrl,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'video',
- fullType,
- idTime,
- companionId: userId,
- owner: id,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: id },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: videoUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- lastMessage: videoUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- 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 idTime = Math.round(Date.now() / 1000);
- 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 fullType = req.file.mimetype;
- let type;
- switch (fullType) {
- case 'application/pdf':
- type = 'pdf';
- break;
- case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
- type = 'docx';
- break;
- case 'application/octet-stream':
- type = 'docx';
- break;
- default:
- break;
- }
- const newNameFile = `${Math.round(Date.now() / 1000)}file.${type}`;
- 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,
- fullType,
- idTime,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: fileUrl,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type,
- fullType,
- idTime,
- companionId: userId,
- owner: id,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: id },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: fileUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- lastMessage: fileUrl,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newMessage,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- module.exports = {
- listMessages,
- removeMessage,
- listMessagesById,
- sentMessage,
- imageMessage,
- audioMessage,
- videoMessage,
- fileMessage,
- };
|