Skip to content

Commit d58fdef

Browse files
committed
Order room list by most recent activity
1 parent a591b66 commit d58fdef

3 files changed

Lines changed: 70 additions & 25 deletions

File tree

‎src/RoomListSorter.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
function tsOfNewestEvent(room) {
20+
return room.timeline[room.timeline.length - 1].getTs();
21+
}
22+
23+
function mostRecentFirst(roomList) {
24+
return roomList.sort(function(a,b) {
25+
return tsOfNewestEvent(b) - tsOfNewestEvent(a);
26+
});
27+
}
28+
29+
module.exports = {
30+
mostRecentFirst: mostRecentFirst
31+
};

‎src/controllers/organisms/RoomList.js‎

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
var React = require("react");
2020
var MatrixClientPeg = require("../../MatrixClientPeg");
21+
var RoomListSorter = require("../../RoomListSorter");
2122

2223
var ComponentBroker = require('../../ComponentBroker');
2324

@@ -30,8 +31,9 @@ module.exports = {
3031
cli.on("Room.timeline", this.onRoomTimeline);
3132
cli.on("Room.name", this.onRoomName);
3233

34+
var rooms = this.getRoomList();
3335
this.setState({
34-
roomList: cli.getRooms(),
36+
roomList: rooms,
3537
activityMap: {}
3638
});
3739
},
@@ -52,42 +54,51 @@ module.exports = {
5254
},
5355

5456
onRoom: function(room) {
55-
var cli = MatrixClientPeg.get();
56-
this.setState({
57-
roomList: cli.getRooms(),
58-
});
57+
this.refreshRoomList();
5958
},
6059

6160
onRoomTimeline: function(ev, room, toStartOfTimeline) {
6261
if (toStartOfTimeline) return;
63-
if (room.roomId == this.props.selectedRoom) return;
64-
if (ev.getSender() == MatrixClientPeg.get().credentials.userId) return;
65-
66-
var hl = 1;
6762

68-
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
69-
if (actions && actions.tweaks && actions.tweaks.highlight) {
70-
hl = 2;
63+
var newState = {
64+
roomList: this.getRoomList()
65+
};
66+
if (
67+
room.roomId != this.props.selectedRoom &&
68+
ev.getSender() != MatrixClientPeg.get().credentials.userId)
69+
{
70+
var hl = 1;
71+
72+
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
73+
if (actions && actions.tweaks && actions.tweaks.highlight) {
74+
hl = 2;
75+
}
76+
if (actions.notify) {
77+
// obviously this won't deep copy but this shouldn't be necessary
78+
var amap = this.state.activityMap;
79+
amap[room.roomId] = Math.max(amap[room.roomId] || 0, hl);
80+
81+
newState.activityMap = amap;
82+
}
7183
}
72-
if (!actions.notify) {
73-
return;
74-
}
75-
76-
// obviously this won't deep copy but this shouldn't be necessary
77-
var amap = this.state.activityMap;
78-
amap[room.roomId] = Math.max(amap[room.roomId] || 0, hl);
79-
this.setState({
80-
activityMap: amap
81-
});
84+
this.setState(newState);
8285
},
8386

8487
onRoomName: function(room) {
85-
var cli = MatrixClientPeg.get();
88+
this.refreshRoomList();
89+
},
90+
91+
refreshRoomList: function() {
92+
var rooms = this.getRoomList();
8693
this.setState({
87-
roomList: cli.getRooms(),
94+
roomList: rooms
8895
});
8996
},
9097

98+
getRoomList() {
99+
return RoomListSorter.mostRecentFirst(MatrixClientPeg.get().getRooms());
100+
},
101+
91102
makeRoomTiles: function() {
92103
var that = this;
93104
return this.state.roomList.map(function(room) {

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
var Loader = require("react-loader");
2121

2222
var MatrixClientPeg = require("../../MatrixClientPeg");
23+
var RoomListSorter = require("../../RoomListSorter");
2324

2425
var dis = require("../../dispatcher");
2526

@@ -106,7 +107,9 @@ module.exports = {
106107
cli.on('syncComplete', function() {
107108
var firstRoom = null;
108109
if (cli.getRooms() && cli.getRooms().length) {
109-
firstRoom = cli.getRooms()[0].roomId;
110+
firstRoom = RoomListSorter.mostRecentFirst(
111+
cli.getRooms()
112+
)[0].roomId;
110113
}
111114
that.setState({ready: true, currentRoom: firstRoom});
112115
dis.dispatch({action: 'focus_composer'});

0 commit comments

Comments
 (0)