messages.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 updateMessageEmoji = async (req, res, next) => {
  39. try {
  40. const id = req.params.id;
  41. const userId = req.user.id;
  42. const { emoji } = req.body;
  43. const userMessage = await MessageModel.updateMessage(id, userId, { emoji });
  44. await MessageModel.findByFieldsAndUpdate(
  45. userId,
  46. userMessage.idTime,
  47. userMessage.companionId,
  48. {
  49. emojiCompanion: emoji,
  50. }
  51. );
  52. return res.status(200).json({
  53. status: 'success',
  54. code: 200,
  55. data: {},
  56. });
  57. } catch (e) {
  58. next(e);
  59. }
  60. };
  61. const updateMessagePin = async (req, res, next) => {
  62. try {
  63. const id = req.params.id;
  64. const userId = req.user.id;
  65. const { pinned } = req.body;
  66. const userMessage = await MessageModel.updateMessage(id, userId, {
  67. pinned,
  68. });
  69. await MessageModel.findByFieldsAndUpdate(
  70. userId,
  71. userMessage.idTime,
  72. userMessage.companionId,
  73. {
  74. pinned,
  75. }
  76. );
  77. return res.status(200).json({
  78. status: 'success',
  79. code: 200,
  80. data: {},
  81. });
  82. } catch (e) {
  83. next(e);
  84. }
  85. };
  86. const unpinAllMessage = async (req, res, next) => {
  87. try {
  88. const userId = req.user.id;
  89. const pinnedMessages = req.body.pinnedMessages;
  90. pinnedMessages.forEach(async (id) => {
  91. const userMessage = await MessageModel.updateMessage(id, userId, {
  92. pinned: false,
  93. });
  94. await MessageModel.findByFieldsAndUpdate(
  95. userId,
  96. userMessage.idTime,
  97. userMessage.companionId,
  98. {
  99. pinned: false,
  100. }
  101. );
  102. });
  103. return res.status(200).json({
  104. status: 'success',
  105. code: 200,
  106. data: {},
  107. });
  108. } catch (e) {
  109. next(e);
  110. }
  111. };
  112. const removeMessage = async (req, res, next) => {
  113. try {
  114. const id = req.params.id;
  115. const userId = req.user.id;
  116. const userMessage = await MessageModel.remove(id, userId);
  117. await MessageModel.findByFieldsAndUpdateMany(
  118. userMessage.companionId,
  119. id,
  120. userId,
  121. {
  122. replyMessage: null,
  123. replyName: null,
  124. replyLastName: null,
  125. replyCaption: null,
  126. oldId: null,
  127. }
  128. );
  129. const companionMessage = await MessageModel.findByFields(
  130. userId,
  131. userMessage.idTime,
  132. userMessage.companionId
  133. );
  134. await MessageModel.removeByFields(
  135. userId,
  136. userMessage.idTime,
  137. userMessage.companionId
  138. );
  139. await MessageModel.findByFieldsAndUpdateMany(
  140. userId,
  141. companionMessage._id,
  142. userMessage.companionId,
  143. {
  144. replyMessage: null,
  145. replyName: null,
  146. replyLastName: null,
  147. replyCaption: null,
  148. oldId: null,
  149. }
  150. );
  151. if (userMessage.type !== 'text') {
  152. const params = {
  153. Bucket: AWS_BUCKET_NAME,
  154. Key: `${userMessage.message}`,
  155. };
  156. s3.deleteObject(params, async function (err, _data) {
  157. if (err) throw err;
  158. });
  159. }
  160. const isChat = await ChatModel.getByField(userMessage.companionId, userId);
  161. const isCompanionChat = await ChatModel.getByField(
  162. userId,
  163. userMessage.companionId
  164. );
  165. const { total } = await MessageModel.getList(
  166. { owner: userId, companionId: userMessage.companionId },
  167. {}
  168. );
  169. const { total: Total } = await MessageModel.getList(
  170. { owner: userMessage.companionId, companionId: userId },
  171. {}
  172. );
  173. const seenCalculate = (seen, total) => {
  174. if (seen <= total) {
  175. return seen;
  176. } else {
  177. return total;
  178. }
  179. };
  180. await ChatModel.update(isChat._id, userId, {
  181. total: total,
  182. seen: seenCalculate(isChat.seen, total),
  183. seenCompanion: seenCalculate(isChat.seenCompanion, total),
  184. });
  185. await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
  186. total: Total,
  187. seen: seenCalculate(isCompanionChat.seen, Total),
  188. seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
  189. });
  190. return res.json({
  191. status: 'success',
  192. code: 200,
  193. data: {},
  194. });
  195. } catch (e) {
  196. next(e);
  197. }
  198. };
  199. const removeSelected = async (req, res, next) => {
  200. try {
  201. const userId = req.user.id;
  202. const { selectedArr, companionId } = req.body;
  203. const toDeleteMessage = async (id) => {
  204. const userMessage = await MessageModel.remove(id, userId);
  205. await MessageModel.findByFieldsAndUpdateMany(companionId, id, userId, {
  206. replyMessage: null,
  207. replyName: null,
  208. replyLastName: null,
  209. replyCaption: null,
  210. oldId: null,
  211. });
  212. const companionMessage = await MessageModel.findByFields(
  213. userId,
  214. userMessage.idTime,
  215. companionId
  216. );
  217. await MessageModel.findByFieldsAndUpdateMany(
  218. userId,
  219. companionMessage._id,
  220. companionId,
  221. {
  222. replyMessage: null,
  223. replyName: null,
  224. replyLastName: null,
  225. replyCaption: null,
  226. oldId: null,
  227. }
  228. );
  229. await MessageModel.removeByFields(
  230. userId,
  231. userMessage.idTime,
  232. companionId
  233. );
  234. if (userMessage.type !== 'text') {
  235. const params = {
  236. Bucket: AWS_BUCKET_NAME,
  237. Key: `${userMessage.message}`,
  238. };
  239. s3.deleteObject(params, async function (err, _data) {
  240. if (err) throw err;
  241. });
  242. }
  243. };
  244. await selectedArr.forEach(async (id) => await toDeleteMessage(id));
  245. const isChat = await ChatModel.getByField(companionId, userId);
  246. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  247. const { total } = await MessageModel.getList(
  248. { owner: userId, companionId },
  249. {}
  250. );
  251. const { total: Total } = await MessageModel.getList(
  252. { owner: companionId, companionId: userId },
  253. {}
  254. );
  255. const seenCalculate = (seen, total) => {
  256. if (seen <= total) {
  257. return seen;
  258. } else {
  259. return total;
  260. }
  261. };
  262. await ChatModel.update(isChat._id, userId, {
  263. total: total,
  264. seen: seenCalculate(isChat.seen, total),
  265. seenCompanion: seenCalculate(isChat.seenCompanion, total),
  266. });
  267. await ChatModel.update(isCompanionChat._id, companionId, {
  268. total: Total,
  269. seen: seenCalculate(isCompanionChat.seen, Total),
  270. seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
  271. });
  272. return res.json({
  273. status: 'success',
  274. code: 200,
  275. data: {},
  276. });
  277. } catch (e) {
  278. next(e);
  279. }
  280. };
  281. const sentMessage = async (req, res, next) => {
  282. try {
  283. const { id, message, caption } = req.body;
  284. const idTime = Math.round(Date.now() / 1000);
  285. const user = req.user;
  286. const userId = user.id;
  287. const companion = await UserModel.findById(id);
  288. const isChat = await ChatModel.getByField(id, userId);
  289. const isCompanionChat = await ChatModel.getByField(userId, id);
  290. const { name, lastName, avatarUrl, color, number } = user;
  291. if (companion && isChat && isCompanionChat) {
  292. const newMessage = await MessageModel.add({
  293. message,
  294. name,
  295. lastName,
  296. avatarUrl,
  297. color,
  298. number,
  299. type: 'text',
  300. caption,
  301. idTime,
  302. companionIdFlow: userId,
  303. companionId: id,
  304. owner: userId,
  305. });
  306. await MessageModel.add({
  307. message,
  308. name: isCompanionChat.name,
  309. lastName: isCompanionChat.lastName,
  310. avatarUrl,
  311. color,
  312. number,
  313. type: 'text',
  314. caption,
  315. idTime,
  316. companionIdFlow: userId,
  317. companionId: userId,
  318. owner: id,
  319. });
  320. const { total } = await MessageModel.getList(
  321. { owner: userId, companionId: id },
  322. {}
  323. );
  324. await ChatModel.update(isChat._id, userId, {
  325. total,
  326. seen: total,
  327. watched: false,
  328. lastMessage: message,
  329. lastMessageCreatedAt: newMessage.createdAt,
  330. });
  331. const { total: Total } = await MessageModel.getList(
  332. { owner: id, companionId: userId },
  333. {}
  334. );
  335. await ChatModel.update(isCompanionChat._id, id, {
  336. total: Total,
  337. seenCompanion: Total,
  338. lastMessage: message,
  339. lastMessageCreatedAt: newMessage.createdAt,
  340. });
  341. return res.status(201).json({
  342. status: 'success',
  343. code: 201,
  344. data: newMessage,
  345. });
  346. }
  347. } catch (e) {
  348. next(e);
  349. }
  350. };
  351. const sentMessageReply = async (req, res, next) => {
  352. try {
  353. const { id, message, caption } = req.body;
  354. const idTime = Math.round(Date.now() / 1000);
  355. const user = req.user;
  356. const userId = user.id;
  357. const userMessage = await MessageModel.getById(id, userId);
  358. const companionMessage = await MessageModel.findByFields(
  359. userId,
  360. userMessage.idTime,
  361. userMessage.companionId
  362. );
  363. const isChat = await ChatModel.getByField(userMessage.companionId, userId);
  364. const isCompanionChat = await ChatModel.getByField(
  365. userId,
  366. userMessage.companionId
  367. );
  368. if (isChat && isCompanionChat && userMessage && companionMessage) {
  369. const newMessage = await MessageModel.add({
  370. message,
  371. replyMessage: userMessage.message,
  372. name: user.name,
  373. lastName: user.lastName,
  374. replyName: userMessage.name,
  375. replyLastName: userMessage.lastName,
  376. avatarUrl: user.avatarUrl,
  377. color: user.color,
  378. number: user.number,
  379. type: 'text',
  380. fullType: userMessage.fullType,
  381. caption,
  382. replyCaption: userMessage.caption,
  383. idTime,
  384. oldId: id,
  385. companionIdFlow: userId,
  386. companionId: userMessage.companionId,
  387. owner: userId,
  388. });
  389. await MessageModel.add({
  390. message,
  391. replyMessage: companionMessage.message,
  392. name: isCompanionChat.name,
  393. lastName: isCompanionChat.lastName,
  394. replyName: companionMessage.name,
  395. replyLastName: companionMessage.lastName,
  396. avatarUrl: user.avatarUrl,
  397. color: user.color,
  398. number: user.number,
  399. type: 'text',
  400. fullType: companionMessage.fullType,
  401. caption,
  402. replyCaption: companionMessage.caption,
  403. idTime,
  404. oldId: companionMessage._id,
  405. companionIdFlow: userId,
  406. companionId: userId,
  407. owner: userMessage.companionId,
  408. });
  409. const { total } = await MessageModel.getList(
  410. { owner: userId, companionId: userMessage.companionId },
  411. {}
  412. );
  413. await ChatModel.update(isChat._id, userId, {
  414. total,
  415. seen: total,
  416. watched: false,
  417. lastMessage: message,
  418. lastMessageCreatedAt: newMessage.createdAt,
  419. });
  420. const { total: Total } = await MessageModel.getList(
  421. { owner: userMessage.companionId, companionId: userId },
  422. {}
  423. );
  424. await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
  425. total: Total,
  426. seenCompanion: Total,
  427. lastMessage: message,
  428. lastMessageCreatedAt: newMessage.createdAt,
  429. });
  430. return res.status(201).json({
  431. status: 'success',
  432. code: 201,
  433. data: newMessage,
  434. });
  435. }
  436. } catch (e) {
  437. next(e);
  438. }
  439. };
  440. const sentMessageForward = async (req, res, next) => {
  441. try {
  442. const { id, companionIdForwardToAndFrom, message, caption } = req.body;
  443. const idTime = Math.round(Date.now() / 1000);
  444. const user = req.user;
  445. const userId = user.id;
  446. const userMessage = await MessageModel.getById(id, userId);
  447. const isChat = await ChatModel.getByField(
  448. companionIdForwardToAndFrom,
  449. userId
  450. );
  451. const companionMessage = await MessageModel.findByFields(
  452. userId,
  453. userMessage.idTime,
  454. userMessage.companionId
  455. );
  456. const isCompanionUser = await UserModel.findById(userMessage.companionId);
  457. const isForwardHasChatWithCompanion = await ChatModel.getByField(
  458. userMessage.companionId,
  459. companionIdForwardToAndFrom
  460. );
  461. const isForwardChat = await ChatModel.getByField(
  462. userId,
  463. companionIdForwardToAndFrom
  464. );
  465. if (
  466. userMessage &&
  467. isCompanionUser &&
  468. isChat &&
  469. isForwardChat &&
  470. companionMessage
  471. ) {
  472. const newMessage = await MessageModel.add({
  473. message,
  474. caption,
  475. forwardMessage: userMessage.message,
  476. forwardCaption: userMessage.caption,
  477. name: user.name,
  478. lastName: user.lastName,
  479. avatarUrl: user.avatarUrl,
  480. color: user.color,
  481. number: user.number,
  482. type: 'text',
  483. fullType: userMessage.fullType,
  484. forwardName:
  485. user.number === userMessage.number ? user.name : isCompanionUser.name,
  486. forwardLastName:
  487. user.number === userMessage.number
  488. ? user.lastName
  489. : isCompanionUser.lastName,
  490. oldId: userMessage._id,
  491. idTime,
  492. companionIdFlow: userId,
  493. companionIdForwardToAndFrom: userMessage.companionId,
  494. companionId: companionIdForwardToAndFrom,
  495. owner: userId,
  496. });
  497. await MessageModel.add({
  498. message,
  499. caption,
  500. forwardMessage: userMessage.message,
  501. forwardCaption: userMessage.caption,
  502. name: isForwardChat.name,
  503. lastName: isForwardChat.lastName,
  504. avatarUrl: user.avatarUrl,
  505. color: user.color,
  506. number: user.number,
  507. type: 'text',
  508. fullType: userMessage.fullType,
  509. forwardName:
  510. user.number === userMessage.number ? user.name : isCompanionUser.name,
  511. forwardLastName:
  512. user.number === userMessage.number
  513. ? user.lastName
  514. : isCompanionUser.lastName,
  515. oldId: companionMessage._id,
  516. idTime,
  517. companionIdFlow: userId,
  518. companionId: userId,
  519. companionIdForwardToAndFrom: userMessage.companionId,
  520. owner: companionIdForwardToAndFrom,
  521. });
  522. const { total } = await MessageModel.getList(
  523. { owner: userId, companionId: companionIdForwardToAndFrom },
  524. {}
  525. );
  526. await ChatModel.update(isChat._id, userId, {
  527. total,
  528. seen: total,
  529. watched: false,
  530. lastMessage: message,
  531. lastMessageCreatedAt: newMessage.createdAt,
  532. });
  533. const { total: Total } = await MessageModel.getList(
  534. { owner: companionIdForwardToAndFrom, companionId: userId },
  535. {}
  536. );
  537. await ChatModel.update(isForwardChat._id, companionIdForwardToAndFrom, {
  538. total: Total,
  539. seenCompanion: Total,
  540. lastMessage: message,
  541. lastMessageCreatedAt: newMessage.createdAt,
  542. });
  543. return res.status(201).json({
  544. status: 'success',
  545. code: 201,
  546. data: newMessage,
  547. });
  548. }
  549. } catch (e) {
  550. next(e);
  551. }
  552. };
  553. const imageMessage = async (req, res, next) => {
  554. try {
  555. const userId = req.user.id;
  556. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  557. const caption = rest.join(' ').slice(0, -1);
  558. const idTime = Math.round(Date.now() / 1000);
  559. const isChat = await ChatModel.getByField(id, userId);
  560. const isCompanionChat = await ChatModel.getByField(userId, id);
  561. const originalName = req.file.originalname;
  562. const pathToFile = req.file.path;
  563. const fullType = req.file.mimetype;
  564. const imgUrl = `${Math.round(Date.now() / 1000)}${userId}${originalName}`;
  565. const fileContent = await fs.readFile(pathToFile);
  566. const params = {
  567. Bucket: AWS_BUCKET_NAME,
  568. Key: `${imgUrl}`,
  569. Body: fileContent,
  570. };
  571. s3.upload(params, async (err, _data) => {
  572. if (err) throw err;
  573. fs.unlink(pathToFile);
  574. });
  575. if (isChat && isCompanionChat) {
  576. const { name, lastName, avatarUrl, color, number } = req.user;
  577. const newMessage = await MessageModel.add({
  578. message: imgUrl,
  579. name,
  580. lastName,
  581. avatarUrl,
  582. color,
  583. number,
  584. type: 'image',
  585. fullType,
  586. caption,
  587. idTime,
  588. companionIdFlow: userId,
  589. companionId: id,
  590. owner: userId,
  591. });
  592. await MessageModel.add({
  593. message: imgUrl,
  594. name: isCompanionChat.name,
  595. lastName: isCompanionChat.lastName,
  596. avatarUrl,
  597. color,
  598. number,
  599. type: 'image',
  600. fullType,
  601. caption,
  602. idTime,
  603. companionIdFlow: userId,
  604. companionId: userId,
  605. owner: id,
  606. });
  607. const { total } = await MessageModel.getList(
  608. { owner: userId, companionId: id },
  609. {}
  610. );
  611. await ChatModel.update(isChat._id, userId, {
  612. total,
  613. seen: total,
  614. watched: false,
  615. lastMessage: imgUrl,
  616. lastMessageCreatedAt: newMessage.createdAt,
  617. });
  618. const { total: Total } = await MessageModel.getList(
  619. { owner: id, companionId: userId },
  620. {}
  621. );
  622. await ChatModel.update(isCompanionChat._id, id, {
  623. total: Total,
  624. seenCompanion: Total,
  625. lastMessage: imgUrl,
  626. lastMessageCreatedAt: newMessage.createdAt,
  627. });
  628. return res.status(201).json({
  629. status: 'success',
  630. code: 201,
  631. data: newMessage,
  632. });
  633. }
  634. } catch (e) {
  635. next(e);
  636. }
  637. };
  638. const audioMessage = async (req, res, next) => {
  639. try {
  640. const userId = req.user.id;
  641. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  642. let caption = rest.join(' ');
  643. if (caption.length === 1) caption.slice(0, -1);
  644. const idTime = Math.round(Date.now() / 1000);
  645. const isChat = await ChatModel.getByField(id, userId);
  646. const isCompanionChat = await ChatModel.getByField(userId, id);
  647. const originalName = req.file.originalname;
  648. const pathToFile = req.file.path;
  649. const fullType = req.file.mimetype;
  650. const audioUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
  651. const fileContent = await fs.readFile(pathToFile);
  652. const params = {
  653. Bucket: AWS_BUCKET_NAME,
  654. Key: `${audioUrl}`,
  655. Body: fileContent,
  656. };
  657. s3.upload(params, async (err, _data) => {
  658. if (err) throw err;
  659. fs.unlink(pathToFile);
  660. });
  661. if (isChat && isCompanionChat) {
  662. const { name, lastName, avatarUrl, color, number } = req.user;
  663. const newMessage = await MessageModel.add({
  664. message: audioUrl,
  665. name,
  666. lastName,
  667. avatarUrl,
  668. color,
  669. number,
  670. type: 'audio',
  671. fullType,
  672. caption,
  673. idTime,
  674. companionIdFlow: userId,
  675. companionId: id,
  676. owner: userId,
  677. });
  678. await MessageModel.add({
  679. message: audioUrl,
  680. name: isCompanionChat.name,
  681. lastName: isCompanionChat.lastName,
  682. avatarUrl,
  683. color,
  684. number,
  685. type: 'audio',
  686. fullType,
  687. caption,
  688. idTime,
  689. companionIdFlow: userId,
  690. companionId: userId,
  691. owner: id,
  692. });
  693. const { total } = await MessageModel.getList(
  694. { owner: userId, companionId: id },
  695. {}
  696. );
  697. await ChatModel.update(isChat._id, userId, {
  698. total,
  699. seen: total,
  700. watched: false,
  701. lastMessage: audioUrl,
  702. lastMessageCreatedAt: newMessage.createdAt,
  703. });
  704. const { total: Total } = await MessageModel.getList(
  705. { owner: id, companionId: userId },
  706. {}
  707. );
  708. await ChatModel.update(isCompanionChat._id, id, {
  709. total: Total,
  710. seenCompanion: Total,
  711. lastMessage: audioUrl,
  712. lastMessageCreatedAt: newMessage.createdAt,
  713. });
  714. return res.status(201).json({
  715. status: 'success',
  716. code: 201,
  717. data: newMessage,
  718. });
  719. }
  720. } catch (e) {
  721. next(e);
  722. }
  723. };
  724. const videoMessage = async (req, res, next) => {
  725. try {
  726. const userId = req.user.id;
  727. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  728. let caption = rest.join(' ');
  729. if (caption.length === 1) caption.slice(0, -1);
  730. const idTime = Math.round(Date.now() / 1000);
  731. const isChat = await ChatModel.getByField(id, userId);
  732. const isCompanionChat = await ChatModel.getByField(userId, id);
  733. const originalName = req.file.originalname;
  734. const pathToFile = req.file.path;
  735. const videoUrl = `${Math.round(Date.now() / 1000)}${originalName}`;
  736. const fullType = req.file.mimetype;
  737. const fileContent = await fs.readFile(pathToFile);
  738. const params = {
  739. Bucket: AWS_BUCKET_NAME,
  740. Key: `${videoUrl}`,
  741. Body: fileContent,
  742. };
  743. s3.upload(params, async (err, _data) => {
  744. if (err) throw err;
  745. fs.unlink(pathToFile);
  746. });
  747. if (isChat && isCompanionChat) {
  748. const { name, lastName, avatarUrl, color, number } = req.user;
  749. const newMessage = await MessageModel.add({
  750. message: videoUrl,
  751. name: isCompanionChat.name,
  752. lastName: isCompanionChat.lastName,
  753. avatarUrl,
  754. color,
  755. number,
  756. type: 'video',
  757. fullType,
  758. caption,
  759. idTime,
  760. companionIdFlow: userId,
  761. companionId: id,
  762. owner: userId,
  763. });
  764. await MessageModel.add({
  765. message: videoUrl,
  766. name,
  767. lastName,
  768. avatarUrl,
  769. color,
  770. number,
  771. type: 'video',
  772. fullType,
  773. caption,
  774. idTime,
  775. companionIdFlow: userId,
  776. companionId: userId,
  777. owner: id,
  778. });
  779. const { total } = await MessageModel.getList(
  780. { owner: userId, companionId: id },
  781. {}
  782. );
  783. await ChatModel.update(isChat._id, userId, {
  784. total,
  785. seen: total,
  786. watched: false,
  787. lastMessage: videoUrl,
  788. lastMessageCreatedAt: newMessage.createdAt,
  789. });
  790. const { total: Total } = await MessageModel.getList(
  791. { owner: id, companionId: userId },
  792. {}
  793. );
  794. await ChatModel.update(isCompanionChat._id, id, {
  795. total: Total,
  796. seenCompanion: Total,
  797. lastMessage: videoUrl,
  798. lastMessageCreatedAt: newMessage.createdAt,
  799. });
  800. return res.status(201).json({
  801. status: 'success',
  802. code: 201,
  803. data: newMessage,
  804. });
  805. }
  806. } catch (e) {
  807. next(e);
  808. }
  809. };
  810. const fileMessage = async (req, res, next) => {
  811. try {
  812. const userId = req.user.id;
  813. const [id, ...rest] = req.params.companionIdAndCaption.split(' ');
  814. let caption = rest.join(' ');
  815. if (caption.length === 1) caption.slice(0, -1);
  816. const idTime = Math.round(Date.now() / 1000);
  817. const isChat = await ChatModel.getByField(id, userId);
  818. const isCompanionChat = await ChatModel.getByField(userId, id);
  819. const pathToFile = req.file.path;
  820. const fullType = req.file.mimetype;
  821. let type;
  822. switch (fullType) {
  823. case 'application/pdf':
  824. type = 'pdf';
  825. break;
  826. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
  827. type = 'docx';
  828. break;
  829. case 'application/octet-stream':
  830. type = 'docx';
  831. break;
  832. default:
  833. break;
  834. }
  835. const fileUrl = `${Math.round(Date.now() / 1000)}file.${type}`;
  836. const fileContent = await fs.readFile(pathToFile);
  837. const params = {
  838. Bucket: AWS_BUCKET_NAME,
  839. Key: `${fileUrl}`,
  840. Body: fileContent,
  841. };
  842. s3.upload(params, async (err, _data) => {
  843. if (err) throw err;
  844. fs.unlink(pathToFile);
  845. });
  846. if (isChat && isCompanionChat) {
  847. const { name, lastName, avatarUrl, color, number } = req.user;
  848. const newMessage = await MessageModel.add({
  849. message: fileUrl,
  850. name,
  851. lastName,
  852. avatarUrl,
  853. color,
  854. number,
  855. type,
  856. fullType,
  857. caption,
  858. idTime,
  859. companionIdFlow: userId,
  860. companionId: id,
  861. owner: userId,
  862. });
  863. await MessageModel.add({
  864. message: fileUrl,
  865. name: isCompanionChat.name,
  866. lastName: isCompanionChat.lastName,
  867. avatarUrl,
  868. color,
  869. number,
  870. type,
  871. fullType,
  872. caption,
  873. idTime,
  874. companionIdFlow: userId,
  875. companionId: userId,
  876. owner: id,
  877. });
  878. const { total } = await MessageModel.getList(
  879. { owner: userId, companionId: id },
  880. {}
  881. );
  882. await ChatModel.update(isChat._id, userId, {
  883. total,
  884. seen: total,
  885. watched: false,
  886. lastMessage: fileUrl,
  887. lastMessageCreatedAt: newMessage.createdAt,
  888. });
  889. const { total: Total } = await MessageModel.getList(
  890. { owner: id, companionId: userId },
  891. {}
  892. );
  893. await ChatModel.update(isCompanionChat._id, id, {
  894. total: Total,
  895. seenCompanion: Total,
  896. lastMessage: fileUrl,
  897. lastMessageCreatedAt: newMessage.createdAt,
  898. });
  899. return res.status(201).json({
  900. status: 'success',
  901. code: 201,
  902. data: newMessage,
  903. });
  904. }
  905. } catch (e) {
  906. next(e);
  907. }
  908. };
  909. module.exports = {
  910. listMessages,
  911. removeMessage,
  912. removeSelected,
  913. updateMessageEmoji,
  914. updateMessagePin,
  915. unpinAllMessage,
  916. listMessagesById,
  917. sentMessage,
  918. sentMessageReply,
  919. sentMessageForward,
  920. imageMessage,
  921. audioMessage,
  922. videoMessage,
  923. fileMessage,
  924. };