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';
|
2020-01-28 13:22:35 +00:00
|
|
|
import { inviteLinksSetToken, inviteLinksRequest } from '../actions/inviteLinks';
|
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';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const roomTypes = {
|
2020-02-07 13:24:16 +00:00
|
|
|
channel: 'c', direct: 'd', group: 'p', channels: 'l'
|
2018-12-21 10:55:35 +00:00
|
|
|
};
|
|
|
|
|
2020-01-28 13:22:35 +00:00
|
|
|
const handleInviteLink = function* handleInviteLink({ params, requireLogin = false }) {
|
|
|
|
if (params.path && params.path.startsWith('invite/')) {
|
|
|
|
const token = params.path.replace('invite/', '');
|
|
|
|
if (requireLogin) {
|
|
|
|
yield put(inviteLinksSetToken(token));
|
|
|
|
} else {
|
|
|
|
yield put(inviteLinksRequest(token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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'));
|
2020-04-01 19:39:30 +00:00
|
|
|
if (params.path) {
|
|
|
|
const room = yield RocketChat.canOpenRoom(params);
|
|
|
|
const [type, name] = params.path.split('/');
|
|
|
|
if (room) {
|
2019-03-18 18:52:38 +00:00
|
|
|
yield Navigation.navigate('RoomsListView');
|
2020-04-09 05:20:57 +00:00
|
|
|
Navigation.navigate('RoomView', {
|
|
|
|
name,
|
|
|
|
t: roomTypes[type],
|
|
|
|
roomUserId: RocketChat.getUidDirectMessage(room),
|
|
|
|
...room
|
|
|
|
});
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2020-01-28 13:22:35 +00:00
|
|
|
} else {
|
|
|
|
yield handleInviteLink({ params });
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
2019-11-27 20:52:49 +00:00
|
|
|
if (server === host && user) {
|
|
|
|
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
|
|
|
}
|
2019-11-27 20:52:49 +00:00
|
|
|
yield navigate({ params });
|
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));
|
2020-01-28 13:22:35 +00:00
|
|
|
yield take(types.LOGIN.SUCCESS);
|
2019-09-16 20:26:32 +00:00
|
|
|
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
|
2019-11-27 20:52:49 +00:00
|
|
|
const result = yield RocketChat.getServerInfo(host);
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return;
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2020-03-30 19:20:50 +00:00
|
|
|
Navigation.navigate('NewServerView', { previousServer: server });
|
2019-09-16 20:26:32 +00:00
|
|
|
yield delay(1000);
|
|
|
|
EventEmitter.emit('NewServer', { server: host });
|
2019-11-27 20:52:49 +00:00
|
|
|
|
|
|
|
if (params.token) {
|
|
|
|
yield take(types.SERVER.SELECT_SUCCESS);
|
|
|
|
yield RocketChat.connect({ server: host, user: { token: params.token } });
|
2020-01-28 13:22:35 +00:00
|
|
|
} else {
|
|
|
|
yield handleInviteLink({ params, requireLogin: true });
|
2019-11-27 20:52:49 +00:00
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(types.DEEP_LINKING.OPEN, handleOpen);
|
|
|
|
};
|
|
|
|
export default root;
|