import {
call as e,
select as o,
put as i,
takeEvery as a,
all as t,
take as l,
delay as n,
takeLatest as p,
getContext as r,
} from "redux-saga/effects"
import { safeCall as d, createSpawnRestarted as s } from "../lib"
import { getRecentRoomsApi as y, getVersionApi as c } from "../../api/core"
import {
PREFERENCES as m,
preferencesInitData as f,
getPreferencesLocale as I,
getPreferencesLastRoomId as g,
getPreferencesNotificationsType as u,
getPreferencesNotificationsAlerts as S,
preferenceUpdateTask as R,
getPreferencesDisclaimerComplete as T,
} from "../preferences"
import { LAYOUT_TYPE as x, setUILayoutType as A } from "../ui-desktop"
import { APP_CONTEXT_KEY as E, PLATFORM_CONTEXT_KEY as N } from "../constants"
import {
roomInvitationHandler as C,
activeRoomSaga as L,
EMPTY_ROOM_ID as k,
setRoomPairingActive as D,
getRoomPairingIdActive as _,
roomDmReplyHandler as h,
getRoomInvitePreviewsByInvitationLink as O,
getRoomItemById as P,
calculateAndUpdateUnreadRoomsCount as M,
setDmContact as v,
getWaitingDmMemberId as w,
switchRoomSubmit as U,
setWaitingDmMemberId as V,
} from "../room"
import {
chatAddMessageHandler as b,
chatUpdateMessageHandler as Y,
getChatMessageAnchorId as G,
chatMessageJumpToCmd as F,
} from "../chat"
import { taskErrorSaga as H } from "../error"
import { APP_STATUS as j } from "./app.constants"
import {
getAppState as q,
setAppCurrentRoomId as B,
getAppCurrentRoomId as J,
initApplication as K,
initAppLocale as z,
setAppNotificationsType as Q,
setAppNotificationsAlerts as W,
getAppLocale as X,
setAppLocaleSuccess as Z,
setAppInitialStatus as $,
setAppStatus as ee,
appRoomInvitationLinkServed as oe,
} from "./app.reducer"
import {
loadIdentitySync as ie,
getIsIdentityNameSet as ae,
getIsIdentityConfirmed as te,
getIsIdentityLoggedOut as le,
} from "../identity"
import { uploadsInitData as ne } from "../uploads"
import { resetModuleStateCmd as pe } from "../lib/rootAction"
export function* appStateCleanup() {
yield i(pe())
}
export function* appGetNextRoomId({ topOnly: e } = {}) {
const i = yield o(g),
a = yield o(J)
if (i && !e) return i
const { rooms: t } = yield* y({ limit: 1 }),
l = t?.[0]?.roomId
return l && l !== a ? l : k
}
const re = 500
export function* appInitialStartupHandler() {
const { config: a, applicationKey: l } = yield r(N)
yield i(K({ applicationKey: l, linkData: a.linkData }))
let p = j.RUNNING
const [d, s] = yield t([e(c), e(ie), e(f), e(ne), n(re)])
if (!s) return void (yield i($({ status: j.CRITICAL_ERROR, appVersion: d })))
const [y, m, g] = yield t([o(te), o(ae), o(le)]),
R = yield o(T)
;((m && !g) || (p = j.IDENTITY_SETUP),
g
? yield i(A(R ? x.IDENTITY_SETUP : x.SETUP_DISCLAIMER))
: m || y
? !m && y && (yield i(A(x.SETUP_IDENTITY_DISPLAY_NAME)))
: yield i(A(R ? x.IDENTITY_SETUP : x.SETUP_DISCLAIMER)))
const { localeManager: C } = yield r(E),
L = yield o(I),
k = yield o(S),
D = yield o(u),
_ = L || C.clientLocale || "en"
;(yield C.setLocale(_),
yield i(z(_)),
yield i(W(k)),
yield i(Q(D)),
yield i($({ status: p, appVersion: d })))
}
export function* appRoomJoinSaga(e) {
const { link: o, setActive: i, skipParse: a } = e.payload
;(yield* C({ link: o, setActive: i, skipParse: a })) ||
(yield H("appRoomJoinSaga", "unable to join room without link"))
}
export function* appDmResponseSaga(e) {
const { replyType: o, requestId: i } = e.payload
;(yield* h({ replyType: o, requestId: i })) ||
(yield H("appDmResponseSaga", "invalid dm reply result"))
}
export function* appRoomSwitchSaga(a) {
const { roomId: t, messageId: l, answeringCallRing: n, resumingCall: p } = a.payload,
r = yield o(J)
if (t)
if (t !== r)
(yield i(B({ roomId: t, messageId: l, answeringCallRing: n, resumingCall: p })),
yield e(R, m.LAST_ROOM_ID, t))
else {
if (l) {
if ((yield o(G)) === l) return
yield i(F({ messageId: l }))
}
n && (yield i(B({ roomId: t, answeringCallRing: n })))
}
}
export function* appChatAddMessageSaga(e) {
try {
const {
text: i,
files: a,
display: t,
replyTo: l,
linkPreviews: n,
locationShared: p,
} = e.payload,
r = yield o(J),
d = yield o(O),
s = { ...n, ...d }
if (r) {
const e = yield o(P(r))
yield b({
roomId: r,
roomVersion: e?.version,
text: i,
files: a,
display: t,
replyTo: l,
previewByLink: s,
locationShared: p,
})
}
} catch (e) {
yield H("appChatAddMessageSaga", e)
}
}
export function* appChatUpdateMessageSaga(e) {
try {
const { messageId: i, text: a, display: t, linkPreviews: l } = e.payload,
n = yield o(J),
p = yield o(O),
r = { ...l, ...p }
if (n) {
const e = yield o(P(n))
yield Y({
roomId: n,
roomVersion: e.version,
messageId: i,
text: a,
display: t,
previewByLink: r,
})
}
} catch (e) {
yield H("appChatUpdateMessageSaga", e)
}
}
export function* setAppNotificationsTypeSaga({ payload: o }) {
;(yield e(R, m.NOTIFICATIONS_TYPE, o), yield e(M))
}
export function* appRoomNotificationsSaga(o) {
const i = Object.values(o.payload)
i.length > 0 && !1 === i[0].notifications && (yield e(M))
}
export function* toggleAppNotificationsAlertSaga({ payload: i }) {
const a = yield o(S),
t = a.includes(i) ? a.filter((e) => e !== i) : [...a, i]
yield e(R, m.NOTIFICATIONS_ALERTS, t)
}
export function* appLocaleSaga() {
try {
const i = yield o(X),
{ localeManager: a } = yield r(E)
;(yield a.setLocale(i), yield e(R, m.LOCALE, i))
} catch (e) {
yield H("appLocaleSaga", e)
} finally {
yield i(Z())
}
}
export function* currentRoomSaga({ payload: e }) {
const { messageId: i } = e,
{ currentRoomId: a, appVersion: t } = yield o(q),
l = yield d(L, { roomId: a, messageId: i, appVersion: t })
l && (yield H("currentRoomSaga", l))
}
export function* appRoomListHandlerSaga() {
yield a(D, function* () {
if (yield o(_)) yield i(B({ roomId: k }))
else {
const e = yield appGetNextRoomId()
yield i(B({ roomId: e }))
}
})
}
export function* appRoomCloseSaga({ payload: o }) {
const { openNextRoom: a } = o || {}
let t = k
;(a && (t = yield appGetNextRoomId({ topOnly: !0 })),
yield i(B({ roomId: t })),
yield i(V({ waitingDmMemberId: k })),
yield e(R, m.LAST_ROOM_ID, t))
}
export function* waitingDmRoomSaga() {
yield p(v, function* ({ payload: { roomId: e, memberId: a } }) {
const t = yield o(w)
e && a && t && a === t && (yield i(U({ roomId: e })))
})
}
export function* appInitialLinkHandler() {
const { linkData: e } = yield o(q)
e && (yield i(oe()), yield C({ link: e, skipParse: !0 }))
}
const de = s({ onError: H })
export function* appRootFunctionalitySaga(e) {
let i = [],
a = ""
for (;;) {
yield l([$, ee])
const { appStatus: n, appVersion: p } = yield o(q)
a !== n &&
((a = n),
i.forEach((e) => {
e.isRunning() && e.cancel()
}),
(i = yield t(e[n].map((e) => de(e, { appVersion: p })))))
}
}