Chore: Migrate normalizeMessage to TS (#3743)

* migrate normalizeMessage to ts

* fix: missing null validations and type aliases
This commit is contained in:
Gerzon Z 2022-03-07 22:54:34 -04:00 committed by GitHub
parent 004f4ab0ee
commit 8decd0f4b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 60 deletions

View File

@ -44,22 +44,22 @@ export type E2EType = 'pending' | 'done';
export interface ILastMessage { export interface ILastMessage {
_id: string; _id: string;
rid: string; rid: string;
tshow: boolean; tshow?: boolean;
t: MessageType; t?: MessageType;
tmid: string; tmid?: string;
msg: string; msg?: string;
e2e: E2EType; e2e?: E2EType;
ts: Date; ts: string | Date;
u: IUserMessage; u: IUserMessage;
_updatedAt: Date; _updatedAt: string | Date;
urls: string[]; urls?: IUrlFromServer[];
mentions: IUserMention[]; mentions?: IUserMention[];
channels: IUserChannel[]; channels?: IUserChannel[];
md: MarkdownAST; md?: MarkdownAST;
attachments: IAttachment[]; attachments?: IAttachment[];
reactions: IReaction[]; reactions?: IReaction[];
unread: boolean; unread?: boolean;
status: boolean; status?: number;
} }
interface IMessageFile { interface IMessageFile {
@ -68,38 +68,21 @@ interface IMessageFile {
type: string; 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 { export interface IMessageFromServer {
_id: string; _id: string;
rid: string; rid: string;
msg: string; msg?: string;
ts: string | Date; // wm date issue ts: string | Date; // wm date issue
u: IUserMessage; u: IUserMessage;
_updatedAt: string | Date; _updatedAt: string | Date;
urls: IUrlFromServer[]; urls?: IUrlFromServer[];
mentions: IUserMention[]; mentions?: IUserMention[];
channels: IUserChannel[]; channels?: IUserChannel[];
md: MarkdownAST; md?: MarkdownAST;
file: IMessageFile; file?: IMessageFile;
files: IMessageFile[]; files?: IMessageFile[];
groupable: false; groupable?: boolean;
attachments: IMessageAttachment[]; attachments?: IAttachment[];
} }
export interface ILoadMoreMessage { export interface ILoadMoreMessage {
@ -135,7 +118,7 @@ export interface IMessage extends IMessageFromServer {
translations?: ITranslations[]; translations?: ITranslations[];
tmsg?: string; tmsg?: string;
blocks?: any; blocks?: any;
e2e?: string; e2e?: E2EType;
tshow?: boolean; tshow?: boolean;
subscription?: { id: string }; subscription?: { id: string };
} }

View File

@ -76,7 +76,7 @@ export interface ISubscription {
jitsiTimeout?: number; jitsiTimeout?: number;
autoTranslate?: boolean; autoTranslate?: boolean;
autoTranslateLanguage?: string; autoTranslateLanguage?: string;
lastMessage?: ILastMessage; // TODO: we need to use IMessage here lastMessage?: ILastMessage | null; // TODO: we need to use IMessage here
hideUnreadStatus?: boolean; hideUnreadStatus?: boolean;
sysMes?: string[] | boolean; sysMes?: string[] | boolean;
uids?: string[]; uids?: string[];

View File

@ -29,7 +29,7 @@ export interface IThreadResult {
channels?: IUserChannel[]; channels?: IUserChannel[];
replies?: string[]; replies?: string[];
tcount?: number; tcount?: number;
status?: string; status?: number;
tlm?: string | Date; tlm?: string | Date;
} }

View File

@ -4,7 +4,10 @@ import { MESSAGES_TABLE } from '../model/Message';
const getCollection = (db: TAppDatabase) => db.get(MESSAGES_TABLE); 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 db = database.active;
const messageCollection = getCollection(db); const messageCollection = getCollection(db);
try { try {

View File

@ -2,7 +2,7 @@ import { ILastMessage, IMessage, IThreadResult } from '../../../definitions';
import messagesStatus from '../../../constants/messagesStatus'; import messagesStatus from '../../../constants/messagesStatus';
import normalizeMessage from './normalizeMessage'; import normalizeMessage from './normalizeMessage';
export default (message: Partial<IMessage> | IThreadResult | ILastMessage): IMessage | IThreadResult => { export default (message: Partial<IMessage> | IThreadResult | ILastMessage): IMessage | IThreadResult | null => {
message.status = messagesStatus.SENT; message.status = messagesStatus.SENT;
return normalizeMessage(message); return normalizeMessage(message);
}; };

View File

@ -1,8 +1,11 @@
import moment from 'moment'; import moment from 'moment';
import parseUrls from './parseUrls'; 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) { if (typeof msg.attachments !== typeof [] || !msg.attachments || !msg.attachments.length) {
msg.attachments = []; msg.attachments = [];
} }
@ -11,17 +14,17 @@ function normalizeAttachments(msg) {
if (att.ts) { if (att.ts) {
att.ts = moment(att.ts).toDate(); att.ts = moment(att.ts).toDate();
} }
att = normalizeAttachments(att); att = normalizeAttachments(att as TMsg);
return att; return att;
}); });
return msg; return msg;
} }
export default msg => { export default (msg: any): IMessage | IThreadResult | null => {
if (!msg) { if (!msg) {
return null; return null;
} }
msg = normalizeAttachments(msg); msg = normalizeAttachments(msg as TMsg);
msg.reactions = msg.reactions || []; msg.reactions = msg.reactions || [];
msg.unread = msg.unread || false; msg.unread = msg.unread || false;
// TODO: api problems // TODO: api problems
@ -30,18 +33,19 @@ export default msg => {
// } else { // } else {
// msg.reactions = Object.keys(msg.reactions).map(key => ({ emoji: key, usernames: msg.reactions[key].usernames.map(username => ({ value: username })) })); // msg.reactions = Object.keys(msg.reactions).map(key => ({ emoji: key, usernames: msg.reactions[key].usernames.map(username => ({ value: username })) }));
// } // }
if (!Array.isArray(msg.reactions)) { if (!Array.isArray(msg.reactions)) {
msg.reactions = Object.keys(msg.reactions).map(key => ({ msg.reactions = Object.keys(msg.reactions).map(key => ({
_id: `${msg._id}${key}`, _id: `${msg._id}${key}`,
emoji: key, emoji: key,
usernames: msg.reactions[key].usernames usernames: msg.reactions ? msg.reactions[key].usernames : []
})); }));
} }
if (msg.translations && Object.keys(msg.translations).length) { if (msg.translations && Object.keys(msg.translations).length) {
msg.translations = Object.keys(msg.translations).map(key => ({ msg.translations = Object.keys(msg.translations).map(key => ({
_id: `${msg._id}${key}`, _id: `${msg._id}${key}`,
language: key, language: key,
value: msg.translations[key] value: msg.translations ? msg.translations[key] : ''
})); }));
msg.autoTranslate = true; msg.autoTranslate = true;
} }

View File

@ -7,7 +7,7 @@ import log from '../../utils/log';
import random from '../../utils/random'; import random from '../../utils/random';
import { Encryption } from '../encryption'; import { Encryption } from '../encryption';
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; 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'; import sdk from '../rocketchat/services/sdk';
const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => { const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => {
@ -145,7 +145,7 @@ export default async function (
tm.u = tMessageRecord.u; tm.u = tMessageRecord.u;
tm.t = message.t; tm.t = message.t;
if (message.t === E2E_MESSAGE_TYPE) { 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; tm.t = message.t;
if (message.t === E2E_MESSAGE_TYPE) { 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; m.t = message.t;
if (message.t === E2E_MESSAGE_TYPE) { if (message.t === E2E_MESSAGE_TYPE) {
m.e2e = E2E_STATUS.DONE; m.e2e = E2E_STATUS.DONE as E2EType;
} }
}) })
); );

View File

@ -195,7 +195,10 @@ const createOrUpdateSubscription = async (subscription: ISubscription, room: IRo
if (tmp.lastMessage && !rooms.includes(tmp.rid)) { if (tmp.lastMessage && !rooms.includes(tmp.rid)) {
const lastMessage = buildMessage(tmp.lastMessage); const lastMessage = buildMessage(tmp.lastMessage);
const messagesCollection = db.get('messages'); 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) { if (messageRecord) {
batch.push( batch.push(
@ -206,9 +209,11 @@ const createOrUpdateSubscription = async (subscription: ISubscription, room: IRo
} else { } else {
batch.push( batch.push(
messagesCollection.prepareCreate(m => { messagesCollection.prepareCreate(m => {
m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema); if (lastMessage) {
if (m.subscription) { m._raw = sanitizedRaw({ id: lastMessage._id }, messagesCollection.schema);
m.subscription.id = lastMessage.rid; if (m.subscription) {
m.subscription.id = lastMessage.rid;
}
} }
return Object.assign(m, lastMessage); return Object.assign(m, lastMessage);
}) })