From 8decd0f4b6fd8024714124a6e9a83be79e214880 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Mon, 7 Mar 2022 22:54:34 -0400 Subject: [PATCH] Chore: Migrate normalizeMessage to TS (#3743) * migrate normalizeMessage to ts * fix: missing null validations and type aliases --- app/definitions/IMessage.ts | 67 +++++++------------ app/definitions/ISubscription.ts | 2 +- app/definitions/IThread.ts | 2 +- app/lib/database/services/Message.ts | 5 +- app/lib/methods/helpers/buildMessage.ts | 2 +- ...ormalizeMessage.js => normalizeMessage.ts} | 16 +++-- app/lib/methods/sendMessage.ts | 8 +-- app/lib/methods/subscriptions/rooms.ts | 13 ++-- 8 files changed, 55 insertions(+), 60 deletions(-) rename app/lib/methods/helpers/{normalizeMessage.js => normalizeMessage.ts} (77%) diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index c0dbe4070..7085d1218 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -44,22 +44,22 @@ export type E2EType = 'pending' | 'done'; export interface ILastMessage { _id: string; rid: string; - tshow: boolean; - t: MessageType; - tmid: string; - msg: string; - e2e: E2EType; - ts: Date; + tshow?: boolean; + t?: MessageType; + tmid?: string; + msg?: string; + e2e?: E2EType; + ts: string | Date; u: IUserMessage; - _updatedAt: Date; - urls: string[]; - mentions: IUserMention[]; - channels: IUserChannel[]; - md: MarkdownAST; - attachments: IAttachment[]; - reactions: IReaction[]; - unread: boolean; - status: boolean; + _updatedAt: string | Date; + urls?: IUrlFromServer[]; + mentions?: IUserMention[]; + channels?: IUserChannel[]; + md?: MarkdownAST; + attachments?: IAttachment[]; + reactions?: IReaction[]; + unread?: boolean; + status?: number; } interface IMessageFile { @@ -68,38 +68,21 @@ interface IMessageFile { type: string; } -interface IMessageAttachment { - ts: string; - title: string; - title_link: string; - title_link_download: true; - image_dimensions: { - width: number; - height: number; - }; - image_preview: string; - image_url: string; - image_type: string; - image_size: number; - type: string; - description: string; -} - export interface IMessageFromServer { _id: string; rid: string; - msg: string; + msg?: string; ts: string | Date; // wm date issue u: IUserMessage; _updatedAt: string | Date; - urls: IUrlFromServer[]; - mentions: IUserMention[]; - channels: IUserChannel[]; - md: MarkdownAST; - file: IMessageFile; - files: IMessageFile[]; - groupable: false; - attachments: IMessageAttachment[]; + urls?: IUrlFromServer[]; + mentions?: IUserMention[]; + channels?: IUserChannel[]; + md?: MarkdownAST; + file?: IMessageFile; + files?: IMessageFile[]; + groupable?: boolean; + attachments?: IAttachment[]; } export interface ILoadMoreMessage { @@ -135,7 +118,7 @@ export interface IMessage extends IMessageFromServer { translations?: ITranslations[]; tmsg?: string; blocks?: any; - e2e?: string; + e2e?: E2EType; tshow?: boolean; subscription?: { id: string }; } diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index 92c095fd4..d54718a41 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -76,7 +76,7 @@ export interface ISubscription { jitsiTimeout?: number; autoTranslate?: boolean; autoTranslateLanguage?: string; - lastMessage?: ILastMessage; // TODO: we need to use IMessage here + lastMessage?: ILastMessage | null; // TODO: we need to use IMessage here hideUnreadStatus?: boolean; sysMes?: string[] | boolean; uids?: string[]; diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index eebb392d8..4e298bdaa 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -29,7 +29,7 @@ export interface IThreadResult { channels?: IUserChannel[]; replies?: string[]; tcount?: number; - status?: string; + status?: number; tlm?: string | Date; } diff --git a/app/lib/database/services/Message.ts b/app/lib/database/services/Message.ts index 60295b371..1fc44d04d 100644 --- a/app/lib/database/services/Message.ts +++ b/app/lib/database/services/Message.ts @@ -4,7 +4,10 @@ import { MESSAGES_TABLE } from '../model/Message'; const getCollection = (db: TAppDatabase) => db.get(MESSAGES_TABLE); -export const getMessageById = async (messageId: string) => { +export const getMessageById = async (messageId: string | null) => { + if (!messageId) { + return null; + } const db = database.active; const messageCollection = getCollection(db); try { diff --git a/app/lib/methods/helpers/buildMessage.ts b/app/lib/methods/helpers/buildMessage.ts index a0fc06013..96ee8ccbe 100644 --- a/app/lib/methods/helpers/buildMessage.ts +++ b/app/lib/methods/helpers/buildMessage.ts @@ -2,7 +2,7 @@ import { ILastMessage, IMessage, IThreadResult } from '../../../definitions'; import messagesStatus from '../../../constants/messagesStatus'; import normalizeMessage from './normalizeMessage'; -export default (message: Partial | IThreadResult | ILastMessage): IMessage | IThreadResult => { +export default (message: Partial | IThreadResult | ILastMessage): IMessage | IThreadResult | null => { message.status = messagesStatus.SENT; return normalizeMessage(message); }; diff --git a/app/lib/methods/helpers/normalizeMessage.js b/app/lib/methods/helpers/normalizeMessage.ts similarity index 77% rename from app/lib/methods/helpers/normalizeMessage.js rename to app/lib/methods/helpers/normalizeMessage.ts index bf34728e5..a0e7d6409 100644 --- a/app/lib/methods/helpers/normalizeMessage.js +++ b/app/lib/methods/helpers/normalizeMessage.ts @@ -1,8 +1,11 @@ import moment from 'moment'; import parseUrls from './parseUrls'; +import type { IAttachment, IMessage, IThreadResult } from '../../../definitions'; -function normalizeAttachments(msg) { +type TMsg = IMessage & IAttachment; + +function normalizeAttachments(msg: TMsg) { if (typeof msg.attachments !== typeof [] || !msg.attachments || !msg.attachments.length) { msg.attachments = []; } @@ -11,17 +14,17 @@ function normalizeAttachments(msg) { if (att.ts) { att.ts = moment(att.ts).toDate(); } - att = normalizeAttachments(att); + att = normalizeAttachments(att as TMsg); return att; }); return msg; } -export default msg => { +export default (msg: any): IMessage | IThreadResult | null => { if (!msg) { return null; } - msg = normalizeAttachments(msg); + msg = normalizeAttachments(msg as TMsg); msg.reactions = msg.reactions || []; msg.unread = msg.unread || false; // TODO: api problems @@ -30,18 +33,19 @@ export default msg => { // } else { // msg.reactions = Object.keys(msg.reactions).map(key => ({ emoji: key, usernames: msg.reactions[key].usernames.map(username => ({ value: username })) })); // } + if (!Array.isArray(msg.reactions)) { msg.reactions = Object.keys(msg.reactions).map(key => ({ _id: `${msg._id}${key}`, emoji: key, - usernames: msg.reactions[key].usernames + usernames: msg.reactions ? msg.reactions[key].usernames : [] })); } if (msg.translations && Object.keys(msg.translations).length) { msg.translations = Object.keys(msg.translations).map(key => ({ _id: `${msg._id}${key}`, language: key, - value: msg.translations[key] + value: msg.translations ? msg.translations[key] : '' })); msg.autoTranslate = true; } diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index 5fbbe317f..d41d38612 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -7,7 +7,7 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; -import { IMessage, IUser, TMessageModel } from '../../definitions'; +import { E2EType, IMessage, IUser, TMessageModel } from '../../definitions'; import sdk from '../rocketchat/services/sdk'; const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => { @@ -145,7 +145,7 @@ export default async function ( tm.u = tMessageRecord.u; tm.t = message.t; if (message.t === E2E_MESSAGE_TYPE) { - tm.e2e = E2E_STATUS.DONE; + tm.e2e = E2E_STATUS.DONE as E2EType; } }) ); @@ -170,7 +170,7 @@ export default async function ( }; tm.t = message.t; if (message.t === E2E_MESSAGE_TYPE) { - tm.e2e = E2E_STATUS.DONE; + tm.e2e = E2E_STATUS.DONE as E2EType; } }) ); @@ -203,7 +203,7 @@ export default async function ( } m.t = message.t; if (message.t === E2E_MESSAGE_TYPE) { - m.e2e = E2E_STATUS.DONE; + m.e2e = E2E_STATUS.DONE as E2EType; } }) ); diff --git a/app/lib/methods/subscriptions/rooms.ts b/app/lib/methods/subscriptions/rooms.ts index ababd496b..3e45b74c9 100644 --- a/app/lib/methods/subscriptions/rooms.ts +++ b/app/lib/methods/subscriptions/rooms.ts @@ -195,7 +195,10 @@ const createOrUpdateSubscription = async (subscription: ISubscription, room: IRo if (tmp.lastMessage && !rooms.includes(tmp.rid)) { const lastMessage = buildMessage(tmp.lastMessage); const messagesCollection = db.get('messages'); - const messageRecord = await getMessageById(lastMessage._id); + let messageRecord = {} as TMessageModel | null; + if (lastMessage) { + messageRecord = await getMessageById(lastMessage._id); + } if (messageRecord) { batch.push( @@ -206,9 +209,11 @@ const createOrUpdateSubscription = async (subscription: ISubscription, room: IRo } else { batch.push( messagesCollection.prepareCreate(m => { - m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema); - if (m.subscription) { - m.subscription.id = lastMessage.rid; + if (lastMessage) { + m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema); + if (m.subscription) { + m.subscription.id = lastMessage.rid; + } } return Object.assign(m, lastMessage); })