2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
|
|
|
|
import buildMessage from './helpers/buildMessage';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import database from '../database';
|
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../encryption';
|
2021-05-26 17:24:54 +00:00
|
|
|
import { MESSAGE_TYPE_ANY_LOAD } from '../../constants/messageTypeLoad';
|
|
|
|
import { generateLoadMoreId } from '../utils';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
export default function updateMessages({
|
|
|
|
rid, update = [], remove = [], loaderItem
|
|
|
|
}) {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
if (!((update && update.length) || (remove && remove.length))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const db = database.active;
|
|
|
|
return db.action(async() => {
|
2020-09-11 14:31:38 +00:00
|
|
|
// Decrypt these messages
|
|
|
|
update = await Encryption.decryptMessages(update);
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2019-09-16 20:26:32 +00:00
|
|
|
let sub;
|
|
|
|
try {
|
|
|
|
sub = await subCollection.find(rid);
|
|
|
|
} catch (error) {
|
|
|
|
sub = { id: rid };
|
|
|
|
console.log('updateMessages: subscription not found');
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:59:14 +00:00
|
|
|
const messagesIds = [...update.map(m => m._id), ...remove.map(m => m._id)];
|
2021-02-26 16:25:51 +00:00
|
|
|
const msgCollection = db.get('messages');
|
|
|
|
const threadCollection = db.get('threads');
|
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2019-09-16 20:26:32 +00:00
|
|
|
const allMessagesRecords = await msgCollection
|
2021-05-26 17:24:54 +00:00
|
|
|
.query(
|
|
|
|
Q.where('rid', rid),
|
|
|
|
Q.or(
|
|
|
|
Q.where('id', Q.oneOf(messagesIds)),
|
|
|
|
Q.where('t', Q.oneOf(MESSAGE_TYPE_ANY_LOAD))
|
|
|
|
)
|
|
|
|
)
|
2019-09-16 20:26:32 +00:00
|
|
|
.fetch();
|
|
|
|
const allThreadsRecords = await threadCollection
|
|
|
|
.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
|
|
|
|
.fetch();
|
|
|
|
const allThreadMessagesRecords = await threadMessagesCollection
|
|
|
|
.query(Q.where('subscription_id', rid), Q.where('id', Q.oneOf(messagesIds)))
|
|
|
|
.fetch();
|
|
|
|
|
|
|
|
update = update.map(m => buildMessage(m));
|
|
|
|
|
|
|
|
// filter messages
|
|
|
|
let msgsToCreate = update.filter(i1 => !allMessagesRecords.find(i2 => i1._id === i2.id));
|
|
|
|
let msgsToUpdate = allMessagesRecords.filter(i1 => update.find(i2 => i1.id === i2._id));
|
|
|
|
|
|
|
|
// filter threads
|
|
|
|
const allThreads = update.filter(m => m.tlm);
|
|
|
|
let threadsToCreate = allThreads.filter(i1 => !allThreadsRecords.find(i2 => i1._id === i2.id));
|
|
|
|
let threadsToUpdate = allThreadsRecords.filter(i1 => allThreads.find(i2 => i1.id === i2._id));
|
|
|
|
|
|
|
|
// filter thread messages
|
|
|
|
const allThreadMessages = update.filter(m => m.tmid);
|
|
|
|
let threadMessagesToCreate = allThreadMessages.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id));
|
|
|
|
let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => allThreadMessages.find(i2 => i1.id === i2._id));
|
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
// filter loaders to delete
|
|
|
|
let loadersToDelete = allMessagesRecords.filter(i1 => update.find(i2 => i1.id === generateLoadMoreId(i2._id)));
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// Create
|
|
|
|
msgsToCreate = msgsToCreate.map(message => msgCollection.prepareCreate(protectedFunction((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
|
|
|
|
m.subscription.id = sub.id;
|
|
|
|
Object.assign(m, message);
|
|
|
|
})));
|
|
|
|
threadsToCreate = threadsToCreate.map(thread => threadCollection.prepareCreate(protectedFunction((t) => {
|
|
|
|
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
|
|
|
|
t.subscription.id = sub.id;
|
|
|
|
Object.assign(t, thread);
|
|
|
|
})));
|
|
|
|
threadMessagesToCreate = threadMessagesToCreate.map(threadMessage => threadMessagesCollection.prepareCreate(protectedFunction((tm) => {
|
|
|
|
tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema);
|
|
|
|
Object.assign(tm, threadMessage);
|
|
|
|
tm.subscription.id = sub.id;
|
|
|
|
tm.rid = threadMessage.tmid;
|
|
|
|
delete threadMessage.tmid;
|
|
|
|
})));
|
|
|
|
|
|
|
|
// Update
|
|
|
|
msgsToUpdate = msgsToUpdate.map((message) => {
|
|
|
|
const newMessage = update.find(m => m._id === message.id);
|
2020-03-02 20:12:41 +00:00
|
|
|
if (message._hasPendingUpdate) {
|
|
|
|
console.log(message);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
return message.prepareUpdate(protectedFunction((m) => {
|
|
|
|
Object.assign(m, newMessage);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
threadsToUpdate = threadsToUpdate.map((thread) => {
|
2020-03-02 20:12:41 +00:00
|
|
|
if (thread._hasPendingUpdate) {
|
|
|
|
console.log(thread);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
const newThread = allThreads.find(t => t._id === thread.id);
|
|
|
|
return thread.prepareUpdate(protectedFunction((t) => {
|
|
|
|
Object.assign(t, newThread);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
threadMessagesToUpdate = threadMessagesToUpdate.map((threadMessage) => {
|
2020-03-02 20:12:41 +00:00
|
|
|
if (threadMessage._hasPendingUpdate) {
|
|
|
|
console.log(threadMessage);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
const newThreadMessage = allThreadMessages.find(t => t._id === threadMessage.id);
|
|
|
|
return threadMessage.prepareUpdate(protectedFunction((tm) => {
|
|
|
|
Object.assign(tm, newThreadMessage);
|
|
|
|
tm.rid = threadMessage.tmid;
|
|
|
|
delete threadMessage.tmid;
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
let msgsToDelete = [];
|
|
|
|
let threadsToDelete = [];
|
|
|
|
let threadMessagesToDelete = [];
|
|
|
|
if (remove && remove.length) {
|
|
|
|
msgsToDelete = allMessagesRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
msgsToDelete = msgsToDelete.map(m => m.prepareDestroyPermanently());
|
|
|
|
threadsToDelete = allThreadsRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
threadsToDelete = threadsToDelete.map(t => t.prepareDestroyPermanently());
|
|
|
|
threadMessagesToDelete = allThreadMessagesRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
threadMessagesToDelete = threadMessagesToDelete.map(tm => tm.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
// Delete loaders
|
|
|
|
loadersToDelete = loadersToDelete.map(m => m.prepareDestroyPermanently());
|
|
|
|
if (loaderItem) {
|
|
|
|
loadersToDelete.push(loaderItem.prepareDestroyPermanently());
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const allRecords = [
|
|
|
|
...msgsToCreate,
|
|
|
|
...msgsToUpdate,
|
|
|
|
...msgsToDelete,
|
|
|
|
...threadsToCreate,
|
|
|
|
...threadsToUpdate,
|
|
|
|
...threadsToDelete,
|
|
|
|
...threadMessagesToCreate,
|
|
|
|
...threadMessagesToUpdate,
|
2021-05-26 17:24:54 +00:00
|
|
|
...threadMessagesToDelete,
|
|
|
|
...loadersToDelete
|
2019-09-16 20:26:32 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.batch(...allRecords);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return allRecords.length;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|