123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- const ChatModel = require('../model/chat');
- const UserModel = require('../model/user');
- const MessageModel = require('../model/message');
- const fs = require('fs').promises;
- const path = require('path');
- require('dotenv').config();
- const listChats = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const { total, limit, page, chats } = await ChatModel.getList(
- userId,
- req.query
- );
- return res.json({
- status: 'success',
- code: 200,
- data: {
- total,
- limit,
- page,
- chats,
- },
- });
- } catch (e) {
- next(e);
- }
- };
- const startChat = async (req, res, next) => {
- try {
- const id = req.body.id;
- const user = req.user;
- const userId = user.id;
- const companion = await UserModel.findById(id);
- const isUser = await ChatModel.getByField(id, userId);
- const isCompanion = await ChatModel.getByField(userId, id);
- const {
- name,
- lastName,
- originalName,
- originalLastName,
- avatarUrl,
- avatarsArr,
- color,
- online,
- number,
- } = companion;
- const {
- name: Name,
- lastName: LastName,
- originalName: OriginalName,
- originalLastName: OriginalLastName,
- avatarUrl: AvatarUrl,
- avatarsArr: AvatarsArr,
- color: Color,
- online: Online,
- number: Number,
- } = user;
- if (companion && isUser && isCompanion) {
- await ChatModel.update(isUser._id, userId, {
- originalName,
- originalLastName,
- avatarUrl,
- avatarsArr,
- color,
- online,
- number,
- });
- await ChatModel.update(isCompanion._id, id, {
- originalName: OriginalName,
- originalLastName: OriginalLastName,
- avatarUrl: AvatarUrl,
- avatarsArr: AvatarsArr,
- color: Color,
- online: Online,
- number: Number,
- });
- const updatedChat = await ChatModel.getByField(id, userId);
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: updatedChat,
- });
- }
- if (companion && !isUser && !isCompanion) {
- const newChat = await ChatModel.add({
- name,
- lastName,
- originalName,
- originalLastName,
- avatarUrl,
- avatarsArr,
- color,
- online,
- number,
- companionId: id,
- owner: userId,
- });
- await ChatModel.add({
- name: Name,
- lastName: LastName,
- originalName: OriginalName,
- originalLastName: originalLastName,
- avatarUrl: AvatarUrl,
- avatarsArr: AvatarsArr,
- color: Color,
- online: Online,
- number: Number,
- companionId: userId,
- owner: id,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newChat,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const removeChatForBoth = async (req, res, next) => {
- try {
- const companionId = 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 isUserChat = await ChatModel.getByField(companionId, userId);
- const isCompanionChat = await ChatModel.getByField(userId, companionId);
- if (isUserChat && isCompanionChat) {
- const deleteFile = async (type, message) => {
- if (type !== 'text') {
- switch (type) {
- case 'image':
- await fs.unlink(path.join(DIR_IMAGES, message));
- break;
- case 'audio':
- await fs.unlink(path.join(DIR_AUDIOS, message));
- break;
- case 'video':
- await fs.unlink(path.join(DIR_VIDEOS, message));
- break;
- default:
- await fs.unlink(path.join(DIR_FILES, message));
- break;
- }
- }
- };
- const { messages } = await MessageModel.getList(
- { owner: userId, companionId },
- {}
- );
- await messages.forEach(
- async ({ type, message }) => await deleteFile(type, message)
- );
- await MessageModel.removeAll(companionId, userId);
- await MessageModel.removeAll(userId, companionId);
- await ChatModel.remove(isUserChat._id, userId);
- await ChatModel.remove(isCompanionChat._id, companionId);
- 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;
- const userId = req.user.id;
- const isChat = await ChatModel.getByField(id, userId);
- if (isChat) {
- const { _id, mute } = isChat;
- await ChatModel.update(_id, userId, { mute: !mute });
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const sortChat = async (req, res, next) => {
- try {
- const id = req.body.id;
- const userId = req.user.id;
- const isChat = await ChatModel.getByField(id, userId);
- if (isChat) {
- const { _id, sort } = isChat;
- await ChatModel.update(_id, userId, { sort: !sort });
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const seenChat = async (req, res, next) => {
- try {
- const id = req.body.id;
- const userId = req.user.id;
- const isChat = await ChatModel.getByField(id, userId);
- const isCompanionChat = await ChatModel.getByField(userId, id);
- const isMessages = await MessageModel.getList(
- { owner: userId, companionId: id },
- {}
- );
- if (isChat && isMessages && isCompanionChat) {
- const { total } = isMessages;
- await ChatModel.update(isChat._id, userId, {
- seen: total,
- watched: false,
- });
- await ChatModel.update(isCompanionChat._id, id, { watched: true });
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const typingChat = async (req, res, next) => {
- try {
- const { id, typing } = req.body;
- const userId = req.user.id;
- const isCompanionChat = await ChatModel.getByField(userId, id);
- if (isCompanionChat) {
- await ChatModel.update(isCompanionChat._id, id, { typing });
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const getChatById = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const companionId = req.params.companionId;
- const isChat = await ChatModel.getByField(companionId, userId);
- if (isChat) {
- return res.json({
- status: 'success',
- code: 200,
- data: {
- ...isChat,
- },
- });
- }
- } catch (e) {
- next(e);
- }
- };
- module.exports = {
- listChats,
- startChat,
- removeChatForBoth,
- muteChat,
- sortChat,
- seenChat,
- typingChat,
- getChatById,
- };
|