2018-05-07 20:43:26 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { takeLatest, take, select, put } from 'redux-saga/effects';
|
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
import * as types from '../actions/actionsTypes';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { appStart } from '../actions';
|
2018-08-10 17:26:36 +00:00
|
|
|
import { selectServerRequest } from '../actions/server';
|
2018-05-07 20:43:26 +00:00
|
|
|
import database from '../lib/realm';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { NavigationActions } from '../Navigation';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
const navigate = function* go({ params, sameServer = true }) {
|
|
|
|
if (!sameServer) {
|
|
|
|
yield put(appStart('inside'));
|
|
|
|
}
|
|
|
|
if (params.rid) {
|
|
|
|
const canOpenRoom = yield RocketChat.canOpenRoom(params);
|
|
|
|
if (canOpenRoom) {
|
|
|
|
return NavigationActions.push({
|
|
|
|
screen: 'RoomView',
|
2018-08-31 18:13:30 +00:00
|
|
|
backButtonTitle: '',
|
2018-07-10 13:40:32 +00:00
|
|
|
passProps: {
|
|
|
|
rid: params.rid
|
|
|
|
}
|
|
|
|
});
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleOpen = function* handleOpen({ params }) {
|
|
|
|
const isReady = yield select(state => state.app.ready);
|
|
|
|
const server = yield select(state => state.server.server);
|
|
|
|
|
|
|
|
if (!isReady) {
|
|
|
|
yield take(types.APP.READY);
|
|
|
|
}
|
|
|
|
|
2018-06-01 21:57:05 +00:00
|
|
|
if (!params.host) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-06-01 21:57:05 +00:00
|
|
|
try {
|
|
|
|
yield RocketChat.testServer(host);
|
|
|
|
} catch (error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
const token = yield AsyncStorage.getItem(`${ RocketChat.TOKEN_KEY }-${ host }`);
|
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
// TODO: needs better test
|
|
|
|
// if deep link is from same server
|
|
|
|
if (server === host) {
|
2018-07-10 13:40:32 +00:00
|
|
|
if (token) {
|
|
|
|
yield navigate({ params });
|
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
} else { // if deep link is from a different server
|
|
|
|
// search if deep link's server already exists
|
|
|
|
const servers = yield database.databases.serversDB.objects('servers').filtered('id = $0', host); // TODO: need better test
|
|
|
|
if (servers.length) {
|
2018-07-10 13:40:32 +00:00
|
|
|
const deepLinkServer = servers[0].id;
|
|
|
|
if (!token) {
|
|
|
|
yield put(appStart('outside'));
|
|
|
|
} else {
|
2018-08-01 19:35:06 +00:00
|
|
|
yield put(selectServerRequest(deepLinkServer));
|
2018-07-10 13:40:32 +00:00
|
|
|
yield take(types.METEOR.REQUEST);
|
|
|
|
yield navigate({ params, sameServer: false });
|
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
} else {
|
2018-08-10 17:26:36 +00:00
|
|
|
yield put(selectServerRequest(host));
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(types.DEEP_LINKING.OPEN, handleOpen);
|
|
|
|
};
|
|
|
|
export default root;
|