[FIX] Channels doesn't load (#1586)
* [FIX] Channels doesn't load * [FIX] Update roomsUpdatedAt when subscriptions.length is 0 * [FIX] Remove unnecessary changes * [FIX] Improve the code Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
parent
8d8b9145f7
commit
862ffa13c6
|
@ -11,7 +11,18 @@ import database from '../lib/database';
|
||||||
import log from '../utils/log';
|
import log from '../utils/log';
|
||||||
import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms';
|
import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms';
|
||||||
import RocketChat from '../lib/rocketchat';
|
import RocketChat from '../lib/rocketchat';
|
||||||
// import buildMessage from '../lib/methods/helpers/buildMessage';
|
|
||||||
|
const updateRooms = function* updateRooms({ server, newRoomsUpdatedAt }) {
|
||||||
|
const serversDB = database.servers;
|
||||||
|
const serversCollection = serversDB.collections.get('servers');
|
||||||
|
const serverRecord = yield serversCollection.find(server);
|
||||||
|
|
||||||
|
return serversDB.action(async() => {
|
||||||
|
await serverRecord.update((record) => {
|
||||||
|
record.roomsUpdatedAt = newRoomsUpdatedAt;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const handleRoomsRequest = function* handleRoomsRequest() {
|
const handleRoomsRequest = function* handleRoomsRequest() {
|
||||||
try {
|
try {
|
||||||
|
@ -26,28 +37,15 @@ const handleRoomsRequest = function* handleRoomsRequest() {
|
||||||
const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult);
|
const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult);
|
||||||
|
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
yield db.action(async() => {
|
const subCollection = db.collections.get('subscriptions');
|
||||||
if (!subscriptions.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const subCollection = db.collections.get('subscriptions');
|
|
||||||
// const messagesCollection = db.collections.get('messages');
|
|
||||||
|
|
||||||
|
if (subscriptions.length) {
|
||||||
const subsIds = subscriptions.map(sub => sub.rid);
|
const subsIds = subscriptions.map(sub => sub.rid);
|
||||||
const existingSubs = await subCollection.query(Q.where('id', Q.oneOf(subsIds))).fetch();
|
const existingSubs = yield subCollection.query(Q.where('id', Q.oneOf(subsIds))).fetch();
|
||||||
const subsToUpdate = existingSubs.filter(i1 => subscriptions.find(i2 => i1._id === i2._id));
|
const subsToUpdate = existingSubs.filter(i1 => subscriptions.find(i2 => i1._id === i2._id));
|
||||||
const subsToCreate = subscriptions.filter(i1 => !existingSubs.find(i2 => i1._id === i2._id));
|
const subsToCreate = subscriptions.filter(i1 => !existingSubs.find(i2 => i1._id === i2._id));
|
||||||
// TODO: subsToDelete?
|
// TODO: subsToDelete?
|
||||||
|
|
||||||
// const lastMessages = subscriptions
|
|
||||||
// .map(sub => sub.lastMessage && buildMessage(sub.lastMessage))
|
|
||||||
// .filter(lm => lm);
|
|
||||||
// const lastMessagesIds = lastMessages.map(lm => lm._id);
|
|
||||||
// const existingMessages = await 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));
|
|
||||||
|
|
||||||
const allRecords = [
|
const allRecords = [
|
||||||
...subsToCreate.map(subscription => subCollection.prepareCreate((s) => {
|
...subsToCreate.map(subscription => subCollection.prepareCreate((s) => {
|
||||||
s._raw = sanitizedRaw({ id: subscription.rid }, subCollection.schema);
|
s._raw = sanitizedRaw({ id: subscription.rid }, subCollection.schema);
|
||||||
|
@ -59,37 +57,14 @@ const handleRoomsRequest = function* handleRoomsRequest() {
|
||||||
Object.assign(subscription, newSub);
|
Object.assign(subscription, newSub);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
// ...messagesToCreate.map(message => messagesCollection.prepareCreate((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(() => {
|
|
||||||
// Object.assign(message, newMessage);
|
|
||||||
// });
|
|
||||||
// })
|
|
||||||
];
|
];
|
||||||
|
|
||||||
try {
|
yield db.action(async() => {
|
||||||
await db.batch(...allRecords);
|
await db.batch(...allRecords);
|
||||||
} catch (e) {
|
});
|
||||||
log(e);
|
}
|
||||||
}
|
|
||||||
return allRecords.length;
|
|
||||||
});
|
|
||||||
|
|
||||||
yield serversDB.action(async() => {
|
|
||||||
try {
|
|
||||||
await serverRecord.update((record) => {
|
|
||||||
record.roomsUpdatedAt = newRoomsUpdatedAt;
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
log(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
yield updateRooms({ server, newRoomsUpdatedAt });
|
||||||
yield put(roomsSuccess());
|
yield put(roomsSuccess());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
yield put(roomsFailure(e));
|
yield put(roomsFailure(e));
|
||||||
|
|
Loading…
Reference in New Issue