From 86cc8a7d16fc15e33c33734b4ed28f161a455594 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 16 Feb 2022 20:07:24 -0400 Subject: [PATCH] Chore: Migrate buildMessage to TS (#3732) * migrate buildMessage to TS * Fix lint * minor tweak * minor tweaks --- app/containers/MessageActions/index.tsx | 2 +- app/definitions/IMessage.ts | 11 ++++++----- app/definitions/IThread.ts | 18 +++++++++--------- app/definitions/IThreadMessage.ts | 8 +++++--- app/lib/encryption/encryption.ts | 19 +++++++++++-------- .../{buildMessage.js => buildMessage.ts} | 3 ++- app/lib/methods/updateMessages.ts | 8 ++++++-- .../rocketchat/methods/getPermalinkMessage.ts | 1 + 8 files changed, 41 insertions(+), 29 deletions(-) rename app/lib/methods/helpers/{buildMessage.js => buildMessage.ts} (66%) diff --git a/app/containers/MessageActions/index.tsx b/app/containers/MessageActions/index.tsx index 87a28e74..13a1e369 100644 --- a/app/containers/MessageActions/index.tsx +++ b/app/containers/MessageActions/index.tsx @@ -283,7 +283,7 @@ const MessageActions = React.memo( if (!translatedMessage) { const m = { _id: message.id, - rid: message.subscription.id, + rid: message.subscription ? message.subscription.id : '', u: message.u, msg: message.msg }; diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index d86492cd..85acfdcc 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -3,6 +3,7 @@ import { MarkdownAST } from '@rocket.chat/message-parser'; import { IAttachment } from './IAttachment'; import { IReaction } from './IReaction'; +import { IUrl } from './IUrl'; export type MessageType = 'jitsi_call_started' | 'discussion-created' | 'e2e' | 'load_more' | 'rm' | 'uj'; @@ -63,15 +64,15 @@ export interface IMessage { rid: string; msg?: string; t?: MessageType; - ts: Date; + ts: string | Date; u: IUserMessage; - alias: string; - parseUrls: boolean; + alias?: string; + parseUrls?: boolean; groupable?: boolean; avatar?: string; emoji?: string; attachments?: IAttachment[]; - urls?: string[]; + urls?: IUrl[]; _updatedAt: Date; status?: number; pinned?: boolean; @@ -96,7 +97,7 @@ export interface IMessage { e2e?: string; tshow?: boolean; md?: MarkdownAST; - subscription: { id: string }; + subscription?: { id: string }; } export type TMessageModel = IMessage & Model; diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index 2095d903..670e10d5 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -15,21 +15,21 @@ interface IFileThread { export interface IThreadResult { _id: string; rid: string; - ts: string; - msg: string; + ts: string | Date; + msg?: string; file?: IFileThread; files?: IFileThread[]; groupable?: boolean; attachments?: IAttachment[]; md?: MarkdownAST; u: IUserMessage; - _updatedAt: string; - urls: IUrl[]; - mentions: IUserMention[]; - channels: IUserChannel[]; - replies: string[]; - tcount: number; - tlm: string; + _updatedAt: Date; + urls?: IUrl[]; + mentions?: IUserMention[]; + channels?: IUserChannel[]; + replies?: string[]; + tcount?: number; + tlm?: Date; } export interface IThread { diff --git a/app/definitions/IThreadMessage.ts b/app/definitions/IThreadMessage.ts index 9792a950..574d4a20 100644 --- a/app/definitions/IThreadMessage.ts +++ b/app/definitions/IThreadMessage.ts @@ -3,13 +3,15 @@ import Model from '@nozbe/watermelondb/Model'; import { IAttachment } from './IAttachment'; import { IEditedBy, ITranslations, IUserChannel, IUserMention, IUserMessage, MessageType } from './IMessage'; import { IReaction } from './IReaction'; +import { IUrl } from './IUrl'; export interface IThreadMessage { + _id: string; tmsg?: string; msg?: string; t?: MessageType; rid: string; - ts: Date; + ts: string | Date; u: IUserMessage; alias?: string; parseUrls?: boolean; @@ -17,7 +19,7 @@ export interface IThreadMessage { avatar?: string; emoji?: string; attachments?: IAttachment[]; - urls?: string[]; + urls?: IUrl[]; _updatedAt?: Date; status?: number; pinned?: boolean; @@ -38,7 +40,7 @@ export interface IThreadMessage { autoTranslate?: boolean; translations?: ITranslations[]; e2e?: string; - subscription: { id: string }; + subscription?: { id: string }; } export type TThreadMessageModel = IThreadMessage & Model; diff --git a/app/lib/encryption/encryption.ts b/app/lib/encryption/encryption.ts index 0f5c1724..8d17ebd4 100644 --- a/app/lib/encryption/encryption.ts +++ b/app/lib/encryption/encryption.ts @@ -252,14 +252,17 @@ class Encryption { toDecrypt = (await Promise.all( toDecrypt.map(async message => { const { t, msg, tmsg } = message; - const { id: rid } = message.subscription; - // WM Object -> Plain Object - const newMessage = await this.decryptMessage({ - t, - rid, - msg, - tmsg - }); + let newMessage: TMessageModel = {} as TMessageModel; + if (message.subscription) { + const { id: rid } = message.subscription; + // WM Object -> Plain Object + newMessage = await this.decryptMessage({ + t, + rid, + msg, + tmsg + }); + } try { return message.prepareUpdate( diff --git a/app/lib/methods/helpers/buildMessage.js b/app/lib/methods/helpers/buildMessage.ts similarity index 66% rename from app/lib/methods/helpers/buildMessage.js rename to app/lib/methods/helpers/buildMessage.ts index e0c09e4a..44c30ffa 100644 --- a/app/lib/methods/helpers/buildMessage.js +++ b/app/lib/methods/helpers/buildMessage.ts @@ -1,7 +1,8 @@ +import { IMessage } from '../../../definitions'; import messagesStatus from '../../../constants/messagesStatus'; import normalizeMessage from './normalizeMessage'; -export default message => { +export default (message: IMessage): IMessage => { message.status = messagesStatus.SENT; return normalizeMessage(message); }; diff --git a/app/lib/methods/updateMessages.ts b/app/lib/methods/updateMessages.ts index f273aaea..ddfd8722 100644 --- a/app/lib/methods/updateMessages.ts +++ b/app/lib/methods/updateMessages.ts @@ -94,7 +94,9 @@ export default async function updateMessages({ msgCollection.prepareCreate( protectedFunction((m: TMessageModel) => { m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema); - m.subscription.id = sub.id; + if (m.subscription) { + m.subscription.id = sub.id; + } Object.assign(m, message); }) ) @@ -113,7 +115,9 @@ export default async function updateMessages({ protectedFunction((tm: TThreadMessageModel) => { tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema); Object.assign(tm, threadMessage); - tm.subscription.id = sub.id; + if (tm.subscription) { + tm.subscription.id = sub.id; + } if (threadMessage.tmid) { tm.rid = threadMessage.tmid; } diff --git a/app/lib/rocketchat/methods/getPermalinkMessage.ts b/app/lib/rocketchat/methods/getPermalinkMessage.ts index 69b13a07..9bc96bdd 100644 --- a/app/lib/rocketchat/methods/getPermalinkMessage.ts +++ b/app/lib/rocketchat/methods/getPermalinkMessage.ts @@ -7,6 +7,7 @@ import isGroupChat from './isGroupChat'; type TRoomType = 'p' | 'c' | 'd'; export default async function getPermalinkMessage(message: TMessageModel): Promise { + if (!message.subscription) return null; let room: TSubscriptionModel; try { room = await getRoom(message.subscription.id);