messages.js 8.4 KB

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