Chore: Migrate sendMessage to TS (#3712)

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Gerzon Z 2022-02-22 08:04:01 -04:00 committed by GitHub
parent 753dec9e27
commit e58bdb8fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 40 deletions

View File

@ -63,7 +63,7 @@ export interface ISubscription {
ignored?: string[];
broadcast?: boolean;
prid?: string;
draftMessage?: string;
draftMessage?: string | null;
lastThreadSync?: Date;
jitsiTimeout?: number;
autoTranslate?: boolean;

View File

@ -39,7 +39,7 @@ export interface IThread {
t?: MessageType;
rid: string;
_updatedAt?: Date;
ts?: Date;
ts?: string | Date;
u?: IUserMessage;
alias?: string;
parseUrls?: boolean;
@ -67,7 +67,7 @@ export interface IThread {
autoTranslate?: boolean;
translations?: any;
e2e?: string;
subscription: { id: string };
subscription?: { id: string };
}
export type TThreadModel = IThread & Model;

View File

@ -1,4 +1,5 @@
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { Model } from '@nozbe/watermelondb';
import messagesStatus from '../../constants/messagesStatus';
import database from '../database';
@ -6,12 +7,14 @@ 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 => {
@ -37,7 +40,7 @@ const changeMessageStatus = async (id, tmid, status, message) => {
}
try {
await db.action(async () => {
await db.write(async () => {
await db.batch(...successBatch);
});
} catch (error) {
@ -45,48 +48,44 @@ const changeMessageStatus = async (id, tmid, status, message) => {
}
};
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) {
try {
const db = database.active;
const subsCollection = db.get('subscriptions');
@ -94,19 +93,18 @@ export default async function (rid, msg, tmid, user, tshow) {
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) {
@ -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;
}
})
);
@ -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;
@ -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;
@ -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;
@ -210,7 +216,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) {
@ -218,7 +224,7 @@ export default async function (rid, msg, tmid, user, tshow) {
return;
}
await sendMessageCall.call(this, message);
await sendMessageCall(message);
} catch (e) {
log(e);
}

View File

@ -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);
})
)

View File

@ -22,7 +22,7 @@ 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',
@ -30,7 +30,7 @@ export const formatDate = (date: Date): string =>
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`,

View File

@ -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;
@ -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