2017-12-04 18:24:21 +00:00
|
|
|
import { takeLatest, select } 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';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { ROOT_OUTSIDE } from '../actions/app';
|
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);
|
2020-06-15 14:00:46 +00:00
|
|
|
if (appRoot === ROOT_OUTSIDE) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 18:24:21 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
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 localAuthenticate(server);
|
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);
|
2020-06-15 14:00:46 +00:00
|
|
|
if (appRoot === ROOT_OUTSIDE) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 18:24:21 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-09 20:19:54 +00:00
|
|
|
const localAuthenticated = yield select(state => state.login.isLocalAuthenticated);
|
|
|
|
if (!localAuthenticated) {
|
|
|
|
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;
|