chats.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. const ChatModel = require('../model/chat');
  2. const UserModel = require('../model/user');
  3. const MessageModel = require('../model/message');
  4. const fs = require('fs').promises;
  5. const path = require('path');
  6. const s3 = require('../helpers/aws');
  7. const AWS_BUCKET_NAME = process.env.AWS_BUCKET_NAME;
  8. require('dotenv').config();
  9. const listChats = async (req, res, next) => {
  10. try {
  11. const userId = req.user.id;
  12. const { total, limit, page, chats } = await ChatModel.getList(
  13. userId,
  14. req.query
  15. );
  16. return res.json({
  17. status: 'success',
  18. code: 200,
  19. data: {
  20. total,
  21. limit,
  22. page,
  23. chats,
  24. },
  25. });
  26. } catch (e) {
  27. next(e);
  28. }
  29. };
  30. const startChat = async (req, res, next) => {
  31. try {
  32. const id = req.body.id;
  33. const user = req.user;
  34. const userId = user.id;
  35. const companion = await UserModel.findById(id);
  36. const userChat = await ChatModel.getByField(id, userId);
  37. if (userChat) {
  38. return res.json({
  39. status: 'success',
  40. code: 200,
  41. data: userChat,
  42. });
  43. }
  44. if (!userChat) {
  45. const {
  46. name,
  47. lastName,
  48. originalName,
  49. originalLastName,
  50. avatarUrl,
  51. avatarsArr,
  52. color,
  53. online,
  54. number,
  55. country,
  56. } = companion;
  57. const {
  58. name: Name,
  59. lastName: LastName,
  60. originalName: OriginalName,
  61. originalLastName: OriginalLastName,
  62. avatarUrl: AvatarUrl,
  63. avatarsArr: AvatarsArr,
  64. color: Color,
  65. online: Online,
  66. number: Number,
  67. country: Country,
  68. } = user;
  69. const newChat = await ChatModel.add({
  70. name,
  71. lastName,
  72. originalName,
  73. originalLastName,
  74. avatarUrl,
  75. avatarsArr,
  76. color,
  77. online,
  78. number,
  79. country,
  80. companionId: id,
  81. owner: userId,
  82. });
  83. await ChatModel.add({
  84. name: Name,
  85. lastName: LastName,
  86. originalName: OriginalName,
  87. originalLastName: originalLastName,
  88. avatarUrl: AvatarUrl,
  89. avatarsArr: AvatarsArr,
  90. color: Color,
  91. online: Online,
  92. number: Number,
  93. country: Country,
  94. companionId: userId,
  95. owner: id,
  96. });
  97. return res.status(201).json({
  98. status: 'success',
  99. code: 201,
  100. data: newChat,
  101. });
  102. }
  103. } catch (e) {
  104. next(e);
  105. }
  106. };
  107. const removeChatForBoth = async (req, res, next) => {
  108. try {
  109. const companionId = req.params.id;
  110. const userId = req.user.id;
  111. const isUserChat = await ChatModel.getByField(companionId, userId);
  112. const isCompanionChat = await ChatModel.getByField(userId, companionId);
  113. if (isUserChat && isCompanionChat) {
  114. const deleteFile = async (type, message) => {
  115. if (type !== 'text') {
  116. const params = {
  117. Bucket: AWS_BUCKET_NAME,
  118. Key: `${message}`,
  119. };
  120. s3.deleteObject(params, async function (err, _data) {
  121. if (err) throw err;
  122. });
  123. }
  124. };
  125. const { messages } = await MessageModel.getList(
  126. { owner: userId, companionId },
  127. {}
  128. );
  129. await messages.forEach(
  130. async ({ type, message }) => await deleteFile(type, message)
  131. );
  132. await MessageModel.removeAll(companionId, userId);
  133. await MessageModel.removeAll(userId, companionId);
  134. await ChatModel.remove(isUserChat._id, userId);
  135. await ChatModel.remove(isCompanionChat._id, companionId);
  136. return res.json({
  137. status: 'success',
  138. code: 200,
  139. data: {},
  140. });
  141. } else {
  142. return res.status(404).json({
  143. status: 'error',
  144. code: 404,
  145. data: 'Not Found',
  146. });
  147. }
  148. } catch (e) {
  149. next(e);
  150. }
  151. };
  152. const muteChat = async (req, res, next) => {
  153. try {
  154. const id = req.body.id;
  155. const userId = req.user.id;
  156. const isChat = await ChatModel.getByField(id, userId);
  157. if (isChat) {
  158. const { _id, mute } = isChat;
  159. await ChatModel.update(_id, userId, { mute: !mute });
  160. return res.status(200).json({
  161. status: 'success',
  162. code: 200,
  163. data: {},
  164. });
  165. }
  166. } catch (e) {
  167. next(e);
  168. }
  169. };
  170. const sortChat = async (req, res, next) => {
  171. try {
  172. const id = req.body.id;
  173. const userId = req.user.id;
  174. const isChat = await ChatModel.getByField(id, userId);
  175. if (isChat) {
  176. const { _id, sort } = isChat;
  177. await ChatModel.update(_id, userId, { sort: !sort });
  178. return res.status(200).json({
  179. status: 'success',
  180. code: 200,
  181. data: {},
  182. });
  183. }
  184. } catch (e) {
  185. next(e);
  186. }
  187. };
  188. const seenChat = async (req, res, next) => {
  189. try {
  190. const id = req.body.id;
  191. const userId = req.user.id;
  192. const isChat = await ChatModel.getByField(id, userId);
  193. const isCompanionChat = await ChatModel.getByField(userId, id);
  194. const isMessages = await MessageModel.getList(
  195. { owner: userId, companionId: id },
  196. {}
  197. );
  198. if (isChat && isMessages && isCompanionChat) {
  199. const { total } = isMessages;
  200. await ChatModel.update(isChat._id, userId, {
  201. seen: total,
  202. watched: false,
  203. });
  204. await ChatModel.update(isCompanionChat._id, id, { watched: true });
  205. return res.status(200).json({
  206. status: 'success',
  207. code: 200,
  208. data: {},
  209. });
  210. }
  211. } catch (e) {
  212. next(e);
  213. }
  214. };
  215. const pinChat = async (req, res, next) => {
  216. try {
  217. const { id, pinned } = req.body;
  218. const userId = req.user.id;
  219. await ChatModel.update(id, userId, { pinned });
  220. return res.status(200).json({
  221. status: 'success',
  222. code: 200,
  223. data: {},
  224. });
  225. } catch (e) {
  226. next(e);
  227. }
  228. };
  229. const typingChat = async (req, res, next) => {
  230. try {
  231. const { id, typing } = req.body;
  232. const userId = req.user.id;
  233. const isCompanionChat = await ChatModel.getByField(userId, id);
  234. if (isCompanionChat) {
  235. await ChatModel.update(isCompanionChat._id, id, { typing });
  236. return res.status(200).json({
  237. status: 'success',
  238. code: 200,
  239. data: {},
  240. });
  241. }
  242. } catch (e) {
  243. next(e);
  244. }
  245. };
  246. const getChatById = async (req, res, next) => {
  247. try {
  248. const userId = req.user.id;
  249. const companionId = req.params.companionId;
  250. const isChat = await ChatModel.getByField(companionId, userId);
  251. if (isChat) {
  252. return res.json({
  253. status: 'success',
  254. code: 200,
  255. data: {
  256. ...isChat,
  257. },
  258. });
  259. }
  260. } catch (e) {
  261. next(e);
  262. }
  263. };
  264. module.exports = {
  265. listChats,
  266. startChat,
  267. removeChatForBoth,
  268. muteChat,
  269. sortChat,
  270. seenChat,
  271. pinChat,
  272. typingChat,
  273. getChatById,
  274. };