Rocket.Chat.ReactNative/app/sagas/state.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-04-14 12:25:17 +00:00
import { takeLatest, select, put } from 'redux-saga/effects';
2020-04-15 16:57:21 +00:00
import { FOREGROUND, BACKGROUND } from '../lib/appStateMiddleware';
import RocketChat from '../lib/rocketchat';
import { setBadgeCount } from '../notifications/push';
2019-05-28 16:18:46 +00:00
import log from '../utils/log';
2020-04-14 14:07:51 +00:00
import { localAuthenticate, saveLastLocalAuthenticationSession } from '../utils/localAuthentication';
2020-04-14 12:25:17 +00:00
import * as actions from '../actions';
const appHasComeBackToForeground = function* appHasComeBackToForeground() {
const appRoot = yield select(state => state.app.root);
if (appRoot === 'outside') {
return;
}
const auth = yield select(state => state.login.isAuthenticated);
if (!auth) {
return;
}
try {
2020-04-14 13:07:13 +00:00
const server = yield select(state => state.server.server);
const localAuthResult = yield localAuthenticate(server);
if (!localAuthResult) {
2020-04-14 12:25:17 +00:00
yield put(actions.appStart('locked'));
}
setBadgeCount();
return yield RocketChat.setUserPresenceOnline();
} catch (e) {
log(e);
}
};
const appHasComeBackToBackground = function* appHasComeBackToBackground() {
const appRoot = yield select(state => state.app.root);
if (appRoot === 'outside') {
return;
}
const auth = yield select(state => state.login.isAuthenticated);
if (!auth) {
return;
}
try {
2020-04-14 14:07:51 +00:00
yield RocketChat.setUserPresenceAway();
const server = yield select(state => state.server.server);
yield saveLastLocalAuthenticationSession(server);
} catch (e) {
log(e);
}
};
const root = function* root() {
yield takeLatest(
FOREGROUND,
appHasComeBackToForeground
);
yield takeLatest(
BACKGROUND,
appHasComeBackToBackground
);
2018-12-05 20:52:08 +00:00
// yield takeLatest(
// INACTIVE,
// appHasComeBackToBackground
// );
};
export default root;