OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / ui-mobile / ui-push.saga.js

107 lines · javascript ref:
import i from "hypercore-id-encoding"
import {
  all as o,
  call as t,
  delay as e,
  fork as r,
  getContext as n,
  put as a,
  race as c,
  select as d,
  take as s,
  takeEvery as u,
  takeLatest as l,
} from "redux-saga/effects"
import y from "b4a"
import { getCoreBackend as f } from "../../backend"
import { getChatLastMessage as m, updateChatLastMessage as p } from "../chat"
import { getAppCurrentRoomId as h } from "../app"
import {
  getRoomListAllIds as b,
  roomNewActivity as g,
  setAllRooms as N,
  getRoomNotificationById as P,
  addOrRemoveRecentRoom as S,
} from "../room"
import { taskErrorSaga as v } from "../error"
import {
  uiClearRoomPushNotification as E,
  uiRequestNotificationPermission as M,
  uiSubscribeToRoomPushNotification as R,
  uiUnsubscribeToRoomPushNotification as I,
  uiUpdatePushNotificationBadge as T,
} from "./ui.action"
import { APP_CONTEXT_KEY as U } from "../constants"
import { apiSubscribeSaga as w } from "../lib"
import { ROOM_ACTIVITY as x } from "./ui.constants"
const C = {
  subscriptionFailure: "Error while subscribing Push Notification",
  unsubscribeFailure: "Error while unsubscribe Push Notification",
}
function* F({ payload: i }) {
  ;(yield c([s(({ type: o, payload: t }) => o === p.type && t.roomId === i), e(5e3)]), yield a(T()))
}
function* k() {
  const { nativePushModule: i } = yield n(U)
  try {
    const t = yield d(h),
      e = yield d(b),
      r = yield o(e.map((i) => d(m, i))),
      n = e.filter((i, o) => r[o].unread > 0 && i !== t),
      a = yield o(n.map((i) => d(P(i)))),
      c = n.filter((i, o) => a[o] && i)
    i.setBadgeCount(c)
  } catch (i) {
    yield v("uiUpdatePushNotificationBadgeSaga", i)
  }
}
function* B({ payload: i }) {
  try {
    const { nativePushModule: e } = yield n(U)
    i && (yield o([t(e.clearRoomNotifications, i), a(T())]))
  } catch (i) {
    yield v("uiClearRoomPushNotificationSaga", i)
  }
}
function* K({ payload: o }) {
  try {
    const { nativePushModule: e } = yield n(U),
      r = y.toString(i.decode(o), "hex")
    if (!(yield t(e.subscribeToRoomNotifications, r))) throw new Error(C.subscriptionFailure)
  } catch (i) {
    yield v("uiSubscribeToRoomPushNotificationSaga", i)
  }
}
function* z({ payload: i }) {
  const { roomId: o, discoveryKey: e } = i
  try {
    const { nativePushModule: i } = yield n(U)
    ;(yield t(i.unsubscribeRoomNotifications, e), yield a(E(o)))
  } catch (i) {
    yield v("uiUnsubscribeToRoomPushNotificationSaga", i)
  }
}
function* A() {
  const o = f()
  yield w([o.subscribePushNotificationChanges], function* (o) {
    const { type: t, discoveryId: e } = o,
      r = i.normalize(e)
    t === x.JOIN || t === x.UNMUTE
      ? yield a(R(e))
      : (t !== x.LEAVE && t !== x.MUTE) || (yield a(I({ roomId: r, discoveryKey: e })))
  })
}
function* D() {
  for (;;)
    if ((yield s([N, S]), (yield d(b)).length)) {
      yield a(M())
      break
    }
}
export function* uiDevicePushNotificationSetupSaga() {
  ;(yield r(A), yield l(R, K), yield u(I, z), yield r(D))
}
export function* uiPushNotificationSaga() {
  ;(yield u(T, k), yield u(g, F), yield l(E, B))
}