messages.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 imageMessage = 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. const audioMessage = async (req, res, next) => {
  136. try {
  137. const userId = req.user.id;
  138. const id = req.params.companionId;
  139. const isChat = await ChatModel.getByField(id, userId);
  140. const isCompanionChat = await ChatModel.getByField(userId, id);
  141. const DIR_AUDIOS = process.env.DIR_AUDIOS;
  142. const pathToFile = req.file.path;
  143. const newNameAudio = req.file.originalname;
  144. await createFolderIsExist(path.join(DIR_AUDIOS, userId));
  145. await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
  146. const audioUrl = path.normalize(path.join(userId, newNameAudio));
  147. if (isChat && isCompanionChat && audioUrl) {
  148. const { name, lastName, avatarUrl, color, number } = req.user;
  149. const newMessage = await MessageModel.add({
  150. message: audioUrl,
  151. name,
  152. lastName,
  153. avatarUrl,
  154. color,
  155. number,
  156. type: 'audio',
  157. companionId: id,
  158. owner: userId,
  159. });
  160. await MessageModel.add({
  161. message: audioUrl,
  162. name,
  163. lastName,
  164. avatarUrl,
  165. color,
  166. number,
  167. type: 'audio',
  168. companionId: userId,
  169. owner: id,
  170. });
  171. const { total } = await MessageModel.getList(userId, {});
  172. await ChatModel.update(isChat._id, { total, seen: total });
  173. const { total: Total } = await MessageModel.getList(id, {});
  174. await ChatModel.update(isCompanionChat._id, { total: Total });
  175. return res.status(201).json({
  176. status: 'success',
  177. code: 201,
  178. data: newMessage,
  179. });
  180. }
  181. } catch (e) {
  182. next(e);
  183. }
  184. };
  185. const videoMessage = async (req, res, next) => {
  186. try {
  187. const userId = req.user.id;
  188. const id = req.params.companionId;
  189. const isChat = await ChatModel.getByField(id, userId);
  190. const isCompanionChat = await ChatModel.getByField(userId, id);
  191. const DIR_VIDEOS = process.env.DIR_VIDEOS;
  192. const pathToFile = req.file.path;
  193. const newNameVideo = req.file.originalname;
  194. await createFolderIsExist(path.join(DIR_VIDEOS, userId));
  195. await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
  196. const videoUrl = path.normalize(path.join(userId, newNameVideo));
  197. if (isChat && isCompanionChat && videoUrl) {
  198. const { name, lastName, avatarUrl, color, number } = req.user;
  199. const newMessage = await MessageModel.add({
  200. message: videoUrl,
  201. name,
  202. lastName,
  203. avatarUrl,
  204. color,
  205. number,
  206. type: 'video',
  207. companionId: id,
  208. owner: userId,
  209. });
  210. await MessageModel.add({
  211. message: videoUrl,
  212. name,
  213. lastName,
  214. avatarUrl,
  215. color,
  216. number,
  217. type: 'video',
  218. companionId: userId,
  219. owner: id,
  220. });
  221. const { total } = await MessageModel.getList(userId, {});
  222. await ChatModel.update(isChat._id, { total, seen: total });
  223. const { total: Total } = await MessageModel.getList(id, {});
  224. await ChatModel.update(isCompanionChat._id, { total: Total });
  225. return res.status(201).json({
  226. status: 'success',
  227. code: 201,
  228. data: newMessage,
  229. });
  230. }
  231. } catch (e) {
  232. next(e);
  233. }
  234. };
  235. const fileMessage = async (req, res, next) => {
  236. try {
  237. const userId = req.user.id;
  238. const id = req.params.companionId;
  239. const isChat = await ChatModel.getByField(id, userId);
  240. const isCompanionChat = await ChatModel.getByField(userId, id);
  241. const DIR_FILES = process.env.DIR_FILES;
  242. const pathToFile = req.file.path;
  243. const newNameFile = req.file.originalname;
  244. await createFolderIsExist(path.join(DIR_FILES, userId));
  245. await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
  246. const fileUrl = path.normalize(path.join(userId, newNameFile));
  247. if (isChat && isCompanionChat && fileUrl) {
  248. const { name, lastName, avatarUrl, color, number } = req.user;
  249. const newMessage = await MessageModel.add({
  250. message: fileUrl,
  251. name,
  252. lastName,
  253. avatarUrl,
  254. color,
  255. number,
  256. type: 'file',
  257. companionId: id,
  258. owner: userId,
  259. });
  260. await MessageModel.add({
  261. message: fileUrl,
  262. name,
  263. lastName,
  264. avatarUrl,
  265. color,
  266. number,
  267. type: 'file',
  268. companionId: userId,
  269. owner: id,
  270. });
  271. const { total } = await MessageModel.getList(userId, {});
  272. await ChatModel.update(isChat._id, { total, seen: total });
  273. const { total: Total } = await MessageModel.getList(id, {});
  274. await ChatModel.update(isCompanionChat._id, { total: Total });
  275. return res.status(201).json({
  276. status: 'success',
  277. code: 201,
  278. data: newMessage,
  279. });
  280. }
  281. } catch (e) {
  282. next(e);
  283. }
  284. };
  285. module.exports = {
  286. listMessages,
  287. listMessagesById,
  288. sentMessage,
  289. imageMessage,
  290. audioMessage,
  291. videoMessage,
  292. fileMessage,
  293. };