messages.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. const fullType = req.file.mimetype;
  94. await Jimp.read(pathToFile);
  95. await createFolderIsExist(path.join(DIR_IMAGES, userId));
  96. await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
  97. const imgUrl = path.normalize(path.join(userId, newNameImg));
  98. if (isChat && isCompanionChat && imgUrl) {
  99. const { name, lastName, avatarUrl, color, number } = req.user;
  100. const newMessage = await MessageModel.add({
  101. message: imgUrl,
  102. name,
  103. lastName,
  104. avatarUrl,
  105. color,
  106. number,
  107. type: 'image',
  108. fullType,
  109. companionId: id,
  110. owner: userId,
  111. });
  112. await MessageModel.add({
  113. message: imgUrl,
  114. name,
  115. lastName,
  116. avatarUrl,
  117. color,
  118. number,
  119. type: 'image',
  120. fullType,
  121. companionId: userId,
  122. owner: id,
  123. });
  124. const { total } = await MessageModel.getList(userId, {});
  125. await ChatModel.update(isChat._id, { total, seen: total });
  126. const { total: Total } = await MessageModel.getList(id, {});
  127. await ChatModel.update(isCompanionChat._id, { total: Total });
  128. return res.status(201).json({
  129. status: 'success',
  130. code: 201,
  131. data: newMessage,
  132. });
  133. }
  134. } catch (e) {
  135. next(e);
  136. }
  137. };
  138. const audioMessage = async (req, res, next) => {
  139. try {
  140. const userId = req.user.id;
  141. const id = req.params.companionId;
  142. const isChat = await ChatModel.getByField(id, userId);
  143. const isCompanionChat = await ChatModel.getByField(userId, id);
  144. const DIR_AUDIOS = process.env.DIR_AUDIOS;
  145. const pathToFile = req.file.path;
  146. const newNameAudio = req.file.originalname;
  147. const fullType = req.file.mimetype;
  148. await createFolderIsExist(path.join(DIR_AUDIOS, userId));
  149. await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
  150. const audioUrl = path.normalize(path.join(userId, newNameAudio));
  151. if (isChat && isCompanionChat && audioUrl) {
  152. const { name, lastName, avatarUrl, color, number } = req.user;
  153. const newMessage = await MessageModel.add({
  154. message: audioUrl,
  155. name,
  156. lastName,
  157. avatarUrl,
  158. color,
  159. number,
  160. type: 'audio',
  161. fullType,
  162. companionId: id,
  163. owner: userId,
  164. });
  165. await MessageModel.add({
  166. message: audioUrl,
  167. name,
  168. lastName,
  169. avatarUrl,
  170. color,
  171. number,
  172. type: 'audio',
  173. fullType,
  174. companionId: userId,
  175. owner: id,
  176. });
  177. const { total } = await MessageModel.getList(userId, {});
  178. await ChatModel.update(isChat._id, { total, seen: total });
  179. const { total: Total } = await MessageModel.getList(id, {});
  180. await ChatModel.update(isCompanionChat._id, { total: Total });
  181. return res.status(201).json({
  182. status: 'success',
  183. code: 201,
  184. data: newMessage,
  185. });
  186. }
  187. } catch (e) {
  188. next(e);
  189. }
  190. };
  191. const videoMessage = async (req, res, next) => {
  192. try {
  193. const userId = req.user.id;
  194. const id = req.params.companionId;
  195. const isChat = await ChatModel.getByField(id, userId);
  196. const isCompanionChat = await ChatModel.getByField(userId, id);
  197. const DIR_VIDEOS = process.env.DIR_VIDEOS;
  198. const pathToFile = req.file.path;
  199. const newNameVideo = req.file.originalname;
  200. const fullType = req.file.mimetype;
  201. await createFolderIsExist(path.join(DIR_VIDEOS, userId));
  202. await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
  203. const videoUrl = path.normalize(path.join(userId, newNameVideo));
  204. if (isChat && isCompanionChat && videoUrl) {
  205. const { name, lastName, avatarUrl, color, number } = req.user;
  206. const newMessage = await MessageModel.add({
  207. message: videoUrl,
  208. name,
  209. lastName,
  210. avatarUrl,
  211. color,
  212. number,
  213. type: 'video',
  214. fullType,
  215. companionId: id,
  216. owner: userId,
  217. });
  218. await MessageModel.add({
  219. message: videoUrl,
  220. name,
  221. lastName,
  222. avatarUrl,
  223. color,
  224. number,
  225. type: 'video',
  226. fullType,
  227. companionId: userId,
  228. owner: id,
  229. });
  230. const { total } = await MessageModel.getList(userId, {});
  231. await ChatModel.update(isChat._id, { total, seen: total });
  232. const { total: Total } = await MessageModel.getList(id, {});
  233. await ChatModel.update(isCompanionChat._id, { total: Total });
  234. return res.status(201).json({
  235. status: 'success',
  236. code: 201,
  237. data: newMessage,
  238. });
  239. }
  240. } catch (e) {
  241. next(e);
  242. }
  243. };
  244. const fileMessage = async (req, res, next) => {
  245. try {
  246. const userId = req.user.id;
  247. const id = req.params.companionId;
  248. const isChat = await ChatModel.getByField(id, userId);
  249. const isCompanionChat = await ChatModel.getByField(userId, id);
  250. const DIR_FILES = process.env.DIR_FILES;
  251. const pathToFile = req.file.path;
  252. const newNameFile = req.file.originalname;
  253. const fullType = req.file.mimetype;
  254. let type;
  255. switch (fullType) {
  256. case 'application/pdf':
  257. type = 'pdf';
  258. break;
  259. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
  260. type = 'docx';
  261. break;
  262. case 'application/octet-stream':
  263. type = 'docx';
  264. break;
  265. default:
  266. break;
  267. }
  268. await createFolderIsExist(path.join(DIR_FILES, userId));
  269. await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
  270. const fileUrl = path.normalize(path.join(userId, newNameFile));
  271. if (isChat && isCompanionChat && fileUrl) {
  272. const { name, lastName, avatarUrl, color, number } = req.user;
  273. const newMessage = await MessageModel.add({
  274. message: fileUrl,
  275. name,
  276. lastName,
  277. avatarUrl,
  278. color,
  279. number,
  280. type,
  281. fullType,
  282. companionId: id,
  283. owner: userId,
  284. });
  285. await MessageModel.add({
  286. message: fileUrl,
  287. name,
  288. lastName,
  289. avatarUrl,
  290. color,
  291. number,
  292. type,
  293. fullType,
  294. companionId: userId,
  295. owner: id,
  296. });
  297. const { total } = await MessageModel.getList(userId, {});
  298. await ChatModel.update(isChat._id, { total, seen: total });
  299. const { total: Total } = await MessageModel.getList(id, {});
  300. await ChatModel.update(isCompanionChat._id, { total: Total });
  301. return res.status(201).json({
  302. status: 'success',
  303. code: 201,
  304. data: newMessage,
  305. });
  306. }
  307. } catch (e) {
  308. next(e);
  309. }
  310. };
  311. module.exports = {
  312. listMessages,
  313. listMessagesById,
  314. sentMessage,
  315. imageMessage,
  316. audioMessage,
  317. videoMessage,
  318. fileMessage,
  319. };