chats.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. 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 && !isUser && !isCompanion) {
  95. const newChat = await ChatModel.add({
  96. name,
  97. lastName,
  98. avatarUrl,
  99. color,
  100. online,
  101. number,
  102. companionId: id,
  103. owner: userId,
  104. });
  105. await ChatModel.add({
  106. name: Name,
  107. lastName: LastName,
  108. avatarUrl: AvatarUrl,
  109. color: Color,
  110. online: Online,
  111. number: Number,
  112. companionId: userId,
  113. owner: id,
  114. });
  115. return res.status(201).json({
  116. status: 'success',
  117. code: 201,
  118. data: newChat,
  119. });
  120. }
  121. } catch (e) {
  122. next(e);
  123. }
  124. };
  125. const muteChat = async (req, res, next) => {
  126. try {
  127. const id = req.body.id;
  128. const userId = req.user.id;
  129. const isChat = await ChatModel.getByField(id, userId);
  130. if (isChat) {
  131. const { _id, mute } = isChat;
  132. await ChatModel.update(_id, { mute: !mute });
  133. return res.status(200).json({
  134. status: 'success',
  135. code: 200,
  136. data: {},
  137. });
  138. }
  139. } catch (e) {
  140. next(e);
  141. }
  142. };
  143. const sortChat = async (req, res, next) => {
  144. try {
  145. const id = req.body.id;
  146. const userId = req.user.id;
  147. const isChat = await ChatModel.getByField(id, userId);
  148. if (isChat) {
  149. const { _id, sort } = isChat;
  150. await ChatModel.update(_id, { sort: !sort });
  151. return res.status(200).json({
  152. status: 'success',
  153. code: 200,
  154. data: {},
  155. });
  156. }
  157. } catch (e) {
  158. next(e);
  159. }
  160. };
  161. const seenChat = async (req, res, next) => {
  162. try {
  163. const id = req.body.id;
  164. const userId = req.user.id;
  165. const isChat = await ChatModel.getByField(id, userId);
  166. const isCompanionChat = await ChatModel.getByField(userId, id);
  167. const isMessage = await MessageModel.getList(userId, {});
  168. if (isChat && isMessage && isCompanionChat) {
  169. const { total } = isMessage;
  170. await ChatModel.update(isChat._id, { seen: total, watched: false });
  171. await ChatModel.update(isCompanionChat._id, { watched: true });
  172. return res.status(200).json({
  173. status: 'success',
  174. code: 200,
  175. data: {},
  176. });
  177. }
  178. } catch (e) {
  179. next(e);
  180. }
  181. };
  182. const typingChat = async (req, res, next) => {
  183. try {
  184. const { id, typing } = req.body;
  185. const userId = req.user.id;
  186. const isCompanionChat = await ChatModel.getByField(userId, id);
  187. if (isCompanionChat) {
  188. await ChatModel.update(isCompanionChat._id, { typing });
  189. return res.status(200).json({
  190. status: 'success',
  191. code: 200,
  192. data: {},
  193. });
  194. }
  195. } catch (e) {
  196. next(e);
  197. }
  198. };
  199. const chatById = async (req, res, next) => {
  200. try {
  201. const userId = req.user.id;
  202. const companionId = req.params.companionId;
  203. const { online } = await UserModel.findById(companionId);
  204. const isChat = await ChatModel.getByField(companionId, userId);
  205. if (isChat) {
  206. const {
  207. name,
  208. lastName,
  209. avatarUrl,
  210. color,
  211. mute,
  212. sort,
  213. seen,
  214. total,
  215. watched,
  216. typing,
  217. number,
  218. _id,
  219. companionId,
  220. owner,
  221. createdAt,
  222. updatedAt,
  223. __v,
  224. } = isChat;
  225. return res.json({
  226. status: 'success',
  227. code: 200,
  228. data: {
  229. name,
  230. lastName,
  231. avatarUrl,
  232. color,
  233. online,
  234. mute,
  235. sort,
  236. seen,
  237. total,
  238. watched,
  239. typing,
  240. number,
  241. _id,
  242. companionId,
  243. owner,
  244. createdAt,
  245. updatedAt,
  246. __v,
  247. },
  248. });
  249. }
  250. } catch (e) {
  251. next(e);
  252. }
  253. };
  254. module.exports = {
  255. listChats,
  256. startChat,
  257. muteChat,
  258. sortChat,
  259. seenChat,
  260. typingChat,
  261. chatById,
  262. };