messages.js 8.8 KB

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