-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* deep linking * Basic deep link working * Deep link routing * Multiple servers working * Send user to the room
- Loading branch information
1 parent
33baf35
commit 69513a8
Showing
21 changed files
with
300 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as types from './actionsTypes'; | ||
|
||
export function deepLinkingOpen(params) { | ||
return { | ||
type: types.DEEP_LINKING.OPEN, | ||
params | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { post } from './helpers/rest'; | ||
import database from '../realm'; | ||
|
||
// TODO: api fix | ||
const ddpTypes = { | ||
channel: 'c', direct: 'd', group: 'p' | ||
}; | ||
const restTypes = { | ||
channel: 'channels', direct: 'im', group: 'groups' | ||
}; | ||
|
||
async function canOpenRoomREST({ type, rid }) { | ||
try { | ||
const { token, id } = this.ddp._login; | ||
const server = this.ddp.url.replace('ws', 'http'); | ||
await post({ token, id, server }, `${ restTypes[type] }.open`, { roomId: rid }); | ||
return true; | ||
} catch (error) { | ||
// TODO: workround for 'already open for the sender' error | ||
if (!error.errorType) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
async function canOpenRoomDDP(...args) { | ||
try { | ||
const [{ type, name }] = args; | ||
await this.ddp.call('getRoomByTypeAndName', ddpTypes[type], name); | ||
return true; | ||
} catch (error) { | ||
if (error.isClientSafe) { | ||
return false; | ||
} | ||
return canOpenRoomREST.call(this, ...args); | ||
} | ||
} | ||
|
||
export default async function canOpenRoom({ rid, path }) { | ||
const { database: db } = database; | ||
const room = db.objects('subscriptions').filtered('rid == $0', rid); | ||
if (room.length) { | ||
return true; | ||
} | ||
|
||
const [type, name] = path.split('/'); | ||
// eslint-disable-next-line | ||
const data = await (this.ddp && this.ddp.status ? canOpenRoomDDP.call(this, { rid, type, name }) : canOpenRoomREST.call(this, { type, rid })); | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function(query) { | ||
return (/^[?#]/.test(query) ? query.slice(1) : query) | ||
.split('&') | ||
.reduce((params, param) => { | ||
const [key, value] = param.split('='); | ||
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : ''; | ||
return params; | ||
}, { }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { AsyncStorage } from 'react-native'; | ||
import { delay } from 'redux-saga'; | ||
import { takeLatest, take, select, call, put } from 'redux-saga/effects'; | ||
import * as types from '../actions/actionsTypes'; | ||
import { setServer, addServer } from '../actions/server'; | ||
import * as NavigationService from '../containers/routes/NavigationService'; | ||
import database from '../lib/realm'; | ||
import RocketChat from '../lib/rocketchat'; | ||
|
||
const navigate = function* go({ server, params, sameServer = true }) { | ||
const user = yield AsyncStorage.getItem(`${ RocketChat.TOKEN_KEY }-${ server }`); | ||
if (user) { | ||
const { rid, path } = params; | ||
if (rid) { | ||
const canOpenRoom = yield RocketChat.canOpenRoom({ rid, path }); | ||
if (canOpenRoom) { | ||
return yield call(NavigationService.goRoom, { rid: params.rid }); | ||
} | ||
} | ||
if (!sameServer) { | ||
yield call(NavigationService.goRoomsList); | ||
} | ||
} | ||
}; | ||
|
||
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); | ||
} | ||
|
||
const host = `https://${ params.host }`; | ||
|
||
// TODO: needs better test | ||
// if deep link is from same server | ||
if (server === host) { | ||
yield navigate({ server, params }); | ||
} 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) { | ||
// if server exists, select it | ||
yield put(setServer(servers[0].id)); | ||
yield delay(2000); | ||
yield navigate({ server: servers[0].id, params, sameServer: false }); | ||
} else { | ||
yield put(addServer(host)); | ||
} | ||
} | ||
}; | ||
|
||
const root = function* root() { | ||
yield takeLatest(types.DEEP_LINKING.OPEN, handleOpen); | ||
}; | ||
export default root; |
Oops, something went wrong.