actionGetMessageForChat.jsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { actionPromise } from "./actionsPromise";
  2. import { gql } from "../helpers/gql";
  3. import { actionAddMessages } from "./actionsMessages";
  4. export const actionGetMessageForChat = (_id, fromChatItem) =>
  5. async (dispatch,getState) => {
  6. let messLen = fromChatItem ? 0 : Object.values(getState().chats[_id]?.messages || {}).length;
  7. let messages = await dispatch(
  8. actionPromise('messages', gql(`query FindMessChat($chat: String) {
  9. MessageFind(query: $chat) {
  10. _id
  11. text
  12. createdAt
  13. chat{
  14. _id
  15. }
  16. media {
  17. url
  18. _id
  19. type
  20. text
  21. originalFileName
  22. }
  23. replyTo{
  24. _id
  25. text
  26. media {
  27. url
  28. _id
  29. type
  30. }
  31. owner {
  32. nick
  33. }
  34. }
  35. replies{
  36. _id text
  37. }
  38. forwarded{
  39. _id
  40. text
  41. owner {
  42. nick
  43. }
  44. media {
  45. url
  46. _id
  47. type
  48. }
  49. }
  50. forwardWith{
  51. _id
  52. text
  53. }
  54. owner {
  55. _id
  56. nick
  57. avatar {
  58. url
  59. }
  60. }
  61. }
  62. }`, {chat: JSON.stringify(
  63. [{"chat._id": _id},
  64. {
  65. sort: [{_id: -1}], limit: [20], skip: [messLen]
  66. }
  67. ]
  68. )}
  69. ))
  70. )
  71. if(messages){
  72. dispatch(actionAddMessages([...messages], _id))
  73. }
  74. return messages
  75. }