Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/sagas/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,40 @@ describe('init saga — restore user-facing roots', () => {
expect(store.getState().app.ready).toBe(true);
expect(store.getState().server.server).toBe(HOST);
});

it('lands on ROOT_OUTSIDE when resolving the stored server throws', async () => {
jest.mocked(getServerById).mockRejectedValue(new Error('database unavailable'));
const { store } = setupStore();

store.dispatch(appInit());
await flushSagaMicrotasks();

expect(store.getState().app.root).toBe(RootEnum.ROOT_OUTSIDE);
expect(store.getState().app.ready).toBe(true);
});

it('stays on the restored server when reading the pending push notification fails', async () => {
jest.mocked(getServerById).mockResolvedValue({ id: HOST, version: '6.0.0' } as any);
jest.mocked(AsyncStorage.getItem).mockRejectedValue(new Error('storage unavailable') as any);
const { store } = setupStore();

store.dispatch(appInit());
await flushSagaMicrotasks();

expect(store.getState().app.root).not.toBe(RootEnum.ROOT_OUTSIDE);
expect(store.getState().server.server).toBe(HOST);
});

it('stays on the restored server when clearing the pending push notification fails', async () => {
jest.mocked(getServerById).mockResolvedValue({ id: HOST, version: '6.0.0' } as any);
jest.mocked(AsyncStorage.getItem).mockResolvedValue(JSON.stringify({ rid: 'room-1' }) as any);
jest.mocked(AsyncStorage.removeItem).mockRejectedValue(new Error('storage unavailable') as any);
const { store } = setupStore();

store.dispatch(appInit());
await flushSagaMicrotasks();

expect(store.getState().app.root).not.toBe(RootEnum.ROOT_OUTSIDE);
expect(store.getState().server.server).toBe(HOST);
});
});
66 changes: 38 additions & 28 deletions app/sagas/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,61 @@ import { getSortPreferences } from '../lib/methods/userPreferencesMethods';
import { deepLinkingClickCallPush } from '../actions/deepLinking';
import { getServerById } from '../lib/database/services/Server';

const PUSH_NOTIFICATION_KEY = 'pushNotification';

export const initLocalSettings = function* initLocalSettings() {
const sortPreferences = getSortPreferences();
yield put(setAllPreferences(sortPreferences));
};

const serverToRestore = function* serverToRestore(server) {
if (!server) {
return null;
}
const serverToRestore = function* serverToRestore() {
try {
const server = UserPreferences.getString(CURRENT_SERVER);
if (!server) {
return null;
}

if (!hasStoredLoginToken(server)) {
return (yield* findLoggedInServer()) || null;
}
if (!hasStoredLoginToken(server)) {
return (yield* findLoggedInServer()) || null;
}

yield localAuthenticate(server);
return (yield getServerById(server)) || null;
yield localAuthenticate(server);
return (yield getServerById(server)) || null;
} catch (e) {
log(e);
return null;
}
};

const restore = function* restore() {
const deliverPendingPushNotification = function* deliverPendingPushNotification(restoredServer) {
try {
const server = UserPreferences.getString(CURRENT_SERVER);
const restoredServer = yield* serverToRestore(server);

if (restoredServer) {
yield put(selectServerRequest(restoredServer.id, restoredServer.version));
} else {
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
const pushNotification = yield call(AsyncStorage.getItem, PUSH_NOTIFICATION_KEY);
if (!pushNotification) {
return;
}

yield put(appReady({}));
const pushNotification = yield call(AsyncStorage.getItem, 'pushNotification');
if (pushNotification) {
yield call(AsyncStorage.removeItem, 'pushNotification');
if (restoredServer) {
try {
yield put(deepLinkingClickCallPush(JSON.parse(pushNotification)));
} catch (e) {
log(e);
}
}
yield call(AsyncStorage.removeItem, PUSH_NOTIFICATION_KEY);

if (restoredServer) {
yield put(deepLinkingClickCallPush(JSON.parse(pushNotification)));
}
} catch (e) {
log(e);
}
};

const restore = function* restore() {
const restoredServer = yield* serverToRestore();

if (restoredServer) {
yield put(selectServerRequest(restoredServer.id, restoredServer.version));
} else {
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
}

yield put(appReady({}));

yield* deliverPendingPushNotification(restoredServer);
};

const start = function* start() {
Expand Down
Loading