2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
import buildMessage from './helpers/buildMessage';
|
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';
|
2019-09-16 20:26:32 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../encryption';
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
async function load({ tmid, offset }) {
|
2019-04-17 17:01:03 +00:00
|
|
|
try {
|
|
|
|
// RC 1.0
|
2019-04-24 18:36:29 +00:00
|
|
|
const result = await this.sdk.get('chat.getThreadMessages', {
|
2019-05-03 13:33:38 +00:00
|
|
|
tmid, count: 50, offset, sort: { ts: -1 }, query: { _hidden: { $ne: true } }
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
if (!result || !result.success) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return [];
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
return result.messages;
|
2019-04-17 17:01:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export default function loadThreadMessages({ tmid, rid, offset = 0 }) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
let data = await load.call(this, { tmid, offset });
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
if (data && data.length) {
|
2021-02-23 18:36:20 +00:00
|
|
|
try {
|
|
|
|
data = data.map(m => buildMessage(m));
|
|
|
|
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-02-23 18:36:20 +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;
|
|
|
|
})));
|
|
|
|
|
|
|
|
threadMessagesToUpdate = threadMessagesToUpdate.map((threadMessage) => {
|
|
|
|
const newThreadMessage = data.find(t => t._id === threadMessage.id);
|
|
|
|
return threadMessage.prepareUpdate(protectedFunction((tm) => {
|
|
|
|
Object.assign(tm, newThreadMessage);
|
2019-09-16 20:26:32 +00:00
|
|
|
tm.rid = threadMessage.tmid;
|
|
|
|
delete threadMessage.tmid;
|
2021-02-23 18:36:20 +00:00
|
|
|
}));
|
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-02-23 18:36:20 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(
|
|
|
|
...threadMessagesToCreate,
|
|
|
|
...threadMessagesToUpdate
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return resolve(data);
|
2019-04-17 17:01:03 +00:00
|
|
|
} else {
|
|
|
|
return resolve([]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|