Chore: Migrate normalizeMessage to TS (#3743)
* migrate normalizeMessage to ts * fix: missing null validations and type aliases
This commit is contained in:
parent
004f4ab0ee
commit
8decd0f4b6
|
@ -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 };
|
||||
}
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -29,7 +29,7 @@ export interface IThreadResult {
|
|||
channels?: IUserChannel[];
|
||||
replies?: string[];
|
||||
tcount?: number;
|
||||
status?: string;
|
||||
status?: number;
|
||||
tlm?: string | Date;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ILastMessage, IMessage, IThreadResult } from '../../../definitions';
|
|||
import messagesStatus from '../../../constants/messagesStatus';
|
||||
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;
|
||||
return normalizeMessage(message);
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
|
@ -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);
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue