chats.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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: [{ message: 'fff' }, { message: 'ddd' }],
  50. lastOnline: [new Date(), new Date()],
  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. avatarUrl,
  77. color,
  78. online,
  79. number,
  80. });
  81. await ChatModel.update(isCompanion._id, {
  82. avatarUrl: AvatarUrl,
  83. color: Color,
  84. online: Online,
  85. number: Number,
  86. });
  87. const updatedChat = await ChatModel.getByField(id, userId);
  88. return res.status(200).json({
  89. status: 'success',
  90. code: 200,
  91. data: updatedChat,
  92. });
  93. }
  94. if (companion) {
  95. const newChat =
  96. !isUser &&
  97. (await ChatModel.add({
  98. name,
  99. lastName,
  100. avatarUrl,
  101. color,
  102. online,
  103. number,
  104. companionId: id,
  105. owner: userId,
  106. }));
  107. !isCompanion &&
  108. (await ChatModel.add({
  109. name: Name,
  110. lastName: LastName,
  111. avatarUrl: AvatarUrl,
  112. color: Color,
  113. online: Online,
  114. number: Number,
  115. companionId: userId,
  116. owner: id,
  117. }));
  118. return res.status(201).json({
  119. status: 'success',
  120. code: 201,
  121. data: newChat ? newChat : isUser,
  122. });
  123. }
  124. } catch (e) {
  125. next(e);
  126. }
  127. };
  128. const removeChatForBoth = async (req, res, next) => {
  129. try {
  130. const companionId = req.params.id;
  131. const userId = req.user.id;
  132. const isUserChat = await ChatModel.getByField(companionId, userId);
  133. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  134. if (isUserChat || isCompanionChat) {
  135. isUserChat && (await MessageModel.removeAll(userId));
  136. isCompanionChat && (await MessageModel.removeAll(companionId));
  137. isUserChat && (await ChatModel.remove(isUserChat._id, userId));
  138. isCompanionChat &&
  139. (await ChatModel.remove(isCompanionChat._id, companionId));
  140. return res.json({
  141. status: 'success',
  142. code: 200,
  143. data: {},
  144. });
  145. } else {
  146. return res.status(404).json({
  147. status: 'error',
  148. code: 404,
  149. data: 'Not Found',
  150. });
  151. }
  152. } catch (e) {
  153. next(e);
  154. }
  155. };
  156. const removeChatForMe = async (req, res, next) => {
  157. try {
  158. const companionId = req.params.id;
  159. const userId = req.user.id;
  160. const isUserChat = await ChatModel.getByField(companionId, userId);
  161. if (isUserChat) {
  162. await MessageModel.removeAll(userId);
  163. await ChatModel.remove(isUserChat._id, userId);
  164. return res.json({
  165. status: 'success',
  166. code: 200,
  167. data: {},
  168. });
  169. } else {
  170. return res.status(404).json({
  171. status: 'error',
  172. code: 404,
  173. data: 'Not Found',
  174. });
  175. }
  176. } catch (e) {
  177. next(e);
  178. }
  179. };
  180. const muteChat = async (req, res, next) => {
  181. try {
  182. const id = req.body.id;
  183. const userId = req.user.id;
  184. const isChat = await ChatModel.getByField(id, userId);
  185. if (isChat) {
  186. const { _id, mute } = isChat;
  187. await ChatModel.update(_id, { mute: !mute });
  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 sortChat = async (req, res, next) => {
  199. try {
  200. const id = req.body.id;
  201. const userId = req.user.id;
  202. const isChat = await ChatModel.getByField(id, userId);
  203. if (isChat) {
  204. const { _id, sort } = isChat;
  205. await ChatModel.update(_id, { sort: !sort });
  206. return res.status(200).json({
  207. status: 'success',
  208. code: 200,
  209. data: {},
  210. });
  211. }
  212. } catch (e) {
  213. next(e);
  214. }
  215. };
  216. const seenChat = async (req, res, next) => {
  217. try {
  218. const id = req.body.id;
  219. const userId = req.user.id;
  220. const isChat = await ChatModel.getByField(id, userId);
  221. const isCompanionChat = await ChatModel.getByField(userId, id);
  222. const isMessage = await MessageModel.getList(userId, {});
  223. if (isChat && isMessage && isCompanionChat) {
  224. const { total } = isMessage;
  225. await ChatModel.update(isChat._id, { seen: total, watched: false });
  226. await ChatModel.update(isCompanionChat._id, { watched: true });
  227. return res.status(200).json({
  228. status: 'success',
  229. code: 200,
  230. data: {},
  231. });
  232. }
  233. } catch (e) {
  234. next(e);
  235. }
  236. };
  237. const typingChat = async (req, res, next) => {
  238. try {
  239. const { id, typing } = req.body;
  240. const userId = req.user.id;
  241. const isCompanionChat = await ChatModel.getByField(userId, id);
  242. if (isCompanionChat) {
  243. await ChatModel.update(isCompanionChat._id, { typing });
  244. return res.status(200).json({
  245. status: 'success',
  246. code: 200,
  247. data: {},
  248. });
  249. }
  250. } catch (e) {
  251. next(e);
  252. }
  253. };
  254. const chatById = async (req, res, next) => {
  255. try {
  256. const userId = req.user.id;
  257. const companionId = req.params.companionId;
  258. const { online } = await UserModel.findById(companionId);
  259. const isChat = await ChatModel.getByField(companionId, userId);
  260. if (isChat) {
  261. const {
  262. name,
  263. lastName,
  264. avatarUrl,
  265. color,
  266. mute,
  267. sort,
  268. seen,
  269. total,
  270. watched,
  271. typing,
  272. number,
  273. _id,
  274. companionId,
  275. owner,
  276. createdAt,
  277. updatedAt,
  278. __v,
  279. } = isChat;
  280. return res.json({
  281. status: 'success',
  282. code: 200,
  283. data: {
  284. name,
  285. lastName,
  286. avatarUrl,
  287. color,
  288. online,
  289. mute,
  290. sort,
  291. seen,
  292. total,
  293. watched,
  294. typing,
  295. number,
  296. _id,
  297. companionId,
  298. owner,
  299. createdAt,
  300. updatedAt,
  301. __v,
  302. },
  303. });
  304. }
  305. } catch (e) {
  306. next(e);
  307. }
  308. };
  309. module.exports = {
  310. listChats,
  311. startChat,
  312. removeChatForBoth,
  313. removeChatForMe,
  314. muteChat,
  315. sortChat,
  316. seenChat,
  317. typingChat,
  318. chatById,
  319. };