1 import { put, select } from 'redux-saga/effects';
4 createAliasesFromPending,
9 } from '@proton/pass/lib/alias/alias.requests';
10 import { parseItemRevision } from '@proton/pass/lib/items/item.parser';
11 import { aliasSyncEnable, aliasSyncPending, aliasSyncStatus, aliasSyncStatusToggle } from '@proton/pass/store/actions';
12 import { userAccessRequest } from '@proton/pass/store/actions/requests';
13 import { requestInvalidate } from '@proton/pass/store/request/actions';
14 import { createRequestSaga } from '@proton/pass/store/request/sagas';
15 import { selectUserDefaultShareID } from '@proton/pass/store/selectors';
16 import type { AliasPending, ItemRevision, ItemRevisionContentsResponse, Maybe } from '@proton/pass/types';
17 import { logger } from '@proton/pass/utils/logger';
19 /* Will invalidate the `user-access` request as to re-request it
20 * from the event channels soon as possible. */
21 const aliasSyncEnableSaga = createRequestSaga({
22 actions: aliasSyncEnable,
23 call: function* (DefaultShareID, { getAuthStore }) {
24 yield enableAliasSync({ DefaultShareID });
25 yield put(requestInvalidate(userAccessRequest(getAuthStore().getUserID()!)));
27 return DefaultShareID;
31 /** Gets all pending aliases from SimpleLogin and
32 * attempts to create alias items for each of them */
33 const aliasSyncPendingSaga = createRequestSaga({
34 actions: aliasSyncPending,
37 const shareId: Maybe<string> = yield select(selectUserDefaultShareID);
38 if (!shareId) throw new Error('Could not resolve user default vault');
40 const pendingAliases: AliasPending[] = yield getPendingAliases();
41 const encryptedItems: ItemRevisionContentsResponse[] = yield createAliasesFromPending({
46 const items: ItemRevision[] = yield Promise.all(encryptedItems.map(parseItemRevision.bind(null, shareId)));
47 return { items, shareId };
49 logger.warn('[SL::Sync] Failed to create pending aliases', error);
55 const aliasSyncStatusSaga = createRequestSaga({
56 actions: aliasSyncStatus,
57 call: getAliasSyncStatus,
60 const aliasSyncToggleStatusSaga = createRequestSaga({
61 actions: aliasSyncStatusToggle,
62 call: async ({ shareId, itemId, enabled: enable }) => {
63 const encryptedItem = await toggleAliasStatus({ shareId, itemId, enabled: enable });
64 if (!encryptedItem) throw new Error();
66 const item = await parseItemRevision(shareId, encryptedItem);
67 return { shareId, itemId, item };
71 export default [aliasSyncEnableSaga, aliasSyncPendingSaga, aliasSyncStatusSaga, aliasSyncToggleStatusSaga];