actionGetMessageForChat.jsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. createdAt
  42. owner {
  43. nick
  44. }
  45. media {
  46. originalFileName
  47. url
  48. _id
  49. type
  50. }
  51. }
  52. forwardWith{
  53. _id
  54. text
  55. }
  56. owner {
  57. _id
  58. nick
  59. avatar {
  60. url
  61. }
  62. }
  63. }
  64. }`, {chat: JSON.stringify(
  65. [{"chat._id": _id},
  66. {
  67. sort: [{_id: -1}], limit: [20], skip: [messLen]
  68. }
  69. ]
  70. )}
  71. ))
  72. )
  73. if(messages){
  74. dispatch(actionAddMessages([...messages], _id))
  75. }
  76. return messages
  77. }