Skip to content

Commit 62344b5

Browse files
authored
Merge pull request element-hq#2362 from vector-im/dbkr/roomdir_filter_3pnetworks
Room dir: New filtering & 3rd party networks
2 parents 7b258bc + 0db12fc commit 62344b5

5 files changed

Lines changed: 169 additions & 53 deletions

File tree

‎src/components/structures/RoomDirectory.js‎

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module.exports = React.createClass({
5353
publicRooms: [],
5454
loading: true,
5555
filterByNetwork: null,
56+
roomServer: null,
5657
}
5758
},
5859

@@ -76,10 +77,6 @@ module.exports = React.createClass({
7677
// });
7778
},
7879

79-
componentDidMount: function() {
80-
this.refreshRoomList();
81-
},
82-
8380
componentWillUnmount: function() {
8481
// dis.dispatch({
8582
// action: 'ui_opacity',
@@ -101,12 +98,16 @@ module.exports = React.createClass({
10198
if (!MatrixClientPeg.get()) return q();
10299

103100
const my_filter_string = this.filterString;
101+
const my_server = this.state.roomServer;
104102
const opts = {limit: 20};
103+
if (my_server != MatrixClientPeg.getHomeServerName()) {
104+
opts.server = my_server;
105+
}
105106
if (this.nextBatch) opts.since = this.nextBatch;
106107
if (this.filterString) opts.filter = { generic_search_term: my_filter_string } ;
107108
return MatrixClientPeg.get().publicRooms(opts).then((data) => {
108-
if (my_filter_string != this.filterString) {
109-
// if the filter has changed since this request was sent,
109+
if (my_filter_string != this.filterString || my_server != this.state.roomServer) {
110+
// if the filter or server has changed since this request was sent,
110111
// throw away the result (don't even clear the busy flag
111112
// since we must still have a request in flight)
112113
return;
@@ -120,7 +121,7 @@ module.exports = React.createClass({
120121
});
121122
return Boolean(data.next_batch);
122123
}, (err) => {
123-
if (my_filter_string != this.filterString) {
124+
if (my_filter_string != this.filterString || my_server != this.state.roomServer) {
124125
// as above: we don't care about errors for old
125126
// requests either
126127
return;
@@ -194,18 +195,23 @@ module.exports = React.createClass({
194195
}
195196
},
196197

197-
onNetworkChange: function(network) {
198+
onOptionChange: function(server, network) {
199+
// clear next batch so we don't try to load more rooms
200+
this.nextBatch = null;
198201
this.setState({
202+
// Clear the public rooms out here otherwise we needlessly
203+
// spend time filtering lots of rooms when we're about to
204+
// to clear the list anyway.
205+
publicRooms: [],
206+
roomServer: server,
199207
filterByNetwork: network,
200-
}, () => {
201-
// we just filtered out a bunch of rooms, so check to see if
202-
// we need to fill up the scrollpanel again
203-
// NB. Because we filter the results, the HS can keep giving
204-
// us more rooms and we'll keep requesting more if none match
205-
// the filter, which is pretty terrible. We need a way
206-
// to filter by network on the server.
207-
if (this.scrollPanel) this.scrollPanel.checkFillState();
208-
});
208+
}, this.refreshRoomList);
209+
// We also refresh the room list each time even though this
210+
// filtering is client-side. It hopefully won't be client side
211+
// for very long, and we may have fetched a thousand rooms to
212+
// find the five gitter ones, at which point we do not want
213+
// to render all those rooms when switching back to 'all networks'.
214+
// Easiest to just blow away the state & re-fetch.
209215
},
210216

211217
onFillRequest: function(backwards) {
@@ -295,7 +301,7 @@ module.exports = React.createClass({
295301

296302
var rooms = this.state.publicRooms.filter((a) => {
297303
if (this.state.filterByNetwork) {
298-
if (!this._isRoomInNetwork(a, this.state.filterByNetwork)) return false;
304+
if (!this._isRoomInNetwork(a, this.state.roomServer, this.state.filterByNetwork)) return false;
299305
}
300306

301307
return true;
@@ -365,14 +371,28 @@ module.exports = React.createClass({
365371
* Terrible temporary function that guess what network a public room
366372
* entry is in, until synapse is able to tell us
367373
*/
368-
_isRoomInNetwork(room, network) {
369-
if (room.aliases && this.networkPatterns[network]) {
370-
for (const alias of room.aliases) {
371-
if (this.networkPatterns[network].test(alias)) return true;
374+
_isRoomInNetwork(room, server, network) {
375+
// We carve rooms into two categories here. 'portal' rooms are
376+
// rooms created by a user joining a bridge 'portal' alias to
377+
// participate in that room or a foreign network. A room is a
378+
// portal room if it has exactly one alias and that alias matches
379+
// a pattern defined in the config. Its network is the key
380+
// of the pattern that it matches.
381+
// All other rooms are considered 'native matrix' rooms, and
382+
// go into the special '_matrix' network.
383+
384+
let roomNetwork = '_matrix';
385+
if (room.aliases && room.aliases.length == 1) {
386+
if (this.props.config.serverConfig && this.props.config.serverConfig[server] && this.props.config.serverConfig[server].networks) {
387+
for (const n of this.props.config.serverConfig[server].networks) {
388+
const pat = this.networkPatterns[n];
389+
if (pat && pat.test(room.aliases[0])) {
390+
roomNetwork = n;
391+
}
392+
}
372393
}
373394
}
374-
375-
return false;
395+
return roomNetwork == network;
376396
},
377397

378398
render: function() {
@@ -411,7 +431,7 @@ module.exports = React.createClass({
411431
className="mx_RoomDirectory_searchbox"
412432
onChange={this.onFilterChange} onClear={this.onFilterClear} onJoinClick={this.onJoinClick}
413433
/>
414-
<NetworkDropdown config={this.props.config} onNetworkChange={this.onNetworkChange} />
434+
<NetworkDropdown config={this.props.config} onOptionChange={this.onOptionChange} />
415435
</div>
416436
{content}
417437
</div>

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

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,61 @@ limitations under the License.
1515
*/
1616

1717
import React from 'react';
18+
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';
1819

1920
export default class NetworkDropdown extends React.Component {
20-
constructor() {
21-
super();
21+
constructor(props) {
22+
super(props);
2223

2324
this.dropdownRootElement = null;
2425
this.ignoreEvent = null;
2526

2627
this.onInputClick = this.onInputClick.bind(this);
2728
this.onRootClick = this.onRootClick.bind(this);
2829
this.onDocumentClick = this.onDocumentClick.bind(this);
29-
this.onNetworkClick = this.onNetworkClick.bind(this);
30+
this.onMenuOptionClick = this.onMenuOptionClick.bind(this);
31+
this.onInputKeyUp = this.onInputKeyUp.bind(this);
3032
this.collectRoot = this.collectRoot.bind(this);
33+
this.collectInputTextBox = this.collectInputTextBox.bind(this);
34+
35+
this.inputTextBox = null;
36+
37+
let defaultNetwork = null;
38+
if (
39+
this.props.config.serverConfig &&
40+
this.props.config.serverConfig[server] &&
41+
this.props.config.serverConfig[server].networks &&
42+
'_matrix' in this.props.config.serverConfig[server].networks
43+
) {
44+
defaultNetwork = '_matrix';
45+
}
3146

3247
this.state = {
3348
expanded: false,
34-
selectedNetwork: null,
49+
selectedServer: MatrixClientPeg.getHomeServerName(),
50+
selectedNetwork: defaultNetwork,
3551
};
3652
}
3753

3854
componentWillMount() {
3955
// Listen for all clicks on the document so we can close the
4056
// menu when the user clicks somewhere else
4157
document.addEventListener('click', this.onDocumentClick, false);
58+
59+
// fire this now so the defaults can be set up
60+
this.props.onOptionChange(this.state.selectedServer, this.state.selectedNetwork);
4261
}
4362

4463
componentWillUnmount() {
4564
document.removeEventListener('click', this.onDocumentClick, false);
4665
}
4766

67+
componentDidUpdate() {
68+
if (this.state.expanded && this.inputTextBox) {
69+
this.inputTextBox.focus();
70+
}
71+
}
72+
4873
onDocumentClick(ev) {
4974
// Close the dropdown if the user clicks anywhere that isn't
5075
// within our root element
@@ -72,12 +97,24 @@ export default class NetworkDropdown extends React.Component {
7297
ev.preventDefault();
7398
}
7499

75-
onNetworkClick(network, ev) {
100+
onMenuOptionClick(server, network, ev) {
76101
this.setState({
77102
expanded: false,
103+
selectedServer: server,
78104
selectedNetwork: network,
79105
});
80-
this.props.onNetworkChange(network);
106+
this.props.onOptionChange(server, network);
107+
}
108+
109+
onInputKeyUp(e) {
110+
if (e.key == 'Enter') {
111+
this.setState({
112+
expanded: false,
113+
selectedServer: e.target.value,
114+
selectedNetwork: null,
115+
});
116+
this.props.onOptionChange(e.target.value, null);
117+
}
81118
}
82119

83120
collectRoot(e) {
@@ -90,41 +127,82 @@ export default class NetworkDropdown extends React.Component {
90127
this.dropdownRootElement = e;
91128
}
92129

93-
_optionForNetwork(network, wire_onclick) {
130+
collectInputTextBox(e) {
131+
this.inputTextBox = e;
132+
}
133+
134+
_getMenuOptions() {
135+
const options = [];
136+
137+
let servers = [];
138+
if (this.props.config.servers) {
139+
servers = servers.concat(this.props.config.servers);
140+
}
141+
142+
if (servers.indexOf(MatrixClientPeg.getHomeServerName()) == -1) {
143+
servers.unshift(MatrixClientPeg.getHomeServerName());
144+
}
145+
146+
for (const server of servers) {
147+
options.push(this._makeMenuOption(server, null));
148+
if (this.props.config.serverConfig && this.props.config.serverConfig[server] && this.props.config.serverConfig[server].networks) {
149+
for (const network of this.props.config.serverConfig[server].networks) {
150+
options.push(this._makeMenuOption(server, network));
151+
}
152+
}
153+
}
154+
155+
return options;
156+
}
157+
158+
_makeMenuOption(server, network, wire_onclick) {
94159
if (wire_onclick === undefined) wire_onclick = true;
95160
let icon;
96161
let name;
97162
let span_class;
98163

99164
if (network === null) {
100-
name = 'All networks';
165+
name = server;
101166
span_class = 'mx_NetworkDropdown_menu_all';
167+
} else if (network == '_matrix') {
168+
name = 'Matrix';
169+
icon = <img src="/img/network-matrix.svg" width="16" height="16" />;
170+
span_class = 'mx_NetworkDropdown_menu_network';
102171
} else {
103172
name = this.props.config.networkNames[network];
104173
icon = <img src={this.props.config.networkIcons[network]} />;
105174
span_class = 'mx_NetworkDropdown_menu_network';
106175
}
107176

108-
const click_handler = wire_onclick ? this.onNetworkClick.bind(this, network) : null;
177+
const click_handler = wire_onclick ? this.onMenuOptionClick.bind(this, server, network) : null;
109178

110-
return <div key={network} className="mx_NetworkDropdown_networkoption" onClick={click_handler}>
179+
let key = server;
180+
if (network !== null) {
181+
key += '_' + network;
182+
}
183+
184+
return <div key={key} className="mx_NetworkDropdown_networkoption" onClick={click_handler}>
111185
{icon}
112186
<span className={span_class}>{name}</span>
113187
</div>;
114188
}
115189

116190
render() {
117-
const current_value = this._optionForNetwork(this.state.selectedNetwork, false);
191+
let current_value;
118192

119193
let menu;
120194
if (this.state.expanded) {
121-
const menu_options = [this._optionForNetwork(null)];
122-
for (const network of this.props.config.networks) {
123-
menu_options.push(this._optionForNetwork(network));
124-
}
195+
const menu_options = this._getMenuOptions();
125196
menu = <div className="mx_NetworkDropdown_menu">
126197
{menu_options}
127198
</div>;
199+
current_value = <input type="text" className="mx_NetworkDropdown_networkoption"
200+
ref={this.collectInputTextBox} onKeyUp={this.onInputKeyUp}
201+
/>
202+
} else {
203+
current_value = this._makeMenuOption(
204+
this.state.selectedServer, this.state.selectedNetwork, false
205+
);
128206
}
129207

130208
return <div className="mx_NetworkDropdown" ref={this.collectRoot}>
@@ -138,7 +216,7 @@ export default class NetworkDropdown extends React.Component {
138216
}
139217

140218
NetworkDropdown.propTypes = {
141-
onNetworkChange: React.PropTypes.func.isRequired,
219+
onOptionChange: React.PropTypes.func.isRequired,
142220
config: React.PropTypes.object,
143221
};
144222

‎src/skins/vector/css/vector-web/views/directory/NetworkDropdown.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ limitations under the License.
5252
vertical-align: middle;
5353
}
5454

55+
input.mx_NetworkDropdown_networkoption, input.mx_NetworkDropdown_networkoption:focus {
56+
border: 0;
57+
}
58+
5559
.mx_NetworkDropdown_menu {
5660
position: absolute;
5761
left: -1px;
Lines changed: 14 additions & 0 deletions
Loading

‎vector/config.sample.json‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
"integrations_rest_url": "http://localhost:5050",
77
"enableLabs": true,
88
"roomDirectory": {
9-
"networks": [
10-
"matrix:example_com",
11-
"matrix:matrix_org",
12-
"gitter",
13-
"irc:freenode",
14-
"irc:mozilla"
9+
"servers": [
10+
"matrix.org"
1511
],
12+
"serverConfig": {
13+
"matrix.org": {
14+
"networks": [
15+
"_matrix",
16+
"gitter",
17+
"irc:freenode",
18+
"irc:mozilla"
19+
]
20+
}
21+
},
1622
"networkPatterns": {
17-
"matrix:example_com": "#.*:example.com",
18-
"matrix:matrix_org": "#.*:matrix.org",
1923
"gitter": "#gitter_.*:matrix.org",
2024
"irc:freenode": "#freenode_.*:matrix.org",
2125
"irc:mozilla": "#mozilla_.*:matrix.org"
2226
},
2327
"networkNames": {
24-
"matrix:example_com": "example.com",
25-
"matrix:matrix_org": "matrix.org",
2628
"irc:freenode": "Freenode",
2729
"irc:mozilla": "Mozilla",
2830
"gitter": "Gitter"
2931
},
3032
"networkIcons": {
31-
"matrix:example_com": "//matrix.org/favicon.ico",
32-
"matrix:matrix_org": "//matrix.org/favicon.ico",
3333
"irc:freenode": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
3434
"irc:mozilla": "//matrix.org/_matrix/media/v1/download/matrix.org/DHLHpDDgWNNejFmrewvwEAHX",
3535
"gitter": "//gitter.im/favicon.ico"

0 commit comments

Comments
 (0)