chats.js 5.6 KB

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