2021-09-13 20:41:05 +00:00
|
|
|
import { all, delay, put, select, take, takeLatest } from 'redux-saga/effects';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2022-04-07 13:13:19 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
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';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { inviteLinksRequest, inviteLinksSetToken } from '../actions/inviteLinks';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../lib/methods/helpers/events';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { appInit, appStart } from '../actions/app';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { localAuthenticate } from '../lib/methods/helpers/localAuthentication';
|
|
|
|
import { goRoom } from '../lib/methods/helpers/goRoom';
|
|
|
|
import { getUidDirectMessage } from '../lib/methods/helpers';
|
2021-03-31 21:01:20 +00:00
|
|
|
import { loginRequest } from '../actions/login';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from '../lib/methods/helpers/log';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { RootEnum } from '../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { CURRENT_SERVER, TOKEN_KEY } from '../lib/constants';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { callJitsi, callJitsiWithoutServer, canOpenRoom } from '../lib/methods';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const roomTypes = {
|
2021-09-13 20:41:05 +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 }) {
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_INSIDE }));
|
2021-03-31 21:01:20 +00:00
|
|
|
if (params.path || params.rid) {
|
|
|
|
let type;
|
|
|
|
let name;
|
2021-10-26 16:11:50 +00:00
|
|
|
let jumpToThreadId;
|
2021-03-31 21:01:20 +00:00
|
|
|
if (params.path) {
|
2021-10-26 16:11:50 +00:00
|
|
|
// Following this pattern: {channelType}/{channelName}/thread/{threadId}
|
|
|
|
[type, name, , jumpToThreadId] = params.path.split('/');
|
2021-03-31 21:01:20 +00:00
|
|
|
}
|
|
|
|
if (type !== 'invite' || params.rid) {
|
2022-04-28 20:37:25 +00:00
|
|
|
const room = yield canOpenRoom(params);
|
2020-04-09 05:27:00 +00:00
|
|
|
if (room) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const item = {
|
2020-04-09 05:27:00 +00:00
|
|
|
name,
|
|
|
|
t: roomTypes[type],
|
2022-04-28 20:37:25 +00:00
|
|
|
roomUserId: getUidDirectMessage(room),
|
2020-04-09 05:27:00 +00:00
|
|
|
...room
|
2020-06-15 14:00:46 +00:00
|
|
|
};
|
2021-03-05 16:10:21 +00:00
|
|
|
|
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
2021-05-26 17:24:54 +00:00
|
|
|
const jumpToMessageId = params.messageId;
|
2021-03-05 16:10:21 +00:00
|
|
|
|
2022-11-25 13:21:56 +00:00
|
|
|
yield goRoom({ item, isMasterDetail, jumpToMessageId, jumpToThreadId, popToRoot: true });
|
2020-07-30 17:25:52 +00:00
|
|
|
if (params.isCall) {
|
2022-04-28 20:37:25 +00:00
|
|
|
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());
|
|
|
|
};
|
|
|
|
|
2021-06-07 17:27:15 +00:00
|
|
|
const handleOAuth = function* handleOAuth({ params }) {
|
|
|
|
const { credentialToken, credentialSecret } = params;
|
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
yield Services.loginOAuthOrSso({ oauth: { credentialToken, credentialSecret } }, false);
|
2021-06-07 17:27:15 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.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;
|
|
|
|
}
|
|
|
|
});
|
2021-11-16 16:04:33 +00:00
|
|
|
|
|
|
|
if (!host && params.fullURL) {
|
2022-04-28 20:37:25 +00:00
|
|
|
callJitsiWithoutServer(params.fullURL);
|
2021-11-16 16:04:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-30 17:25:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 17:27:15 +00:00
|
|
|
if (params.type === 'oauth') {
|
|
|
|
yield handleOAuth({ params });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2021-03-31 21:01:20 +00:00
|
|
|
if (/^localhost(:\d+)?/.test(host)) {
|
2021-09-13 20:41:05 +00:00
|
|
|
host = `http://${host}`;
|
2021-03-31 21:01:20 +00:00
|
|
|
} else {
|
2021-09-13 20:41:05 +00:00
|
|
|
host = `https://${host}`;
|
2021-03-31 21:01:20 +00:00
|
|
|
}
|
2020-07-30 19:41:23 +00:00
|
|
|
} 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([
|
2022-04-28 20:37:25 +00:00
|
|
|
UserPreferences.getString(CURRENT_SERVER),
|
|
|
|
UserPreferences.getString(`${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 {
|
2021-03-05 16:10:21 +00:00
|
|
|
const hostServerRecord = yield serversCollection.find(host);
|
|
|
|
if (hostServerRecord && user) {
|
2020-05-08 17:04:37 +00:00
|
|
|
yield localAuthenticate(host);
|
2021-03-05 16:10:21 +00:00
|
|
|
yield put(selectServerRequest(host, hostServerRecord.version, true, true));
|
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
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = yield Services.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
|
|
|
}
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
2020-07-24 15:41:59 +00:00
|
|
|
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);
|
2021-03-31 21:01:20 +00:00
|
|
|
yield put(loginRequest({ resume: params.token }, true));
|
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;
|