From 1f4985bfeea051df5f7e453acdefcf9e47ddc415 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Wed, 9 Feb 2022 16:40:54 -0300 Subject: [PATCH 1/3] chore: change getThreadName to typescript --- app/definitions/IThread.ts | 1 + .../{getThreadName.js => getThreadName.ts} | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) rename app/lib/methods/{getThreadName.js => getThreadName.ts} (65%) diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index 3edefbcadf4..cc9271c8c37 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -73,6 +73,7 @@ export interface IThread { autoTranslate?: boolean; translations?: any; e2e?: string; + subscription: { id: string }; } export type TThreadModel = IThread & Model; diff --git a/app/lib/methods/getThreadName.js b/app/lib/methods/getThreadName.ts similarity index 65% rename from app/lib/methods/getThreadName.js rename to app/lib/methods/getThreadName.ts index a490a7d40a8..cf37b093730 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) => 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,8 +19,8 @@ const getThreadName = async (rid, tmid, 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; }); }); @@ -27,14 +28,14 @@ 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; Object.assign(t, thread); }), - messageRecord?.prepareUpdate(m => { + messageRecord?.prepareUpdate((m: { tmsg: string | undefined }) => { m.tmsg = tmsg; }) ); From 742356487d20ef97cc10ec715fd98c9c2ec5dae1 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Mon, 21 Feb 2022 14:52:27 -0300 Subject: [PATCH 2/3] chore: change types after merge develop into current --- app/definitions/IAttachment.ts | 2 +- app/definitions/IMessage.ts | 8 ++++---- app/definitions/ISubscription.ts | 2 +- app/definitions/IThread.ts | 15 ++++++++------- app/definitions/IThreadMessage.ts | 9 +++++---- app/lib/encryption/encryption.ts | 2 +- app/lib/methods/getSingleMessage.ts | 3 ++- app/lib/methods/getThreadName.ts | 8 ++++---- app/lib/methods/updateMessages.ts | 2 +- app/utils/room.ts | 2 +- 10 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/definitions/IAttachment.ts b/app/definitions/IAttachment.ts index 4ff3aa8df31..788d5876383 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 15f046c8b9a..bc8f793da62 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 3e523656b8d..bd7e2a13680 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -29,7 +29,7 @@ export interface ISubscription { id: string; // id from server 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 670e10d52cb..824b2544e07 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,8 +39,8 @@ export interface IThread { msg?: string; t?: MessageType; rid: string; - _updatedAt?: Date; - ts?: Date; + _updatedAt?: string | Date; + ts?: string | Date; u?: IUserMessage; alias?: string; parseUrls?: boolean; @@ -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[]; @@ -67,7 +68,7 @@ export interface IThread { autoTranslate?: boolean; translations?: any; e2e?: string; - subscription: { id: string }; + subscription?: { id: string }; } export type TThreadModel = IThread & Model; diff --git a/app/definitions/IThreadMessage.ts b/app/definitions/IThreadMessage.ts index 574d4a20034..347da0e8ac7 100644 --- a/app/definitions/IThreadMessage.ts +++ b/app/definitions/IThreadMessage.ts @@ -6,12 +6,13 @@ import { IReaction } from './IReaction'; import { IUrl } from './IUrl'; export interface IThreadMessage { + id: string; _id: string; tmsg?: string; msg?: string; t?: MessageType; rid: string; - ts: string | Date; + ts: string; u: IUserMessage; alias?: string; parseUrls?: boolean; @@ -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 8d17ebd4629..be979e278a6 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 4d395a0db92..cc153e2f414 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.ts b/app/lib/methods/getThreadName.ts index cf37b093730..b6ed94580a0 100644 --- a/app/lib/methods/getThreadName.ts +++ b/app/lib/methods/getThreadName.ts @@ -8,7 +8,7 @@ import { Encryption } from '../encryption'; import getSingleMessage from './getSingleMessage'; import { IThread, TThreadModel } from '../../definitions'; -const buildThreadName = (thread: IThread) => thread.msg || thread?.attachments?.[0]?.title; +const buildThreadName = (thread: IThread): string | undefined => thread.msg || thread?.attachments?.[0]?.title; const getThreadName = async (rid: string, tmid: string, messageId: string): Promise => { let tmsg: string | undefined; @@ -20,7 +20,7 @@ const getThreadName = async (rid: string, tmid: string, messageId: string): Prom if (threadRecord) { tmsg = buildThreadName(threadRecord); await db.write(async () => { - await messageRecord?.update((m: { tmsg: string | undefined }) => { + await messageRecord?.update(m => { m.tmsg = tmsg; }); }); @@ -32,10 +32,10 @@ const getThreadName = async (rid: string, tmid: string, messageId: string): Prom await db.batch( 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: { tmsg: string | undefined }) => { + messageRecord?.prepareUpdate(m => { m.tmsg = tmsg; }) ); diff --git a/app/lib/methods/updateMessages.ts b/app/lib/methods/updateMessages.ts index dd1f535cbd0..1e86d799721 100644 --- a/app/lib/methods/updateMessages.ts +++ b/app/lib/methods/updateMessages.ts @@ -105,7 +105,7 @@ export default async function updateMessages({ threadCollection.prepareCreate( protectedFunction((t: TThreadModel) => { t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema); - t.subscription.id = sub.id; + if (t.subscription) t.subscription.id = sub.id; Object.assign(t, thread); }) ) diff --git a/app/utils/room.ts b/app/utils/room.ts index d39e4049035..bcd384de6e0 100644 --- a/app/utils/room.ts +++ b/app/utils/room.ts @@ -30,7 +30,7 @@ export const formatDate = (date: Date): string => sameElse: 'L' }); -export const formatDateThreads = (date: Date): string => +export const formatDateThreads = (date: string | Date): string => moment(date).calendar(null, { sameDay: 'LT', lastDay: `[${I18n.t('Yesterday')}] LT`, From 71e2c987f4c2689dd94ec5d0acb163aedb2abbe0 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Tue, 22 Feb 2022 12:02:38 -0300 Subject: [PATCH 3/3] chore: minor tweak --- app/definitions/IThreadMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/definitions/IThreadMessage.ts b/app/definitions/IThreadMessage.ts index 347da0e8ac7..4461dd4e754 100644 --- a/app/definitions/IThreadMessage.ts +++ b/app/definitions/IThreadMessage.ts @@ -12,7 +12,7 @@ export interface IThreadMessage { msg?: string; t?: MessageType; rid: string; - ts: string; + ts: string | Date; u: IUserMessage; alias?: string; parseUrls?: boolean;