Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/definitions/ISubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface ISubscription {
ignored?: string[];
broadcast?: boolean;
prid?: string;
draftMessage?: string;
draftMessage?: string | null;
lastThreadSync?: Date;
jitsiTimeout?: number;
autoTranslate?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions app/definitions/IThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface IThread {
t?: MessageType;
rid: string;
_updatedAt?: Date;
ts?: Date;
ts?: string | Date;
u?: IUserMessage;
alias?: string;
parseUrls?: boolean;
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface IThread {
autoTranslate?: boolean;
translations?: any;
e2e?: string;
subscription: { id: string };
subscription?: { id: string };
}

export type TThreadModel = IThread & Model;
72 changes: 39 additions & 33 deletions app/lib/methods/sendMessage.js → app/lib/methods/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { Model } from '@nozbe/watermelondb';

import messagesStatus from '../../constants/messagesStatus';
import database from '../database';
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 sdk from '../rocketchat/services/sdk';

const changeMessageStatus = async (id, tmid, status, message) => {
const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => {
const db = database.active;
const msgCollection = db.get('messages');
const threadMessagesCollection = db.get('thread_messages');
const successBatch = [];
const successBatch: Model[] = [];
const messageRecord = await msgCollection.find(id);
successBatch.push(
messageRecord.prepareUpdate(m => {
Expand All @@ -37,76 +40,71 @@ 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: any) {
const { _id, tmid } = message;
try {
const sdk = this.shareSDK || this.sdk;
// RC 0.60.0
// @ts-ignore
const result = await sdk.post('chat.sendMessage', { message });
if (result.success) {
return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message);
// @ts-ignore
return changeMessageStatus(_id, messagesStatus.SENT, tmid, result.message);
}
} catch {
// do nothing
}
return changeMessageStatus(_id, tmid, messagesStatus.ERROR);
return changeMessageStatus(_id, messagesStatus.ERROR, tmid);
}

export async function resendMessage(message, tmid) {
export async function resendMessage(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;
});
});
let m = {
const m = await Encryption.encryptMessage({
_id: message.id,
rid: message.subscription.id,
msg: message.msg
};
if (tmid) {
m = {
...m,
tmid
};
}
m = await Encryption.encryptMessage(m);
await sendMessageCall.call(this, m);
rid: message.subscription ? message.subscription.id : '',
msg: message.msg,
...(tmid && { tmid })
} as IMessage);

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: Model[] = [];

let message = {
const message = await Encryption.encryptMessage({
_id: messageId,
rid,
msg,
tmid,
tshow
};
message = await Encryption.encryptMessage(message);
} as IMessage);

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

// If it's replying to a thread
if (tmid) {
Expand All @@ -116,7 +114,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 +128,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 +149,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 All @@ -173,7 +177,9 @@ export default async function (rid, msg, tmid, user, tshow) {
batch.push(
msgCollection.prepareCreate(m => {
m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema);
m.subscription.id = rid;
if (m.subscription) {
m.subscription.id = rid;
}
m.msg = msg;
m.ts = messageDate;
m._updatedAt = messageDate;
Expand Down Expand Up @@ -210,15 +216,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
4 changes: 3 additions & 1 deletion app/lib/methods/updateMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ export default async function updateMessages({
threadCollection.prepareCreate(
protectedFunction((t: TThreadModel) => {
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
t.subscription.id = sub.id;
if (t.subscription) {
t.subscription.id = sub.id;
}
Object.assign(t, thread);
})
)
Expand Down
4 changes: 2 additions & 2 deletions app/utils/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export const capitalize = (s: string): string => {
return s.charAt(0).toUpperCase() + s.slice(1);
};

export const formatDate = (date: Date): string =>
export const formatDate = (date: string | Date): string =>
moment(date).calendar(null, {
lastDay: `[${I18n.t('Yesterday')}]`,
sameDay: 'LT',
lastWeek: 'dddd',
sameElse: 'L'
});

export const formatDateThreads = (date: Date): string =>
export const formatDateThreads = (date: string | Date): string =>
moment(date).calendar(null, {
sameDay: 'LT',
lastDay: `[${I18n.t('Yesterday')}] LT`,
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 @@ -240,7 +241,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