chats.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. const ChatModel = require('../model/chat');
  2. const UserModel = require('../model/user');
  3. const MessageModel = require('../model/message');
  4. const fs = require('fs').promises;
  5. const path = require('path');
  6. require('dotenv').config();
  7. const listChats = async (req, res, next) => {
  8. try {
  9. const userId = req.user.id;
  10. const { total, limit, page, chats } = await ChatModel.getList(
  11. userId,
  12. req.query
  13. );
  14. return res.json({
  15. status: 'success',
  16. code: 200,
  17. data: {
  18. total,
  19. limit,
  20. page,
  21. chats,
  22. },
  23. });
  24. } catch (e) {
  25. next(e);
  26. }
  27. };
  28. const startChat = async (req, res, next) => {
  29. try {
  30. const id = req.body.id;
  31. const user = req.user;
  32. const userId = user.id;
  33. const companion = await UserModel.findById(id);
  34. const isUser = await ChatModel.getByField(id, userId);
  35. const isCompanion = await ChatModel.getByField(userId, id);
  36. const {
  37. name,
  38. lastName,
  39. originalName,
  40. originalLastName,
  41. avatarUrl,
  42. avatarsArr,
  43. color,
  44. online,
  45. number,
  46. } = companion;
  47. const {
  48. name: Name,
  49. lastName: LastName,
  50. originalName: OriginalName,
  51. originalLastName: OriginalLastName,
  52. avatarUrl: AvatarUrl,
  53. avatarsArr: AvatarsArr,
  54. color: Color,
  55. online: Online,
  56. number: Number,
  57. } = user;
  58. if (companion && isUser && isCompanion) {
  59. await ChatModel.update(isUser._id, userId, {
  60. originalName,
  61. originalLastName,
  62. avatarUrl,
  63. avatarsArr,
  64. color,
  65. online,
  66. number,
  67. });
  68. await ChatModel.update(isCompanion._id, id, {
  69. originalName: OriginalName,
  70. originalLastName: OriginalLastName,
  71. avatarUrl: AvatarUrl,
  72. avatarsArr: AvatarsArr,
  73. color: Color,
  74. online: Online,
  75. number: Number,
  76. });
  77. const updatedChat = await ChatModel.getByField(id, userId);
  78. return res.status(200).json({
  79. status: 'success',
  80. code: 200,
  81. data: updatedChat,
  82. });
  83. }
  84. if (companion && !isUser && !isCompanion) {
  85. const newChat = await ChatModel.add({
  86. name,
  87. lastName,
  88. originalName,
  89. originalLastName,
  90. avatarUrl,
  91. avatarsArr,
  92. color,
  93. online,
  94. number,
  95. companionId: id,
  96. owner: userId,
  97. });
  98. await ChatModel.add({
  99. name: Name,
  100. lastName: LastName,
  101. originalName: OriginalName,
  102. originalLastName: originalLastName,
  103. avatarUrl: AvatarUrl,
  104. avatarsArr: AvatarsArr,
  105. color: Color,
  106. online: Online,
  107. number: Number,
  108. companionId: userId,
  109. owner: id,
  110. });
  111. return res.status(201).json({
  112. status: 'success',
  113. code: 201,
  114. data: newChat,
  115. });
  116. }
  117. } catch (e) {
  118. next(e);
  119. }
  120. };
  121. const removeChatForBoth = async (req, res, next) => {
  122. try {
  123. const companionId = req.params.id;
  124. const userId = req.user.id;
  125. const DIR_IMAGES = process.env.DIR_IMAGES;
  126. const DIR_AUDIOS = process.env.DIR_AUDIOS;
  127. const DIR_VIDEOS = process.env.DIR_VIDEOS;
  128. const DIR_FILES = process.env.DIR_FILES;
  129. const isUserChat = await ChatModel.getByField(companionId, userId);
  130. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  131. if (isUserChat && isCompanionChat) {
  132. const deleteFile = async (type, message) => {
  133. if (type !== 'text') {
  134. switch (type) {
  135. case 'image':
  136. await fs.unlink(path.join(DIR_IMAGES, message));
  137. break;
  138. case 'audio':
  139. await fs.unlink(path.join(DIR_AUDIOS, message));
  140. break;
  141. case 'video':
  142. await fs.unlink(path.join(DIR_VIDEOS, message));
  143. break;
  144. default:
  145. await fs.unlink(path.join(DIR_FILES, message));
  146. break;
  147. }
  148. }
  149. };
  150. const { messages } = await MessageModel.getList(
  151. { owner: userId, companionId },
  152. {}
  153. );
  154. await messages.forEach(
  155. async ({ type, message }) => await deleteFile(type, message)
  156. );
  157. await MessageModel.removeAll(companionId, userId);
  158. await MessageModel.removeAll(userId, companionId);
  159. await ChatModel.remove(isUserChat._id, userId);
  160. await ChatModel.remove(isCompanionChat._id, companionId);
  161. return res.json({
  162. status: 'success',
  163. code: 200,
  164. data: {},
  165. });
  166. } else {
  167. return res.status(404).json({
  168. status: 'error',
  169. code: 404,
  170. data: 'Not Found',
  171. });
  172. }
  173. } catch (e) {
  174. next(e);
  175. }
  176. };
  177. const muteChat = async (req, res, next) => {
  178. try {
  179. const id = req.body.id;
  180. const userId = req.user.id;
  181. const isChat = await ChatModel.getByField(id, userId);
  182. if (isChat) {
  183. const { _id, mute } = isChat;
  184. await ChatModel.update(_id, userId, { mute: !mute });
  185. return res.status(200).json({
  186. status: 'success',
  187. code: 200,
  188. data: {},
  189. });
  190. }
  191. } catch (e) {
  192. next(e);
  193. }
  194. };
  195. const sortChat = async (req, res, next) => {
  196. try {
  197. const id = req.body.id;
  198. const userId = req.user.id;
  199. const isChat = await ChatModel.getByField(id, userId);
  200. if (isChat) {
  201. const { _id, sort } = isChat;
  202. await ChatModel.update(_id, userId, { sort: !sort });
  203. return res.status(200).json({
  204. status: 'success',
  205. code: 200,
  206. data: {},
  207. });
  208. }
  209. } catch (e) {
  210. next(e);
  211. }
  212. };
  213. const seenChat = async (req, res, next) => {
  214. try {
  215. const id = req.body.id;
  216. const userId = req.user.id;
  217. const isChat = await ChatModel.getByField(id, userId);
  218. const isCompanionChat = await ChatModel.getByField(userId, id);
  219. const isMessages = await MessageModel.getList(
  220. { owner: userId, companionId: id },
  221. {}
  222. );
  223. if (isChat && isMessages && isCompanionChat) {
  224. const { total } = isMessages;
  225. await ChatModel.update(isChat._id, userId, {
  226. seen: total,
  227. watched: false,
  228. });
  229. await ChatModel.update(isCompanionChat._id, id, { watched: true });
  230. return res.status(200).json({
  231. status: 'success',
  232. code: 200,
  233. data: {},
  234. });
  235. }
  236. } catch (e) {
  237. next(e);
  238. }
  239. };
  240. const typingChat = async (req, res, next) => {
  241. try {
  242. const { id, typing } = req.body;
  243. const userId = req.user.id;
  244. const isCompanionChat = await ChatModel.getByField(userId, id);
  245. if (isCompanionChat) {
  246. await ChatModel.update(isCompanionChat._id, id, { typing });
  247. return res.status(200).json({
  248. status: 'success',
  249. code: 200,
  250. data: {},
  251. });
  252. }
  253. } catch (e) {
  254. next(e);
  255. }
  256. };
  257. const getChatById = async (req, res, next) => {
  258. try {
  259. const userId = req.user.id;
  260. const companionId = req.params.companionId;
  261. const isChat = await ChatModel.getByField(companionId, userId);
  262. if (isChat) {
  263. return res.json({
  264. status: 'success',
  265. code: 200,
  266. data: {
  267. ...isChat,
  268. },
  269. });
  270. }
  271. } catch (e) {
  272. next(e);
  273. }
  274. };
  275. module.exports = {
  276. listChats,
  277. startChat,
  278. removeChatForBoth,
  279. muteChat,
  280. sortChat,
  281. seenChat,
  282. typingChat,
  283. getChatById,
  284. };