OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / reported-messages / reported-messages.saga.js

132 lines · javascript ref:
import {
  call as e,
  put as r,
  takeLatest as o,
  select as i,
  take as t,
  fork as s,
  cancel as l,
  all as a,
} from "redux-saga/effects"
import { getCoreBackend as d } from "../../backend"
import { apiCall as c } from "../../api/core"
import { apiSubscribeSaga as n } from "../lib/external-src"
import { taskErrorSaga as y } from "../error"
import { LOADING_STATUS as m } from "../lib/clock-list"
import {
  updateLatestReportedMessagesOrder as p,
  appendOlderReportedOrder as g,
  upsertReportedMessagesById as f,
  updateReportedMessagesLoadingStatus as u,
  setReportedMessagesInViewRange as b,
  updateSelectedReportedMessage as S,
  resetReportedMessages as M,
  resetSelectedReportedMessage as h,
  subscribeReportedMessages as R,
  loadOlderReportedMessages as E,
  subscribeReportedMessage as L,
} from "./reported-messages.action"
import {
  getReportedMessagesMinClock as I,
  getReportedMessagesLoadingStatus as O,
  getReportedMessagesAnchorClock as k,
  getSelectedReportedMessage as x,
} from "./reported-messages.selectors"
import { REPORTED_MESSAGES_DEFAULT_LIMIT as A } from "./reported-messages.constant"
import { updateFileEntries as D } from "../media/file"
import { parseMessage as C } from "../chat"
import {
  canModerate as G,
  getMyMemberCapabilities as N,
  memberWaitProfileData as W,
  setMemberProfile as _,
} from "../member"
function* j(e) {
  yield r(M())
  try {
    const o = d(),
      i = A
    yield n([o.subscribeReportedMessages, e, { limit: i }], (e) => r(p(e)), {
      onError: (e) => y("reportedMessagesLatestSubscribeSaga", e),
    })
  } catch (e) {
    ;(yield r(u(m.ERROR)), yield y("reportedMessagesLatestSubscribeSaga", e))
  }
}
function* q(o) {
  try {
    const t = yield i(O),
      s = yield i(I)
    if (t === m.ALL_LOADED || null == s) return
    yield r(u(m.LOADING))
    const l = d(),
      a = A,
      n = yield e(c, l.getReportedMessages, o, { clock: s, limit: a })
    yield r(g(n))
  } catch (e) {
    ;(yield r(u(m.ERROR)), yield y("loadOlderReportedMessagesSaga", e))
  }
}
function* v(e) {
  let o = null
  try {
    for (;;) {
      yield t([b, p, g])
      const a = yield i(k),
        c = null === a ? { limit: A } : { clock: a, limit: A }
      ;(o && (yield l(o), (o = null)),
        (o = yield s(function* () {
          const o = d()
          yield n(
            [o.subscribeReportedMessages, e, c],
            function* (e) {
              yield r(f(e))
            },
            { onError: (e) => y("reportedMessagesAnchorWatcherSaga", e) },
          )
        })))
    }
  } finally {
    ;(o && (yield l(o)), (o = null))
  }
}
export function* reportedMessageSubscribeSaga(e, o) {
  const t = o.payload
  ;((yield i(x)) && (yield r(h())),
    yield n(
      [d().subscribeChatMessages, e, { gte: t, lte: t }],
      function* (e) {
        const o = e?.[0]
        if (!o) return
        const { message: i, files: t } = C(o)
        ;(yield r(D(t)), yield r(S(i)))
      },
      { onError: (e) => y("reportedMessageSubscribeSaga", e) },
    ))
}
export function* reportedMessagesSaga(e, d) {
  let c = []
  try {
    for (;;) {
      yield W({ roomId: e, memberId: d })
      const n = yield i(N(e))
      ;(G(n)
        ? 0 === c.length &&
          (c = [
            yield s(v, e),
            yield o(R, j, e),
            yield o(E, q, e),
            yield o(L, reportedMessageSubscribeSaga, e),
          ])
        : (c.length > 0 && (yield a(c.map((e) => l(e))), (c = [])), yield r(M())),
        yield t(
          (r) => r.type === _.type && r.payload?.roomId === e && r.payload?.profile?.memberId === d,
        ))
    }
  } catch (e) {
    yield y("reportedMessagesSaga", e)
  } finally {
    c.length > 0 && (yield a(c.map((e) => l(e))), (c = []))
  }
}