Chore: Migrate methods/getThreadName to typescript (#3707)
* chore: change getThreadName to typescript * chore: change types after merge develop into current * chore: minor tweak
This commit is contained in:
parent
c5cc192dc9
commit
cbfca132c5
|
@ -1,5 +1,5 @@
|
||||||
export interface IAttachment {
|
export interface IAttachment {
|
||||||
ts: Date;
|
ts: string | Date;
|
||||||
title: string;
|
title: string;
|
||||||
type: string;
|
type: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
|
|
@ -63,7 +63,7 @@ export interface IMessage {
|
||||||
_id: string;
|
_id: string;
|
||||||
rid: string;
|
rid: string;
|
||||||
msg?: string;
|
msg?: string;
|
||||||
id?: string;
|
id: string;
|
||||||
t?: MessageType;
|
t?: MessageType;
|
||||||
ts: string | Date;
|
ts: string | Date;
|
||||||
u: IUserMessage;
|
u: IUserMessage;
|
||||||
|
@ -74,7 +74,7 @@ export interface IMessage {
|
||||||
emoji?: string;
|
emoji?: string;
|
||||||
attachments?: IAttachment[];
|
attachments?: IAttachment[];
|
||||||
urls?: IUrl[];
|
urls?: IUrl[];
|
||||||
_updatedAt: Date;
|
_updatedAt: string | Date;
|
||||||
status?: number;
|
status?: number;
|
||||||
pinned?: boolean;
|
pinned?: boolean;
|
||||||
starred?: boolean;
|
starred?: boolean;
|
||||||
|
@ -83,10 +83,10 @@ export interface IMessage {
|
||||||
role?: string;
|
role?: string;
|
||||||
drid?: string;
|
drid?: string;
|
||||||
dcount?: number;
|
dcount?: number;
|
||||||
dlm?: Date;
|
dlm?: string | Date;
|
||||||
tmid?: string;
|
tmid?: string;
|
||||||
tcount?: number;
|
tcount?: number;
|
||||||
tlm?: Date;
|
tlm?: string | Date;
|
||||||
replies?: string[];
|
replies?: string[];
|
||||||
mentions?: IUserMention[];
|
mentions?: IUserMention[];
|
||||||
channels?: IUserChannel[];
|
channels?: IUserChannel[];
|
||||||
|
|
|
@ -31,7 +31,7 @@ export interface ISubscription {
|
||||||
v?: IVisitor;
|
v?: IVisitor;
|
||||||
f: boolean;
|
f: boolean;
|
||||||
t: SubscriptionType;
|
t: SubscriptionType;
|
||||||
ts: Date;
|
ts: string | Date;
|
||||||
ls: Date;
|
ls: Date;
|
||||||
name: string;
|
name: string;
|
||||||
fname?: string;
|
fname?: string;
|
||||||
|
|
|
@ -13,6 +13,7 @@ interface IFileThread {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IThreadResult {
|
export interface IThreadResult {
|
||||||
|
id: string;
|
||||||
_id: string;
|
_id: string;
|
||||||
rid: string;
|
rid: string;
|
||||||
ts: string | Date;
|
ts: string | Date;
|
||||||
|
@ -23,13 +24,13 @@ export interface IThreadResult {
|
||||||
attachments?: IAttachment[];
|
attachments?: IAttachment[];
|
||||||
md?: MarkdownAST;
|
md?: MarkdownAST;
|
||||||
u: IUserMessage;
|
u: IUserMessage;
|
||||||
_updatedAt: Date;
|
_updatedAt: string | Date;
|
||||||
urls?: IUrl[];
|
urls?: IUrl[];
|
||||||
mentions?: IUserMention[];
|
mentions?: IUserMention[];
|
||||||
channels?: IUserChannel[];
|
channels?: IUserChannel[];
|
||||||
replies?: string[];
|
replies?: string[];
|
||||||
tcount?: number;
|
tcount?: number;
|
||||||
tlm?: Date;
|
tlm?: string | Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IThread {
|
export interface IThread {
|
||||||
|
@ -38,7 +39,7 @@ export interface IThread {
|
||||||
msg?: string;
|
msg?: string;
|
||||||
t?: MessageType;
|
t?: MessageType;
|
||||||
rid: string;
|
rid: string;
|
||||||
_updatedAt?: Date;
|
_updatedAt?: string | Date;
|
||||||
ts?: string | Date;
|
ts?: string | Date;
|
||||||
u?: IUserMessage;
|
u?: IUserMessage;
|
||||||
alias?: string;
|
alias?: string;
|
||||||
|
@ -56,10 +57,10 @@ export interface IThread {
|
||||||
role?: string;
|
role?: string;
|
||||||
drid?: string;
|
drid?: string;
|
||||||
dcount?: number | string;
|
dcount?: number | string;
|
||||||
dlm?: number;
|
dlm?: string | Date;
|
||||||
tmid?: string;
|
tmid?: string;
|
||||||
tcount?: number | string;
|
tcount?: number | string;
|
||||||
tlm?: string;
|
tlm?: string | Date;
|
||||||
replies?: string[];
|
replies?: string[];
|
||||||
mentions?: IUserMention[];
|
mentions?: IUserMention[];
|
||||||
channels?: IUserChannel[];
|
channels?: IUserChannel[];
|
||||||
|
|
|
@ -6,6 +6,7 @@ import { IReaction } from './IReaction';
|
||||||
import { IUrl } from './IUrl';
|
import { IUrl } from './IUrl';
|
||||||
|
|
||||||
export interface IThreadMessage {
|
export interface IThreadMessage {
|
||||||
|
id: string;
|
||||||
_id: string;
|
_id: string;
|
||||||
tmsg?: string;
|
tmsg?: string;
|
||||||
msg?: string;
|
msg?: string;
|
||||||
|
@ -20,7 +21,7 @@ export interface IThreadMessage {
|
||||||
emoji?: string;
|
emoji?: string;
|
||||||
attachments?: IAttachment[];
|
attachments?: IAttachment[];
|
||||||
urls?: IUrl[];
|
urls?: IUrl[];
|
||||||
_updatedAt?: Date;
|
_updatedAt?: string | Date;
|
||||||
status?: number;
|
status?: number;
|
||||||
pinned?: boolean;
|
pinned?: boolean;
|
||||||
starred?: boolean;
|
starred?: boolean;
|
||||||
|
@ -29,10 +30,10 @@ export interface IThreadMessage {
|
||||||
role?: string;
|
role?: string;
|
||||||
drid?: string;
|
drid?: string;
|
||||||
dcount?: number;
|
dcount?: number;
|
||||||
dlm?: Date;
|
dlm?: string | Date;
|
||||||
tmid?: string;
|
tmid?: string;
|
||||||
tcount?: number;
|
tcount?: number;
|
||||||
tlm?: Date;
|
tlm?: string | Date;
|
||||||
replies?: string[];
|
replies?: string[];
|
||||||
mentions?: IUserMention[];
|
mentions?: IUserMention[];
|
||||||
channels?: IUserChannel[];
|
channels?: IUserChannel[];
|
||||||
|
|
|
@ -443,7 +443,7 @@ class Encryption {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Decrypt a message
|
// Decrypt a message
|
||||||
decryptMessage = async (message: Partial<IMessage>) => {
|
decryptMessage = async (message: Pick<IMessage, 't' | 'e2e' | 'rid' | 'msg' | 'tmsg'>) => {
|
||||||
const { t, e2e } = message;
|
const { t, e2e } = message;
|
||||||
|
|
||||||
// Prevent create a new instance if this room was encrypted sometime ago
|
// Prevent create a new instance if this room was encrypted sometime ago
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import RocketChat from '../rocketchat';
|
import RocketChat from '../rocketchat';
|
||||||
|
import { IMessage } from '../../definitions';
|
||||||
|
|
||||||
const getSingleMessage = (messageId: string) =>
|
const getSingleMessage = (messageId: string): Promise<IMessage> =>
|
||||||
new Promise(async (resolve, reject) => {
|
new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const result = await RocketChat.getSingleMessage(messageId);
|
const result = await RocketChat.getSingleMessage(messageId);
|
||||||
|
|
|
@ -6,11 +6,12 @@ import { getThreadById } from '../database/services/Thread';
|
||||||
import log from '../../utils/log';
|
import log from '../../utils/log';
|
||||||
import { Encryption } from '../encryption';
|
import { Encryption } from '../encryption';
|
||||||
import getSingleMessage from './getSingleMessage';
|
import getSingleMessage from './getSingleMessage';
|
||||||
|
import { IThread, TThreadModel } from '../../definitions';
|
||||||
|
|
||||||
const buildThreadName = thread => thread.msg || thread?.attachments?.[0]?.title;
|
const buildThreadName = (thread: IThread): string | undefined => thread.msg || thread?.attachments?.[0]?.title;
|
||||||
|
|
||||||
const getThreadName = async (rid, tmid, messageId) => {
|
const getThreadName = async (rid: string, tmid: string, messageId: string): Promise<string | undefined> => {
|
||||||
let tmsg;
|
let tmsg: string | undefined;
|
||||||
try {
|
try {
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const threadCollection = db.get('threads');
|
const threadCollection = db.get('threads');
|
||||||
|
@ -18,7 +19,7 @@ const getThreadName = async (rid, tmid, messageId) => {
|
||||||
const threadRecord = await getThreadById(tmid);
|
const threadRecord = await getThreadById(tmid);
|
||||||
if (threadRecord) {
|
if (threadRecord) {
|
||||||
tmsg = buildThreadName(threadRecord);
|
tmsg = buildThreadName(threadRecord);
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await messageRecord?.update(m => {
|
await messageRecord?.update(m => {
|
||||||
m.tmsg = tmsg;
|
m.tmsg = tmsg;
|
||||||
});
|
});
|
||||||
|
@ -27,11 +28,11 @@ const getThreadName = async (rid, tmid, messageId) => {
|
||||||
let thread = await getSingleMessage(tmid);
|
let thread = await getSingleMessage(tmid);
|
||||||
thread = await Encryption.decryptMessage(thread);
|
thread = await Encryption.decryptMessage(thread);
|
||||||
tmsg = buildThreadName(thread);
|
tmsg = buildThreadName(thread);
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await db.batch(
|
await db.batch(
|
||||||
threadCollection?.prepareCreate(t => {
|
threadCollection?.prepareCreate((t: TThreadModel) => {
|
||||||
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
|
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
|
||||||
t.subscription.id = rid;
|
if (t.subscription) t.subscription.id = rid;
|
||||||
Object.assign(t, thread);
|
Object.assign(t, thread);
|
||||||
}),
|
}),
|
||||||
messageRecord?.prepareUpdate(m => {
|
messageRecord?.prepareUpdate(m => {
|
|
@ -105,9 +105,7 @@ export default async function updateMessages({
|
||||||
threadCollection.prepareCreate(
|
threadCollection.prepareCreate(
|
||||||
protectedFunction((t: TThreadModel) => {
|
protectedFunction((t: TThreadModel) => {
|
||||||
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
|
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
|
||||||
if (t.subscription) {
|
if (t.subscription) t.subscription.id = sub.id;
|
||||||
t.subscription.id = sub.id;
|
|
||||||
}
|
|
||||||
Object.assign(t, thread);
|
Object.assign(t, thread);
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue