Order room list by most recent activity

This commit is contained in:
David Baker 2015-07-08 18:18:03 +01:00
parent a591b66732
commit d58fdef362
3 changed files with 69 additions and 24 deletions

31
src/RoomListSorter.js Normal file
View File

@ -0,0 +1,31 @@
/*
Copyright 2015 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
function tsOfNewestEvent(room) {
return room.timeline[room.timeline.length - 1].getTs();
}
function mostRecentFirst(roomList) {
return roomList.sort(function(a,b) {
return tsOfNewestEvent(b) - tsOfNewestEvent(a);
});
}
module.exports = {
mostRecentFirst: mostRecentFirst
};

View File

@ -18,6 +18,7 @@ limitations under the License.
var React = require("react");
var MatrixClientPeg = require("../../MatrixClientPeg");
var RoomListSorter = require("../../RoomListSorter");
var ComponentBroker = require('../../ComponentBroker');
@ -30,8 +31,9 @@ module.exports = {
cli.on("Room.timeline", this.onRoomTimeline);
cli.on("Room.name", this.onRoomName);
var rooms = this.getRoomList();
this.setState({
roomList: cli.getRooms(),
roomList: rooms,
activityMap: {}
});
},
@ -52,42 +54,51 @@ module.exports = {
},
onRoom: function(room) {
var cli = MatrixClientPeg.get();
this.setState({
roomList: cli.getRooms(),
});
this.refreshRoomList();
},
onRoomTimeline: function(ev, room, toStartOfTimeline) {
if (toStartOfTimeline) return;
if (room.roomId == this.props.selectedRoom) return;
if (ev.getSender() == MatrixClientPeg.get().credentials.userId) return;
var newState = {
roomList: this.getRoomList()
};
if (
room.roomId != this.props.selectedRoom &&
ev.getSender() != MatrixClientPeg.get().credentials.userId)
{
var hl = 1;
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
if (actions && actions.tweaks && actions.tweaks.highlight) {
hl = 2;
}
if (!actions.notify) {
return;
}
if (actions.notify) {
// obviously this won't deep copy but this shouldn't be necessary
var amap = this.state.activityMap;
amap[room.roomId] = Math.max(amap[room.roomId] || 0, hl);
this.setState({
activityMap: amap
});
newState.activityMap = amap;
}
}
this.setState(newState);
},
onRoomName: function(room) {
var cli = MatrixClientPeg.get();
this.refreshRoomList();
},
refreshRoomList: function() {
var rooms = this.getRoomList();
this.setState({
roomList: cli.getRooms(),
roomList: rooms
});
},
getRoomList() {
return RoomListSorter.mostRecentFirst(MatrixClientPeg.get().getRooms());
},
makeRoomTiles: function() {
var that = this;
return this.state.roomList.map(function(room) {

View File

@ -20,6 +20,7 @@ limitations under the License.
var Loader = require("react-loader");
var MatrixClientPeg = require("../../MatrixClientPeg");
var RoomListSorter = require("../../RoomListSorter");
var dis = require("../../dispatcher");
@ -106,7 +107,9 @@ module.exports = {
cli.on('syncComplete', function() {
var firstRoom = null;
if (cli.getRooms() && cli.getRooms().length) {
firstRoom = cli.getRooms()[0].roomId;
firstRoom = RoomListSorter.mostRecentFirst(
cli.getRooms()
)[0].roomId;
}
that.setState({ready: true, currentRoom: firstRoom});
dis.dispatch({action: 'focus_composer'});