chats.js 6.2 KB

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