chatsActions.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { history } from "../App";
  2. import { gql } from "../helpers";
  3. import {
  4. actionPromise,
  5. actionChatList,
  6. actionChatOne,
  7. actionChatLeft,
  8. actionAboutMe,
  9. store,
  10. } from "../reducers";
  11. import { actionGetAllLastMsg } from "./msgActions";
  12. import { actionUploadFile } from "./mediaActions";
  13. // в массив newMemders передавать объекты только с полем _id
  14. const actionUpdateChat = (title, members, chatId) =>
  15. actionPromise(
  16. "updateChat",
  17. gql(
  18. `mutation updateChat($chat:ChatInput) {
  19. ChatUpsert(chat:$chat) {
  20. _id
  21. title
  22. avatar {
  23. _id
  24. url
  25. }
  26. owner {
  27. _id
  28. login
  29. avatar {
  30. _id
  31. url
  32. }
  33. }
  34. members {
  35. _id
  36. login
  37. nick
  38. avatar {
  39. _id
  40. url
  41. }
  42. }
  43. lastModified
  44. }
  45. }`,
  46. { chat: { _id: chatId, title, members } }
  47. )
  48. );
  49. // MediaUpsert нужен только для добавления данных для загруженного файла
  50. // и дальнейшего отображения его через эти данные (через аватары, сообщения)
  51. const actionUpdateChatAvatar = (mediaId, chatId) =>
  52. actionPromise(
  53. "uploadFile",
  54. gql(
  55. `mutation uploadFile($media: MediaInput) {
  56. MediaUpsert(media: $media) {
  57. _id
  58. url
  59. }
  60. }`,
  61. { media: { _id: mediaId, chatAvatars: { _id: chatId } } }
  62. )
  63. );
  64. export const actionSetChatInfo =
  65. (name, file, title, members, chatId) => async (dispatch) => {
  66. const chat = await dispatch(actionUpdateChat(title, members, chatId));
  67. if (file && chat._id) {
  68. const fileObj = await dispatch(actionUploadFile(name, file));
  69. const chatAvatar = await dispatch(
  70. actionUpdateChatAvatar(fileObj?._id, chat._id)
  71. );
  72. await dispatch(
  73. actionChatOne({ _id: chat._id, avatar: chatAvatar })
  74. );
  75. }
  76. };
  77. // поиск по значению в массиве объектов - { 'members._id': userId }
  78. export const actionGetChatsByUser = (userId, skipCount = 0, limitCount = 50) =>
  79. actionPromise(
  80. "userChats",
  81. gql(
  82. `query userChats($q: String) {
  83. ChatFind (query: $q){
  84. _id
  85. title
  86. avatar {
  87. _id
  88. url
  89. }
  90. owner {
  91. _id
  92. login
  93. avatar {
  94. _id
  95. url
  96. }
  97. }
  98. members {
  99. _id
  100. login
  101. nick
  102. avatar {
  103. _id
  104. url
  105. }
  106. }
  107. lastModified
  108. lastMessage {
  109. _id text
  110. }
  111. }
  112. }`,
  113. {
  114. q: JSON.stringify([
  115. {
  116. $or: [{ ___owner: userId }, { "members._id": userId }],
  117. },
  118. {
  119. sort: [{ lastModified: -1 }],
  120. skip: [skipCount],
  121. limit: [limitCount],
  122. },
  123. ]),
  124. }
  125. )
  126. );
  127. export const actionFullChatList =
  128. (userId, currentCount, limitCount = 50) =>
  129. async (dispatch) => {
  130. const payload = await dispatch(
  131. actionGetChatsByUser(userId, currentCount, limitCount)
  132. );
  133. if (payload) {
  134. await dispatch(actionChatList(payload));
  135. await dispatch(actionGetAllLastMsg(payload));
  136. }
  137. };
  138. export const actionGetChatById = (chatId) =>
  139. actionPromise(
  140. "chatById",
  141. gql(
  142. `query chatById($q: String) {
  143. ChatFindOne (query: $q){
  144. _id
  145. title
  146. avatar {
  147. _id
  148. url
  149. }
  150. owner {
  151. _id
  152. login
  153. avatar {
  154. _id
  155. url
  156. }
  157. }
  158. members {
  159. _id
  160. login
  161. nick
  162. avatar {
  163. _id
  164. url
  165. }
  166. }
  167. lastModified
  168. messages {
  169. _id text
  170. }
  171. }
  172. }`,
  173. {
  174. q: JSON.stringify([{ _id: chatId }]),
  175. }
  176. )
  177. );
  178. export const actionChatsCount = (userId) =>
  179. actionPromise(
  180. "chatsCount",
  181. gql(
  182. `query chatsCount($q: String) {
  183. ChatCount (query: $q)
  184. }`,
  185. {
  186. q: JSON.stringify([{ ___owner: userId }]),
  187. }
  188. )
  189. );
  190. // происходит когда юзер уходит сам, иначе в чат добавляются юзер, а не наоборот
  191. const actionUpdateUserChats = (userId, newChats) =>
  192. actionPromise(
  193. "updateUserChats",
  194. gql(
  195. `mutation updateUserChats($user:UserInput) {
  196. UserUpsert(user:$user) {
  197. _id
  198. login
  199. nick
  200. chats {
  201. title
  202. _id
  203. }
  204. }
  205. }`,
  206. { user: { _id: userId, chats: newChats } }
  207. )
  208. );