chats.js 5.4 KB

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