From 18afdd843e6f2fde4ee1891b814e264986742fbd Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Wed, 1 Apr 2020 16:39:30 -0300 Subject: [PATCH] [FIX] Deeplinking without RoomId (#1925) * [FIX] Deeplinking without rid * [FIX] Join channel * [FIX] Deep linking without rid * Update app/lib/methods/canOpenRoom.js Co-authored-by: Diego Mello --- app/lib/methods/canOpenRoom.js | 71 +++++++++++++++++++++++++--------- app/sagas/deepLinking.js | 10 ++--- app/views/RoomView/index.js | 3 +- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/app/lib/methods/canOpenRoom.js b/app/lib/methods/canOpenRoom.js index 7509d6ed6..ab79d1cd3 100644 --- a/app/lib/methods/canOpenRoom.js +++ b/app/lib/methods/canOpenRoom.js @@ -4,15 +4,51 @@ const restTypes = { channel: 'channels', direct: 'im', group: 'groups' }; -async function open({ type, rid }) { +async function open({ type, rid, name }) { try { - // RC 0.61.0 - await this.sdk.post(`${ restTypes[type] }.open`, { roomId: rid }); - return true; - } catch (e) { - if (e.data && /is already open/.test(e.data.error)) { - return true; + const params = rid ? { roomId: rid } : { roomName: name }; + + // if it's a direct link without rid we'll create a new dm + // if the dm already exists it'll return the existent + if (type === 'direct' && !rid) { + const result = await this.createDirectMessage(name); + if (result.success) { + const { room } = result; + room.rid = room._id; + return room; + } } + + // if it's a group we need to check if you can open + if (type === 'group') { + try { + // RC 0.61.0 + await this.sdk.post(`${ restTypes[type] }.open`, params); + } catch (e) { + if (!(e.data && /is already open/.test(e.data.error))) { + return false; + } + } + } + + // if it's a channel or group and the link don't have rid + // we'll get info from the room + if ((type === 'channel' || type === 'group') && !rid) { + // RC 0.72.0 + const result = await this.sdk.get(`${ restTypes[type] }.info`, params); + if (result.success) { + const room = result[type]; + room.rid = room._id; + return room; + } + } + + // if rid was sent by link + if (rid) { + return { rid }; + } + return false; + } catch (e) { return false; } } @@ -21,20 +57,19 @@ export default async function canOpenRoom({ rid, path }) { try { const db = database.active; const subsCollection = db.collections.get('subscriptions'); - const [type] = path.split('/'); - if (type === 'channel') { - return true; + const [type, name] = path.split('/'); + + if (rid) { + try { + await subsCollection.find(rid); + return { rid }; + } catch (e) { + // Do nothing + } } try { - await subsCollection.find(rid); - return true; - } catch (error) { - // Do nothing - } - - try { - return await open.call(this, { type, rid }); + return await open.call(this, { type, rid, name }); } catch (e) { return false; } diff --git a/app/sagas/deepLinking.js b/app/sagas/deepLinking.js index 3a7d8f63f..86467d3e2 100644 --- a/app/sagas/deepLinking.js +++ b/app/sagas/deepLinking.js @@ -29,12 +29,12 @@ const handleInviteLink = function* handleInviteLink({ params, requireLogin = fal const navigate = function* navigate({ params }) { yield put(appStart('inside')); - if (params.rid) { - const canOpenRoom = yield RocketChat.canOpenRoom(params); - if (canOpenRoom) { - const [type, name] = params.path.split('/'); + if (params.path) { + const room = yield RocketChat.canOpenRoom(params); + const [type, name] = params.path.split('/'); + if (room) { yield Navigation.navigate('RoomsListView'); - Navigation.navigate('RoomView', { rid: params.rid, name, t: roomTypes[type] }); + Navigation.navigate('RoomView', { name, t: roomTypes[type], ...room }); } } else { yield handleInviteLink({ params }); diff --git a/app/views/RoomView/index.js b/app/views/RoomView/index.js index 41d14e6f5..47566d4a9 100644 --- a/app/views/RoomView/index.js +++ b/app/views/RoomView/index.js @@ -164,10 +164,11 @@ class RoomView extends React.Component { const selectedMessage = props.navigation.getParam('message'); const name = props.navigation.getParam('name'); const fname = props.navigation.getParam('fname'); + const prid = props.navigation.getParam('prid'); this.state = { joined: true, room: room || { - rid: this.rid, t: this.t, name, fname + rid: this.rid, t: this.t, name, fname, prid }, roomUpdate: {}, member: {},