Skip to content

Commit 4720da3

Browse files
authored
Merge pull request element-hq#2241 from vector-im/dbkr/paginate_publicrooms
Paginate Room Directory
2 parents e2b3fc3 + 53fd3f5 commit 4720da3

3 files changed

Lines changed: 121 additions & 50 deletions

File tree

‎src/components/structures/RoomDirectory.js‎

Lines changed: 115 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var linkify = require('linkifyjs');
2929
var linkifyString = require('linkifyjs/string');
3030
var linkifyMatrix = require('matrix-react-sdk/lib/linkify-matrix');
3131
var sanitizeHtml = require('sanitize-html');
32+
var q = require('q');
3233

3334
linkifyMatrix(linkify);
3435

@@ -50,7 +51,6 @@ module.exports = React.createClass({
5051
getInitialState: function() {
5152
return {
5253
publicRooms: [],
53-
roomAlias: '',
5454
loading: true,
5555
filterByNetwork: null,
5656
}
@@ -64,6 +64,10 @@ module.exports = React.createClass({
6464
this.networkPatterns[network] = new RegExp(this.props.config.networkPatterns[network]);
6565
}
6666
}
67+
this.nextBatch = null;
68+
this.filterString = null;
69+
this.filterTimeout = null;
70+
this.scrollPanel = null;
6771

6872
// dis.dispatch({
6973
// action: 'ui_opacity',
@@ -73,7 +77,7 @@ module.exports = React.createClass({
7377
},
7478

7579
componentDidMount: function() {
76-
this.getPublicRooms();
80+
this.refreshRoomList();
7781
},
7882

7983
componentWillUnmount: function() {
@@ -84,24 +88,48 @@ module.exports = React.createClass({
8488
// });
8589
},
8690

87-
getPublicRooms: function() {
88-
var self = this;
89-
MatrixClientPeg.get().publicRooms(function (err, data) {
90-
if (err) {
91-
self.setState({ loading: false });
92-
console.error("Failed to get publicRooms: %s", JSON.stringify(err));
93-
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
94-
Modal.createDialog(ErrorDialog, {
95-
title: "Failed to get public room list",
96-
description: err.message
97-
});
91+
refreshRoomList: function() {
92+
this.nextBatch = null;
93+
this.setState({
94+
publicRooms: [],
95+
loading: true,
96+
});
97+
this.getMoreRooms().done();
98+
},
99+
100+
getMoreRooms: function() {
101+
const my_filter_string = this.filterString;
102+
const opts = {limit: 20};
103+
if (this.nextBatch) opts.since = this.nextBatch;
104+
if (this.filterString) opts.filter = { generic_search_term: my_filter_string } ;
105+
return MatrixClientPeg.get().publicRooms(opts).then((data) => {
106+
if (my_filter_string != this.filterString) {
107+
// if the filter has changed since this request was sent,
108+
// throw away the result (don't even clear the busy flag
109+
// since we must still have a request in flight)
110+
return;
98111
}
99-
else {
100-
self.setState({
101-
publicRooms: data.chunk,
102-
loading: false,
103-
});
112+
113+
this.nextBatch = data.next_batch;
114+
this.setState((s) => {
115+
s.publicRooms.push(...data.chunk);
116+
s.loading = false;
117+
return s;
118+
});
119+
return Boolean(data.next_batch);
120+
}, (err) => {
121+
if (my_filter_string != this.filterString) {
122+
// as above: we don't care about errors for old
123+
// requests either
124+
return;
104125
}
126+
this.setState({ loading: false });
127+
console.error("Failed to get publicRooms: %s", JSON.stringify(err));
128+
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
129+
Modal.createDialog(ErrorDialog, {
130+
title: "Failed to get public room list",
131+
description: err.message
132+
});
105133
});
106134
},
107135

@@ -142,10 +170,10 @@ module.exports = React.createClass({
142170
return MatrixClientPeg.get().deleteAlias(alias);
143171
}).done(() => {
144172
modal.close();
145-
this.getPublicRooms();
173+
this.refreshRoomList();
146174
}, function(err) {
147175
modal.close();
148-
this.getPublicRooms();
176+
this.refreshRoomList();
149177
Modal.createDialog(ErrorDialog, {
150178
title: "Failed to "+step,
151179
description: err.toString()
@@ -167,9 +195,47 @@ module.exports = React.createClass({
167195
onNetworkChange: function(network) {
168196
this.setState({
169197
filterByNetwork: network,
198+
}, () => {
199+
// we just filtered out a bunch of rooms, so check to see if
200+
// we need to fill up the scrollpanel again
201+
// NB. Because we filter the results, the HS can keep giving
202+
// us more rooms and we'll keep requesting more if none match
203+
// the filter, which is pretty terrible. We need a way
204+
// to filter by network on the server.
205+
if (this.scrollPanel) this.scrollPanel.checkFillState();
170206
});
171207
},
172208

209+
onFillRequest: function(backwards) {
210+
if (backwards || !this.nextBatch) return q(false);
211+
212+
return this.getMoreRooms();
213+
},
214+
215+
onFilterChange: function(ev) {
216+
const alias = ev.target.value;
217+
218+
this.filterString = alias || null;
219+
220+
// don't send the request for a little bit,
221+
// no point hammering the server with a
222+
// request for every keystroke, let the
223+
// user finish typing.
224+
if (this.filterTimeout) {
225+
clearTimeout(this.filterTimeout);
226+
}
227+
this.filterTimeout = setTimeout(() => {
228+
this.filterTimeout = null;
229+
this.refreshRoomList();
230+
}, 300);
231+
},
232+
233+
onFilterKeyUp: function(ev) {
234+
if (ev.key == "Enter") {
235+
this.showRoomAlias(ev.target.value);
236+
}
237+
},
238+
173239
showRoomAlias: function(alias) {
174240
this.showRoom(null, alias);
175241
},
@@ -214,23 +280,17 @@ module.exports = React.createClass({
214280
dis.dispatch(payload);
215281
},
216282

217-
getRows: function(filter) {
283+
getRows: function() {
218284
var BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
219285

220286
if (!this.state.publicRooms) return [];
221287

222288
var rooms = this.state.publicRooms.filter((a) => {
223-
// FIXME: if incrementally typing, keep narrowing down the search set
224-
// incrementally rather than starting over each time.
225289
if (this.state.filterByNetwork) {
226290
if (!this._isRoomInNetwork(a, this.state.filterByNetwork)) return false;
227291
}
228292

229-
return (((a.name && a.name.toLowerCase().search(filter.toLowerCase()) >= 0) ||
230-
(a.aliases && a.aliases[0].toLowerCase().search(filter.toLowerCase()) >= 0)) &&
231-
a.num_joined_members > 0);
232-
}).sort(function(a,b) {
233-
return a.num_joined_members - b.num_joined_members;
293+
return true;
234294
});
235295
var rows = [];
236296
var self = this;
@@ -259,7 +319,7 @@ module.exports = React.createClass({
259319
var topic = rooms[i].topic || '';
260320
topic = linkifyString(sanitizeHtml(topic));
261321

262-
rows.unshift(
322+
rows.push(
263323
<tr key={ rooms[i].room_id }
264324
onClick={self.onRoomClicked.bind(self, rooms[i])}
265325
// cancel onMouseDown otherwise shift-clicking highlights text
@@ -289,12 +349,8 @@ module.exports = React.createClass({
289349
return rows;
290350
},
291351

292-
onKeyUp: function(ev) {
293-
this.forceUpdate();
294-
this.setState({ roomAlias : this.refs.roomAlias.value })
295-
if (ev.key == "Enter") {
296-
this.showRoomAlias(this.refs.roomAlias.value);
297-
}
352+
collectScrollPanel: function(element) {
353+
this.scrollPanel = element;
298354
},
299355

300356
/**
@@ -312,13 +368,27 @@ module.exports = React.createClass({
312368
},
313369

314370
render: function() {
371+
let content;
315372
if (this.state.loading) {
316-
var Loader = sdk.getComponent("elements.Spinner");
317-
return (
318-
<div className="mx_RoomDirectory">
319-
<Loader />
320-
</div>
321-
);
373+
const Loader = sdk.getComponent("elements.Spinner");
374+
content = <div className="mx_RoomDirectory">
375+
<Loader />
376+
</div>;
377+
} else {
378+
const ScrollPanel = sdk.getComponent("structures.ScrollPanel");
379+
content = <ScrollPanel ref={this.collectScrollPanel}
380+
className="mx_RoomDirectory_tableWrapper"
381+
onFillRequest={ this.onFillRequest }
382+
stickyBottom={false}
383+
startAtBottom={false}
384+
onResize={function(){}}
385+
>
386+
<table ref="directory_table" className="mx_RoomDirectory_table">
387+
<tbody>
388+
{ this.getRows() }
389+
</tbody>
390+
</table>
391+
</ScrollPanel>;
322392
}
323393

324394
const SimpleRoomHeader = sdk.getComponent('rooms.SimpleRoomHeader');
@@ -328,16 +398,12 @@ module.exports = React.createClass({
328398
<SimpleRoomHeader title="Directory" />
329399
<div className="mx_RoomDirectory_list">
330400
<div className="mx_RoomDirectory_listheader">
331-
<input ref="roomAlias" placeholder="Join a room (e.g. #foo:domain.com)" className="mx_RoomDirectory_input" size="64" onKeyUp={ this.onKeyUp }/>
401+
<input type="text" placeholder="Find a room by keyword or room ID (#foo:matrix.org)"
402+
className="mx_RoomDirectory_input" size="64" onChange={this.onFilterChange} onKeyUp={this.onFilterKeyUp}
403+
/>
332404
<NetworkDropdown config={this.props.config} onNetworkChange={this.onNetworkChange} />
333405
</div>
334-
<GeminiScrollbar className="mx_RoomDirectory_tableWrapper">
335-
<table ref="directory_table" className="mx_RoomDirectory_table">
336-
<tbody>
337-
{ this.getRows(this.state.roomAlias) }
338-
</tbody>
339-
</table>
340-
</GeminiScrollbar>
406+
{content}
341407
</div>
342408
</div>
343409
);

‎src/skins/vector/css/vector-web/structures/RoomDirectory.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ limitations under the License.
4646
-webkit-flex-direction: column;
4747
}
4848

49+
.mx_RoomDirectory_list .mx_RoomView_messageListWrapper {
50+
justify-content: flex-start;
51+
-webkit-justify-content: flex-start;
52+
}
53+
4954
.mx_RoomDirectory_listheader {
5055
display: table;
5156
width: 100%;

‎test/app-tests/joining.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('joining a room', function () {
7676
httpBackend.when('GET', '/pushrules').respond(200, {});
7777
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
7878
httpBackend.when('GET', '/sync').respond(200, {});
79-
httpBackend.when('GET', '/publicRooms').respond(200, {chunk: []});
79+
httpBackend.when('POST', '/publicRooms').respond(200, {chunk: []});
8080
httpBackend.when('GET', '/directory/room/'+encodeURIComponent(ROOM_ALIAS)).respond(200, { room_id: ROOM_ID });
8181

8282
// start with a logged-in client

0 commit comments

Comments
 (0)