[IMPROVEMENT] Save last message as message when subscription is updated (#1344)

This commit is contained in:
Diego Mello 2019-10-30 15:31:26 -03:00 committed by GitHub
parent d03699622a
commit 2b82ec4ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import random from '../../../utils/random';
import store from '../../createStore'; import store from '../../createStore';
import { roomsRequest } from '../../../actions/rooms'; import { roomsRequest } from '../../../actions/rooms';
import { notificationReceived } from '../../../actions/notification'; import { notificationReceived } from '../../../actions/notification';
import buildMessage from '../helpers/buildMessage';
const removeListener = listener => listener.stop(); const removeListener = listener => listener.stop();
@ -103,19 +104,53 @@ const createOrUpdateSubscription = async(subscription, room) => {
// Do nothing // Do nothing
} }
const batch = [];
if (sub) { if (sub) {
await sub.update(protectedFunction((s) => { batch.push(
Object.assign(s, tmp); sub.prepareUpdate(protectedFunction((s) => {
})); Object.assign(s, tmp);
}))
);
} else { } else {
await subCollection.create(protectedFunction((s) => { batch.push(
s._raw = sanitizedRaw({ id: tmp.rid }, subCollection.schema); subCollection.prepareCreate(protectedFunction((s) => {
Object.assign(s, tmp); s._raw = sanitizedRaw({ id: tmp.rid }, subCollection.schema);
if (s.roomUpdatedAt) { Object.assign(s, tmp);
s.roomUpdatedAt = new Date(); if (s.roomUpdatedAt) {
} s.roomUpdatedAt = new Date();
})); }
}))
);
} }
if (sub.lastMessage) {
const lastMessage = buildMessage(sub.lastMessage);
const messagesCollection = db.collections.get('messages');
let messageRecord;
try {
messageRecord = await messagesCollection.find(lastMessage._id);
} catch (error) {
// Do nothing
}
if (messageRecord) {
batch.push(
messageRecord.prepareUpdate(() => {
Object.assign(messageRecord, lastMessage);
})
);
} else {
batch.push(
messagesCollection.prepareCreate((m) => {
m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema);
m.subscription.id = lastMessage.rid;
return Object.assign(m, lastMessage);
})
);
}
}
await db.batch(...batch);
}); });
} catch (e) { } catch (e) {
log(e); log(e);

View File

@ -11,6 +11,7 @@ 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 handleRoomsRequest = function* handleRoomsRequest() { const handleRoomsRequest = function* handleRoomsRequest() {
try { try {
@ -26,17 +27,27 @@ const handleRoomsRequest = function* handleRoomsRequest() {
const db = database.active; const db = database.active;
yield db.action(async() => { yield db.action(async() => {
const subCollection = db.collections.get('subscriptions');
if (!subscriptions.length) { if (!subscriptions.length) {
return; return;
} }
const subCollection = db.collections.get('subscriptions');
const messagesCollection = db.collections.get('messages');
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 = await 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);
@ -47,6 +58,17 @@ const handleRoomsRequest = function* handleRoomsRequest() {
return subscription.prepareUpdate(() => { return subscription.prepareUpdate(() => {
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);
});
}) })
]; ];