2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
|
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';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2020-03-02 20:12:41 +00:00
|
|
|
const changeMessageStatus = async(id, tmid, status, message) => {
|
2019-12-13 13:35:12 +00:00
|
|
|
const db = database.active;
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
const threadMessagesCollection = db.collections.get('thread_messages');
|
|
|
|
const successBatch = [];
|
|
|
|
const messageRecord = await msgCollection.find(id);
|
|
|
|
successBatch.push(
|
|
|
|
messageRecord.prepareUpdate((m) => {
|
|
|
|
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(
|
|
|
|
threadMessageRecord.prepareUpdate((tm) => {
|
|
|
|
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 {
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(...successBatch);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-08 12:36:15 +00:00
|
|
|
export async function sendMessageCall(message) {
|
|
|
|
const {
|
|
|
|
id: _id, subscription: { id: rid }, msg, tmid
|
|
|
|
} = message;
|
2018-05-23 13:39:18 +00:00
|
|
|
try {
|
2019-12-04 16:41:37 +00:00
|
|
|
const sdk = this.shareSDK || this.sdk;
|
2019-10-08 12:36:15 +00:00
|
|
|
// RC 0.60.0
|
2020-03-02 20:12:41 +00:00
|
|
|
const result = await sdk.post('chat.sendMessage', {
|
2019-10-08 12:36:15 +00:00
|
|
|
message: {
|
|
|
|
_id, rid, msg, tmid
|
|
|
|
}
|
|
|
|
});
|
2020-03-05 16:30:13 +00:00
|
|
|
if (result.success) {
|
|
|
|
return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2018-05-23 13:39:18 +00:00
|
|
|
}
|
2020-03-05 16:30:13 +00:00
|
|
|
return changeMessageStatus(_id, tmid, messagesStatus.ERROR);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:33:28 +00:00
|
|
|
export default async function(rid, msg, tmid, user) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2019-10-08 12:36:15 +00:00
|
|
|
const subsCollection = db.collections.get('subscriptions');
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
const threadCollection = db.collections.get('threads');
|
|
|
|
const threadMessagesCollection = db.collections.get('thread_messages');
|
|
|
|
const messageId = random(17);
|
|
|
|
const batch = [];
|
|
|
|
const message = {
|
|
|
|
id: messageId, subscription: { id: rid }, msg, tmid
|
|
|
|
};
|
|
|
|
const messageDate = new Date();
|
|
|
|
let tMessageRecord;
|
|
|
|
|
|
|
|
// If it's replying to a thread
|
|
|
|
if (tmid) {
|
|
|
|
try {
|
|
|
|
// Find thread message header in Messages collection
|
|
|
|
tMessageRecord = await msgCollection.find(tmid);
|
|
|
|
batch.push(
|
|
|
|
tMessageRecord.prepareUpdate((m) => {
|
|
|
|
m.tlm = messageDate;
|
|
|
|
m.tcount += 1;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Find thread message header in Threads collection
|
|
|
|
await threadCollection.find(tmid);
|
|
|
|
} catch (error) {
|
|
|
|
// If there's no record, create one
|
|
|
|
batch.push(
|
|
|
|
threadCollection.prepareCreate((tm) => {
|
|
|
|
tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema);
|
|
|
|
tm.subscription.id = rid;
|
|
|
|
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;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the message sent in ThreadMessages collection
|
|
|
|
batch.push(
|
|
|
|
threadMessagesCollection.prepareCreate((tm) => {
|
|
|
|
tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema);
|
|
|
|
tm.subscription.id = rid;
|
|
|
|
tm.rid = tmid;
|
|
|
|
tm.msg = msg;
|
|
|
|
tm.ts = messageDate;
|
|
|
|
tm._updatedAt = messageDate;
|
|
|
|
tm.status = messagesStatus.TEMP;
|
|
|
|
tm.u = {
|
|
|
|
_id: user.id || '1',
|
|
|
|
username: user.username
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} 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(
|
|
|
|
msgCollection.prepareCreate((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema);
|
|
|
|
m.subscription.id = rid;
|
|
|
|
m.msg = msg;
|
|
|
|
m.ts = messageDate;
|
|
|
|
m._updatedAt = messageDate;
|
|
|
|
m.status = messagesStatus.TEMP;
|
|
|
|
m.u = {
|
|
|
|
_id: user.id || '1',
|
|
|
|
username: user.username
|
|
|
|
};
|
2020-03-05 16:31:21 +00:00
|
|
|
if (tmid && tMessageRecord) {
|
2019-10-08 12:36:15 +00:00
|
|
|
m.tmid = tmid;
|
|
|
|
m.tlm = messageDate;
|
|
|
|
m.tmsg = tMessageRecord.msg;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
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(
|
|
|
|
room.prepareUpdate((r) => {
|
|
|
|
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 {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.action(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
|
|
|
|
|
|
|
await sendMessageCall.call(this, 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
|
|
|
}
|
|
|
|
}
|