chats.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 isUser = await ChatModel.getByField(id, userId);
  36. const isCompanion = await ChatModel.getByField(userId, id);
  37. const {
  38. name,
  39. lastName,
  40. originalName,
  41. originalLastName,
  42. avatarUrl,
  43. avatarsArr,
  44. color,
  45. online,
  46. number,
  47. } = companion;
  48. const {
  49. name: Name,
  50. lastName: LastName,
  51. originalName: OriginalName,
  52. originalLastName: OriginalLastName,
  53. avatarUrl: AvatarUrl,
  54. avatarsArr: AvatarsArr,
  55. color: Color,
  56. online: Online,
  57. number: Number,
  58. } = user;
  59. if (companion && isUser && isCompanion) {
  60. await ChatModel.update(isUser._id, userId, {
  61. originalName,
  62. originalLastName,
  63. avatarUrl,
  64. avatarsArr,
  65. color,
  66. online,
  67. number,
  68. });
  69. await ChatModel.update(isCompanion._id, id, {
  70. originalName: OriginalName,
  71. originalLastName: OriginalLastName,
  72. avatarUrl: AvatarUrl,
  73. avatarsArr: AvatarsArr,
  74. color: Color,
  75. online: Online,
  76. number: Number,
  77. });
  78. const updatedChat = await ChatModel.getByField(id, userId);
  79. return res.status(200).json({
  80. status: 'success',
  81. code: 200,
  82. data: updatedChat,
  83. });
  84. }
  85. if (companion && !isUser && !isCompanion) {
  86. const newChat = await ChatModel.add({
  87. name,
  88. lastName,
  89. originalName,
  90. originalLastName,
  91. avatarUrl,
  92. avatarsArr,
  93. color,
  94. online,
  95. number,
  96. companionId: id,
  97. owner: userId,
  98. });
  99. await ChatModel.add({
  100. name: Name,
  101. lastName: LastName,
  102. originalName: OriginalName,
  103. originalLastName: originalLastName,
  104. avatarUrl: AvatarUrl,
  105. avatarsArr: AvatarsArr,
  106. color: Color,
  107. online: Online,
  108. number: Number,
  109. companionId: userId,
  110. owner: id,
  111. });
  112. return res.status(201).json({
  113. status: 'success',
  114. code: 201,
  115. data: newChat,
  116. });
  117. }
  118. } catch (e) {
  119. next(e);
  120. }
  121. };
  122. const removeChatForBoth = async (req, res, next) => {
  123. try {
  124. const companionId = req.params.id;
  125. const userId = req.user.id;
  126. const isUserChat = await ChatModel.getByField(companionId, userId);
  127. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  128. if (isUserChat && isCompanionChat) {
  129. const deleteFile = async (type, message) => {
  130. if (type !== 'text') await fs.unlink(path.join(DIR_STATIC, message));
  131. };
  132. const { messages } = await MessageModel.getList(
  133. { owner: userId, companionId },
  134. {}
  135. );
  136. await messages.forEach(
  137. async ({ type, message }) => await deleteFile(type, message)
  138. );
  139. await MessageModel.removeAll(companionId, userId);
  140. await MessageModel.removeAll(userId, companionId);
  141. await ChatModel.remove(isUserChat._id, userId);
  142. await ChatModel.remove(isCompanionChat._id, companionId);
  143. return res.json({
  144. status: 'success',
  145. code: 200,
  146. data: {},
  147. });
  148. } else {
  149. return res.status(404).json({
  150. status: 'error',
  151. code: 404,
  152. data: 'Not Found',
  153. });
  154. }
  155. } catch (e) {
  156. next(e);
  157. }
  158. };
  159. const muteChat = async (req, res, next) => {
  160. try {
  161. const id = req.body.id;
  162. const userId = req.user.id;
  163. const isChat = await ChatModel.getByField(id, userId);
  164. if (isChat) {
  165. const { _id, mute } = isChat;
  166. await ChatModel.update(_id, userId, { mute: !mute });
  167. return res.status(200).json({
  168. status: 'success',
  169. code: 200,
  170. data: {},
  171. });
  172. }
  173. } catch (e) {
  174. next(e);
  175. }
  176. };
  177. const sortChat = 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, sort } = isChat;
  184. await ChatModel.update(_id, userId, { sort: !sort });
  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 seenChat = 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. const isCompanionChat = await ChatModel.getByField(userId, id);
  201. const isMessages = await MessageModel.getList(
  202. { owner: userId, companionId: id },
  203. {}
  204. );
  205. if (isChat && isMessages && isCompanionChat) {
  206. const { total } = isMessages;
  207. await ChatModel.update(isChat._id, userId, {
  208. seen: total,
  209. watched: false,
  210. });
  211. await ChatModel.update(isCompanionChat._id, id, { watched: true });
  212. return res.status(200).json({
  213. status: 'success',
  214. code: 200,
  215. data: {},
  216. });
  217. }
  218. } catch (e) {
  219. next(e);
  220. }
  221. };
  222. const typingChat = async (req, res, next) => {
  223. try {
  224. const { id, typing } = req.body;
  225. const userId = req.user.id;
  226. const isCompanionChat = await ChatModel.getByField(userId, id);
  227. if (isCompanionChat) {
  228. await ChatModel.update(isCompanionChat._id, id, { typing });
  229. return res.status(200).json({
  230. status: 'success',
  231. code: 200,
  232. data: {},
  233. });
  234. }
  235. } catch (e) {
  236. next(e);
  237. }
  238. };
  239. const getChatById = async (req, res, next) => {
  240. try {
  241. const userId = req.user.id;
  242. const companionId = req.params.companionId;
  243. const isChat = await ChatModel.getByField(companionId, userId);
  244. if (isChat) {
  245. return res.json({
  246. status: 'success',
  247. code: 200,
  248. data: {
  249. ...isChat,
  250. },
  251. });
  252. }
  253. } catch (e) {
  254. next(e);
  255. }
  256. };
  257. module.exports = {
  258. listChats,
  259. startChat,
  260. removeChatForBoth,
  261. muteChat,
  262. sortChat,
  263. seenChat,
  264. typingChat,
  265. getChatById,
  266. };