2019-10-07 20:56:30 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
import messagesStatus from '../../constants/messagesStatus';
|
2019-10-07 20:56:30 +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
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
export const getMessage = async(rid, msg = '', tmid, user) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
const _id = random(17);
|
2019-07-29 20:44:39 +00:00
|
|
|
const { id, username } = user;
|
2018-05-23 13:39:18 +00:00
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
const db = database.active;
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
let message;
|
|
|
|
await db.action(async() => {
|
|
|
|
message = await msgCollection.create((m) => {
|
|
|
|
m._raw = sanitizedRaw({ id: _id }, msgCollection.schema);
|
|
|
|
m.subscription.id = rid;
|
|
|
|
m.msg = msg;
|
|
|
|
m.tmid = tmid;
|
|
|
|
m.ts = new Date();
|
|
|
|
m._updatedAt = new Date();
|
|
|
|
m.status = messagesStatus.TEMP;
|
|
|
|
m.u = {
|
|
|
|
_id: id || '1',
|
|
|
|
username
|
|
|
|
};
|
|
|
|
});
|
2018-05-23 13:39:18 +00:00
|
|
|
});
|
2019-10-07 20:56:30 +00:00
|
|
|
return message;
|
2018-05-23 13:39:18 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.warn('getMessage', error);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
export async function sendMessageCall(message) {
|
2019-04-17 17:01:03 +00:00
|
|
|
const {
|
2019-10-07 20:56:30 +00:00
|
|
|
id: _id, subscription: { id: rid }, msg, tmid
|
2019-04-17 17:01:03 +00:00
|
|
|
} = message;
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.60.0
|
2019-04-17 17:01:03 +00:00
|
|
|
const data = await this.sdk.post('chat.sendMessage', {
|
|
|
|
message: {
|
|
|
|
_id, rid, msg, tmid
|
|
|
|
}
|
|
|
|
});
|
2018-11-16 11:06:29 +00:00
|
|
|
return data;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 20:44:39 +00:00
|
|
|
export default async function(rid, msg, tmid, user) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
const db = database.active;
|
|
|
|
const subsCollections = db.collections.get('subscriptions');
|
|
|
|
const message = await getMessage(rid, msg, tmid, user);
|
|
|
|
if (!message) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
try {
|
|
|
|
const room = await subsCollections.find(rid);
|
|
|
|
await db.action(async() => {
|
|
|
|
await room.update((r) => {
|
|
|
|
r.draftMessage = null;
|
|
|
|
});
|
2019-04-17 17:01:03 +00:00
|
|
|
});
|
2019-10-07 20:56:30 +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-10-07 20:56:30 +00:00
|
|
|
await sendMessageCall.call(this, message);
|
|
|
|
await db.action(async() => {
|
|
|
|
await message.update((m) => {
|
|
|
|
m.status = messagesStatus.SENT;
|
|
|
|
});
|
2018-11-16 11:06:29 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-10-07 20:56:30 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
await message.update((m) => {
|
|
|
|
m.status = messagesStatus.ERROR;
|
|
|
|
});
|
2018-11-16 11:06:29 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|