2021-09-13 20:41:05 +00:00
|
|
|
import { select, takeLatest } from 'redux-saga/effects';
|
2017-11-28 11:01:18 +00:00
|
|
|
|
2017-12-04 18:24:21 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2019-06-10 16:23:19 +00:00
|
|
|
import { setBadgeCount } from '../notifications/push';
|
2019-05-28 16:18:46 +00:00
|
|
|
import log from '../utils/log';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { localAuthenticate, saveLastLocalAuthenticationSession } from '../utils/localAuthentication';
|
|
|
|
import { APP_STATE } from '../actions/actionsTypes';
|
2022-01-07 20:40:48 +00:00
|
|
|
import { RootEnum } from '../definitions';
|
2017-12-04 18:24:21 +00:00
|
|
|
|
2017-11-28 11:01:18 +00:00
|
|
|
const appHasComeBackToForeground = function* appHasComeBackToForeground() {
|
2018-08-01 19:35:06 +00:00
|
|
|
const appRoot = yield select(state => state.app.root);
|
2022-01-07 20:40:48 +00:00
|
|
|
if (appRoot === RootEnum.ROOT_OUTSIDE) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-05 16:10:21 +00:00
|
|
|
const login = yield select(state => state.login);
|
|
|
|
const server = yield select(state => state.server);
|
|
|
|
if (!login.isAuthenticated || login.isFetching || server.connecting || server.loading || server.changingServer) {
|
2017-12-04 18:24:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2021-03-05 16:10:21 +00:00
|
|
|
yield localAuthenticate(server.server);
|
|
|
|
RocketChat.checkAndReopen();
|
2018-10-31 18:40:39 +00:00
|
|
|
setBadgeCount();
|
2018-05-18 17:55:08 +00:00
|
|
|
return yield RocketChat.setUserPresenceOnline();
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-12-04 18:24:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const appHasComeBackToBackground = function* appHasComeBackToBackground() {
|
2018-08-01 19:35:06 +00:00
|
|
|
const appRoot = yield select(state => state.app.root);
|
2022-01-07 20:40:48 +00:00
|
|
|
if (appRoot === RootEnum.ROOT_OUTSIDE) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2020-05-08 17:04:37 +00:00
|
|
|
const server = yield select(state => state.server.server);
|
|
|
|
yield saveLastLocalAuthenticationSession(server);
|
|
|
|
|
|
|
|
yield RocketChat.setUserPresenceAway();
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2020-05-08 17:04:37 +00:00
|
|
|
yield takeLatest(APP_STATE.FOREGROUND, appHasComeBackToForeground);
|
|
|
|
yield takeLatest(APP_STATE.BACKGROUND, appHasComeBackToBackground);
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default root;
|