2019-06-18 20:12:33 +00:00
|
|
|
import { delay } from 'redux-saga';
|
2019-02-27 20:29:37 +00:00
|
|
|
import {
|
2019-06-17 13:57:07 +00:00
|
|
|
put, select, race, take, fork, cancel, takeLatest
|
2019-02-27 20:29:37 +00:00
|
|
|
} from 'redux-saga/effects';
|
2019-06-18 20:12:33 +00:00
|
|
|
import { BACKGROUND, INACTIVE } from 'redux-enhancer-react-native-appstate';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2017-08-17 02:06:22 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2019-02-07 16:13:21 +00:00
|
|
|
import { roomsSuccess, roomsFailure } from '../actions/rooms';
|
2018-01-15 18:44:20 +00:00
|
|
|
import database from '../lib/realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2019-02-07 16:13:21 +00:00
|
|
|
import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms';
|
2019-02-27 20:29:37 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
let roomsSub;
|
|
|
|
|
|
|
|
const removeSub = function removeSub() {
|
|
|
|
if (roomsSub && roomsSub.stop) {
|
|
|
|
roomsSub.stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-07 16:13:21 +00:00
|
|
|
const handleRoomsRequest = function* handleRoomsRequest() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-06-17 13:57:07 +00:00
|
|
|
removeSub();
|
|
|
|
roomsSub = yield RocketChat.subscribeRooms();
|
2019-02-27 20:29:37 +00:00
|
|
|
const newRoomsUpdatedAt = new Date();
|
|
|
|
const server = yield select(state => state.server.server);
|
|
|
|
const [serverRecord] = database.databases.serversDB.objects('servers').filtered('id = $0', server);
|
|
|
|
const { roomsUpdatedAt } = serverRecord;
|
|
|
|
const [subscriptionsResult, roomsResult] = yield RocketChat.getRooms(roomsUpdatedAt);
|
2019-02-07 16:13:21 +00:00
|
|
|
const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult);
|
2018-09-19 14:18:32 +00:00
|
|
|
|
2019-02-07 16:13:21 +00:00
|
|
|
database.write(() => {
|
2019-05-20 20:43:50 +00:00
|
|
|
subscriptions.forEach((subscription) => {
|
|
|
|
try {
|
|
|
|
database.create('subscriptions', subscription, true);
|
|
|
|
} catch (error) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_rooms_request_create_sub', error);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-18 17:55:08 +00:00
|
|
|
});
|
2019-02-27 20:29:37 +00:00
|
|
|
database.databases.serversDB.write(() => {
|
|
|
|
try {
|
|
|
|
database.databases.serversDB.create('servers', { id: server, roomsUpdatedAt: newRoomsUpdatedAt }, true);
|
|
|
|
} catch (e) {
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_rooms_request_update', e);
|
2019-02-27 20:29:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-07 16:13:21 +00:00
|
|
|
yield put(roomsSuccess());
|
2018-03-29 17:55:37 +00:00
|
|
|
} catch (e) {
|
2019-02-07 16:13:21 +00:00
|
|
|
yield put(roomsFailure(e));
|
2019-05-28 16:18:46 +00:00
|
|
|
log('err_rooms_request', e);
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-17 13:57:07 +00:00
|
|
|
const handleLogout = function handleLogout() {
|
|
|
|
removeSub();
|
|
|
|
};
|
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const root = function* root() {
|
2019-06-17 13:57:07 +00:00
|
|
|
yield takeLatest(types.LOGOUT, handleLogout);
|
|
|
|
while (true) {
|
|
|
|
const params = yield take(types.ROOMS.REQUEST);
|
2019-06-18 20:12:33 +00:00
|
|
|
const isAuthenticated = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (isAuthenticated) {
|
|
|
|
const roomsRequestTask = yield fork(handleRoomsRequest, params);
|
|
|
|
yield race({
|
|
|
|
roomsSuccess: take(types.ROOMS.SUCCESS),
|
|
|
|
roomsFailure: take(types.ROOMS.FAILURE),
|
|
|
|
serverReq: take(types.SERVER.SELECT_REQUEST),
|
|
|
|
background: take(BACKGROUND),
|
|
|
|
inactive: take(INACTIVE),
|
|
|
|
logout: take(types.LOGOUT),
|
|
|
|
timeout: delay(30000)
|
|
|
|
});
|
|
|
|
yield cancel(roomsRequestTask);
|
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
}
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|