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