2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-08-07 13:51:34 +00:00
|
|
|
takeLatest, take, select, put, all, delay
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'redux-saga/effects';
|
2019-06-26 19:50:03 +00:00
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-05-07 20:43:26 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2018-08-10 17:26:36 +00:00
|
|
|
import { selectServerRequest } from '../actions/server';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2018-05-07 20:43:26 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-10-23 21:39:48 +00:00
|
|
|
import EventEmitter from '../utils/events';
|
2019-03-18 18:52:38 +00:00
|
|
|
import { appStart } from '../actions';
|
2019-10-05 13:56:18 +00:00
|
|
|
import { IDENTIFIER } from '../constants/credentials';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const roomTypes = {
|
|
|
|
channel: 'c', direct: 'd', group: 'p'
|
|
|
|
};
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
const navigate = function* navigate({ params }) {
|
2019-03-18 18:52:38 +00:00
|
|
|
yield put(appStart('inside'));
|
2018-07-10 13:40:32 +00:00
|
|
|
if (params.rid) {
|
|
|
|
const canOpenRoom = yield RocketChat.canOpenRoom(params);
|
|
|
|
if (canOpenRoom) {
|
2018-12-21 10:55:35 +00:00
|
|
|
const [type, name] = params.path.split('/');
|
2019-03-18 18:52:38 +00:00
|
|
|
yield Navigation.navigate('RoomsListView');
|
2019-03-12 16:23:06 +00:00
|
|
|
Navigation.navigate('RoomView', { rid: params.rid, name, t: roomTypes[type] });
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleOpen = function* handleOpen({ params }) {
|
2018-06-01 21:57:05 +00:00
|
|
|
if (!params.host) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-05 13:56:18 +00:00
|
|
|
yield RNUserDefaults.setName(IDENTIFIER);
|
2019-07-12 13:44:31 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
let { host } = params;
|
|
|
|
if (!/^(http|https)/.test(host)) {
|
|
|
|
host = `https://${ params.host }`;
|
|
|
|
}
|
|
|
|
// remove last "/" from host
|
|
|
|
if (host.slice(-1) === '/') {
|
|
|
|
host = host.slice(0, host.length - 1);
|
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2018-12-07 17:47:50 +00:00
|
|
|
const [server, user] = yield all([
|
2019-06-26 19:50:03 +00:00
|
|
|
RNUserDefaults.get('currentServer'),
|
|
|
|
RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ host }`)
|
2018-12-07 17:47:50 +00:00
|
|
|
]);
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
// TODO: needs better test
|
|
|
|
// if deep link is from same server
|
|
|
|
if (server === host) {
|
2018-12-07 17:47:50 +00:00
|
|
|
if (user) {
|
2019-03-18 18:52:38 +00:00
|
|
|
const connected = yield select(state => state.server.connected);
|
|
|
|
if (!connected) {
|
|
|
|
yield put(selectServerRequest(host));
|
|
|
|
yield take(types.SERVER.SELECT_SUCCESS);
|
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
yield navigate({ params });
|
2019-03-18 18:52:38 +00:00
|
|
|
} else {
|
|
|
|
yield put(appStart('outside'));
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-12-07 17:47:50 +00:00
|
|
|
} else {
|
2018-05-07 20:43:26 +00:00
|
|
|
// search if deep link's server already exists
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
|
|
|
try {
|
|
|
|
const servers = yield serversCollection.find(host);
|
|
|
|
if (servers && user) {
|
|
|
|
yield put(selectServerRequest(host));
|
|
|
|
yield take(types.SERVER.SELECT_SUCCESS);
|
|
|
|
yield navigate({ params });
|
2019-03-18 18:52:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
// do nothing?
|
|
|
|
}
|
|
|
|
// if deep link is from a different server
|
|
|
|
const result = yield RocketChat.getServerInfo(server);
|
|
|
|
if (!result.success) {
|
|
|
|
return;
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
Navigation.navigate('OnboardingView', { previousServer: server });
|
|
|
|
yield delay(1000);
|
|
|
|
EventEmitter.emit('NewServer', { server: host });
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(types.DEEP_LINKING.OPEN, handleOpen);
|
|
|
|
};
|
|
|
|
export default root;
|