Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/definitions/IThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface IThread {
autoTranslate?: boolean;
translations?: any;
e2e?: string;
subscription: { id: string };
}

export type TThreadModel = IThread & Model;
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@ import { getThreadById } from '../database/services/Thread';
import log from '../../utils/log';
import { Encryption } from '../encryption';
import getSingleMessage from './getSingleMessage';
import { IThread, TThreadModel } from '../../definitions';

const buildThreadName = thread => thread.msg || thread?.attachments?.[0]?.title;
const buildThreadName = (thread: IThread) => thread.msg || thread?.attachments?.[0]?.title;

const getThreadName = async (rid, tmid, messageId) => {
let tmsg;
const getThreadName = async (rid: string, tmid: string, messageId: string): Promise<string | undefined> => {
let tmsg: string | undefined;
try {
const db = database.active;
const threadCollection = db.get('threads');
const messageRecord = await getMessageById(messageId);
const threadRecord = await getThreadById(tmid);
if (threadRecord) {
tmsg = buildThreadName(threadRecord);
await db.action(async () => {
await messageRecord?.update(m => {
await db.write(async () => {
await messageRecord?.update((m: { tmsg: string | undefined }) => {
m.tmsg = tmsg;
});
});
} else {
let thread = await getSingleMessage(tmid);
thread = await Encryption.decryptMessage(thread);
tmsg = buildThreadName(thread);
await db.action(async () => {
await db.write(async () => {
await db.batch(
threadCollection?.prepareCreate(t => {
threadCollection?.prepareCreate((t: TThreadModel) => {
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
t.subscription.id = rid;
Object.assign(t, thread);
}),
messageRecord?.prepareUpdate(m => {
messageRecord?.prepareUpdate((m: { tmsg: string | undefined }) => {
m.tmsg = tmsg;
})
);
Expand Down