2019-02-27 20:29:37 +00:00
|
|
|
import {
|
2019-08-27 17:41:07 +00:00
|
|
|
put, select, race, take, fork, cancel, delay
|
2019-02-27 20:29:37 +00:00
|
|
|
} from 'redux-saga/effects';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2017-08-17 02:06:22 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2020-02-11 20:13:44 +00:00
|
|
|
import { roomsSuccess, roomsFailure, roomsRefresh } from '../actions/rooms';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
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';
|
2020-02-05 15:22:29 +00:00
|
|
|
import buildMessage from '../lib/methods/helpers/buildMessage';
|
|
|
|
import protectedFunction from '../lib/methods/helpers/protectedFunction';
|
2020-01-28 13:19:17 +00:00
|
|
|
|
|
|
|
const updateRooms = function* updateRooms({ server, newRoomsUpdatedAt }) {
|
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2020-09-23 17:16:04 +00:00
|
|
|
try {
|
|
|
|
const serverRecord = yield serversCollection.find(server);
|
2020-01-28 13:19:17 +00:00
|
|
|
|
2020-09-23 17:16:04 +00:00
|
|
|
return serversDB.action(async() => {
|
|
|
|
await serverRecord.update((record) => {
|
|
|
|
record.roomsUpdatedAt = newRoomsUpdatedAt;
|
|
|
|
});
|
2020-01-28 13:19:17 +00:00
|
|
|
});
|
2020-09-23 17:16:04 +00:00
|
|
|
} catch {
|
|
|
|
// Server not found
|
|
|
|
}
|
2020-01-28 13:19:17 +00:00
|
|
|
};
|
2018-03-23 16:49:51 +00:00
|
|
|
|
2020-02-11 20:13:44 +00:00
|
|
|
const handleRoomsRequest = function* handleRoomsRequest({ params }) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2020-02-11 20:13:44 +00:00
|
|
|
RocketChat.subscribeRooms();
|
2019-02-27 20:29:37 +00:00
|
|
|
const newRoomsUpdatedAt = new Date();
|
2020-02-11 20:13:44 +00:00
|
|
|
let roomsUpdatedAt;
|
2019-02-27 20:29:37 +00:00
|
|
|
const server = yield select(state => state.server.server);
|
2020-02-11 20:13:44 +00:00
|
|
|
if (params.allData) {
|
|
|
|
yield put(roomsRefresh());
|
|
|
|
} else {
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2020-09-23 17:16:04 +00:00
|
|
|
try {
|
|
|
|
const serverRecord = yield serversCollection.find(server);
|
|
|
|
({ roomsUpdatedAt } = serverRecord);
|
|
|
|
} catch {
|
|
|
|
// Server not found
|
|
|
|
}
|
2020-02-11 20:13:44 +00:00
|
|
|
}
|
2019-02-27 20:29:37 +00:00
|
|
|
const [subscriptionsResult, roomsResult] = yield RocketChat.getRooms(roomsUpdatedAt);
|
2020-04-06 20:23:13 +00:00
|
|
|
const { subscriptions } = yield mergeSubscriptionsRooms(subscriptionsResult, roomsResult);
|
2018-09-19 14:18:32 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
|
|
|
const messagesCollection = db.get('messages');
|
2019-10-30 18:31:26 +00:00
|
|
|
|
2020-05-08 17:36:55 +00:00
|
|
|
const subsIds = subscriptions.map(sub => sub.rid).concat(roomsResult.remove.map(room => room._id));
|
|
|
|
if (subsIds.length) {
|
2020-01-28 13:19:17 +00:00
|
|
|
const existingSubs = yield subCollection.query(Q.where('id', Q.oneOf(subsIds))).fetch();
|
2019-09-16 20:26:32 +00:00
|
|
|
const subsToUpdate = existingSubs.filter(i1 => subscriptions.find(i2 => i1._id === i2._id));
|
|
|
|
const subsToCreate = subscriptions.filter(i1 => !existingSubs.find(i2 => i1._id === i2._id));
|
2020-05-08 17:36:55 +00:00
|
|
|
const subsToDelete = existingSubs.filter(i1 => !subscriptions.find(i2 => i1._id === i2._id));
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-01-13 14:16:00 +00:00
|
|
|
const openedRooms = yield select(state => state.room.rooms);
|
2020-02-05 15:22:29 +00:00
|
|
|
const lastMessages = subscriptions
|
2021-01-13 14:16:00 +00:00
|
|
|
/** Checks for opened rooms and filter them out.
|
|
|
|
* It prevents this process to try persisting the same last message on the room messages fetch.
|
|
|
|
* This race condition is easy to reproduce on push notification tap.
|
|
|
|
*/
|
|
|
|
.filter(sub => !openedRooms.includes(sub.rid))
|
2020-02-05 15:22:29 +00:00
|
|
|
.map(sub => sub.lastMessage && buildMessage(sub.lastMessage))
|
|
|
|
.filter(lm => lm);
|
2021-01-13 14:16:00 +00:00
|
|
|
const lastMessagesIds = lastMessages.map(lm => lm._id).filter(lm => lm);
|
2020-02-05 15:22:29 +00:00
|
|
|
const existingMessages = yield messagesCollection.query(Q.where('id', Q.oneOf(lastMessagesIds))).fetch();
|
|
|
|
const messagesToUpdate = existingMessages.filter(i1 => lastMessages.find(i2 => i1.id === i2._id));
|
|
|
|
const messagesToCreate = lastMessages.filter(i1 => !existingMessages.find(i2 => i1._id === i2.id));
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const allRecords = [
|
|
|
|
...subsToCreate.map(subscription => subCollection.prepareCreate((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: subscription.rid }, subCollection.schema);
|
|
|
|
return Object.assign(s, subscription);
|
|
|
|
})),
|
|
|
|
...subsToUpdate.map((subscription) => {
|
|
|
|
const newSub = subscriptions.find(s => s._id === subscription._id);
|
|
|
|
return subscription.prepareUpdate(() => {
|
2020-05-08 12:57:04 +00:00
|
|
|
if (newSub.announcement) {
|
|
|
|
if (newSub.announcement !== subscription.announcement) {
|
|
|
|
subscription.bannerClosed = false;
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
Object.assign(subscription, newSub);
|
|
|
|
});
|
2020-02-05 15:22:29 +00:00
|
|
|
}),
|
2020-05-08 17:36:55 +00:00
|
|
|
...subsToDelete.map(subscription => subscription.prepareDestroyPermanently()),
|
2020-02-05 15:22:29 +00:00
|
|
|
...messagesToCreate.map(message => messagesCollection.prepareCreate(protectedFunction((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: message._id }, messagesCollection.schema);
|
|
|
|
m.subscription.id = message.rid;
|
|
|
|
return Object.assign(m, message);
|
|
|
|
}))),
|
|
|
|
...messagesToUpdate.map((message) => {
|
|
|
|
const newMessage = lastMessages.find(m => m._id === message.id);
|
|
|
|
return message.prepareUpdate(protectedFunction(() => {
|
|
|
|
Object.assign(message, newMessage);
|
|
|
|
}));
|
2019-09-16 20:26:32 +00:00
|
|
|
})
|
|
|
|
];
|
|
|
|
|
2020-01-28 13:19:17 +00:00
|
|
|
yield db.action(async() => {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.batch(...allRecords);
|
2020-01-28 13:19:17 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-27 20:29:37 +00:00
|
|
|
|
2020-01-28 13:19:17 +00:00
|
|
|
yield updateRooms({ server, newRoomsUpdatedAt });
|
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-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const root = function* root() {
|
2019-06-17 13:57:07 +00:00
|
|
|
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),
|
2020-05-08 17:04:37 +00:00
|
|
|
background: take(types.APP_STATE.BACKGROUND),
|
2019-06-18 20:12:33 +00:00
|
|
|
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;
|