OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / room / forward-message / forward-message.saga.js

173 lines · javascript ref:
import {
  cancel as e,
  debounce as o,
  fork as r,
  put as a,
  select as t,
  take as i,
  takeEvery as s,
  takeLatest as d,
} from "redux-saga/effects"
import { FEATURES as m, isFeatureSupported as n } from "@holepunchto/keet-core/features"
import {
  forwardMessage as l,
  forwardMsgRoomListInViewIdsChangeEvt as c,
  getForwardedMessageByRoomId as f,
  getForwardMsgRoomListInViewIds as y,
  getForwardMsgRoomListIsSearching as p,
  getForwardMsgRoomListSearchIds as g,
  getForwardMsgRoomListSearchText as u,
  resetForwardMessage as h,
  SEARCH_FORWARD_INPUT_THROTTLE as I,
  searchForwardMsgAllRooms as w,
  searchForwardMsgInputEvt as M,
  setForwardMessage as S,
  setForwardMessageRoomTo as R,
  setForwardMsgListSearchRoomIds as b,
  setForwardMsgRoomListIsSearching as x,
  setForwardMsgRoomListItemsInView as k,
  setForwardMsgRoomListSearchError as L,
} from "./forward-message.reducer"
import { getChatMessageById as V } from "../../chat/list-view"
import { createMessageId as E, getCoreId as T } from "../../chat/message/message"
import {
  addOrRemoveRecentRoom as v,
  setAllRooms as O,
  switchRoomSubmit as A,
  updateRecentRooms as D,
} from "../room.action"
import { searchRoomsApi as C, apiCall as F } from "../../../api/core"
import { getCoreBackend as U } from "../../../backend"
import {
  clearDraftMessage as $,
  getDraftMessageByRoomId as j,
} from "../../chat/draft-message/draft-message.reducer"
import { updateChatLastMessageBlocked as B } from "../../chat/last-message/last-message.action"
import { CHAT_MESSAGE_TYPE as H } from "../../chat/chat.constant"
import { taskErrorSaga as N } from "../../error"
import { setBlockedMembers as _ } from "../../identity"
import { forkChildren as G, apiSubscribeLatest as W } from "../../lib"
import { normalizedIncludes as q } from "../../lib/string"
import { roomListItemUpdateSaga as z } from "../list/room-list-item-update-saga"
import { getAppCurrentRoomId as J } from "../../app/app.reducer"
import { getRoomItemById as K } from "../room.selector"
export function* forwardMessageRoomListInViewSaga() {
  const e = {}
  yield d([b, O, D, v, k], function* () {
    const o = yield t(y)
    let r = !1
    ;(Object.keys(e).forEach((a) => {
      o.includes(a) || (delete e[a], (r = !0))
    }),
      o
        .filter((o) => !e[o])
        .forEach((o) => {
          ;((e[o] = !0), (r = !0))
        }),
      r && (yield a(c(Object.keys(e)))))
  })
}
export function* forwardMessageRoomDataSaga({ appVersion: e }) {
  ;(yield s(_, function* ({ payload: { blockedByMemberId: e } }) {
    yield a(B({ blockedByMemberId: e }))
  }),
    yield G(
      c,
      ({ payload: e }) => e,
      function* ({ taskId: o }) {
        yield W(
          [U().subscribeRoomInfo, o],
          function* (r) {
            yield z({
              data: r,
              roomId: o,
              appVersion: e,
              trackLastMsgUpdates: !1,
              trackNotificationsUpdates: !1,
            })
          },
          { onError: (e) => N("roomListItemTask", e) },
        )
      },
      (e) => N("roomListItem", e),
    ))
}
export function* forwardMessageRoomSearchSaga() {
  const e = yield t(u)
  if (e)
    try {
      ;(yield t(p)) || (yield a(x(!0)))
      const o = (yield* C()).reduce((o, r) => {
        const { title: a, roomId: t } = r
        return (a && q(a, e) && o.push(t), o)
      }, [])
      yield a(b(o))
    } catch (e) {
      ;(yield a(L(e.message)), yield N("roomSearchAll", e, { logOnly: !0 }))
    }
  else (yield t(g)).length > 0 && (yield a(b([])))
}
export function* forwardMessageRoomListSaga({ appVersion: e }) {
  ;(yield o(I, M, function* (e) {
    yield a(w(e.payload))
  }),
    yield d([w, v], forwardMessageRoomSearchSaga),
    yield r(forwardMessageRoomListInViewSaga),
    yield r(forwardMessageRoomDataSaga, { appVersion: e }))
}
export function* forwardMessageToRoomSaga({ appVersion: o }) {
  for (;;) {
    const {
        payload: { messageId: s, originRoomId: d },
      } = yield i(S),
      m = yield t(V, s)
    if (!m) {
      yield N("forwardMessageToRoomSaga", `Unable to find message by given id ${s}`)
      continue
    }
    const n = m.event?.type === H.LOCATION_SHARED
    if (!m.chat.text && !n) {
      yield N("forwardMessageToRoomSaga", "text message only unsupported")
      continue
    }
    const c = yield r(forwardMessageRoomListSaga, { appVersion: o }),
      f = yield i([R, h])
    if ((yield e(c), R.match(f))) {
      const e = f.payload
      ;((yield t(j, e)) && (yield a($({ roomId: e }))),
        yield a(
          l({
            text: n ? `${m.event.latitude}, ${m.event.longitude}` : m.chat.text,
            display: m.chat.display,
            segments: m.chat.segments,
            roomIdTo: e,
            roomIdFrom: d,
            messageId: E({ id: m.coreId }),
          }),
        ),
        yield a(A({ roomId: e })))
    }
  }
}
export function* forwardMessageCall(e) {
  if (!e) return
  const o = yield t(J)
  if (!o) return
  const r = yield t(K(o))
  if (!n(r?.version, m.FORWARDED_MESSAGE)) return
  const i = T(e.messageId)
  try {
    const o = U()
    yield F(o.forwardChatMessage, e.roomIdFrom, i, e.roomIdTo)
  } catch (e) {
    yield N("forwardMessage", e)
  } finally {
    yield a(h())
  }
}
export function* chatForwardMessageHandler() {
  const e = yield t(J),
    o = yield t((o) => f(o, e))
  yield forwardMessageCall(o)
}