2019-10-07 20:56:30 +00:00
|
|
|
import { takeLatest } from 'redux-saga/effects';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { MESSAGES } from '../actions/actionsTypes';
|
2017-08-17 06:28:41 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2019-10-07 20:56:30 +00:00
|
|
|
import database from '../lib/database';
|
2018-05-24 20:17:45 +00:00
|
|
|
import log from '../utils/log';
|
2017-08-17 06:28:41 +00:00
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
const goRoom = function goRoom({
|
|
|
|
rid, name, fname, message
|
|
|
|
}) {
|
2019-03-12 16:23:06 +00:00
|
|
|
Navigation.navigate('RoomsListView');
|
2019-10-07 20:56:30 +00:00
|
|
|
Navigation.navigate('RoomView', {
|
2019-12-11 23:01:12 +00:00
|
|
|
rid, name, fname, t: 'd', message
|
2019-10-07 20:56:30 +00:00
|
|
|
});
|
2018-07-10 13:40:32 +00:00
|
|
|
};
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
const handleReplyBroadcast = function* handleReplyBroadcast({ message }) {
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
const db = database.active;
|
2019-12-11 23:01:12 +00:00
|
|
|
const { username, name } = message.u;
|
2019-10-07 20:56:30 +00:00
|
|
|
const subsCollection = db.collections.get('subscriptions');
|
|
|
|
const subscriptions = yield subsCollection.query(Q.where('name', username)).fetch();
|
2018-05-24 20:17:45 +00:00
|
|
|
if (subscriptions.length) {
|
2019-12-11 23:01:12 +00:00
|
|
|
yield goRoom({
|
|
|
|
rid: subscriptions[0].rid, name: username, fname: name, message
|
|
|
|
});
|
2018-05-24 20:17:45 +00:00
|
|
|
} else {
|
|
|
|
const room = yield RocketChat.createDirectMessage(username);
|
2019-12-11 23:01:12 +00:00
|
|
|
yield goRoom({
|
|
|
|
rid: room.rid, name: username, fname: name, message
|
|
|
|
});
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
const root = function* root() {
|
2018-05-24 20:17:45 +00:00
|
|
|
yield takeLatest(MESSAGES.REPLY_BROADCAST, handleReplyBroadcast);
|
2017-08-17 06:28:41 +00:00
|
|
|
};
|
2017-11-21 14:55:50 +00:00
|
|
|
export default root;
|