From cbfca132c5d31cedbf595736771a4f12f09d4d85 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Tue, 22 Feb 2022 13:01:35 -0300 Subject: [PATCH] Chore: Migrate methods/getThreadName to typescript (#3707) * chore: change getThreadName to typescript * chore: change types after merge develop into current * chore: minor tweak --- app/definitions/IAttachment.ts | 2 +- app/definitions/IMessage.ts | 8 ++++---- app/definitions/ISubscription.ts | 2 +- app/definitions/IThread.ts | 11 ++++++----- app/definitions/IThreadMessage.ts | 7 ++++--- app/lib/encryption/encryption.ts | 2 +- app/lib/methods/getSingleMessage.ts | 3 ++- .../{getThreadName.js => getThreadName.ts} | 15 ++++++++------- app/lib/methods/updateMessages.ts | 4 +--- 9 files changed, 28 insertions(+), 26 deletions(-) rename app/lib/methods/{getThreadName.js => getThreadName.ts} (69%) diff --git a/app/definitions/IAttachment.ts b/app/definitions/IAttachment.ts index 4ff3aa8df..788d58763 100644 --- a/app/definitions/IAttachment.ts +++ b/app/definitions/IAttachment.ts @@ -1,5 +1,5 @@ export interface IAttachment { - ts: Date; + ts: string | Date; title: string; type: string; description: string; diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index 15f046c8b..bc8f793da 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -63,7 +63,7 @@ export interface IMessage { _id: string; rid: string; msg?: string; - id?: string; + id: string; t?: MessageType; ts: string | Date; u: IUserMessage; @@ -74,7 +74,7 @@ export interface IMessage { emoji?: string; attachments?: IAttachment[]; urls?: IUrl[]; - _updatedAt: Date; + _updatedAt: string | Date; status?: number; pinned?: boolean; starred?: boolean; @@ -83,10 +83,10 @@ export interface IMessage { role?: string; drid?: string; dcount?: number; - dlm?: Date; + dlm?: string | Date; tmid?: string; tcount?: number; - tlm?: Date; + tlm?: string | Date; replies?: string[]; mentions?: IUserMention[]; channels?: IUserChannel[]; diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index 9f88cf572..06c4db9b6 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -31,7 +31,7 @@ export interface ISubscription { v?: IVisitor; f: boolean; t: SubscriptionType; - ts: Date; + ts: string | Date; ls: Date; name: string; fname?: string; diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index be5dac1ba..824b2544e 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -13,6 +13,7 @@ interface IFileThread { } export interface IThreadResult { + id: string; _id: string; rid: string; ts: string | Date; @@ -23,13 +24,13 @@ export interface IThreadResult { attachments?: IAttachment[]; md?: MarkdownAST; u: IUserMessage; - _updatedAt: Date; + _updatedAt: string | Date; urls?: IUrl[]; mentions?: IUserMention[]; channels?: IUserChannel[]; replies?: string[]; tcount?: number; - tlm?: Date; + tlm?: string | Date; } export interface IThread { @@ -38,7 +39,7 @@ export interface IThread { msg?: string; t?: MessageType; rid: string; - _updatedAt?: Date; + _updatedAt?: string | Date; ts?: string | Date; u?: IUserMessage; alias?: string; @@ -56,10 +57,10 @@ export interface IThread { role?: string; drid?: string; dcount?: number | string; - dlm?: number; + dlm?: string | Date; tmid?: string; tcount?: number | string; - tlm?: string; + tlm?: string | Date; replies?: string[]; mentions?: IUserMention[]; channels?: IUserChannel[]; diff --git a/app/definitions/IThreadMessage.ts b/app/definitions/IThreadMessage.ts index 574d4a200..4461dd4e7 100644 --- a/app/definitions/IThreadMessage.ts +++ b/app/definitions/IThreadMessage.ts @@ -6,6 +6,7 @@ import { IReaction } from './IReaction'; import { IUrl } from './IUrl'; export interface IThreadMessage { + id: string; _id: string; tmsg?: string; msg?: string; @@ -20,7 +21,7 @@ export interface IThreadMessage { emoji?: string; attachments?: IAttachment[]; urls?: IUrl[]; - _updatedAt?: Date; + _updatedAt?: string | Date; status?: number; pinned?: boolean; starred?: boolean; @@ -29,10 +30,10 @@ export interface IThreadMessage { role?: string; drid?: string; dcount?: number; - dlm?: Date; + dlm?: string | Date; tmid?: string; tcount?: number; - tlm?: Date; + tlm?: string | Date; replies?: string[]; mentions?: IUserMention[]; channels?: IUserChannel[]; diff --git a/app/lib/encryption/encryption.ts b/app/lib/encryption/encryption.ts index 8d17ebd46..be979e278 100644 --- a/app/lib/encryption/encryption.ts +++ b/app/lib/encryption/encryption.ts @@ -443,7 +443,7 @@ class Encryption { }; // Decrypt a message - decryptMessage = async (message: Partial) => { + decryptMessage = async (message: Pick) => { const { t, e2e } = message; // Prevent create a new instance if this room was encrypted sometime ago diff --git a/app/lib/methods/getSingleMessage.ts b/app/lib/methods/getSingleMessage.ts index 4d395a0db..cc153e2f4 100644 --- a/app/lib/methods/getSingleMessage.ts +++ b/app/lib/methods/getSingleMessage.ts @@ -1,6 +1,7 @@ import RocketChat from '../rocketchat'; +import { IMessage } from '../../definitions'; -const getSingleMessage = (messageId: string) => +const getSingleMessage = (messageId: string): Promise => new Promise(async (resolve, reject) => { try { const result = await RocketChat.getSingleMessage(messageId); diff --git a/app/lib/methods/getThreadName.js b/app/lib/methods/getThreadName.ts similarity index 69% rename from app/lib/methods/getThreadName.js rename to app/lib/methods/getThreadName.ts index a490a7d40..b6ed94580 100644 --- a/app/lib/methods/getThreadName.js +++ b/app/lib/methods/getThreadName.ts @@ -6,11 +6,12 @@ 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): string | undefined => thread.msg || thread?.attachments?.[0]?.title; -const getThreadName = async (rid, tmid, messageId) => { - let tmsg; +const getThreadName = async (rid: string, tmid: string, messageId: string): Promise => { + let tmsg: string | undefined; try { const db = database.active; const threadCollection = db.get('threads'); @@ -18,7 +19,7 @@ const getThreadName = async (rid, tmid, messageId) => { const threadRecord = await getThreadById(tmid); if (threadRecord) { tmsg = buildThreadName(threadRecord); - await db.action(async () => { + await db.write(async () => { await messageRecord?.update(m => { m.tmsg = tmsg; }); @@ -27,11 +28,11 @@ const getThreadName = async (rid, tmid, messageId) => { 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; + if (t.subscription) t.subscription.id = rid; Object.assign(t, thread); }), messageRecord?.prepareUpdate(m => { diff --git a/app/lib/methods/updateMessages.ts b/app/lib/methods/updateMessages.ts index 75294bc83..1e86d7997 100644 --- a/app/lib/methods/updateMessages.ts +++ b/app/lib/methods/updateMessages.ts @@ -105,9 +105,7 @@ export default async function updateMessages({ threadCollection.prepareCreate( protectedFunction((t: TThreadModel) => { t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema); - if (t.subscription) { - t.subscription.id = sub.id; - } + if (t.subscription) t.subscription.id = sub.id; Object.assign(t, thread); }) )