Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Migrate loadThreadMessages to TS #3718

Merged
merged 12 commits into from
Mar 2, 2022
1 change: 1 addition & 0 deletions app/definitions/IThreadMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IReaction } from './IReaction';
import { SubscriptionType } from './ISubscription';

export interface IThreadMessage {
_id: string;
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
msg?: string;
t?: SubscriptionType;
rid: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import log from '../../utils/log';
import { Encryption } from '../encryption';
import protectedFunction from './helpers/protectedFunction';
import buildMessage from './helpers/buildMessage';
import { TThreadMessageModel } from '../../definitions';
import sdk from '../rocketchat/services/sdk';

async function load({ tmid }) {
async function load({ tmid }: { tmid: string }) {
try {
// RC 1.0
const result = await this.methodCallWrapper('getThreadMessages', { tmid });
const result = await sdk.methodCallWrapper('getThreadMessages', { tmid });
if (!result) {
return [];
}
Expand All @@ -22,53 +24,62 @@ async function load({ tmid }) {
}
}

export default function loadThreadMessages({ tmid, rid }) {
export default function loadThreadMessages({ tmid, rid }: { tmid: string; rid: string }) {
return new Promise(async (resolve, reject) => {
try {
let data = await load.call(this, { tmid });
let data = await load({ tmid });
if (data && data.length) {
try {
data = data.filter(m => m.tmid).map(m => buildMessage(m));
data = data.filter((m: TThreadMessageModel) => m.tmid).map((m: TThreadMessageModel) => buildMessage(m));
data = await Encryption.decryptMessages(data);
const db = database.active;
const threadMessagesCollection = db.get('thread_messages');
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));
let threadMessagesToCreate = data.filter(
gerzonc marked this conversation as resolved.
Show resolved Hide resolved
(i1: TThreadMessageModel) => !allThreadMessagesRecords.find(i2 => i1._id === i2.id)
);
let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 =>
data.find((i2: TThreadMessageModel) => i1.id === i2._id)
);

threadMessagesToCreate = threadMessagesToCreate.map(threadMessage =>
threadMessagesToCreate = threadMessagesToCreate.map((threadMessage: TThreadMessageModel) =>
threadMessagesCollection.prepareCreate(
protectedFunction(tm => {
protectedFunction((tm: TThreadMessageModel) => {
tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema);
Object.assign(tm, threadMessage);
tm.subscription.id = rid;
tm.rid = threadMessage.tmid;
if (tm.subscription) {
tm.subscription.id = rid;
}
if (threadMessage.tmid) {
tm.rid = threadMessage.tmid;
}
delete threadMessage.tmid;
})
)
);

threadMessagesToUpdate = threadMessagesToUpdate.map(threadMessage => {
const newThreadMessage = data.find(t => t._id === threadMessage.id);
const newThreadMessage = data.find((t: TThreadMessageModel) => t._id === threadMessage.id);
return threadMessage.prepareUpdate(
protectedFunction(tm => {
protectedFunction((tm: TThreadMessageModel) => {
Object.assign(tm, newThreadMessage);
tm.rid = threadMessage.tmid;
if (threadMessage.tmid) {
tm.rid = threadMessage.tmid;
}
delete threadMessage.tmid;
})
);
});

await db.action(async () => {
await db.write(async () => {
await db.batch(...threadMessagesToCreate, ...threadMessagesToUpdate);
});
} catch (e) {
log(e);
}
return resolve(data);
} else {
return resolve([]);
}
return resolve([]);
} catch (e) {
reject(e);
}
Expand Down