2018-05-07 20:43:26 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { delay } from 'redux-saga';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2018-12-21 10:55:35 +00:00
|
|
|
takeLatest, take, select, put, all, race
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'redux-saga/effects';
|
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-11-22 18:54:38 +00:00
|
|
|
import { appStart, setStackRoot } 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-10-23 21:39:48 +00:00
|
|
|
import EventEmitter from '../utils/events';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const roomTypes = {
|
|
|
|
channel: 'c', direct: 'd', group: 'p'
|
|
|
|
};
|
|
|
|
|
2018-11-22 18:54:38 +00:00
|
|
|
const navigate = function* navigate({ params, sameServer = true }) {
|
2018-07-10 13:40:32 +00:00
|
|
|
if (!sameServer) {
|
|
|
|
yield put(appStart('inside'));
|
|
|
|
}
|
|
|
|
if (params.rid) {
|
|
|
|
const canOpenRoom = yield RocketChat.canOpenRoom(params);
|
|
|
|
if (canOpenRoom) {
|
2018-11-22 18:54:38 +00:00
|
|
|
const stack = 'RoomsListView';
|
|
|
|
const stackRoot = yield select(state => state.app.stackRoot);
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
// Make sure current stack is RoomsListView before navigate to RoomView
|
2018-11-22 18:54:38 +00:00
|
|
|
if (stackRoot !== stack) {
|
|
|
|
yield Navigation.setStackRoot('AppRoot', {
|
|
|
|
component: {
|
|
|
|
id: stack,
|
|
|
|
name: stack
|
|
|
|
}
|
|
|
|
});
|
|
|
|
yield put(setStackRoot(stack));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
yield Navigation.popToRoot(stack);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
const [type, name] = params.path.split('/');
|
2018-11-22 18:54:38 +00:00
|
|
|
Navigation.push(stack, {
|
2018-10-23 21:39:48 +00:00
|
|
|
component: {
|
|
|
|
name: 'RoomView',
|
|
|
|
passProps: {
|
2018-12-21 10:55:35 +00:00
|
|
|
rid: params.rid, name, t: roomTypes[type]
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleOpen = function* handleOpen({ params }) {
|
|
|
|
const isReady = yield select(state => state.app.ready);
|
|
|
|
|
|
|
|
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-12-07 17:47:50 +00:00
|
|
|
const [server, user] = yield all([
|
|
|
|
AsyncStorage.getItem('currentServer'),
|
|
|
|
AsyncStorage.getItem(`${ RocketChat.TOKEN_KEY }-${ host }`)
|
|
|
|
]);
|
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) {
|
2018-12-21 10:55:35 +00:00
|
|
|
yield race({
|
|
|
|
typing: take(types.SERVER.SELECT_SUCCESS),
|
|
|
|
timeout: delay(3000)
|
|
|
|
});
|
2018-07-10 13:40:32 +00:00
|
|
|
yield navigate({ params });
|
|
|
|
}
|
2018-12-07 17:47:50 +00:00
|
|
|
} else {
|
|
|
|
// if deep link is from a different server
|
2018-12-21 10:55:35 +00:00
|
|
|
const result = yield RocketChat.testServer(server);
|
|
|
|
if (!result.success) {
|
2018-12-07 17:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
// search if deep link's server already exists
|
|
|
|
const servers = yield database.databases.serversDB.objects('servers').filtered('id = $0', host); // TODO: need better test
|
2018-12-07 17:47:50 +00:00
|
|
|
if (servers.length && user) {
|
2018-08-10 17:26:36 +00:00
|
|
|
yield put(selectServerRequest(host));
|
2018-10-23 21:39:48 +00:00
|
|
|
yield navigate({ params, sameServer: false });
|
|
|
|
} else {
|
|
|
|
yield put(appStart('outside'));
|
|
|
|
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;
|