messages.js 23 KB

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