messages.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const MessageModel = require('../model/message');
  2. const UserModel = require('../model/user');
  3. const ChatModel = require('../model/chat');
  4. const Jimp = require('jimp');
  5. const fs = require('fs').promises;
  6. const path = require('path');
  7. const createFolderIsExist = require('../helpers/create-directory');
  8. require('dotenv').config();
  9. const listMessages = async (req, res, next) => {
  10. try {
  11. const userId = req.user.id;
  12. const messages = await MessageModel.getList(userId, req.query);
  13. return res.json({
  14. status: 'success',
  15. code: 200,
  16. data: {
  17. ...messages,
  18. },
  19. });
  20. } catch (e) {
  21. next(e);
  22. }
  23. };
  24. const listMessagesById = async (req, res, next) => {
  25. try {
  26. const userId = req.user.id;
  27. const companionId = req.params.companionId;
  28. const messages = await MessageModel.getListById(companionId, userId);
  29. return res.json({
  30. status: 'success',
  31. code: 200,
  32. data: [...messages],
  33. });
  34. } catch (e) {
  35. next(e);
  36. }
  37. };
  38. const sentMessage = async (req, res, next) => {
  39. try {
  40. const { id, message } = req.body;
  41. const user = req.user;
  42. const userId = user.id;
  43. const companion = await UserModel.findById(id);
  44. const isChat = await ChatModel.getByField(id, userId);
  45. const isCompanionChat = await ChatModel.getByField(userId, id);
  46. const { name, lastName, avatarUrl, color, number } = user;
  47. if (companion && isChat && isCompanionChat) {
  48. const newMessage = await MessageModel.add({
  49. message,
  50. name,
  51. lastName,
  52. avatarUrl,
  53. color,
  54. number,
  55. type: 'text',
  56. companionId: id,
  57. owner: userId,
  58. });
  59. await MessageModel.add({
  60. message,
  61. name,
  62. lastName,
  63. avatarUrl,
  64. color,
  65. number,
  66. type: 'text',
  67. companionId: userId,
  68. owner: id,
  69. });
  70. const { total } = await MessageModel.getList(userId, {});
  71. await ChatModel.update(isChat._id, { total, seen: total });
  72. const { total: Total } = await MessageModel.getList(id, {});
  73. await ChatModel.update(isCompanionChat._id, { total: Total });
  74. return res.status(201).json({
  75. status: 'success',
  76. code: 201,
  77. data: newMessage,
  78. });
  79. }
  80. } catch (e) {
  81. next(e);
  82. }
  83. };
  84. const imgMessage = async (req, res, next) => {
  85. try {
  86. const userId = req.user.id;
  87. const id = req.params.companionId;
  88. const isChat = await ChatModel.getByField(id, userId);
  89. const isCompanionChat = await ChatModel.getByField(userId, id);
  90. const DIR_IMAGES = process.env.DIR_IMAGES;
  91. const pathToFile = req.file.path;
  92. const newNameImg = req.file.originalname;
  93. await Jimp.read(pathToFile);
  94. await createFolderIsExist(path.join(DIR_IMAGES, userId));
  95. await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
  96. const imgUrl = path.normalize(path.join(userId, newNameImg));
  97. if (isChat && isCompanionChat && imgUrl) {
  98. const { name, lastName, avatarUrl, color, number } = req.user;
  99. const newMessage = await MessageModel.add({
  100. message: imgUrl,
  101. name,
  102. lastName,
  103. avatarUrl,
  104. color,
  105. number,
  106. type: 'image',
  107. companionId: id,
  108. owner: userId,
  109. });
  110. await MessageModel.add({
  111. message: imgUrl,
  112. name,
  113. lastName,
  114. avatarUrl,
  115. color,
  116. number,
  117. type: 'image',
  118. companionId: userId,
  119. owner: id,
  120. });
  121. const { total } = await MessageModel.getList(userId, {});
  122. await ChatModel.update(isChat._id, { total, seen: total });
  123. const { total: Total } = await MessageModel.getList(id, {});
  124. await ChatModel.update(isCompanionChat._id, { total: Total });
  125. return res.status(201).json({
  126. status: 'success',
  127. code: 201,
  128. data: newMessage,
  129. });
  130. }
  131. } catch (e) {
  132. next(e);
  133. }
  134. };
  135. module.exports = {
  136. listMessages,
  137. listMessagesById,
  138. sentMessage,
  139. imgMessage,
  140. };