2021-09-13 20:41:05 +00:00
|
|
|
import { select, takeLatest } from 'redux-saga/effects';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2022-04-07 13:22:19 +00:00
|
|
|
import Navigation from '../lib/navigation/appNavigation';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { MESSAGES } from '../actions/actionsTypes';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from '../lib/methods/helpers/log';
|
|
|
|
import { goRoom } from '../lib/methods/helpers/goRoom';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
const handleReplyBroadcast = function* handleReplyBroadcast({ message }) {
|
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { username } = message.u;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subsCollection = db.get('subscriptions');
|
2019-09-16 20:26:32 +00:00
|
|
|
const subscriptions = yield subsCollection.query(Q.where('name', username)).fetch();
|
2020-06-15 14:00:46 +00:00
|
|
|
|
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
if (subscriptions.length) {
|
2020-06-15 14:00:46 +00:00
|
|
|
goRoom({ item: subscriptions[0], isMasterDetail, message });
|
2018-05-24 20:17:45 +00:00
|
|
|
} else {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = yield Services.createDirectMessage(username);
|
2020-05-20 16:33:40 +00:00
|
|
|
if (result?.success) {
|
2020-06-15 14:00:46 +00:00
|
|
|
goRoom({ item: result?.room, isMasterDetail, message });
|
2020-05-20 16:33:40 +00:00
|
|
|
}
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +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;
|