Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
8 changes: 5 additions & 3 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,9 +55,10 @@ export interface ILastMessage {
}

export interface IMessage {
_id?: string;
_id: string;
rid?: string;
msg?: string;
t?: SubscriptionType;
t?: MessageType;
ts: Date;
u: IUserMessage;
alias: string;
Expand Down
1 change: 1 addition & 0 deletions 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;
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
2 changes: 2 additions & 0 deletions app/definitions/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface IUserSettings {

export interface IUser extends IRocketChatRecord {
_id: string;
id: string;
createdAt: Date;
roles: string[];
type: string;
Expand All @@ -108,6 +109,7 @@ export interface IUser extends IRocketChatRecord {
lastLogin?: Date;
avatarOrigin?: string;
avatarETag?: string;
token: string;
utcOffset?: number;
language?: string;
statusDefault?: UserStatus;
Expand Down
54 changes: 32 additions & 22 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,16 @@ 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, TSubscriptionModel, TThreadMessageModel, TThreadModel } from '../../definitions';
import sdk from '../rocketchat/services/sdk';

const changeMessageStatus = async (id, tmid, status, message) => {
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 +41,32 @@ 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(message: TMessageModel) {
const { _id, tmid } = message;
try {
const sdk = this.shareSDK || this.sdk;
// RC 0.60.0
const result = await sdk.post('chat.sendMessage', { message });
const result = (await sdk.post('chat.sendMessage' as any, { message } as any)) as any;
Comment thread
gerzonc marked this conversation as resolved.
Outdated
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(message: TMessageModel, tmid: string) {
Comment thread
gerzonc marked this conversation as resolved.
Outdated
const db = database.active;
try {
await db.action(async () => {
await db.write(async () => {
await message.update(m => {
m.status = messagesStatus.TEMP;
});
Expand All @@ -72,41 +75,42 @@ 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);

await sendMessageCall(m);
} catch (e) {
log(e);
}
}

export default async function (rid, msg, tmid, user, tshow) {
export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IUser is wrong here.
We're actually expecting id and token only.

@gerzonc gerzonc Feb 17, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use username and name as well 👀 Maybe Partial<IUser>?

tm.u = {
    _id: user.id || '1',
    username: user.username,
    name: user.name
};

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 +120,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 +134,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 +155,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,15 +220,15 @@ export default async function (rid, msg, tmid, user, tshow) {
}

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

await sendMessageCall.call(this, message);
await sendMessageCall(message);
} catch (e) {
log(e);
}
Expand Down
3 changes: 2 additions & 1 deletion app/views/ShareView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Header from './Header';
import styles from './styles';
import { IAttachment } from './interfaces';
import { ISubscription } from '../../definitions/ISubscription';
import { IUser } from '../../definitions';

interface IShareViewState {
selected: IAttachment;
Expand Down Expand Up @@ -239,7 +240,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {

// Send text message
} else if (text.length) {
await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token });
await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser);
}
} catch {
// Do nothing
Expand Down