Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 25 additions & 42 deletions app/definitions/IMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -135,7 +118,7 @@ export interface IMessage extends IMessageFromServer {
translations?: ITranslations[];
tmsg?: string;
blocks?: any;
e2e?: string;
e2e?: E2EType;
tshow?: boolean;
subscription?: { id: string };
}
Expand Down
2 changes: 1 addition & 1 deletion app/definitions/ISubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
2 changes: 1 addition & 1 deletion app/definitions/IThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface IThreadResult {
channels?: IUserChannel[];
replies?: string[];
tcount?: number;
status?: string;
status?: number;
tlm?: string | Date;
}

Expand Down
5 changes: 4 additions & 1 deletion app/lib/database/services/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/methods/helpers/buildMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Original file line number Diff line number Diff line change
@@ -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 = [];
}
Expand All @@ -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 => {
Comment thread
gerzonc marked this conversation as resolved.
if (!msg) {
return null;
}
msg = normalizeAttachments(msg);
msg = normalizeAttachments(msg as TMsg);
msg.reactions = msg.reactions || [];
msg.unread = msg.unread || false;
// TODO: api problems
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions app/lib/methods/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
}
})
);
Expand All @@ -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;
}
})
);
Expand Down Expand Up @@ -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;
}
})
);
Expand Down
13 changes: 9 additions & 4 deletions app/lib/methods/subscriptions/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
})
Expand Down