Chore: Migrate sendMessage to TS (#3712)
Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
parent
753dec9e27
commit
e58bdb8fe7
|
@ -63,7 +63,7 @@ export interface ISubscription {
|
||||||
ignored?: string[];
|
ignored?: string[];
|
||||||
broadcast?: boolean;
|
broadcast?: boolean;
|
||||||
prid?: string;
|
prid?: string;
|
||||||
draftMessage?: string;
|
draftMessage?: string | null;
|
||||||
lastThreadSync?: Date;
|
lastThreadSync?: Date;
|
||||||
jitsiTimeout?: number;
|
jitsiTimeout?: number;
|
||||||
autoTranslate?: boolean;
|
autoTranslate?: boolean;
|
||||||
|
|
|
@ -39,7 +39,7 @@ export interface IThread {
|
||||||
t?: MessageType;
|
t?: MessageType;
|
||||||
rid: string;
|
rid: string;
|
||||||
_updatedAt?: Date;
|
_updatedAt?: Date;
|
||||||
ts?: Date;
|
ts?: string | Date;
|
||||||
u?: IUserMessage;
|
u?: IUserMessage;
|
||||||
alias?: string;
|
alias?: string;
|
||||||
parseUrls?: boolean;
|
parseUrls?: boolean;
|
||||||
|
@ -67,7 +67,7 @@ export interface IThread {
|
||||||
autoTranslate?: boolean;
|
autoTranslate?: boolean;
|
||||||
translations?: any;
|
translations?: any;
|
||||||
e2e?: string;
|
e2e?: string;
|
||||||
subscription: { id: string };
|
subscription?: { id: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TThreadModel = IThread & Model;
|
export type TThreadModel = IThread & Model;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
||||||
|
import { Model } from '@nozbe/watermelondb';
|
||||||
|
|
||||||
import messagesStatus from '../../constants/messagesStatus';
|
import messagesStatus from '../../constants/messagesStatus';
|
||||||
import database from '../database';
|
import database from '../database';
|
||||||
|
@ -6,12 +7,14 @@ import log from '../../utils/log';
|
||||||
import random from '../../utils/random';
|
import random from '../../utils/random';
|
||||||
import { Encryption } from '../encryption';
|
import { Encryption } from '../encryption';
|
||||||
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants';
|
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 db = database.active;
|
||||||
const msgCollection = db.get('messages');
|
const msgCollection = db.get('messages');
|
||||||
const threadMessagesCollection = db.get('thread_messages');
|
const threadMessagesCollection = db.get('thread_messages');
|
||||||
const successBatch = [];
|
const successBatch: Model[] = [];
|
||||||
const messageRecord = await msgCollection.find(id);
|
const messageRecord = await msgCollection.find(id);
|
||||||
successBatch.push(
|
successBatch.push(
|
||||||
messageRecord.prepareUpdate(m => {
|
messageRecord.prepareUpdate(m => {
|
||||||
|
@ -37,7 +40,7 @@ const changeMessageStatus = async (id, tmid, status, message) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await db.batch(...successBatch);
|
await db.batch(...successBatch);
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} 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;
|
const { _id, tmid } = message;
|
||||||
try {
|
try {
|
||||||
const sdk = this.shareSDK || this.sdk;
|
|
||||||
// RC 0.60.0
|
// RC 0.60.0
|
||||||
|
// @ts-ignore
|
||||||
const result = await sdk.post('chat.sendMessage', { message });
|
const result = await sdk.post('chat.sendMessage', { message });
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message);
|
// @ts-ignore
|
||||||
|
return changeMessageStatus(_id, messagesStatus.SENT, tmid, result.message);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// do nothing
|
// 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;
|
const db = database.active;
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await message.update(m => {
|
await message.update(m => {
|
||||||
m.status = messagesStatus.TEMP;
|
m.status = messagesStatus.TEMP;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
let m = {
|
const m = await Encryption.encryptMessage({
|
||||||
_id: message.id,
|
_id: message.id,
|
||||||
rid: message.subscription.id,
|
rid: message.subscription ? message.subscription.id : '',
|
||||||
msg: message.msg
|
msg: message.msg,
|
||||||
};
|
...(tmid && { tmid })
|
||||||
if (tmid) {
|
} as IMessage);
|
||||||
m = {
|
|
||||||
...m,
|
await sendMessageCall(m);
|
||||||
tmid
|
|
||||||
};
|
|
||||||
}
|
|
||||||
m = await Encryption.encryptMessage(m);
|
|
||||||
await sendMessageCall.call(this, m);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log(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 {
|
try {
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const subsCollection = db.get('subscriptions');
|
const subsCollection = db.get('subscriptions');
|
||||||
|
@ -94,19 +93,18 @@ export default async function (rid, msg, tmid, user, tshow) {
|
||||||
const threadCollection = db.get('threads');
|
const threadCollection = db.get('threads');
|
||||||
const threadMessagesCollection = db.get('thread_messages');
|
const threadMessagesCollection = db.get('thread_messages');
|
||||||
const messageId = random(17);
|
const messageId = random(17);
|
||||||
const batch = [];
|
const batch: Model[] = [];
|
||||||
|
|
||||||
let message = {
|
const message = await Encryption.encryptMessage({
|
||||||
_id: messageId,
|
_id: messageId,
|
||||||
rid,
|
rid,
|
||||||
msg,
|
msg,
|
||||||
tmid,
|
tmid,
|
||||||
tshow
|
tshow
|
||||||
};
|
} as IMessage);
|
||||||
message = await Encryption.encryptMessage(message);
|
|
||||||
|
|
||||||
const messageDate = new Date();
|
const messageDate = new Date();
|
||||||
let tMessageRecord;
|
let tMessageRecord: TMessageModel;
|
||||||
|
|
||||||
// If it's replying to a thread
|
// If it's replying to a thread
|
||||||
if (tmid) {
|
if (tmid) {
|
||||||
|
@ -116,7 +114,9 @@ export default async function (rid, msg, tmid, user, tshow) {
|
||||||
batch.push(
|
batch.push(
|
||||||
tMessageRecord.prepareUpdate(m => {
|
tMessageRecord.prepareUpdate(m => {
|
||||||
m.tlm = messageDate;
|
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(
|
batch.push(
|
||||||
threadCollection.prepareCreate(tm => {
|
threadCollection.prepareCreate(tm => {
|
||||||
tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema);
|
tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema);
|
||||||
tm.subscription.id = rid;
|
if (tm.subscription) {
|
||||||
|
tm.subscription.id = rid;
|
||||||
|
}
|
||||||
tm.tmid = tmid;
|
tm.tmid = tmid;
|
||||||
tm.msg = tMessageRecord.msg;
|
tm.msg = tMessageRecord.msg;
|
||||||
tm.ts = tMessageRecord.ts;
|
tm.ts = tMessageRecord.ts;
|
||||||
|
@ -147,7 +149,9 @@ export default async function (rid, msg, tmid, user, tshow) {
|
||||||
batch.push(
|
batch.push(
|
||||||
threadMessagesCollection.prepareCreate(tm => {
|
threadMessagesCollection.prepareCreate(tm => {
|
||||||
tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema);
|
tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema);
|
||||||
tm.subscription.id = rid;
|
if (tm.subscription) {
|
||||||
|
tm.subscription.id = rid;
|
||||||
|
}
|
||||||
tm.rid = tmid;
|
tm.rid = tmid;
|
||||||
tm.msg = msg;
|
tm.msg = msg;
|
||||||
tm.ts = messageDate;
|
tm.ts = messageDate;
|
||||||
|
@ -173,7 +177,9 @@ export default async function (rid, msg, tmid, user, tshow) {
|
||||||
batch.push(
|
batch.push(
|
||||||
msgCollection.prepareCreate(m => {
|
msgCollection.prepareCreate(m => {
|
||||||
m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema);
|
m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema);
|
||||||
m.subscription.id = rid;
|
if (m.subscription) {
|
||||||
|
m.subscription.id = rid;
|
||||||
|
}
|
||||||
m.msg = msg;
|
m.msg = msg;
|
||||||
m.ts = messageDate;
|
m.ts = messageDate;
|
||||||
m._updatedAt = messageDate;
|
m._updatedAt = messageDate;
|
||||||
|
@ -210,7 +216,7 @@ export default async function (rid, msg, tmid, user, tshow) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await db.batch(...batch);
|
await db.batch(...batch);
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -218,7 +224,7 @@ export default async function (rid, msg, tmid, user, tshow) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await sendMessageCall.call(this, message);
|
await sendMessageCall(message);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log(e);
|
log(e);
|
||||||
}
|
}
|
|
@ -105,7 +105,9 @@ export default async function updateMessages({
|
||||||
threadCollection.prepareCreate(
|
threadCollection.prepareCreate(
|
||||||
protectedFunction((t: TThreadModel) => {
|
protectedFunction((t: TThreadModel) => {
|
||||||
t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema);
|
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);
|
Object.assign(t, thread);
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
|
@ -22,7 +22,7 @@ export const capitalize = (s: string): string => {
|
||||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
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, {
|
moment(date).calendar(null, {
|
||||||
lastDay: `[${I18n.t('Yesterday')}]`,
|
lastDay: `[${I18n.t('Yesterday')}]`,
|
||||||
sameDay: 'LT',
|
sameDay: 'LT',
|
||||||
|
@ -30,7 +30,7 @@ export const formatDate = (date: Date): string =>
|
||||||
sameElse: 'L'
|
sameElse: 'L'
|
||||||
});
|
});
|
||||||
|
|
||||||
export const formatDateThreads = (date: Date): string =>
|
export const formatDateThreads = (date: string | Date): string =>
|
||||||
moment(date).calendar(null, {
|
moment(date).calendar(null, {
|
||||||
sameDay: 'LT',
|
sameDay: 'LT',
|
||||||
lastDay: `[${I18n.t('Yesterday')}] LT`,
|
lastDay: `[${I18n.t('Yesterday')}] LT`,
|
||||||
|
|
|
@ -29,6 +29,7 @@ import Header from './Header';
|
||||||
import styles from './styles';
|
import styles from './styles';
|
||||||
import { IAttachment } from './interfaces';
|
import { IAttachment } from './interfaces';
|
||||||
import { ISubscription } from '../../definitions/ISubscription';
|
import { ISubscription } from '../../definitions/ISubscription';
|
||||||
|
import { IUser } from '../../definitions';
|
||||||
|
|
||||||
interface IShareViewState {
|
interface IShareViewState {
|
||||||
selected: IAttachment;
|
selected: IAttachment;
|
||||||
|
@ -240,7 +241,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
|
||||||
|
|
||||||
// Send text message
|
// Send text message
|
||||||
} else if (text.length) {
|
} 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 {
|
} catch {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
|
|
Loading…
Reference in New Issue