chats.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 seenChat = async (req, res, next) => {
  187. try {
  188. const id = req.body.id;
  189. const userId = req.user.id;
  190. const isChat = await ChatModel.getByField(id, userId);
  191. const isCompanionChat = await ChatModel.getByField(userId, id);
  192. const isMessages = await MessageModel.getList(
  193. { owner: userId, companionId: id },
  194. {}
  195. );
  196. if (isChat && isMessages && isCompanionChat) {
  197. const { total } = isMessages;
  198. await ChatModel.update(isChat._id, userId, {
  199. seen: total,
  200. watched: false,
  201. });
  202. await ChatModel.update(isCompanionChat._id, id, {
  203. watched: true,
  204. seenCompanion: total,
  205. });
  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 pinChat = async (req, res, next) => {
  217. try {
  218. const { id, pinned } = req.body;
  219. const userId = req.user.id;
  220. await ChatModel.update(id, userId, { pinned });
  221. return res.status(200).json({
  222. status: 'success',
  223. code: 200,
  224. data: {},
  225. });
  226. } catch (e) {
  227. next(e);
  228. }
  229. };
  230. const typingChat = async (req, res, next) => {
  231. try {
  232. const { id, typing } = req.body;
  233. const userId = req.user.id;
  234. const isCompanionChat = await ChatModel.getByField(userId, id);
  235. if (isCompanionChat) {
  236. await ChatModel.update(isCompanionChat._id, id, { typing });
  237. return res.status(200).json({
  238. status: 'success',
  239. code: 200,
  240. data: {},
  241. });
  242. }
  243. } catch (e) {
  244. next(e);
  245. }
  246. };
  247. const getChatById = async (req, res, next) => {
  248. try {
  249. const userId = req.user.id;
  250. const companionId = req.params.companionId;
  251. const isChat = await ChatModel.getByField(companionId, userId);
  252. if (isChat) {
  253. return res.json({
  254. status: 'success',
  255. code: 200,
  256. data: {
  257. ...isChat,
  258. },
  259. });
  260. }
  261. } catch (e) {
  262. next(e);
  263. }
  264. };
  265. module.exports = {
  266. listChats,
  267. startChat,
  268. removeChatForBoth,
  269. muteChat,
  270. sortChat,
  271. seenChat,
  272. pinChat,
  273. typingChat,
  274. getChatById,
  275. };