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';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from '../lib/userPreferences';
|
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';
|
2020-07-24 15:41:59 +00:00
|
|
|
import { selectServerRequest, serverInitAdd } 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';
|
2020-07-30 19:41:23 +00:00
|
|
|
import {
|
|
|
|
appStart, ROOT_INSIDE, ROOT_NEW_SERVER, appInit
|
|
|
|
} from '../actions/app';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { localAuthenticate } from '../utils/localAuthentication';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { goRoom } from '../utils/goRoom';
|
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 }) {
|
2020-06-15 14:00:46 +00:00
|
|
|
yield put(appStart({ root: ROOT_INSIDE }));
|
2020-04-01 19:39:30 +00:00
|
|
|
if (params.path) {
|
|
|
|
const [type, name] = params.path.split('/');
|
2020-04-09 05:27:00 +00:00
|
|
|
if (type !== 'invite') {
|
|
|
|
const room = yield RocketChat.canOpenRoom(params);
|
|
|
|
if (room) {
|
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');
|
|
|
|
}
|
|
|
|
const item = {
|
2020-04-09 05:27:00 +00:00
|
|
|
name,
|
|
|
|
t: roomTypes[type],
|
|
|
|
roomUserId: RocketChat.getUidDirectMessage(room),
|
|
|
|
...room
|
2020-06-15 14:00:46 +00:00
|
|
|
};
|
2020-07-30 17:25:52 +00:00
|
|
|
yield goRoom({ item, isMasterDetail });
|
|
|
|
|
|
|
|
if (params.isCall) {
|
2021-02-22 21:37:13 +00:00
|
|
|
RocketChat.callJitsi(item);
|
2020-07-30 17:25:52 +00:00
|
|
|
}
|
2020-04-09 05:27:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield handleInviteLink({ params });
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-30 19:41:23 +00:00
|
|
|
const fallbackNavigation = function* fallbackNavigation() {
|
|
|
|
const currentRoot = yield select(state => state.app.root);
|
|
|
|
if (currentRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
yield put(appInit());
|
|
|
|
};
|
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
const handleOpen = function* handleOpen({ params }) {
|
2020-07-30 17:25:52 +00:00
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
2018-06-01 21:57:05 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
let { host } = params;
|
2020-07-30 17:25:52 +00:00
|
|
|
if (params.isCall && !host) {
|
|
|
|
const servers = yield serversCollection.query().fetch();
|
|
|
|
// search from which server is that call
|
|
|
|
servers.forEach(({ uniqueID, id }) => {
|
|
|
|
if (params.path.includes(uniqueID)) {
|
|
|
|
host = id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-30 19:41:23 +00:00
|
|
|
// If there's no host on the deep link params and the app is opened, just call appInit()
|
2020-07-30 17:25:52 +00:00
|
|
|
if (!host) {
|
2020-07-30 19:41:23 +00:00
|
|
|
yield fallbackNavigation();
|
2020-07-30 17:25:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-30 19:41:23 +00:00
|
|
|
|
|
|
|
// If there's host, continue
|
2018-07-10 13:40:32 +00:00
|
|
|
if (!/^(http|https)/.test(host)) {
|
2020-07-30 19:41:23 +00:00
|
|
|
host = `https://${ host }`;
|
|
|
|
} else {
|
|
|
|
// Notification should always come from https
|
|
|
|
host = host.replace('http://', 'https://');
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
// 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([
|
2020-08-19 17:14:22 +00:00
|
|
|
UserPreferences.getStringAsync(RocketChat.CURRENT_SERVER),
|
|
|
|
UserPreferences.getStringAsync(`${ 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) {
|
2020-05-08 17:04:37 +00:00
|
|
|
yield localAuthenticate(host);
|
2019-11-27 20:52:49 +00:00
|
|
|
yield put(selectServerRequest(host));
|
2020-05-13 18:57:54 +00:00
|
|
|
yield take(types.LOGIN.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
|
|
|
try {
|
|
|
|
const servers = yield serversCollection.find(host);
|
|
|
|
if (servers && user) {
|
2020-05-08 17:04:37 +00:00
|
|
|
yield localAuthenticate(host);
|
2019-09-16 20:26:32 +00:00
|
|
|
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) {
|
2020-07-30 19:41:23 +00:00
|
|
|
// Fallback to prevent the app from being stuck on splash screen
|
|
|
|
yield fallbackNavigation();
|
2019-09-16 20:26:32 +00:00
|
|
|
return;
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2020-07-24 15:41:59 +00:00
|
|
|
yield put(appStart({ root: ROOT_NEW_SERVER }));
|
|
|
|
yield put(serverInitAdd(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-05-13 18:57:54 +00:00
|
|
|
yield take(types.LOGIN.SUCCESS);
|
|
|
|
yield navigate({ params });
|
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;
|