OverviewFilesCommitsIssuesDocsReleasesPeers

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

206 lines · javascript ref:
import {
  all as e,
  call as o,
  select as i,
  put as r,
  fork as t,
  takeEvery as n,
  takeLeading as d,
  takeLatest as m,
} from "redux-saga/effects"
import { channel as a } from "redux-saga"
import { isFeatureSupported as l, FEATURES as s } from "@holepunchto/keet-core/features"
import { PREFERENCES as c } from "@holepunchto/keet-core/legacy-constants"
import { getCoreBackend as y } from "../../backend"
import { apiCall as p } from "../../api/core"
import { block as f, apiTakeMatch as u, apiTakeEvery as g } from "../lib"
import { resetFileEntries as v } from "../media/file/file.action"
import { getMyMember as I } from "../member"
import { userInfoNotification as b, userWarnNotification as h } from "../notification"
import { taskErrorSaga as x } from "../error"
import {
  roomPairCancel as R,
  roomPairingSaga as S,
  roomPairingCancelSaga as M,
  roomsPairingDataSaga as T,
} from "./joint"
import { roomDmPairingSaga as C } from "./dm"
import {
  setRoomNotSupported as E,
  updateRoomAvatarSaga as V,
  commonRoomSaga as D,
  dmRoomSaga as H,
  updateRoomTitle as k,
  updateRoomConfigSaga as A,
} from "./main"
import {
  switchRoomSubmit as N,
  setMainRoom as w,
  closeRoomSubmit as F,
  setNotifications as O,
  toggleRoomNotifications as B,
  setCreatedRoomMeta as G,
  repairRoom as L,
  roomSetRepairingStatus as U,
  moveIndexer as _,
  roomSetMoveIndexerStatus as j,
  toggleRoomMembershipEvents as P,
  fetchRoomStats as Y,
  setRoomStats as q,
} from "./room.action"
import {
  getRoomIsMembershipEventsDisabled as z,
  getRoomItemById as J,
  getRoomNotificationById as K,
} from "./room.selector"
import { createRoomInitialConfig as Q, sanitizeRoomOpts as W } from "./room.model"
import { ROOM_TYPE as X } from "./room.constant"
import { getMyMemberId as Z } from "../identity"
export function* roomToggleNotificationsHandler({ payload: { roomId: e } }) {
  if (!e) return
  const o = !(yield i(K(e))),
    t = y()
  ;(yield p(t.updateRoomPreferences, e, c.IS_FULLY_MUTED, !o),
    yield r(O({ [e]: { notifications: o } })))
}
export function* roomToggleMembershipEventsHandler({ payload: e }) {
  try {
    const o = yield i(z(e)),
      r = y()
    o ? yield p(r.enableMembershipEvents, e) : yield p(r.disableMembershipEvents, e)
  } catch (e) {
    yield x("roomToggleMembershipEventsHandler", e)
  }
}
export function* roomCreateHandler(e) {
  try {
    const {
      payload: { title: o, opts: i },
    } = e
    if (!o) return void (yield x("room create", "unable to create room without title"))
    const n = Q(o, W(i)),
      d = y(),
      m = yield p(d.createRoom, { config: n })
    if (!m) return void (yield x("room create", "backend failure: no room created"))
    ;(yield r(G({ roomId: m, title: o })), yield t(V, { roomId: m }), yield r(N({ roomId: m })))
  } catch (e) {
    yield x("roomCreateHandler", e)
  }
}
export function* roomGetStartStateData(o) {
  const [r, t] = yield e([i(I(o)), i(J(o))])
  return r && t && t.roomType && Number.isInteger(t.version)
    ? [{ roomId: o, roomType: t.roomType, roomVersion: t.version }, !0]
    : [{ roomId: o }, !1]
}
export function* activeRoomSaga({ roomId: e, messageId: o, appVersion: n }) {
  const d = y()
  try {
    if ((yield r(w({ roomId: e })), !e)) return
    yield g([d.subscribeActivateRoom, e], () => {}, {
      onError: (e) => x("subscribeActivateRoom", e),
      onCleanup: ({ apiClose: e }) => {
        if (e) return r(F({ openNextRoom: !0 }))
      },
    })
    const [m, a] = yield roomGetStartStateData(e),
      c = yield i(Z)
    if (!a) {
      const o = yield u([d.subscribeConfig, e], {
          matchFn: (e) => "string" == typeof e?.roomType,
          onError: (e) => x("activeRoomSaga: subscribeConfig match", e),
        }),
        i = yield u([d.subscribeRoomVersion, e], {
          matchFn: (e) => "number" == typeof e?.version,
          onError: (e) => x("activeRoomSaga: subscribeRoomVersion match", e),
        })
      ;((m.roomType = o.roomType), (m.roomVersion = i.version))
    }
    switch (((m.messageId = o), (m.myMemberId = c), (m.appVersion = n), m.roomType)) {
      case X.DM:
        l(m.roomVersion, s.DMS) ? yield t(H, m) : yield r(E())
        break
      case X.BROADCAST:
      case X.ROOM:
      case void 0:
        yield t(D, m)
        break
      default:
        yield r(E())
    }
    yield f()
  } catch (e) {
    yield x("active room", e)
  } finally {
    yield r(v())
  }
}
function* $({ payload: { roomId: e } }) {
  try {
    const o = y()
    ;(yield r(U({ roomId: e, status: !0 })), yield p(o.repair, e))
  } catch (e) {
    yield x("roomRepairingSaga", e)
  } finally {
    yield r(U({ roomId: e, status: !1 }))
  }
}
function* ee({ payload: { roomId: e } }) {
  try {
    yield r(j({ roomId: e, status: !0 }))
    const o = y(),
      t = yield i(Z),
      n = yield p(o.getDevicesByMemberId, e, t),
      d = yield p(o.getCurrentDeviceId, e),
      m = n.find((e) => e.status.isIndexing)
    if (!m)
      return void (yield r(
        h({ title: "Move Indexer", message: "Could not find any device indexing" }),
      ))
    if (m.id === d)
      return void (yield r(b({ title: "Move Indexer", message: "This device is now the indexer" })))
    ;(yield p(o.moveIndexer, e, d, m.id),
      yield r(b({ title: "Move Indexer", message: "Indexer moved to this device" })))
  } catch (e) {
    yield x("roomMoveIndexerSaga", e)
  } finally {
    yield r(j({ roomId: e, status: !1 }))
  }
}
function* oe({ payload: { roomId: o } }) {
  try {
    const i = y(),
      [t, n] = yield e([p(i.getRoomStats, o), p(i.getRoomInfo, o)])
    yield r(
      q({
        roomId: o,
        stats: {
          roomInfo: {
            version: n?.digest?.abi.version,
            experimental: n?.digest?.settings?.isExperimental,
          },
          ...t,
        },
      }),
    )
  } catch (e) {
    yield x("fetchRoomStatsSaga", e)
  }
}
export function* roomSaga({ appVersion: e }) {
  const i = yield o(a)
  ;(yield t(T, { pendingRoomChannel: i }),
    yield t(S, { pendingRoomChannel: i }),
    yield t(C, { appVersion: e }),
    yield n(R, M),
    yield d(B, roomToggleNotificationsHandler),
    yield m(P, roomToggleMembershipEventsHandler),
    yield n(L, $),
    yield n(_, ee),
    yield n(Y, oe),
    yield d(k, function* (e) {
      const { roomId: o, title: i } = e.payload
      o && i && (yield A(o, "title", i))
    }))
}