123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002 |
- const MessageModel = require('../model/message');
- const UserModel = require('../model/user');
- const ChatModel = require('../model/chat');
- const fs = require('fs').promises;
- const s3 = require('../helpers/aws');
- const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
- 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 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 updateMessageEmoji = async (req, res, next) => {
- try {
- const id = req.params.id;
- const userId = req.user.id;
- const { emoji } = req.body;
- const userMessage = await MessageModel.updateMessage(id, userId, { emoji });
- await MessageModel.findByFieldsAndUpdate(
- userId,
- userMessage.idTime,
- userMessage.companionId,
- {
- emojiCompanion: emoji,
- }
- );
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- } catch (e) {
- next(e);
- }
- };
- const updateMessagePin = async (req, res, next) => {
- try {
- const id = req.params.id;
- const userId = req.user.id;
- const { pinned } = req.body;
- const userMessage = await MessageModel.updateMessage(id, userId, {
- pinned,
- });
- await MessageModel.findByFieldsAndUpdate(
- userId,
- userMessage.idTime,
- userMessage.companionId,
- {
- pinned,
- }
- );
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- } catch (e) {
- next(e);
- }
- };
- const unpinAllMessage = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const pinnedMessages = req.body.pinnedMessages;
- pinnedMessages.forEach(async (id) => {
- const userMessage = await MessageModel.updateMessage(id, userId, {
- pinned: false,
- });
- await MessageModel.findByFieldsAndUpdate(
- userId,
- userMessage.idTime,
- userMessage.companionId,
- {
- pinned: false,
- }
- );
- });
- return res.status(200).json({
- status: 'success',
- code: 200,
- data: {},
- });
- } catch (e) {
- next(e);
- }
- };
- const removeMessage = async (req, res, next) => {
- try {
- const id = req.params.id;
- const userId = req.user.id;
- const userMessage = await MessageModel.remove(id, userId);
- await MessageModel.findByFieldsAndUpdateMany(
- {
- oldId: userMessage._id,
- },
- {
- deleted: true,
- replyMessage: null,
- replyName: null,
- replyLastName: null,
- replyCaption: null,
- forwardMessage: null,
- forwardMessage: null,
- forwardName: null,
- forwardLastName: null,
- forwardCaption: null,
- fullType: null,
- companionIdForwardToAndFrom: null,
- oldId: null,
- }
- );
- const companionMessage = await MessageModel.findByFields(
- userId,
- userMessage.idTime,
- userMessage.companionId
- );
- await MessageModel.findByFieldsAndUpdateMany(
- {
- oldId: companionMessage._id,
- },
- {
- deleted: true,
- replyMessage: null,
- replyName: null,
- replyLastName: null,
- replyCaption: null,
- forwardMessage: null,
- forwardMessage: null,
- forwardName: null,
- forwardLastName: null,
- forwardCaption: null,
- fullType: null,
- companionIdForwardToAndFrom: null,
- oldId: null,
- }
- );
- await MessageModel.removeByFields(
- userId,
- userMessage.idTime,
- userMessage.companionId
- );
- 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(
- userId,
- userMessage.companionId
- );
- const { total, messages } = await MessageModel.getList(
- { owner: userId, companionId: userMessage.companionId },
- {}
- );
- const { total: Total } = await MessageModel.getList(
- { owner: userMessage.companionId, companionId: userId },
- {}
- );
- const seenCalculate = (seen, total) => {
- if (seen <= total) {
- return seen;
- } else {
- return total;
- }
- };
- const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
- await ChatModel.update(isChat._id, userId, {
- total: total,
- seen: seenCalculate(isChat.seen, total),
- seenCompanion: seenCalculate(isChat.seenCompanion, total),
- lastMessage: isLastMessages
- ? isLastMessages.type === 'text'
- ? isLastMessages.message
- : isLastMessages.fullType
- : null,
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
- });
- await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
- total: Total,
- seen: seenCalculate(isCompanionChat.seen, Total),
- seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
- lastMessage: isLastMessages
- ? isLastMessages.type === 'text'
- ? isLastMessages.message
- : isLastMessages.fullType
- : null,
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
- });
- return res.json({
- status: 'success',
- code: 200,
- data: {},
- });
- } catch (e) {
- next(e);
- }
- };
- const removeSelected = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const { selectedArr, companionId } = req.body;
- const toDeleteMessage = async (id) => {
- const userMessage = await MessageModel.remove(id, userId);
- await MessageModel.findByFieldsAndUpdateMany(
- {
- oldId: userMessage._id,
- },
- {
- deleted: true,
- replyMessage: null,
- replyName: null,
- replyLastName: null,
- replyCaption: null,
- forwardMessage: null,
- forwardMessage: null,
- forwardName: null,
- forwardLastName: null,
- forwardCaption: null,
- fullType: null,
- companionIdForwardToAndFrom: null,
- oldId: null,
- }
- );
- const companionMessage = await MessageModel.findByFields(
- userId,
- userMessage.idTime,
- companionId
- );
- await MessageModel.findByFieldsAndUpdateMany(
- {
- oldId: companionMessage._id,
- },
- {
- deleted: true,
- replyMessage: null,
- replyName: null,
- replyLastName: null,
- replyCaption: null,
- forwardMessage: null,
- forwardMessage: null,
- forwardName: null,
- forwardLastName: null,
- forwardCaption: null,
- fullType: null,
- companionIdForwardToAndFrom: null,
- oldId: null,
- }
- );
- await MessageModel.removeByFields(
- userId,
- userMessage.idTime,
- companionId
- );
- if (userMessage.type !== 'text') {
- const params = {
- Bucket: AWS_BUCKET_NAME,
- Key: `${userMessage.message}`,
- };
- s3.deleteObject(params, async function (err, _data) {
- if (err) throw err;
- });
- }
- };
- await selectedArr.forEach(async (id) => await toDeleteMessage(id));
- const isChat = await ChatModel.getByField(companionId, userId);
- const isCompanionChat = await ChatModel.getByField(userId, companionId);
- const { total, messages } = await MessageModel.getList(
- { owner: userId, companionId },
- {}
- );
- const { total: Total } = await MessageModel.getList(
- { owner: companionId, companionId: userId },
- {}
- );
- const seenCalculate = (seen, total) => {
- if (seen <= total) {
- return seen;
- } else {
- return total;
- }
- };
- const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
- await ChatModel.update(isChat._id, userId, {
- total: total,
- seen: seenCalculate(isChat.seen, total),
- seenCompanion: seenCalculate(isChat.seenCompanion, total),
- lastMessage: isLastMessages
- ? isLastMessages.type === 'text'
- ? isLastMessages.message
- : isLastMessages.fullType
- : null,
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
- });
- await ChatModel.update(isCompanionChat._id, companionId, {
- total: Total,
- seen: seenCalculate(isCompanionChat.seen, Total),
- seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
- lastMessage: isLastMessages
- ? isLastMessages.type === 'text'
- ? isLastMessages.message
- : isLastMessages.fullType
- : null,
- lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
- });
- return res.json({
- status: 'success',
- code: 200,
- data: {},
- });
- } catch (e) {
- next(e);
- }
- };
- const sentMessage = async (req, res, next) => {
- try {
- const { id, message, caption } = 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',
- caption,
- idTime,
- companionIdFlow: userId,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message,
- name: isCompanionChat.name,
- lastName: isCompanionChat.lastName,
- avatarUrl,
- color,
- number,
- type: 'text',
- caption,
- idTime,
- companionIdFlow: userId,
- 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,
- seenCompanion: Total,
- lastMessage: message,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newMessage,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const sentMessageReply = async (req, res, next) => {
- try {
- const { id, message, caption } = req.body;
- const idTime = Math.round(Date.now() / 1000);
- const user = req.user;
- const userId = user.id;
- const userMessage = await MessageModel.getById(id, userId);
- const companionMessage = await MessageModel.findByFields(
- userId,
- userMessage.idTime,
- userMessage.companionId
- );
- const isChat = await ChatModel.getByField(userMessage.companionId, userId);
- const isCompanionChat = await ChatModel.getByField(
- userId,
- userMessage.companionId
- );
- if (isChat && isCompanionChat && userMessage && companionMessage) {
- const newMessage = await MessageModel.add({
- message,
- replyMessage: userMessage.message,
- name: user.name,
- lastName: user.lastName,
- replyName: userMessage.name,
- replyLastName: userMessage.lastName,
- avatarUrl: user.avatarUrl,
- color: user.color,
- number: user.number,
- type: 'text',
- fullType: userMessage.fullType,
- caption,
- replyCaption: userMessage.caption,
- idTime,
- oldId: id,
- companionIdFlow: userId,
- companionId: userMessage.companionId,
- owner: userId,
- });
- await MessageModel.add({
- message,
- replyMessage: companionMessage.message,
- name: isCompanionChat.name,
- lastName: isCompanionChat.lastName,
- replyName: companionMessage.name,
- replyLastName: companionMessage.lastName,
- avatarUrl: user.avatarUrl,
- color: user.color,
- number: user.number,
- type: 'text',
- fullType: companionMessage.fullType,
- caption,
- replyCaption: companionMessage.caption,
- idTime,
- oldId: companionMessage._id,
- companionIdFlow: userId,
- companionId: userId,
- owner: userMessage.companionId,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: userMessage.companionId },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: message,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: userMessage.companionId, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
- total: Total,
- seenCompanion: Total,
- lastMessage: message,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newMessage,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const sentMessageForward = async (req, res, next) => {
- try {
- const { id, companionIdForwardToAndFrom, message, caption } = req.body;
- const idTime = Math.round(Date.now() / 1000);
- const user = req.user;
- const userId = user.id;
- const userMessage = await MessageModel.getById(id, userId);
- const isChat = await ChatModel.getByField(
- companionIdForwardToAndFrom,
- userId
- );
- const companionMessage = await MessageModel.findByFields(
- userId,
- userMessage.idTime,
- userMessage.companionId
- );
- const isCompanionUser = await UserModel.findById(userMessage.companionId);
- const isForwardChat = await ChatModel.getByField(
- userId,
- companionIdForwardToAndFrom
- );
- if (
- userMessage &&
- isCompanionUser &&
- isChat &&
- isForwardChat &&
- companionMessage
- ) {
- const newMessage = await MessageModel.add({
- message,
- caption,
- forwardMessage: userMessage.message,
- forwardCaption: userMessage.caption,
- name: user.name,
- lastName: user.lastName,
- avatarUrl: user.avatarUrl,
- color: user.color,
- number: user.number,
- type: 'text',
- fullType: userMessage.fullType,
- forwardName:
- user.number === userMessage.number ? user.name : isCompanionUser.name,
- forwardLastName:
- user.number === userMessage.number
- ? user.lastName
- : isCompanionUser.lastName,
- oldId: userMessage._id,
- idTime,
- companionIdFlow: userId,
- companionIdForwardToAndFrom: userMessage.companionId,
- companionId: companionIdForwardToAndFrom,
- owner: userId,
- });
- await MessageModel.add({
- message,
- caption,
- forwardMessage: userMessage.message,
- forwardCaption: userMessage.caption,
- name: isForwardChat.name,
- lastName: isForwardChat.lastName,
- avatarUrl: user.avatarUrl,
- color: user.color,
- number: user.number,
- type: 'text',
- fullType: userMessage.fullType,
- forwardName:
- user.number === userMessage.number ? user.name : isCompanionUser.name,
- forwardLastName:
- user.number === userMessage.number
- ? user.lastName
- : isCompanionUser.lastName,
- oldId: companionMessage._id,
- idTime,
- companionIdFlow: userId,
- companionId: userId,
- companionIdForwardToAndFrom: userMessage.companionId,
- owner: companionIdForwardToAndFrom,
- });
- const { total } = await MessageModel.getList(
- { owner: userId, companionId: companionIdForwardToAndFrom },
- {}
- );
- await ChatModel.update(isChat._id, userId, {
- total,
- seen: total,
- watched: false,
- lastMessage: message,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: companionIdForwardToAndFrom, companionId: userId },
- {}
- );
- await ChatModel.update(isForwardChat._id, companionIdForwardToAndFrom, {
- total: Total,
- seenCompanion: 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, ...rest] = req.params.companionIdAndCaption.split(' ');
- const caption = rest.join(' ').slice(0, -1);
- 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;
- 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,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'image',
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: imgUrl,
- name: isCompanionChat.name,
- lastName: isCompanionChat.lastName,
- avatarUrl,
- color,
- number,
- type: 'image',
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- 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: fullType,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- seenCompanion: Total,
- lastMessage: fullType,
- 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, ...rest] = req.params.companionIdAndCaption.split(' ');
- let caption = rest.join(' ');
- if (caption.length === 1) caption.slice(0, -1);
- 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;
- 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,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'audio',
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: audioUrl,
- name: isCompanionChat.name,
- lastName: isCompanionChat.lastName,
- avatarUrl,
- color,
- number,
- type: 'audio',
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- 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: fullType,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- seenCompanion: Total,
- lastMessage: fullType,
- 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, ...rest] = req.params.companionIdAndCaption.split(' ');
- let caption = rest.join(' ');
- if (caption.length === 1) caption.slice(0, -1);
- 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 videoUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
- const fullType = req.file.mimetype;
- 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,
- name: isCompanionChat.name,
- lastName: isCompanionChat.lastName,
- avatarUrl,
- color,
- number,
- type: 'video',
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: videoUrl,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type: 'video',
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- 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: fullType,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- seenCompanion: Total,
- lastMessage: fullType,
- 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, ...rest] = req.params.companionIdAndCaption.split(' ');
- let caption = rest.join(' ');
- if (caption.length === 1) caption.slice(0, -1);
- const idTime = Math.round(Date.now() / 1000);
- const isChat = await ChatModel.getByField(id, userId);
- const isCompanionChat = await ChatModel.getByField(userId, id);
- 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 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,
- name,
- lastName,
- avatarUrl,
- color,
- number,
- type,
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- companionId: id,
- owner: userId,
- });
- await MessageModel.add({
- message: fileUrl,
- name: isCompanionChat.name,
- lastName: isCompanionChat.lastName,
- avatarUrl,
- color,
- number,
- type,
- fullType,
- caption,
- idTime,
- companionIdFlow: userId,
- 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: fullType,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- const { total: Total } = await MessageModel.getList(
- { owner: id, companionId: userId },
- {}
- );
- await ChatModel.update(isCompanionChat._id, id, {
- total: Total,
- seenCompanion: Total,
- lastMessage: fullType,
- lastMessageCreatedAt: newMessage.createdAt,
- });
- return res.status(201).json({
- status: 'success',
- code: 201,
- data: newMessage,
- });
- }
- } catch (e) {
- next(e);
- }
- };
- module.exports = {
- listMessages,
- removeMessage,
- removeSelected,
- updateMessageEmoji,
- updateMessagePin,
- unpinAllMessage,
- listMessagesById,
- sentMessage,
- sentMessageReply,
- sentMessageForward,
- imageMessage,
- audioMessage,
- videoMessage,
- fileMessage,
- };
|