chats.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, userId, {
  44. avatarUrl,
  45. color,
  46. online,
  47. number,
  48. });
  49. await ChatModel.update(isCompanion._id, 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, userId, { 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, userId, { 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 isMessages = await MessageModel.getList(
  167. { owner: userId, companionId: id },
  168. {}
  169. );
  170. if (isChat && isMessages && isCompanionChat) {
  171. const { total } = isMessages;
  172. await ChatModel.update(isChat._id, userId, {
  173. seen: total,
  174. watched: false,
  175. });
  176. await ChatModel.update(isCompanionChat._id, id, { watched: true });
  177. return res.status(200).json({
  178. status: 'success',
  179. code: 200,
  180. data: {},
  181. });
  182. }
  183. } catch (e) {
  184. next(e);
  185. }
  186. };
  187. const typingChat = async (req, res, next) => {
  188. try {
  189. const { id, typing } = req.body;
  190. const userId = req.user.id;
  191. const isCompanionChat = await ChatModel.getByField(userId, id);
  192. if (isCompanionChat) {
  193. await ChatModel.update(isCompanionChat._id, id, { typing });
  194. return res.status(200).json({
  195. status: 'success',
  196. code: 200,
  197. data: {},
  198. });
  199. }
  200. } catch (e) {
  201. next(e);
  202. }
  203. };
  204. const chatById = async (req, res, next) => {
  205. try {
  206. const userId = req.user.id;
  207. const companionId = req.params.companionId;
  208. const isChat = await ChatModel.getByField(companionId, userId);
  209. if (isChat) {
  210. return res.json({
  211. status: 'success',
  212. code: 200,
  213. data: {
  214. ...isChat,
  215. },
  216. });
  217. }
  218. } catch (e) {
  219. next(e);
  220. }
  221. };
  222. module.exports = {
  223. listChats,
  224. startChat,
  225. removeChatForBoth,
  226. muteChat,
  227. sortChat,
  228. seenChat,
  229. typingChat,
  230. chatById,
  231. };