messages.js 9.8 KB

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