chats.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 && !isUser && !isCompanion) {
  63. const newChat = await ChatModel.add({
  64. name,
  65. lastName,
  66. avatarUrl,
  67. color,
  68. online,
  69. number,
  70. companionId: id,
  71. owner: userId,
  72. });
  73. await ChatModel.add({
  74. name: Name,
  75. lastName: LastName,
  76. avatarUrl: AvatarUrl,
  77. color: Color,
  78. online: Online,
  79. number: Number,
  80. companionId: userId,
  81. owner: id,
  82. });
  83. return res.status(201).json({
  84. status: 'success',
  85. code: 201,
  86. data: newChat,
  87. });
  88. }
  89. } catch (e) {
  90. next(e);
  91. }
  92. };
  93. const removeChatForBoth = async (req, res, next) => {
  94. try {
  95. const companionId = req.params.id;
  96. const userId = req.user.id;
  97. const isUserChat = await ChatModel.getByField(companionId, userId);
  98. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  99. if (isUserChat || isCompanionChat) {
  100. isUserChat && (await MessageModel.removeAll(companionId, userId));
  101. isCompanionChat && (await MessageModel.removeAll(userId, companionId));
  102. isUserChat && (await ChatModel.remove(isUserChat._id, userId));
  103. isCompanionChat &&
  104. (await ChatModel.remove(isCompanionChat._id, companionId));
  105. return res.json({
  106. status: 'success',
  107. code: 200,
  108. data: {},
  109. });
  110. } else {
  111. return res.status(404).json({
  112. status: 'error',
  113. code: 404,
  114. data: 'Not Found',
  115. });
  116. }
  117. } catch (e) {
  118. next(e);
  119. }
  120. };
  121. const muteChat = async (req, res, next) => {
  122. try {
  123. const id = req.body.id;
  124. const userId = req.user.id;
  125. const isChat = await ChatModel.getByField(id, userId);
  126. if (isChat) {
  127. const { _id, mute } = isChat;
  128. await ChatModel.update(_id, userId, { mute: !mute });
  129. return res.status(200).json({
  130. status: 'success',
  131. code: 200,
  132. data: {},
  133. });
  134. }
  135. } catch (e) {
  136. next(e);
  137. }
  138. };
  139. const sortChat = 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, sort } = isChat;
  146. await ChatModel.update(_id, userId, { sort: !sort });
  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 seenChat = 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. const isCompanionChat = await ChatModel.getByField(userId, id);
  163. const isMessages = await MessageModel.getList(
  164. { owner: userId, companionId: id },
  165. {}
  166. );
  167. if (isChat && isMessages && isCompanionChat) {
  168. const { total } = isMessages;
  169. await ChatModel.update(isChat._id, userId, {
  170. seen: total,
  171. watched: false,
  172. });
  173. await ChatModel.update(isCompanionChat._id, id, { watched: true });
  174. return res.status(200).json({
  175. status: 'success',
  176. code: 200,
  177. data: {},
  178. });
  179. }
  180. } catch (e) {
  181. next(e);
  182. }
  183. };
  184. const typingChat = async (req, res, next) => {
  185. try {
  186. const { id, typing } = req.body;
  187. const userId = req.user.id;
  188. const isCompanionChat = await ChatModel.getByField(userId, id);
  189. if (isCompanionChat) {
  190. await ChatModel.update(isCompanionChat._id, id, { typing });
  191. return res.status(200).json({
  192. status: 'success',
  193. code: 200,
  194. data: {},
  195. });
  196. }
  197. } catch (e) {
  198. next(e);
  199. }
  200. };
  201. const chatById = async (req, res, next) => {
  202. try {
  203. const userId = req.user.id;
  204. const companionId = req.params.companionId;
  205. const isChat = await ChatModel.getByField(companionId, userId);
  206. if (isChat) {
  207. return res.json({
  208. status: 'success',
  209. code: 200,
  210. data: {
  211. ...isChat,
  212. },
  213. });
  214. }
  215. } catch (e) {
  216. next(e);
  217. }
  218. };
  219. module.exports = {
  220. listChats,
  221. startChat,
  222. removeChatForBoth,
  223. muteChat,
  224. sortChat,
  225. seenChat,
  226. typingChat,
  227. chatById,
  228. };