Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
7 changes: 5 additions & 2 deletions app/definitions/IMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { MarkdownAST } from '@rocket.chat/message-parser';

import { IAttachment } from './IAttachment';
import { IReaction } from './IReaction';
import { SubscriptionType } from './ISubscription';

export type MessageType = 'jitsi_call_started' | 'discussion-created' | 'e2e' | 'load_more' | 'rm' | 'uj';

export interface IUserMessage {
_id: string;
Expand Down Expand Up @@ -54,8 +55,10 @@ export interface ILastMessage {
}

export interface IMessage {
_id: string;
rid?: string;
msg?: string;
t?: SubscriptionType;
t?: MessageType;
ts: Date;
u: IUserMessage;
alias: string;
Expand Down
3 changes: 2 additions & 1 deletion app/definitions/IRocketChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type TRocketChat = typeof rocketchat;

export interface IRocketChat extends TRocketChat {
sdk: any;
activeUsersSubTimeout: any;
shareSDK: any;
Comment thread
gerzonc marked this conversation as resolved.
Outdated
activeUsersSubTimeout: any;
roomsSub: any;
}
2 changes: 1 addition & 1 deletion app/definitions/ISubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface ISubscription {
ignored?: string[];
broadcast?: boolean;
prid?: string;
draftMessage?: string;
draftMessage?: string | null;
lastThreadSync?: Date;
jitsiTimeout?: number;
autoTranslate?: boolean;
Expand Down
6 changes: 3 additions & 3 deletions app/definitions/IThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import Model from '@nozbe/watermelondb/Model';
import { MarkdownAST } from '@rocket.chat/message-parser';

import { IAttachment } from './IAttachment';
import { IEditedBy, IUserChannel, IUserMention, IUserMessage } from './IMessage';
import { IEditedBy, IUserChannel, IUserMention, IUserMessage, MessageType } from './IMessage';
import { IReaction } from './IReaction';
import { SubscriptionType } from './ISubscription';

export interface IUrl {
title: string;
Expand Down Expand Up @@ -42,7 +41,7 @@ export interface IThreadResult {
export interface IThread {
id: string;
msg?: string;
t?: SubscriptionType;
t?: MessageType;
rid: string;
_updatedAt?: Date;
ts?: Date;
Expand Down Expand Up @@ -73,6 +72,7 @@ export interface IThread {
autoTranslate?: boolean;
translations?: any;
e2e?: string;
subscription?: { id: string };
}

export type TThreadModel = IThread & Model;
5 changes: 2 additions & 3 deletions app/definitions/IThreadMessage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Model from '@nozbe/watermelondb/Model';

import { IAttachment } from './IAttachment';
import { IEditedBy, ITranslations, IUserChannel, IUserMention, IUserMessage } from './IMessage';
import { IEditedBy, ITranslations, IUserChannel, IUserMention, IUserMessage, MessageType } from './IMessage';
import { IReaction } from './IReaction';
import { SubscriptionType } from './ISubscription';

export interface IThreadMessage {
msg?: string;
t?: SubscriptionType;
t?: MessageType;
rid: string;
ts: Date;
u: IUserMessage;
Expand Down
1 change: 1 addition & 0 deletions app/definitions/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Model from '@nozbe/watermelondb/Model';

export interface IUser {
_id: string;
id: string;
name?: string;
username: string;
avatarETag?: string;
Expand Down
55 changes: 36 additions & 19 deletions app/lib/methods/sendMessage.js → app/lib/methods/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import log from '../../utils/log';
import random from '../../utils/random';
import { Encryption } from '../encryption';
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants';

const changeMessageStatus = async (id, tmid, status, message) => {
import {
IMessage,
IRocketChat,
IUser,
TMessageModel,
TSubscriptionModel,
TThreadMessageModel,
TThreadModel
} from '../../definitions';

type TMessages = TMessageModel | TThreadMessageModel;

const changeMessageStatus = async (id: string, tmid: string, status: number, message?: IMessage) => {
const db = database.active;
const msgCollection = db.get('messages');
const threadMessagesCollection = db.get('thread_messages');
const successBatch = [];
const successBatch = [] as TMessages[];
Comment thread
gerzonc marked this conversation as resolved.
Outdated
const messageRecord = await msgCollection.find(id);
successBatch.push(
messageRecord.prepareUpdate(m => {
Expand All @@ -37,33 +48,33 @@ const changeMessageStatus = async (id, tmid, status, message) => {
}

try {
await db.action(async () => {
await db.write(async () => {
await db.batch(...successBatch);
});
} catch (error) {
// Do nothing
}
};

export async function sendMessageCall(message) {
export async function sendMessageCall(this: IRocketChat, message: TMessageModel) {
Comment thread
gerzonc marked this conversation as resolved.
Outdated
const { _id, tmid } = message;
try {
const sdk = this.shareSDK || this.sdk;
// RC 0.60.0
const result = await sdk.post('chat.sendMessage', { message });
if (result.success) {
return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message);
return changeMessageStatus(_id, tmid as string, messagesStatus.SENT, result.message);
Comment thread
gerzonc marked this conversation as resolved.
Outdated
}
} catch {
// do nothing
}
return changeMessageStatus(_id, tmid, messagesStatus.ERROR);
return changeMessageStatus(_id, tmid as string, messagesStatus.ERROR);
}

export async function resendMessage(message, tmid) {
export async function resendMessage(this: IRocketChat, message: TMessageModel, tmid: string) {
const db = database.active;
try {
await db.action(async () => {
await db.write(async () => {
await message.update(m => {
m.status = messagesStatus.TEMP;
});
Expand All @@ -72,12 +83,12 @@ export async function resendMessage(message, tmid) {
_id: message.id,
rid: message.subscription.id,
msg: message.msg
};
} as TMessageModel;
if (tmid) {
m = {
...m,
tmid
};
} as TMessageModel;
}
m = await Encryption.encryptMessage(m);
await sendMessageCall.call(this, m);
Expand All @@ -86,27 +97,27 @@ export async function resendMessage(message, tmid) {
}
}

export default async function (rid, msg, tmid, user, tshow) {
export default async function (this: IRocketChat, rid: string, msg: string, tmid: string, user: IUser, tshow: boolean) {
try {
const db = database.active;
const subsCollection = db.get('subscriptions');
const msgCollection = db.get('messages');
const threadCollection = db.get('threads');
const threadMessagesCollection = db.get('thread_messages');
const messageId = random(17);
const batch = [];
const batch: (TMessageModel | TThreadMessageModel | TThreadModel | TSubscriptionModel)[] = [];
Comment thread
gerzonc marked this conversation as resolved.
Outdated

let message = {
_id: messageId,
rid,
msg,
tmid,
tshow
};
} as TMessageModel;
Comment thread
gerzonc marked this conversation as resolved.
Outdated
message = await Encryption.encryptMessage(message);

const messageDate = new Date();
let tMessageRecord;
let tMessageRecord: TMessageModel;

// If it's replying to a thread
if (tmid) {
Expand All @@ -116,7 +127,9 @@ export default async function (rid, msg, tmid, user, tshow) {
batch.push(
tMessageRecord.prepareUpdate(m => {
m.tlm = messageDate;
m.tcount += 1;
if (m.tcount) {
m.tcount += 1;
}
})
);

Expand All @@ -128,7 +141,9 @@ export default async function (rid, msg, tmid, user, tshow) {
batch.push(
threadCollection.prepareCreate(tm => {
tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema);
tm.subscription.id = rid;
if (tm.subscription) {
tm.subscription.id = rid;
}
tm.tmid = tmid;
tm.msg = tMessageRecord.msg;
tm.ts = tMessageRecord.ts;
Expand All @@ -147,7 +162,9 @@ export default async function (rid, msg, tmid, user, tshow) {
batch.push(
threadMessagesCollection.prepareCreate(tm => {
tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema);
tm.subscription.id = rid;
if (tm.subscription) {
tm.subscription.id = rid;
}
tm.rid = tmid;
tm.msg = msg;
tm.ts = messageDate;
Expand Down Expand Up @@ -210,7 +227,7 @@ export default async function (rid, msg, tmid, user, tshow) {
}

try {
await db.action(async () => {
await db.write(async () => {
await db.batch(...batch);
});
} catch (e) {
Expand Down