chats.js 5.4 KB

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