messages.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. const MessageModel = require('../model/message');
  2. const UserModel = require('../model/user');
  3. const ChatModel = require('../model/chat');
  4. const fs = require('fs').promises;
  5. const s3 = require('../helpers/aws');
  6. const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
  7. require('dotenv').config();
  8. const listMessages = async (req, res, next) => {
  9. try {
  10. const userId = req.user.id;
  11. const messages = await MessageModel.getList({ owner: userId }, req.query);
  12. return res.json({
  13. status: 'success',
  14. code: 200,
  15. data: messages,
  16. });
  17. } catch (e) {
  18. next(e);
  19. }
  20. };
  21. const listMessagesById = async (req, res, next) => {
  22. try {
  23. const userId = req.user.id;
  24. const companionId = req.params.companionId;
  25. const messages = await MessageModel.getList(
  26. { owner: userId, companionId },
  27. {}
  28. );
  29. return res.json({
  30. status: 'success',
  31. code: 200,
  32. data: messages,
  33. });
  34. } catch (e) {
  35. next(e);
  36. }
  37. };
  38. const editMessage = async (req, res, next) => {
  39. try {
  40. const userId = req.user.id;
  41. const { id, message, caption } = req.body;
  42. const userMessage = await MessageModel.getById(id, userId);
  43. await MessageModel.updateMessage(id, userId, {
  44. message: userMessage.fullType === null ? message : userMessage.message,
  45. caption,
  46. edited: true,
  47. });
  48. await MessageModel.findByFieldsAndUpdateMany(
  49. {
  50. oldId: userMessage._id,
  51. },
  52. {
  53. forwardMessage:
  54. userMessage.fullType === null ? message : userMessage.forwardMessage,
  55. forwardCaption: caption,
  56. replyMessage:
  57. userMessage.fullType === null ? message : userMessage.replyMessage,
  58. replyCaption: caption,
  59. edited: true,
  60. }
  61. );
  62. const companionMessage = await MessageModel.findByFields(
  63. userId,
  64. userMessage.idTime,
  65. userMessage.companionId
  66. );
  67. await MessageModel.findByFieldsAndUpdate(
  68. userId,
  69. userMessage.idTime,
  70. userMessage.companionId,
  71. {
  72. message: userMessage.fullType === null ? message : userMessage.message,
  73. caption,
  74. edited: true,
  75. }
  76. );
  77. await MessageModel.findByFieldsAndUpdateMany(
  78. {
  79. oldId: companionMessage._id,
  80. },
  81. {
  82. forwardMessage:
  83. userMessage.fullType === null ? message : userMessage.forwardMessage,
  84. forwardCaption: caption,
  85. replyMessage:
  86. userMessage.fullType === null ? message : userMessage.replyMessage,
  87. replyCaption: caption,
  88. edited: true,
  89. }
  90. );
  91. const isChat = await ChatModel.getByField(userMessage.companionId, userId);
  92. const isCompanionChat = await ChatModel.getByField(
  93. userId,
  94. userMessage.companionId
  95. );
  96. const updatedAt = new Date();
  97. await ChatModel.update(isChat._id, userId, {
  98. lastMessage: 'The Message was Edited',
  99. lastMessageCreatedAt: updatedAt,
  100. });
  101. await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
  102. lastMessage: 'The Message was Edited',
  103. lastMessageCreatedAt: updatedAt,
  104. });
  105. return res.status(200).json({
  106. status: 'success',
  107. code: 200,
  108. data: {},
  109. });
  110. } catch (e) {
  111. next(e);
  112. }
  113. };
  114. const updateMessageEmoji = async (req, res, next) => {
  115. try {
  116. const id = req.params.id;
  117. const userId = req.user.id;
  118. const { emoji } = req.body;
  119. const userMessage = await MessageModel.updateMessage(id, userId, { emoji });
  120. await MessageModel.findByFieldsAndUpdate(
  121. userId,
  122. userMessage.idTime,
  123. userMessage.companionId,
  124. {
  125. emojiCompanion: emoji,
  126. }
  127. );
  128. return res.status(200).json({
  129. status: 'success',
  130. code: 200,
  131. data: {},
  132. });
  133. } catch (e) {
  134. next(e);
  135. }
  136. };
  137. const updateMessagePin = async (req, res, next) => {
  138. try {
  139. const id = req.params.id;
  140. const userId = req.user.id;
  141. const { pinned } = req.body;
  142. const userMessage = await MessageModel.updateMessage(id, userId, {
  143. pinned,
  144. });
  145. await MessageModel.findByFieldsAndUpdate(
  146. userId,
  147. userMessage.idTime,
  148. userMessage.companionId,
  149. {
  150. pinned,
  151. }
  152. );
  153. return res.status(200).json({
  154. status: 'success',
  155. code: 200,
  156. data: {},
  157. });
  158. } catch (e) {
  159. next(e);
  160. }
  161. };
  162. const unpinAllMessage = async (req, res, next) => {
  163. try {
  164. const userId = req.user.id;
  165. const pinnedMessages = req.body.pinnedMessages;
  166. pinnedMessages.forEach(async (id) => {
  167. const userMessage = await MessageModel.updateMessage(id, userId, {
  168. pinned: false,
  169. });
  170. await MessageModel.findByFieldsAndUpdate(
  171. userId,
  172. userMessage.idTime,
  173. userMessage.companionId,
  174. {
  175. pinned: false,
  176. }
  177. );
  178. });
  179. return res.status(200).json({
  180. status: 'success',
  181. code: 200,
  182. data: {},
  183. });
  184. } catch (e) {
  185. next(e);
  186. }
  187. };
  188. const removeMessage = async (req, res, next) => {
  189. try {
  190. const id = req.params.id;
  191. const userId = req.user.id;
  192. const userMessage = await MessageModel.remove(id, userId);
  193. await MessageModel.findByFieldsAndUpdateMany(
  194. {
  195. oldId: userMessage._id,
  196. },
  197. {
  198. deleted: true,
  199. replyMessage: null,
  200. replyCaption: null,
  201. forwardMessage: null,
  202. forwardCaption: null,
  203. fullType: null,
  204. oldId: null,
  205. }
  206. );
  207. const companionMessage = await MessageModel.findByFields(
  208. userId,
  209. userMessage.idTime,
  210. userMessage.companionId
  211. );
  212. await MessageModel.findByFieldsAndUpdateMany(
  213. {
  214. oldId: companionMessage._id,
  215. },
  216. {
  217. deleted: true,
  218. replyMessage: null,
  219. replyCaption: null,
  220. forwardMessage: null,
  221. forwardCaption: null,
  222. fullType: null,
  223. oldId: null,
  224. }
  225. );
  226. await MessageModel.removeByFields(
  227. userId,
  228. userMessage.idTime,
  229. userMessage.companionId
  230. );
  231. if (userMessage.type !== 'text') {
  232. const params = {
  233. Bucket: AWS_BUCKET_NAME,
  234. Key: `${userMessage.message}`,
  235. };
  236. s3.deleteObject(params, async function (err, _data) {
  237. if (err) throw err;
  238. });
  239. }
  240. const isChat = await ChatModel.getByField(userMessage.companionId, userId);
  241. const isCompanionChat = await ChatModel.getByField(
  242. userId,
  243. userMessage.companionId
  244. );
  245. const { total } = await MessageModel.getList(
  246. { owner: userId, companionId: userMessage.companionId },
  247. {}
  248. );
  249. const { total: Total } = await MessageModel.getList(
  250. { owner: userMessage.companionId, companionId: userId },
  251. {}
  252. );
  253. const seenCalculate = (seen, total) => {
  254. if (seen <= total) {
  255. return seen;
  256. } else {
  257. return total;
  258. }
  259. };
  260. const updatedAt = new Date();
  261. await ChatModel.update(isChat._id, userId, {
  262. total: total,
  263. seen: seenCalculate(isChat.seen, total),
  264. seenCompanion: seenCalculate(isChat.seenCompanion, total),
  265. lastMessage: 'The Message was Deleted',
  266. lastMessageCreatedAt: updatedAt,
  267. });
  268. await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
  269. total: Total,
  270. seen: seenCalculate(isCompanionChat.seen, Total),
  271. seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
  272. lastMessage: 'The Message was Deleted',
  273. lastMessageCreatedAt: updatedAt,
  274. });
  275. return res.json({
  276. status: 'success',
  277. code: 200,
  278. data: {},
  279. });
  280. } catch (e) {
  281. next(e);
  282. }
  283. };
  284. const removeSelected = async (req, res, next) => {
  285. try {
  286. const userId = req.user.id;
  287. const { selectedArr, companionId } = req.body;
  288. const toDeleteMessage = async (id) => {
  289. const userMessage = await MessageModel.remove(id, userId);
  290. await MessageModel.findByFieldsAndUpdateMany(
  291. {
  292. oldId: userMessage._id,
  293. },
  294. {
  295. deleted: true,
  296. replyMessage: null,
  297. replyCaption: null,
  298. forwardMessage: null,
  299. forwardCaption: null,
  300. fullType: null,
  301. oldId: null,
  302. }
  303. );
  304. const companionMessage = await MessageModel.findByFields(
  305. userId,
  306. userMessage.idTime,
  307. companionId
  308. );
  309. await MessageModel.findByFieldsAndUpdateMany(
  310. {
  311. oldId: companionMessage._id,
  312. },
  313. {
  314. deleted: true,
  315. replyMessage: null,
  316. replyCaption: null,
  317. forwardMessage: null,
  318. forwardCaption: null,
  319. fullType: null,
  320. oldId: null,
  321. }
  322. );
  323. await MessageModel.removeByFields(
  324. userId,
  325. userMessage.idTime,
  326. companionId
  327. );
  328. if (userMessage.type !== 'text') {
  329. const params = {
  330. Bucket: AWS_BUCKET_NAME,
  331. Key: `${userMessage.message}`,
  332. };
  333. s3.deleteObject(params, async function (err, _data) {
  334. if (err) throw err;
  335. });
  336. }
  337. };
  338. await selectedArr.forEach(async (id) => await toDeleteMessage(id));
  339. const isChat = await ChatModel.getByField(companionId, userId);
  340. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  341. const { total } = await MessageModel.getList(
  342. { owner: userId, companionId },
  343. {}
  344. );
  345. const { total: Total } = await MessageModel.getList(
  346. { owner: companionId, companionId: userId },
  347. {}
  348. );
  349. const seenCalculate = (seen, total) => {
  350. if (seen <= total) {
  351. return seen;
  352. } else {
  353. return total;
  354. }
  355. };
  356. const updatedAt = new Date();
  357. await ChatModel.update(isChat._id, userId, {
  358. total: total,
  359. seen: seenCalculate(isChat.seen, total),
  360. seenCompanion: seenCalculate(isChat.seenCompanion, total),
  361. lastMessage: 'Some Messages were Deleted',
  362. lastMessageCreatedAt: updatedAt,
  363. });
  364. await ChatModel.update(isCompanionChat._id, companionId, {
  365. total: Total,
  366. seen: seenCalculate(isCompanionChat.seen, Total),
  367. seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
  368. lastMessage: 'Some Messages were Deleted',
  369. lastMessageCreatedAt: updatedAt,
  370. });
  371. return res.json({
  372. status: 'success',
  373. code: 200,
  374. data: {},
  375. });
  376. } catch (e) {
  377. next(e);
  378. }
  379. };
  380. const sentMessage = async (req, res, next) => {
  381. try {
  382. const { id, message, caption } = req.body;
  383. const idTime = Math.round(Date.now() / 1000);
  384. const user = req.user;
  385. const userId = user.id;
  386. const companion = await UserModel.findById(id);
  387. const isChat = await ChatModel.getByField(id, userId);
  388. const isCompanionChat = await ChatModel.getByField(userId, id);
  389. const { name, lastName, avatarUrl, color, number } = user;
  390. if (companion && isChat && isCompanionChat) {
  391. const newMessage = await MessageModel.add({
  392. message,
  393. name,
  394. lastName,
  395. avatarUrl,
  396. color,
  397. number,
  398. type: 'text',
  399. caption,
  400. idTime,
  401. companionIdFlow: userId,
  402. companionId: id,
  403. owner: userId,
  404. });
  405. await MessageModel.add({
  406. message,
  407. name: isCompanionChat.name,
  408. lastName: isCompanionChat.lastName,
  409. avatarUrl,
  410. color,
  411. number,
  412. type: 'text',
  413. caption,
  414. idTime,
  415. companionIdFlow: userId,
  416. companionId: userId,
  417. owner: id,
  418. });
  419. const { total } = await MessageModel.getList(
  420. { owner: userId, companionId: id },
  421. {}
  422. );
  423. await ChatModel.update(isChat._id, userId, {
  424. total,
  425. seen: total,
  426. watched: false,
  427. lastMessage: message,
  428. lastMessageCreatedAt: newMessage.createdAt,
  429. });
  430. const { total: Total } = await MessageModel.getList(
  431. { owner: id, companionId: userId },
  432. {}
  433. );
  434. await ChatModel.update(isCompanionChat._id, id, {
  435. total: Total,
  436. seenCompanion: Total,
  437. lastMessage: message,
  438. lastMessageCreatedAt: newMessage.createdAt,
  439. });
  440. return res.status(201).json({
  441. status: 'success',
  442. code: 201,
  443. data: newMessage,
  444. });
  445. }
  446. } catch (e) {
  447. next(e);
  448. }
  449. };
  450. const callMessage = async (req, res, next) => {
  451. try {
  452. const { id, duration } = req.body;
  453. const idTime = Math.round(Date.now() / 1000);
  454. const user = req.user;
  455. const userId = user.id;
  456. const companion = await UserModel.findById(id);
  457. const isChat = await ChatModel.getByField(id, userId);
  458. const isCompanionChat = await ChatModel.getByField(userId, id);
  459. const { name, lastName, avatarUrl, color, number } = user;
  460. if (companion && isChat && isCompanionChat) {
  461. const newMessage = await MessageModel.add({
  462. initiator: true,
  463. duration,
  464. message: 'Outgoing Call',
  465. name,
  466. lastName,
  467. avatarUrl,
  468. color,
  469. number,
  470. type: 'text',
  471. idTime,
  472. companionIdFlow: userId,
  473. companionId: id,
  474. owner: userId,
  475. });
  476. await MessageModel.add({
  477. duration,
  478. message: 'Incoming Call',
  479. name: isCompanionChat.name,
  480. lastName: isCompanionChat.lastName,
  481. avatarUrl,
  482. color,
  483. number,
  484. type: 'text',
  485. idTime,
  486. companionIdFlow: userId,
  487. companionId: userId,
  488. owner: id,
  489. });
  490. const { total } = await MessageModel.getList(
  491. { owner: userId, companionId: id },
  492. {}
  493. );
  494. await ChatModel.update(isChat._id, userId, {
  495. total,
  496. seen: total,
  497. watched: false,
  498. lastMessage: 'Outgoing Call',
  499. lastMessageCreatedAt: newMessage.createdAt,
  500. });
  501. const { total: Total } = await MessageModel.getList(
  502. { owner: id, companionId: userId },
  503. {}
  504. );
  505. await ChatModel.update(isCompanionChat._id, id, {
  506. total: Total,
  507. seenCompanion: Total,
  508. lastMessage: 'Incoming Call',
  509. lastMessageCreatedAt: newMessage.createdAt,
  510. });
  511. return res.status(201).json({
  512. status: 'success',
  513. code: 201,
  514. data: newMessage,
  515. });
  516. }
  517. } catch (e) {
  518. next(e);
  519. }
  520. };
  521. const updateMessageCall = async (req, res, next) => {
  522. try {
  523. const { id, duration } = req.body;
  524. const userId = req.user.id;
  525. const userMessage = await MessageModel.getById(id, userId);
  526. const isChat = await ChatModel.getByField(userMessage.companionId, userId);
  527. const isCompanionChat = await ChatModel.getByField(
  528. userId,
  529. userMessage.companionId
  530. );
  531. const messageOne = duration ? 'Outgoing Call' : 'Canceled Call';
  532. const messageTwo = duration ? 'Incoming Call' : 'Missed Call';
  533. await MessageModel.updateMessage(id, userId, {
  534. message: messageOne,
  535. duration,
  536. });
  537. await MessageModel.findByFieldsAndUpdate(
  538. userId,
  539. userMessage.idTime,
  540. userMessage.companionId,
  541. {
  542. message: messageTwo,
  543. duration,
  544. }
  545. );
  546. await ChatModel.update(isChat._id, userId, {
  547. lastMessage: messageOne,
  548. });
  549. await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
  550. lastMessage: messageTwo,
  551. });
  552. return res.status(200).json({
  553. status: 'success',
  554. code: 200,
  555. data: {},
  556. });
  557. } catch (e) {
  558. next(e);
  559. }
  560. };
  561. const sentMessageReply = async (req, res, next) => {
  562. try {
  563. const { id, message, caption } = req.body;
  564. const idTime = Math.round(Date.now() / 1000);
  565. const user = req.user;
  566. const userId = user.id;
  567. const userMessage = await MessageModel.getById(id, userId);
  568. const companionMessage = await MessageModel.findByFields(
  569. userId,
  570. userMessage.idTime,
  571. userMessage.companionId
  572. );
  573. const isChat = await ChatModel.getByField(userMessage.companionId, userId);
  574. const isCompanionChat = await ChatModel.getByField(
  575. userId,
  576. userMessage.companionId
  577. );
  578. if (isChat && isCompanionChat && userMessage && companionMessage) {
  579. const newMessage = await MessageModel.add({
  580. message,
  581. replyMessage: userMessage.message,
  582. name: user.name,
  583. lastName: user.lastName,
  584. replyName: userMessage.name,
  585. replyLastName: userMessage.lastName,
  586. avatarUrl: user.avatarUrl,
  587. color: user.color,
  588. number: user.number,
  589. type: 'text',
  590. fullType: userMessage.fullType,
  591. caption,
  592. replyCaption: userMessage.caption,
  593. idTime,
  594. oldId: id,
  595. companionIdFlow: userId,
  596. companionId: userMessage.companionId,
  597. owner: userId,
  598. });
  599. await MessageModel.add({
  600. message,
  601. replyMessage: companionMessage.message,
  602. name: isCompanionChat.name,
  603. lastName: isCompanionChat.lastName,
  604. replyName: companionMessage.name,
  605. replyLastName: companionMessage.lastName,
  606. avatarUrl: user.avatarUrl,
  607. color: user.color,
  608. number: user.number,
  609. type: 'text',
  610. fullType: companionMessage.fullType,
  611. caption,
  612. replyCaption: companionMessage.caption,
  613. idTime,
  614. oldId: companionMessage._id,
  615. companionIdFlow: userId,
  616. companionId: userId,
  617. owner: userMessage.companionId,
  618. });
  619. const { total } = await MessageModel.getList(
  620. { owner: userId, companionId: userMessage.companionId },
  621. {}
  622. );
  623. await ChatModel.update(isChat._id, userId, {
  624. total,
  625. seen: total,
  626. watched: false,
  627. lastMessage: message,
  628. lastMessageCreatedAt: newMessage.createdAt,
  629. });
  630. const { total: Total } = await MessageModel.getList(
  631. { owner: userMessage.companionId, companionId: userId },
  632. {}
  633. );
  634. await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
  635. total: Total,
  636. seenCompanion: Total,
  637. lastMessage: message,
  638. lastMessageCreatedAt: newMessage.createdAt,
  639. });
  640. return res.status(201).json({
  641. status: 'success',
  642. code: 201,
  643. data: newMessage,
  644. });
  645. }
  646. } catch (e) {
  647. next(e);
  648. }
  649. };
  650. const sentMessageForward = async (req, res, next) => {
  651. try {
  652. const { id, companionIdForwardToAndFrom, message, caption } = req.body;
  653. const idTime = Math.round(Date.now() / 1000);
  654. const user = req.user;
  655. const userId = user.id;
  656. const userMessage = await MessageModel.getById(id, userId);
  657. const isChat = await ChatModel.getByField(
  658. companionIdForwardToAndFrom,
  659. userId
  660. );
  661. const companionMessage = await MessageModel.findByFields(
  662. userId,
  663. userMessage.idTime,
  664. userMessage.companionId
  665. );
  666. const isCompanionUser = await UserModel.findById(userMessage.companionId);
  667. const isForwardChat = await ChatModel.getByField(
  668. userId,
  669. companionIdForwardToAndFrom
  670. );
  671. if (
  672. userMessage &&
  673. isCompanionUser &&
  674. isChat &&
  675. isForwardChat &&
  676. companionMessage
  677. ) {
  678. const newMessage = await MessageModel.add({
  679. message,
  680. caption,
  681. forwardMessage: userMessage.message,
  682. forwardCaption: userMessage.caption,
  683. name: user.name,
  684. lastName: user.lastName,
  685. avatarUrl: user.avatarUrl,
  686. color: user.color,
  687. number: user.number,
  688. type: 'text',
  689. fullType: userMessage.fullType,
  690. forwardName:
  691. user.number === userMessage.number ? user.name : isCompanionUser.name,
  692. forwardLastName:
  693. user.number === userMessage.number
  694. ? user.lastName
  695. : isCompanionUser.lastName,
  696. oldId: userMessage._id,
  697. idTime,
  698. companionIdFlow: userId,
  699. companionIdForwardToAndFrom: userMessage.companionId,
  700. companionId: companionIdForwardToAndFrom,
  701. owner: userId,
  702. });
  703. await MessageModel.add({
  704. message,
  705. caption,
  706. forwardMessage: userMessage.message,
  707. forwardCaption: userMessage.caption,
  708. name: isForwardChat.name,
  709. lastName: isForwardChat.lastName,
  710. avatarUrl: user.avatarUrl,
  711. color: user.color,
  712. number: user.number,
  713. type: 'text',
  714. fullType: userMessage.fullType,
  715. forwardName:
  716. user.number === userMessage.number ? user.name : isCompanionUser.name,
  717. forwardLastName:
  718. user.number === userMessage.number
  719. ? user.lastName
  720. : isCompanionUser.lastName,
  721. oldId: companionMessage._id,
  722. idTime,
  723. companionIdFlow: userId,
  724. companionId: userId,
  725. companionIdForwardToAndFrom: userMessage.companionId,
  726. owner: companionIdForwardToAndFrom,
  727. });
  728. const { total } = await MessageModel.getList(
  729. { owner: userId, companionId: companionIdForwardToAndFrom },
  730. {}
  731. );
  732. await ChatModel.update(isChat._id, userId, {
  733. total,
  734. seen: total,
  735. watched: false,
  736. lastMessage: message,
  737. lastMessageCreatedAt: newMessage.createdAt,
  738. });
  739. const { total: Total } = await MessageModel.getList(
  740. { owner: companionIdForwardToAndFrom, companionId: userId },
  741. {}
  742. );
  743. await ChatModel.update(isForwardChat._id, companionIdForwardToAndFrom, {
  744. total: Total,
  745. seenCompanion: Total,
  746. lastMessage: message,
  747. lastMessageCreatedAt: newMessage.createdAt,
  748. });
  749. return res.status(201).json({
  750. status: 'success',
  751. code: 201,
  752. data: newMessage,
  753. });
  754. }
  755. } catch (e) {
  756. next(e);
  757. }
  758. };
  759. const imageMessage = async (req, res, next) => {
  760. try {
  761. const userId = req.user.id;
  762. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  763. const caption = rest.join(' ').slice(0, -1);
  764. const idTime = Math.round(Date.now() / 1000);
  765. const isChat = await ChatModel.getByField(id, userId);
  766. const isCompanionChat = await ChatModel.getByField(userId, id);
  767. const originalName = req.file.originalname;
  768. const pathToFile = req.file.path;
  769. const fullType = req.file.mimetype;
  770. const imgUrl = `${Math.round(Date.now() / 1000)}${userId}${originalName}`;
  771. const fileContent = await fs.readFile(pathToFile);
  772. const params = {
  773. Bucket: AWS_BUCKET_NAME,
  774. Key: `${imgUrl}`,
  775. Body: fileContent,
  776. };
  777. s3.upload(params, async (err, _data) => {
  778. if (err) throw err;
  779. fs.unlink(pathToFile);
  780. });
  781. if (isChat && isCompanionChat) {
  782. const { name, lastName, avatarUrl, color, number } = req.user;
  783. const newMessage = await MessageModel.add({
  784. message: imgUrl,
  785. name,
  786. lastName,
  787. avatarUrl,
  788. color,
  789. number,
  790. type: 'image',
  791. fullType,
  792. caption,
  793. idTime,
  794. companionIdFlow: userId,
  795. companionId: id,
  796. owner: userId,
  797. });
  798. await MessageModel.add({
  799. message: imgUrl,
  800. name: isCompanionChat.name,
  801. lastName: isCompanionChat.lastName,
  802. avatarUrl,
  803. color,
  804. number,
  805. type: 'image',
  806. fullType,
  807. caption,
  808. idTime,
  809. companionIdFlow: userId,
  810. companionId: userId,
  811. owner: id,
  812. });
  813. const { total } = await MessageModel.getList(
  814. { owner: userId, companionId: id },
  815. {}
  816. );
  817. await ChatModel.update(isChat._id, userId, {
  818. total,
  819. seen: total,
  820. watched: false,
  821. lastMessage: fullType,
  822. lastMessageCreatedAt: newMessage.createdAt,
  823. });
  824. const { total: Total } = await MessageModel.getList(
  825. { owner: id, companionId: userId },
  826. {}
  827. );
  828. await ChatModel.update(isCompanionChat._id, id, {
  829. total: Total,
  830. seenCompanion: Total,
  831. lastMessage: fullType,
  832. lastMessageCreatedAt: newMessage.createdAt,
  833. });
  834. return res.status(201).json({
  835. status: 'success',
  836. code: 201,
  837. data: newMessage,
  838. });
  839. }
  840. } catch (e) {
  841. next(e);
  842. }
  843. };
  844. const audioMessage = async (req, res, next) => {
  845. try {
  846. const userId = req.user.id;
  847. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  848. let caption = rest.join(' ');
  849. if (caption.length === 1) caption.slice(0, -1);
  850. const idTime = Math.round(Date.now() / 1000);
  851. const isChat = await ChatModel.getByField(id, userId);
  852. const isCompanionChat = await ChatModel.getByField(userId, id);
  853. const originalName = req.file.originalname;
  854. const pathToFile = req.file.path;
  855. const fullType = req.file.mimetype;
  856. const audioUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
  857. const fileContent = await fs.readFile(pathToFile);
  858. const params = {
  859. Bucket: AWS_BUCKET_NAME,
  860. Key: `${audioUrl}`,
  861. Body: fileContent,
  862. };
  863. s3.upload(params, async (err, _data) => {
  864. if (err) throw err;
  865. fs.unlink(pathToFile);
  866. });
  867. if (isChat && isCompanionChat) {
  868. const { name, lastName, avatarUrl, color, number } = req.user;
  869. const newMessage = await MessageModel.add({
  870. message: audioUrl,
  871. name,
  872. lastName,
  873. avatarUrl,
  874. color,
  875. number,
  876. type: 'audio',
  877. fullType,
  878. caption,
  879. idTime,
  880. companionIdFlow: userId,
  881. companionId: id,
  882. owner: userId,
  883. });
  884. await MessageModel.add({
  885. message: audioUrl,
  886. name: isCompanionChat.name,
  887. lastName: isCompanionChat.lastName,
  888. avatarUrl,
  889. color,
  890. number,
  891. type: 'audio',
  892. fullType,
  893. caption,
  894. idTime,
  895. companionIdFlow: userId,
  896. companionId: userId,
  897. owner: id,
  898. });
  899. const { total } = await MessageModel.getList(
  900. { owner: userId, companionId: id },
  901. {}
  902. );
  903. await ChatModel.update(isChat._id, userId, {
  904. total,
  905. seen: total,
  906. watched: false,
  907. lastMessage: fullType,
  908. lastMessageCreatedAt: newMessage.createdAt,
  909. });
  910. const { total: Total } = await MessageModel.getList(
  911. { owner: id, companionId: userId },
  912. {}
  913. );
  914. await ChatModel.update(isCompanionChat._id, id, {
  915. total: Total,
  916. seenCompanion: Total,
  917. lastMessage: fullType,
  918. lastMessageCreatedAt: newMessage.createdAt,
  919. });
  920. return res.status(201).json({
  921. status: 'success',
  922. code: 201,
  923. data: newMessage,
  924. });
  925. }
  926. } catch (e) {
  927. next(e);
  928. }
  929. };
  930. const videoMessage = async (req, res, next) => {
  931. try {
  932. const userId = req.user.id;
  933. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  934. let caption = rest.join(' ');
  935. if (caption.length === 1) caption.slice(0, -1);
  936. const idTime = Math.round(Date.now() / 1000);
  937. const isChat = await ChatModel.getByField(id, userId);
  938. const isCompanionChat = await ChatModel.getByField(userId, id);
  939. const originalName = req.file.originalname;
  940. const pathToFile = req.file.path;
  941. const videoUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
  942. const fullType = req.file.mimetype;
  943. const fileContent = await fs.readFile(pathToFile);
  944. const params = {
  945. Bucket: AWS_BUCKET_NAME,
  946. Key: `${videoUrl}`,
  947. Body: fileContent,
  948. };
  949. s3.upload(params, async (err, _data) => {
  950. if (err) throw err;
  951. fs.unlink(pathToFile);
  952. });
  953. if (isChat && isCompanionChat) {
  954. const { name, lastName, avatarUrl, color, number } = req.user;
  955. const newMessage = await MessageModel.add({
  956. message: videoUrl,
  957. name: isCompanionChat.name,
  958. lastName: isCompanionChat.lastName,
  959. avatarUrl,
  960. color,
  961. number,
  962. type: 'video',
  963. fullType,
  964. caption,
  965. idTime,
  966. companionIdFlow: userId,
  967. companionId: id,
  968. owner: userId,
  969. });
  970. await MessageModel.add({
  971. message: videoUrl,
  972. name,
  973. lastName,
  974. avatarUrl,
  975. color,
  976. number,
  977. type: 'video',
  978. fullType,
  979. caption,
  980. idTime,
  981. companionIdFlow: userId,
  982. companionId: userId,
  983. owner: id,
  984. });
  985. const { total } = await MessageModel.getList(
  986. { owner: userId, companionId: id },
  987. {}
  988. );
  989. await ChatModel.update(isChat._id, userId, {
  990. total,
  991. seen: total,
  992. watched: false,
  993. lastMessage: fullType,
  994. lastMessageCreatedAt: newMessage.createdAt,
  995. });
  996. const { total: Total } = await MessageModel.getList(
  997. { owner: id, companionId: userId },
  998. {}
  999. );
  1000. await ChatModel.update(isCompanionChat._id, id, {
  1001. total: Total,
  1002. seenCompanion: Total,
  1003. lastMessage: fullType,
  1004. lastMessageCreatedAt: newMessage.createdAt,
  1005. });
  1006. return res.status(201).json({
  1007. status: 'success',
  1008. code: 201,
  1009. data: newMessage,
  1010. });
  1011. }
  1012. } catch (e) {
  1013. next(e);
  1014. }
  1015. };
  1016. const fileMessage = async (req, res, next) => {
  1017. try {
  1018. const userId = req.user.id;
  1019. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  1020. let caption = rest.join(' ');
  1021. if (caption.length === 1) caption.slice(0, -1);
  1022. const idTime = Math.round(Date.now() / 1000);
  1023. const isChat = await ChatModel.getByField(id, userId);
  1024. const isCompanionChat = await ChatModel.getByField(userId, id);
  1025. const pathToFile = req.file.path;
  1026. const fullType = req.file.mimetype;
  1027. let type;
  1028. switch (fullType) {
  1029. case 'application/pdf':
  1030. type = 'pdf';
  1031. break;
  1032. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
  1033. type = 'docx';
  1034. break;
  1035. case 'application/octet-stream':
  1036. type = 'docx';
  1037. break;
  1038. default:
  1039. break;
  1040. }
  1041. const fileUrl = `${Math.round(Date.now() / 1000)}file.${type}`;
  1042. const fileContent = await fs.readFile(pathToFile);
  1043. const params = {
  1044. Bucket: AWS_BUCKET_NAME,
  1045. Key: `${fileUrl}`,
  1046. Body: fileContent,
  1047. };
  1048. s3.upload(params, async (err, _data) => {
  1049. if (err) throw err;
  1050. fs.unlink(pathToFile);
  1051. });
  1052. if (isChat && isCompanionChat) {
  1053. const { name, lastName, avatarUrl, color, number } = req.user;
  1054. const newMessage = await MessageModel.add({
  1055. message: fileUrl,
  1056. name,
  1057. lastName,
  1058. avatarUrl,
  1059. color,
  1060. number,
  1061. type,
  1062. fullType,
  1063. caption,
  1064. idTime,
  1065. companionIdFlow: userId,
  1066. companionId: id,
  1067. owner: userId,
  1068. });
  1069. await MessageModel.add({
  1070. message: fileUrl,
  1071. name: isCompanionChat.name,
  1072. lastName: isCompanionChat.lastName,
  1073. avatarUrl,
  1074. color,
  1075. number,
  1076. type,
  1077. fullType,
  1078. caption,
  1079. idTime,
  1080. companionIdFlow: userId,
  1081. companionId: userId,
  1082. owner: id,
  1083. });
  1084. const { total } = await MessageModel.getList(
  1085. { owner: userId, companionId: id },
  1086. {}
  1087. );
  1088. await ChatModel.update(isChat._id, userId, {
  1089. total,
  1090. seen: total,
  1091. watched: false,
  1092. lastMessage: fullType,
  1093. lastMessageCreatedAt: newMessage.createdAt,
  1094. });
  1095. const { total: Total } = await MessageModel.getList(
  1096. { owner: id, companionId: userId },
  1097. {}
  1098. );
  1099. await ChatModel.update(isCompanionChat._id, id, {
  1100. total: Total,
  1101. seenCompanion: Total,
  1102. lastMessage: fullType,
  1103. lastMessageCreatedAt: newMessage.createdAt,
  1104. });
  1105. return res.status(201).json({
  1106. status: 'success',
  1107. code: 201,
  1108. data: newMessage,
  1109. });
  1110. }
  1111. } catch (e) {
  1112. next(e);
  1113. }
  1114. };
  1115. module.exports = {
  1116. listMessages,
  1117. removeMessage,
  1118. removeSelected,
  1119. editMessage,
  1120. updateMessageEmoji,
  1121. updateMessagePin,
  1122. unpinAllMessage,
  1123. listMessagesById,
  1124. sentMessage,
  1125. callMessage,
  1126. updateMessageCall,
  1127. sentMessageReply,
  1128. sentMessageForward,
  1129. imageMessage,
  1130. audioMessage,
  1131. videoMessage,
  1132. fileMessage,
  1133. };