messages.js 22 KB

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