2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2022-02-22 12:04:01 +00:00
|
|
|
import { Model } from '@nozbe/watermelondb';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
import messagesStatus from '../../constants/messagesStatus';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-12-05 20:52:08 +00:00
|
|
|
import random from '../../utils/random';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Encryption } from '../encryption';
|
2022-04-04 19:15:29 +00:00
|
|
|
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../constants';
|
2022-03-08 02:54:34 +00:00
|
|
|
import { E2EType, IMessage, IUser, TMessageModel } from '../../definitions';
|
2022-02-22 12:04:01 +00:00
|
|
|
import sdk from '../rocketchat/services/sdk';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-02-22 12:04:01 +00:00
|
|
|
const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => {
|
2019-12-13 13:35:12 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const msgCollection = db.get('messages');
|
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2022-02-22 12:04:01 +00:00
|
|
|
const successBatch: Model[] = [];
|
2019-12-13 13:35:12 +00:00
|
|
|
const messageRecord = await msgCollection.find(id);
|
|
|
|
successBatch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
messageRecord.prepareUpdate(m => {
|
2019-12-13 13:35:12 +00:00
|
|
|
m.status = status;
|
2020-03-05 16:30:13 +00:00
|
|
|
if (message) {
|
|
|
|
m.mentions = message.mentions;
|
|
|
|
m.channels = message.channels;
|
|
|
|
}
|
2019-12-13 13:35:12 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
if (tmid) {
|
|
|
|
const threadMessageRecord = await threadMessagesCollection.find(id);
|
|
|
|
successBatch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
threadMessageRecord.prepareUpdate(tm => {
|
2019-12-13 13:35:12 +00:00
|
|
|
tm.status = status;
|
2020-03-05 16:30:13 +00:00
|
|
|
if (message) {
|
|
|
|
tm.mentions = message.mentions;
|
|
|
|
tm.channels = message.channels;
|
|
|
|
}
|
2019-12-13 13:35:12 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-02-22 12:04:01 +00:00
|
|
|
await db.write(async () => {
|
2019-12-13 13:35:12 +00:00
|
|
|
await db.batch(...successBatch);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-22 12:04:01 +00:00
|
|
|
export async function sendMessageCall(message: any) {
|
2020-09-11 14:31:38 +00:00
|
|
|
const { _id, tmid } = message;
|
2018-05-23 13:39:18 +00:00
|
|
|
try {
|
2019-10-08 12:36:15 +00:00
|
|
|
// RC 0.60.0
|
2022-02-22 12:04:01 +00:00
|
|
|
// @ts-ignore
|
2020-09-11 14:31:38 +00:00
|
|
|
const result = await sdk.post('chat.sendMessage', { message });
|
2020-03-05 16:30:13 +00:00
|
|
|
if (result.success) {
|
2022-02-22 12:04:01 +00:00
|
|
|
// @ts-ignore
|
|
|
|
return changeMessageStatus(_id, messagesStatus.SENT, tmid, result.message);
|
2020-03-05 16:30:13 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2018-05-23 13:39:18 +00:00
|
|
|
}
|
2022-02-22 12:04:01 +00:00
|
|
|
return changeMessageStatus(_id, messagesStatus.ERROR, tmid);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:04:01 +00:00
|
|
|
export async function resendMessage(message: TMessageModel, tmid?: string) {
|
2020-09-11 14:31:38 +00:00
|
|
|
const db = database.active;
|
|
|
|
try {
|
2022-02-22 12:04:01 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await message.update(m => {
|
2020-09-11 14:31:38 +00:00
|
|
|
m.status = messagesStatus.TEMP;
|
|
|
|
});
|
|
|
|
});
|
2022-02-22 12:04:01 +00:00
|
|
|
const m = await Encryption.encryptMessage({
|
2020-09-11 14:31:38 +00:00
|
|
|
_id: message.id,
|
2022-02-22 12:04:01 +00:00
|
|
|
rid: message.subscription ? message.subscription.id : '',
|
|
|
|
msg: message.msg,
|
|
|
|
...(tmid && { tmid })
|
|
|
|
} as IMessage);
|
|
|
|
|
|
|
|
await sendMessageCall(m);
|
2020-09-11 14:31:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:15:29 +00:00
|
|
|
export async function sendMessage(
|
2022-03-02 14:18:01 +00:00
|
|
|
rid: string,
|
|
|
|
msg: string,
|
|
|
|
tmid: string | undefined,
|
|
|
|
user: Partial<Pick<IUser, 'id' | 'username' | 'name'>>,
|
|
|
|
tshow?: boolean
|
|
|
|
): Promise<void> {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subsCollection = db.get('subscriptions');
|
|
|
|
const msgCollection = db.get('messages');
|
|
|
|
const threadCollection = db.get('threads');
|
|
|
|
const threadMessagesCollection = db.get('thread_messages');
|
2019-10-08 12:36:15 +00:00
|
|
|
const messageId = random(17);
|
2022-02-22 12:04:01 +00:00
|
|
|
const batch: Model[] = [];
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-02-22 12:04:01 +00:00
|
|
|
const message = await Encryption.encryptMessage({
|
2021-09-13 20:41:05 +00:00
|
|
|
_id: messageId,
|
|
|
|
rid,
|
|
|
|
msg,
|
|
|
|
tmid,
|
|
|
|
tshow
|
2022-02-22 12:04:01 +00:00
|
|
|
} as IMessage);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2019-10-08 12:36:15 +00:00
|
|
|
const messageDate = new Date();
|
2022-02-22 12:04:01 +00:00
|
|
|
let tMessageRecord: TMessageModel;
|
2019-10-08 12:36:15 +00:00
|
|
|
|
|
|
|
// If it's replying to a thread
|
|
|
|
if (tmid) {
|
|
|
|
try {
|
|
|
|
// Find thread message header in Messages collection
|
|
|
|
tMessageRecord = await msgCollection.find(tmid);
|
|
|
|
batch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
tMessageRecord.prepareUpdate(m => {
|
2019-10-08 12:36:15 +00:00
|
|
|
m.tlm = messageDate;
|
2022-02-22 12:04:01 +00:00
|
|
|
if (m.tcount) {
|
|
|
|
m.tcount += 1;
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Find thread message header in Threads collection
|
|
|
|
await threadCollection.find(tmid);
|
|
|
|
} catch (error) {
|
|
|
|
// If there's no record, create one
|
|
|
|
batch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
threadCollection.prepareCreate(tm => {
|
2019-10-08 12:36:15 +00:00
|
|
|
tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema);
|
2022-02-22 12:04:01 +00:00
|
|
|
if (tm.subscription) {
|
|
|
|
tm.subscription.id = rid;
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
tm.tmid = tmid;
|
|
|
|
tm.msg = tMessageRecord.msg;
|
|
|
|
tm.ts = tMessageRecord.ts;
|
|
|
|
tm._updatedAt = messageDate;
|
|
|
|
tm.status = messagesStatus.SENT; // Original message was sent already
|
|
|
|
tm.u = tMessageRecord.u;
|
2020-09-11 14:31:38 +00:00
|
|
|
tm.t = message.t;
|
|
|
|
if (message.t === E2E_MESSAGE_TYPE) {
|
2022-03-08 02:54:34 +00:00
|
|
|
tm.e2e = E2E_STATUS.DONE as E2EType;
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the message sent in ThreadMessages collection
|
|
|
|
batch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
threadMessagesCollection.prepareCreate(tm => {
|
2019-10-08 12:36:15 +00:00
|
|
|
tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema);
|
2022-02-22 12:04:01 +00:00
|
|
|
if (tm.subscription) {
|
|
|
|
tm.subscription.id = rid;
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
tm.rid = tmid;
|
|
|
|
tm.msg = msg;
|
|
|
|
tm.ts = messageDate;
|
|
|
|
tm._updatedAt = messageDate;
|
|
|
|
tm.status = messagesStatus.TEMP;
|
|
|
|
tm.u = {
|
|
|
|
_id: user.id || '1',
|
2021-02-23 19:05:34 +00:00
|
|
|
username: user.username,
|
|
|
|
name: user.name
|
2019-10-08 12:36:15 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
tm.t = message.t;
|
|
|
|
if (message.t === E2E_MESSAGE_TYPE) {
|
2022-03-08 02:54:34 +00:00
|
|
|
tm.e2e = E2E_STATUS.DONE as E2EType;
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-10-08 12:36:15 +00:00
|
|
|
// Create the message sent in Messages collection
|
|
|
|
batch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
msgCollection.prepareCreate(m => {
|
2019-10-08 12:36:15 +00:00
|
|
|
m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema);
|
2022-02-22 12:04:01 +00:00
|
|
|
if (m.subscription) {
|
|
|
|
m.subscription.id = rid;
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
m.msg = msg;
|
|
|
|
m.ts = messageDate;
|
|
|
|
m._updatedAt = messageDate;
|
|
|
|
m.status = messagesStatus.TEMP;
|
|
|
|
m.u = {
|
|
|
|
_id: user.id || '1',
|
2021-02-23 19:05:34 +00:00
|
|
|
username: user.username,
|
|
|
|
name: user.name
|
2019-10-08 12:36:15 +00:00
|
|
|
};
|
2020-03-05 16:31:21 +00:00
|
|
|
if (tmid && tMessageRecord) {
|
2019-10-08 12:36:15 +00:00
|
|
|
m.tmid = tmid;
|
2020-10-30 17:35:07 +00:00
|
|
|
// m.tlm = messageDate; // I don't think this is necessary... leaving it commented just in case...
|
2019-10-08 12:36:15 +00:00
|
|
|
m.tmsg = tMessageRecord.msg;
|
2020-10-30 17:35:07 +00:00
|
|
|
m.tshow = tshow;
|
2019-10-08 12:36:15 +00:00
|
|
|
}
|
2020-09-11 14:31:38 +00:00
|
|
|
m.t = message.t;
|
|
|
|
if (message.t === E2E_MESSAGE_TYPE) {
|
2022-03-08 02:54:34 +00:00
|
|
|
m.e2e = E2E_STATUS.DONE as E2EType;
|
2020-09-11 14:31:38 +00:00
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2019-10-08 12:36:15 +00:00
|
|
|
const room = await subsCollection.find(rid);
|
|
|
|
if (room.draftMessage) {
|
|
|
|
batch.push(
|
2021-09-13 20:41:05 +00:00
|
|
|
room.prepareUpdate(r => {
|
2019-10-08 12:36:15 +00:00
|
|
|
r.draftMessage = null;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-11-16 11:06:29 +00:00
|
|
|
try {
|
2022-02-22 12:04:01 +00:00
|
|
|
await db.write(async () => {
|
2019-10-08 12:36:15 +00:00
|
|
|
await db.batch(...batch);
|
2018-11-16 11:06:29 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-10-08 12:36:15 +00:00
|
|
|
log(e);
|
|
|
|
return;
|
2018-11-16 11:06:29 +00:00
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
|
2022-02-22 12:04:01 +00:00
|
|
|
await sendMessageCall(message);
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|