messages.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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({ owner: userId }, req.query);
  13. return res.json({
  14. status: 'success',
  15. code: 200,
  16. data: messages,
  17. });
  18. } catch (e) {
  19. next(e);
  20. }
  21. };
  22. const listMessagesById = async (req, res, next) => {
  23. try {
  24. const userId = req.user.id;
  25. const companionId = req.params.companionId;
  26. const messages = await MessageModel.getList(
  27. { owner: userId, companionId },
  28. {}
  29. );
  30. return res.json({
  31. status: 'success',
  32. code: 200,
  33. data: messages,
  34. });
  35. } catch (e) {
  36. next(e);
  37. }
  38. };
  39. const sentMessage = async (req, res, next) => {
  40. try {
  41. const { id, message } = req.body;
  42. const user = req.user;
  43. const userId = user.id;
  44. const companion = await UserModel.findById(id);
  45. const isChat = await ChatModel.getByField(id, userId);
  46. const isCompanionChat = await ChatModel.getByField(userId, id);
  47. const { name, lastName, avatarUrl, color, number } = user;
  48. if (companion && isChat && isCompanionChat) {
  49. const newMessage = await MessageModel.add({
  50. message,
  51. name,
  52. lastName,
  53. avatarUrl,
  54. color,
  55. number,
  56. type: 'text',
  57. companionId: id,
  58. owner: userId,
  59. });
  60. await MessageModel.add({
  61. message,
  62. name,
  63. lastName,
  64. avatarUrl,
  65. color,
  66. number,
  67. type: 'text',
  68. companionId: userId,
  69. owner: id,
  70. });
  71. const { total } = await MessageModel.getList(
  72. { owner: userId, companionId: id },
  73. {}
  74. );
  75. await ChatModel.update(isChat._id, userId, {
  76. total,
  77. seen: total,
  78. watched: false,
  79. lastMessage: message,
  80. lastMessageCreatedAt: newMessage.createdAt,
  81. });
  82. const { total: Total } = await MessageModel.getList(
  83. { owner: id, companionId: userId },
  84. {}
  85. );
  86. await ChatModel.update(isCompanionChat._id, id, {
  87. total: Total,
  88. lastMessage: message,
  89. lastMessageCreatedAt: newMessage.createdAt,
  90. });
  91. return res.status(201).json({
  92. status: 'success',
  93. code: 201,
  94. data: newMessage,
  95. });
  96. }
  97. } catch (e) {
  98. next(e);
  99. }
  100. };
  101. const imageMessage = async (req, res, next) => {
  102. try {
  103. const userId = req.user.id;
  104. const id = req.params.companionId;
  105. const isChat = await ChatModel.getByField(id, userId);
  106. const isCompanionChat = await ChatModel.getByField(userId, id);
  107. const DIR_IMAGES = process.env.DIR_IMAGES;
  108. const pathToFile = req.file.path;
  109. const newNameImg = req.file.originalname;
  110. const fullType = req.file.mimetype;
  111. await Jimp.read(pathToFile);
  112. await createFolderIsExist(path.join(DIR_IMAGES, userId));
  113. await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameImg));
  114. const imgUrl = path.normalize(path.join(userId, newNameImg));
  115. if (isChat && isCompanionChat && imgUrl) {
  116. const { name, lastName, avatarUrl, color, number } = req.user;
  117. const newMessage = await MessageModel.add({
  118. message: imgUrl,
  119. name,
  120. lastName,
  121. avatarUrl,
  122. color,
  123. number,
  124. type: 'image',
  125. fullType,
  126. companionId: id,
  127. owner: userId,
  128. });
  129. await MessageModel.add({
  130. message: imgUrl,
  131. name,
  132. lastName,
  133. avatarUrl,
  134. color,
  135. number,
  136. type: 'image',
  137. fullType,
  138. companionId: userId,
  139. owner: id,
  140. });
  141. const { total } = await MessageModel.getList(
  142. { owner: userId, companionId: id },
  143. {}
  144. );
  145. await ChatModel.update(isChat._id, userId, {
  146. total,
  147. seen: total,
  148. watched: false,
  149. lastMessage: imgUrl,
  150. lastMessageCreatedAt: newMessage.createdAt,
  151. });
  152. const { total: Total } = await MessageModel.getList(
  153. { owner: id, companionId: userId },
  154. {}
  155. );
  156. await ChatModel.update(isCompanionChat._id, id, {
  157. total: Total,
  158. lastMessage: imgUrl,
  159. lastMessageCreatedAt: newMessage.createdAt,
  160. });
  161. return res.status(201).json({
  162. status: 'success',
  163. code: 201,
  164. data: newMessage,
  165. });
  166. }
  167. } catch (e) {
  168. next(e);
  169. }
  170. };
  171. const audioMessage = async (req, res, next) => {
  172. try {
  173. const userId = req.user.id;
  174. const id = req.params.companionId;
  175. const isChat = await ChatModel.getByField(id, userId);
  176. const isCompanionChat = await ChatModel.getByField(userId, id);
  177. const DIR_AUDIOS = process.env.DIR_AUDIOS;
  178. const pathToFile = req.file.path;
  179. const newNameAudio = req.file.originalname;
  180. const fullType = req.file.mimetype;
  181. await createFolderIsExist(path.join(DIR_AUDIOS, userId));
  182. await fs.rename(pathToFile, path.join(DIR_AUDIOS, userId, newNameAudio));
  183. const audioUrl = path.normalize(path.join(userId, newNameAudio));
  184. if (isChat && isCompanionChat && audioUrl) {
  185. const { name, lastName, avatarUrl, color, number } = req.user;
  186. const newMessage = await MessageModel.add({
  187. message: audioUrl,
  188. name,
  189. lastName,
  190. avatarUrl,
  191. color,
  192. number,
  193. type: 'audio',
  194. fullType,
  195. companionId: id,
  196. owner: userId,
  197. });
  198. await MessageModel.add({
  199. message: audioUrl,
  200. name,
  201. lastName,
  202. avatarUrl,
  203. color,
  204. number,
  205. type: 'audio',
  206. fullType,
  207. companionId: userId,
  208. owner: id,
  209. });
  210. const { total } = await MessageModel.getList(
  211. { owner: userId, companionId: id },
  212. {}
  213. );
  214. await ChatModel.update(isChat._id, userId, {
  215. total,
  216. seen: total,
  217. watched: false,
  218. lastMessage: audioUrl,
  219. lastMessageCreatedAt: newMessage.createdAt,
  220. });
  221. const { total: Total } = await MessageModel.getList(
  222. { owner: id, companionId: userId },
  223. {}
  224. );
  225. await ChatModel.update(isCompanionChat._id, id, {
  226. total: Total,
  227. lastMessage: audioUrl,
  228. lastMessageCreatedAt: newMessage.createdAt,
  229. });
  230. return res.status(201).json({
  231. status: 'success',
  232. code: 201,
  233. data: newMessage,
  234. });
  235. }
  236. } catch (e) {
  237. next(e);
  238. }
  239. };
  240. const videoMessage = async (req, res, next) => {
  241. try {
  242. const userId = req.user.id;
  243. const id = req.params.companionId;
  244. const isChat = await ChatModel.getByField(id, userId);
  245. const isCompanionChat = await ChatModel.getByField(userId, id);
  246. const DIR_VIDEOS = process.env.DIR_VIDEOS;
  247. const pathToFile = req.file.path;
  248. const newNameVideo = req.file.originalname;
  249. const fullType = req.file.mimetype;
  250. await createFolderIsExist(path.join(DIR_VIDEOS, userId));
  251. await fs.rename(pathToFile, path.join(DIR_VIDEOS, userId, newNameVideo));
  252. const videoUrl = path.normalize(path.join(userId, newNameVideo));
  253. if (isChat && isCompanionChat && videoUrl) {
  254. const { name, lastName, avatarUrl, color, number } = req.user;
  255. const newMessage = await MessageModel.add({
  256. message: videoUrl,
  257. name,
  258. lastName,
  259. avatarUrl,
  260. color,
  261. number,
  262. type: 'video',
  263. fullType,
  264. companionId: id,
  265. owner: userId,
  266. });
  267. await MessageModel.add({
  268. message: videoUrl,
  269. name,
  270. lastName,
  271. avatarUrl,
  272. color,
  273. number,
  274. type: 'video',
  275. fullType,
  276. companionId: userId,
  277. owner: id,
  278. });
  279. const { total } = await MessageModel.getList(
  280. { owner: userId, companionId: id },
  281. {}
  282. );
  283. await ChatModel.update(isChat._id, userId, {
  284. total,
  285. seen: total,
  286. watched: false,
  287. lastMessage: videoUrl,
  288. lastMessageCreatedAt: newMessage.createdAt,
  289. });
  290. const { total: Total } = await MessageModel.getList(
  291. { owner: id, companionId: userId },
  292. {}
  293. );
  294. await ChatModel.update(isCompanionChat._id, id, {
  295. total: Total,
  296. lastMessage: videoUrl,
  297. lastMessageCreatedAt: newMessage.createdAt,
  298. });
  299. return res.status(201).json({
  300. status: 'success',
  301. code: 201,
  302. data: newMessage,
  303. });
  304. }
  305. } catch (e) {
  306. next(e);
  307. }
  308. };
  309. const fileMessage = async (req, res, next) => {
  310. try {
  311. const userId = req.user.id;
  312. const id = req.params.companionId;
  313. const isChat = await ChatModel.getByField(id, userId);
  314. const isCompanionChat = await ChatModel.getByField(userId, id);
  315. const DIR_FILES = process.env.DIR_FILES;
  316. const pathToFile = req.file.path;
  317. const newNameFile = req.file.originalname;
  318. const fullType = req.file.mimetype;
  319. let type;
  320. switch (fullType) {
  321. case 'application/pdf':
  322. type = 'pdf';
  323. break;
  324. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
  325. type = 'docx';
  326. break;
  327. case 'application/octet-stream':
  328. type = 'docx';
  329. break;
  330. default:
  331. break;
  332. }
  333. await createFolderIsExist(path.join(DIR_FILES, userId));
  334. await fs.rename(pathToFile, path.join(DIR_FILES, userId, newNameFile));
  335. const fileUrl = path.normalize(path.join(userId, newNameFile));
  336. if (isChat && isCompanionChat && fileUrl) {
  337. const { name, lastName, avatarUrl, color, number } = req.user;
  338. const newMessage = await MessageModel.add({
  339. message: fileUrl,
  340. name,
  341. lastName,
  342. avatarUrl,
  343. color,
  344. number,
  345. type,
  346. fullType,
  347. companionId: id,
  348. owner: userId,
  349. });
  350. await MessageModel.add({
  351. message: fileUrl,
  352. name,
  353. lastName,
  354. avatarUrl,
  355. color,
  356. number,
  357. type,
  358. fullType,
  359. companionId: userId,
  360. owner: id,
  361. });
  362. const { total } = await MessageModel.getList(
  363. { owner: userId, companionId: id },
  364. {}
  365. );
  366. await ChatModel.update(isChat._id, userId, {
  367. total,
  368. seen: total,
  369. watched: false,
  370. lastMessage: fileUrl,
  371. lastMessageCreatedAt: newMessage.createdAt,
  372. });
  373. const { total: Total } = await MessageModel.getList(
  374. { owner: id, companionId: userId },
  375. {}
  376. );
  377. await ChatModel.update(isCompanionChat._id, id, {
  378. total: Total,
  379. lastMessage: fileUrl,
  380. lastMessageCreatedAt: newMessage.createdAt,
  381. });
  382. return res.status(201).json({
  383. status: 'success',
  384. code: 201,
  385. data: newMessage,
  386. });
  387. }
  388. } catch (e) {
  389. next(e);
  390. }
  391. };
  392. module.exports = {
  393. listMessages,
  394. listMessagesById,
  395. sentMessage,
  396. imageMessage,
  397. audioMessage,
  398. videoMessage,
  399. fileMessage,
  400. };