Skip to content

Commit 761600f

Browse files
committed
Add native joining of 3p networks to room dir
Use the 3rd party location lookup API to accept third-party locations in their native form and look up the corresponding portal room for that location. Also give the network dropdown some placeholder text. Fixes element-hq#2374
1 parent 65f14c7 commit 761600f

3 files changed

Lines changed: 205 additions & 38 deletions

File tree

‎src/components/structures/RoomDirectory.js‎

Lines changed: 146 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,41 @@ module.exports = React.createClass({
5252
return {
5353
publicRooms: [],
5454
loading: true,
55-
filterByNetwork: null,
55+
network: null,
5656
roomServer: null,
57+
filterString: null,
5758
}
5859
},
5960

6061
componentWillMount: function() {
6162
// precompile Regexps
62-
this.networkPatterns = {};
63-
if (this.props.config.networkPatterns) {
64-
for (const network of Object.keys(this.props.config.networkPatterns)) {
65-
this.networkPatterns[network] = new RegExp(this.props.config.networkPatterns[network]);
63+
this.portalRoomPatterns = {};
64+
this.nativePatterns = {};
65+
if (this.props.config.networks) {
66+
for (const network of Object.keys(this.props.config.networks)) {
67+
if (this.props.config.networks[network].portalRoomPattern) {
68+
this.portalRoomPatterns[network] = new RegExp(this.props.config.networks[network].portalRoomPattern);
69+
}
70+
if (this.props.config.networks[network].nativePattern) {
71+
this.nativePatterns[network] = new RegExp(this.props.config.networks[network].nativePattern);
72+
}
6673
}
6774
}
75+
6876
this.nextBatch = null;
69-
this.filterString = null;
7077
this.filterTimeout = null;
7178
this.scrollPanel = null;
79+
this.protocols = null;
80+
81+
MatrixClientPeg.get().getThirdpartyProtocols().done((response) => {
82+
this.protocols = response;
83+
}, (err) => {
84+
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
85+
Modal.createDialog(ErrorDialog, {
86+
title: "Failed to get protocol list from Home Server",
87+
description: "The Home Server may be too old to support third party networks",
88+
});
89+
});
7290

7391
// dis.dispatch({
7492
// action: 'ui_opacity',
@@ -97,7 +115,7 @@ module.exports = React.createClass({
97115
getMoreRooms: function() {
98116
if (!MatrixClientPeg.get()) return q();
99117

100-
const my_filter_string = this.filterString;
118+
const my_filter_string = this.state.filterString;
101119
const my_server = this.state.roomServer;
102120
// remember the next batch token when we sent the request
103121
// too. If it's changed, appending to the list will corrupt it.
@@ -107,10 +125,10 @@ module.exports = React.createClass({
107125
opts.server = my_server;
108126
}
109127
if (this.nextBatch) opts.since = this.nextBatch;
110-
if (this.filterString) opts.filter = { generic_search_term: my_filter_string } ;
128+
if (this.state.filterString) opts.filter = { generic_search_term: my_filter_string } ;
111129
return MatrixClientPeg.get().publicRooms(opts).then((data) => {
112130
if (
113-
my_filter_string != this.filterString ||
131+
my_filter_string != this.state.filterString ||
114132
my_server != this.state.roomServer ||
115133
my_next_batch != this.nextBatch)
116134
{
@@ -129,7 +147,7 @@ module.exports = React.createClass({
129147
return Boolean(data.next_batch);
130148
}, (err) => {
131149
if (
132-
my_filter_string != this.filterString ||
150+
my_filter_string != this.state.filterString ||
133151
my_server != this.state.roomServer ||
134152
my_next_batch != this.nextBatch)
135153
{
@@ -215,7 +233,7 @@ module.exports = React.createClass({
215233
// to clear the list anyway.
216234
publicRooms: [],
217235
roomServer: server,
218-
filterByNetwork: network,
236+
network: network,
219237
}, this.refreshRoomList);
220238
// We also refresh the room list each time even though this
221239
// filtering is client-side. It hopefully won't be client side
@@ -232,7 +250,9 @@ module.exports = React.createClass({
232250
},
233251

234252
onFilterChange: function(alias) {
235-
this.filterString = alias || null;
253+
this.setState({
254+
filterString: alias || null,
255+
});
236256

237257
// don't send the request for a little bit,
238258
// no point hammering the server with a
@@ -248,17 +268,52 @@ module.exports = React.createClass({
248268
},
249269

250270
onFilterClear: function() {
251-
this.filterString = null;
271+
// update immediately
272+
this.setState({
273+
filterString: null,
274+
}, this.refreshRoomList);
252275

253276
if (this.filterTimeout) {
254277
clearTimeout(this.filterTimeout);
255278
}
256-
// update immediately
257-
this.refreshRoomList();
258279
},
259280

260281
onJoinClick: function(alias) {
261-
this.showRoomAlias(alias);
282+
// If we're on the 'Matrix' network (or all networks),
283+
// just show that rooms alias
284+
if (this.state.network == null || this.state.network == '_matrix') {
285+
this.showRoomAlias(alias);
286+
} else {
287+
// This is a 3rd party protocol. Let's see if we
288+
// can join it
289+
const fields = this._getFieldsForThirdPartyLocation(alias, this.state.network);
290+
if (!fields) {
291+
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
292+
Modal.createDialog(ErrorDialog, {
293+
title: "Unable to join network",
294+
description: "Riot does not know how to join a room on this network",
295+
});
296+
return;
297+
}
298+
const protocol = this._protocolForThirdPartyNetwork(this.state.network);
299+
MatrixClientPeg.get().getThirdpartyLocation(protocol, fields).done((resp) => {
300+
if (resp.length > 0 && resp[0].alias) {
301+
this.showRoomAlias(resp[0].alias);
302+
} else {
303+
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
304+
Modal.createDialog(ErrorDialog, {
305+
title: "Room not found",
306+
description: "Couldn't find a matching Matrix room",
307+
});
308+
}
309+
}, (e) => {
310+
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
311+
Modal.createDialog(ErrorDialog, {
312+
title: "Fetching third party location failed",
313+
description: "Unable to look up room ID from server",
314+
});
315+
});
316+
}
262317
},
263318

264319
showRoomAlias: function(alias) {
@@ -311,8 +366,8 @@ module.exports = React.createClass({
311366
if (!this.state.publicRooms) return [];
312367

313368
var rooms = this.state.publicRooms.filter((a) => {
314-
if (this.state.filterByNetwork) {
315-
if (!this._isRoomInNetwork(a, this.state.roomServer, this.state.filterByNetwork)) return false;
369+
if (this.state.network) {
370+
if (!this._isRoomInNetwork(a, this.state.roomServer, this.state.network)) return false;
316371
}
317372

318373
return true;
@@ -382,7 +437,7 @@ module.exports = React.createClass({
382437
* Terrible temporary function that guess what network a public room
383438
* entry is in, until synapse is able to tell us
384439
*/
385-
_isRoomInNetwork(room, server, network) {
440+
_isRoomInNetwork: function(room, server, network) {
386441
// We carve rooms into two categories here. 'portal' rooms are
387442
// rooms created by a user joining a bridge 'portal' alias to
388443
// participate in that room or a foreign network. A room is a
@@ -396,7 +451,7 @@ module.exports = React.createClass({
396451
if (room.aliases && room.aliases.length == 1) {
397452
if (this.props.config.serverConfig && this.props.config.serverConfig[server] && this.props.config.serverConfig[server].networks) {
398453
for (const n of this.props.config.serverConfig[server].networks) {
399-
const pat = this.networkPatterns[n];
454+
const pat = this.portalRoomPatterns[n];
400455
if (pat && pat.test(room.aliases[0])) {
401456
roomNetwork = n;
402457
}
@@ -406,6 +461,59 @@ module.exports = React.createClass({
406461
return roomNetwork == network;
407462
},
408463

464+
_stringLooksLikeId: function(s, network) {
465+
let pat = /^#[^\s]+:[^\s]/;
466+
if (
467+
network && network != '_matrix' &&
468+
this.nativePatterns[network]
469+
) {
470+
pat = this.nativePatterns[network];
471+
}
472+
473+
return pat.test(s);
474+
},
475+
476+
_protocolForThirdPartyNetwork: function(network) {
477+
if (
478+
this.props.config.networks &&
479+
this.props.config.networks[network] &&
480+
this.props.config.networks[network].protocol
481+
) {
482+
return this.props.config.networks[network].protocol;
483+
}
484+
},
485+
486+
_getFieldsForThirdPartyLocation: function(user_input, network) {
487+
const getfields_funcs = {
488+
irc: (user_input, network) => {
489+
if (!this.protocols.irc) return;
490+
const domain = this.props.config.networks[network].domain;
491+
// search through to make sure this is a domain actually
492+
// recognised by the HS
493+
for (const inst of this.protocols.irc.instances) {
494+
if (inst.fields && inst.fields.domain == domain) {
495+
return {
496+
domain: domain,
497+
channel: user_input,
498+
}
499+
}
500+
}
501+
return null;
502+
},
503+
gitter: (user_input, network) => {
504+
if (!this.protocols.gitter) return;
505+
return {
506+
room: user_input,
507+
}
508+
},
509+
};
510+
511+
const protocol = this._protocolForThirdPartyNetwork(network);
512+
if (getfields_funcs[protocol]) {
513+
return getfields_funcs[protocol](user_input, network);
514+
}
515+
},
516+
409517
render: function() {
410518
let content;
411519
if (this.state.loading) {
@@ -430,6 +538,23 @@ module.exports = React.createClass({
430538
</ScrollPanel>;
431539
}
432540

541+
let placeholder = 'Search for a room';
542+
if (this.state.network === null || this.state.network === '_matrix') {
543+
placeholder = '#example:' + this.state.roomServer;
544+
} else if (
545+
this.props.config.networks &&
546+
this.props.config.networks[this.state.network] &&
547+
this.props.config.networks[this.state.network].example &&
548+
this._getFieldsForThirdPartyLocation(this.state.filterString, this.state.network)
549+
) {
550+
placeholder = this.props.config.networks[this.state.network].example;
551+
}
552+
553+
const showJoinButton = (
554+
this._stringLooksLikeId(this.state.filterString, this.state.network) &&
555+
this._getFieldsForThirdPartyLocation(this.state.filterString, this.state.network)
556+
);
557+
433558
const SimpleRoomHeader = sdk.getComponent('rooms.SimpleRoomHeader');
434559
const NetworkDropdown = sdk.getComponent('directory.NetworkDropdown');
435560
const DirectorySearchBox = sdk.getComponent('elements.DirectorySearchBox');
@@ -441,6 +566,7 @@ module.exports = React.createClass({
441566
<DirectorySearchBox
442567
className="mx_RoomDirectory_searchbox"
443568
onChange={this.onFilterChange} onClear={this.onFilterClear} onJoinClick={this.onJoinClick}
569+
placeholder={placeholder} showJoinButton={showJoinButton}
444570
/>
445571
<NetworkDropdown config={this.props.config} onOptionChange={this.onOptionChange} />
446572
</div>

‎src/components/views/directory/NetworkDropdown.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class NetworkDropdown extends React.Component {
4040
this.props.config.serverConfig &&
4141
this.props.config.serverConfig[server] &&
4242
this.props.config.serverConfig[server].networks &&
43-
'_matrix' in this.props.config.serverConfig[server].networks
43+
this.props.config.serverConfig[server].networks.indexOf('_matrix') > -1
4444
) {
4545
defaultNetwork = '_matrix';
4646
}
@@ -170,8 +170,19 @@ export default class NetworkDropdown extends React.Component {
170170
icon = <img src="img/network-matrix.svg" width="16" height="16" />;
171171
span_class = 'mx_NetworkDropdown_menu_network';
172172
} else {
173-
name = this.props.config.networkNames[network];
174-
icon = <img src={this.props.config.networkIcons[network]} />;
173+
if (this.props.config.networks[network]) {
174+
if (this.props.config.networks[network].name) {
175+
name = this.props.config.networks[network].name;
176+
} else {
177+
name = network;
178+
}
179+
if (this.props.config.networks[network].icon) {
180+
icon = <img src={this.props.config.networks[network].icon} />;
181+
} else {
182+
icon = <img src={iconPath} width="16" height="16" />;
183+
}
184+
}
185+
175186
span_class = 'mx_NetworkDropdown_menu_network';
176187
}
177188

@@ -199,6 +210,7 @@ export default class NetworkDropdown extends React.Component {
199210
</div>;
200211
current_value = <input type="text" className="mx_NetworkDropdown_networkoption"
201212
ref={this.collectInputTextBox} onKeyUp={this.onInputKeyUp}
213+
placeholder="matrix.org"
202214
/>
203215
} else {
204216
current_value = this._makeMenuOption(

‎vector/config.sample.json‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,53 @@
1515
"_matrix",
1616
"gitter",
1717
"irc:freenode",
18-
"irc:mozilla"
18+
"irc:mozilla",
19+
"irc:snoonet",
20+
"irc:oftc"
1921
]
2022
}
2123
},
22-
"networkPatterns": {
23-
"gitter": "#gitter_.*:matrix.org",
24-
"irc:freenode": "#freenode_.*:matrix.org",
25-
"irc:mozilla": "#mozilla_.*:matrix.org"
26-
},
27-
"networkNames": {
28-
"irc:freenode": "Freenode",
29-
"irc:mozilla": "Mozilla",
30-
"gitter": "Gitter"
31-
},
32-
"networkIcons": {
33-
"irc:freenode": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
34-
"irc:mozilla": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
35-
"gitter": "//gitter.im/favicon.ico"
24+
"networks": {
25+
"gitter": {
26+
"protocol": "gitter",
27+
"portalRoomPattern": "#gitter_.*:matrix.org",
28+
"name": "Gitter",
29+
"icon": "//gitter.im/favicon.ico",
30+
"example": "org/community",
31+
"nativePattern": "[^\\s]+/[^\\s]+$"
32+
},
33+
"irc:freenode": {
34+
"portalRoomPattern": "#freenode_.*:matrix.org",
35+
"name": "Freenode",
36+
"icon": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
37+
"example": "#channel",
38+
"nativePattern": "^#[^\\s]+$"
39+
},
40+
"irc:mozilla": {
41+
"portalRoomPattern": "#mozilla_.*:matrix.org",
42+
"name": "Mozilla",
43+
"icon": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
44+
"example": "#channel",
45+
"nativePattern": "^#[^\\s]+$"
46+
},
47+
"irc:snoonet": {
48+
"protocol": "irc",
49+
"domain": "ipv6-irc.snoonet.org",
50+
"portalRoomPattern": "#_snoonet_.*:matrix.org",
51+
"name": "Snoonet",
52+
"icon": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
53+
"example": "#channel",
54+
"nativePattern": "^#[^\\s]+$"
55+
},
56+
"irc:oftc": {
57+
"protocol": "irc",
58+
"domain": "irc.oftc.net",
59+
"portalRoomPattern": "#_oftc_.*:matrix.org",
60+
"name": "OFTC",
61+
"icon": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
62+
"example": "#channel",
63+
"nativePattern": "^#[^\\s]+$"
64+
}
3665
}
3766
}
3867
}

0 commit comments

Comments
 (0)