2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2021-05-26 17:24:54 +00:00
|
|
|
import EJSON from 'ejson';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2019-04-17 17:01:03 +00:00
|
|
|
import log from '../../utils/log';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../encryption';
|
2021-09-13 20:41:05 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
|
|
|
import buildMessage from './helpers/buildMessage';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
async function load({ tmid }) {
|
2019-04-17 17:01:03 +00:00
|
|
|
try {
|
|
|
|
// RC 1.0
|
2021-05-26 17:24:54 +00:00
|
|
|
const result = await this.methodCallWrapper('getThreadMessages', { tmid });
|
|
|
|
if (!result) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return [];
|
|
|
|
}
|
2021-05-26 17:24:54 +00:00
|
|
|
return EJSON.fromJSONValue(result);
|
2019-04-17 17:01:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
export default function loadThreadMessages({ tmid, rid }) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2019-04-17 17:01:03 +00:00
|
|
|
try {
|
2021-05-26 17:24:54 +00:00
|
|
|
let data = await load.call(this, { tmid });
|
2019-04-17 17:01:03 +00:00
|
|
|
if (data && data.length) {
|
2021-02-23 18:36:20 +00:00
|
|
|
try {
|
2021-05-26 17:24:54 +00:00
|
|
|
data = data.filter(m => m.tmid).map(m => buildMessage(m));
|
2021-02-23 18:36:20 +00:00
|
|
|
data = await Encryption.decryptMessages(data);
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2021-02-23 18:36:20 +00:00
|
|
|
const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch();
|
|
|
|
let threadMessagesToCreate = data.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id));
|
|
|
|
let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => data.find(i2 => i1.id === i2._id));
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
threadMessagesToCreate = threadMessagesToCreate.map(threadMessage =>
|
|
|
|
threadMessagesCollection.prepareCreate(
|
|
|
|
protectedFunction(tm => {
|
|
|
|
tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema);
|
|
|
|
Object.assign(tm, threadMessage);
|
|
|
|
tm.subscription.id = rid;
|
|
|
|
tm.rid = threadMessage.tmid;
|
|
|
|
delete threadMessage.tmid;
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-02-23 18:36:20 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
threadMessagesToUpdate = threadMessagesToUpdate.map(threadMessage => {
|
2021-02-23 18:36:20 +00:00
|
|
|
const newThreadMessage = data.find(t => t._id === threadMessage.id);
|
2021-09-13 20:41:05 +00:00
|
|
|
return threadMessage.prepareUpdate(
|
|
|
|
protectedFunction(tm => {
|
|
|
|
Object.assign(tm, newThreadMessage);
|
|
|
|
tm.rid = threadMessage.tmid;
|
|
|
|
delete threadMessage.tmid;
|
|
|
|
})
|
|
|
|
);
|
2021-02-23 18:36:20 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
await db.action(async () => {
|
|
|
|
await db.batch(...threadMessagesToCreate, ...threadMessagesToUpdate);
|
2021-02-23 18:36:20 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return resolve(data);
|
2019-04-17 17:01:03 +00:00
|
|
|
} else {
|
|
|
|
return resolve([]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|