Order room list by most recent activity
This commit is contained in:
parent
a591b66732
commit
d58fdef362
3 changed files with 69 additions and 24 deletions
31
src/RoomListSorter.js
Normal file
31
src/RoomListSorter.js
Normal 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
|
||||||
|
};
|
|
@ -18,6 +18,7 @@ limitations under the License.
|
||||||
|
|
||||||
var React = require("react");
|
var React = require("react");
|
||||||
var MatrixClientPeg = require("../../MatrixClientPeg");
|
var MatrixClientPeg = require("../../MatrixClientPeg");
|
||||||
|
var RoomListSorter = require("../../RoomListSorter");
|
||||||
|
|
||||||
var ComponentBroker = require('../../ComponentBroker');
|
var ComponentBroker = require('../../ComponentBroker');
|
||||||
|
|
||||||
|
@ -30,8 +31,9 @@ module.exports = {
|
||||||
cli.on("Room.timeline", this.onRoomTimeline);
|
cli.on("Room.timeline", this.onRoomTimeline);
|
||||||
cli.on("Room.name", this.onRoomName);
|
cli.on("Room.name", this.onRoomName);
|
||||||
|
|
||||||
|
var rooms = this.getRoomList();
|
||||||
this.setState({
|
this.setState({
|
||||||
roomList: cli.getRooms(),
|
roomList: rooms,
|
||||||
activityMap: {}
|
activityMap: {}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -52,42 +54,51 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
onRoom: function(room) {
|
onRoom: function(room) {
|
||||||
var cli = MatrixClientPeg.get();
|
this.refreshRoomList();
|
||||||
this.setState({
|
|
||||||
roomList: cli.getRooms(),
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onRoomTimeline: function(ev, room, toStartOfTimeline) {
|
onRoomTimeline: function(ev, room, toStartOfTimeline) {
|
||||||
if (toStartOfTimeline) return;
|
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 hl = 1;
|
||||||
|
|
||||||
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
var actions = MatrixClientPeg.get().getPushActionsForEvent(ev);
|
||||||
if (actions && actions.tweaks && actions.tweaks.highlight) {
|
if (actions && actions.tweaks && actions.tweaks.highlight) {
|
||||||
hl = 2;
|
hl = 2;
|
||||||
}
|
}
|
||||||
if (!actions.notify) {
|
if (actions.notify) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// obviously this won't deep copy but this shouldn't be necessary
|
// obviously this won't deep copy but this shouldn't be necessary
|
||||||
var amap = this.state.activityMap;
|
var amap = this.state.activityMap;
|
||||||
amap[room.roomId] = Math.max(amap[room.roomId] || 0, hl);
|
amap[room.roomId] = Math.max(amap[room.roomId] || 0, hl);
|
||||||
this.setState({
|
|
||||||
activityMap: amap
|
newState.activityMap = amap;
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
this.setState(newState);
|
||||||
},
|
},
|
||||||
|
|
||||||
onRoomName: function(room) {
|
onRoomName: function(room) {
|
||||||
var cli = MatrixClientPeg.get();
|
this.refreshRoomList();
|
||||||
|
},
|
||||||
|
|
||||||
|
refreshRoomList: function() {
|
||||||
|
var rooms = this.getRoomList();
|
||||||
this.setState({
|
this.setState({
|
||||||
roomList: cli.getRooms(),
|
roomList: rooms
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getRoomList() {
|
||||||
|
return RoomListSorter.mostRecentFirst(MatrixClientPeg.get().getRooms());
|
||||||
|
},
|
||||||
|
|
||||||
makeRoomTiles: function() {
|
makeRoomTiles: function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
return this.state.roomList.map(function(room) {
|
return this.state.roomList.map(function(room) {
|
||||||
|
|
|
@ -20,6 +20,7 @@ limitations under the License.
|
||||||
var Loader = require("react-loader");
|
var Loader = require("react-loader");
|
||||||
|
|
||||||
var MatrixClientPeg = require("../../MatrixClientPeg");
|
var MatrixClientPeg = require("../../MatrixClientPeg");
|
||||||
|
var RoomListSorter = require("../../RoomListSorter");
|
||||||
|
|
||||||
var dis = require("../../dispatcher");
|
var dis = require("../../dispatcher");
|
||||||
|
|
||||||
|
@ -106,7 +107,9 @@ module.exports = {
|
||||||
cli.on('syncComplete', function() {
|
cli.on('syncComplete', function() {
|
||||||
var firstRoom = null;
|
var firstRoom = null;
|
||||||
if (cli.getRooms() && cli.getRooms().length) {
|
if (cli.getRooms() && cli.getRooms().length) {
|
||||||
firstRoom = cli.getRooms()[0].roomId;
|
firstRoom = RoomListSorter.mostRecentFirst(
|
||||||
|
cli.getRooms()
|
||||||
|
)[0].roomId;
|
||||||
}
|
}
|
||||||
that.setState({ready: true, currentRoom: firstRoom});
|
that.setState({ready: true, currentRoom: firstRoom});
|
||||||
dis.dispatch({action: 'focus_composer'});
|
dis.dispatch({action: 'focus_composer'});
|
||||||
|
|
Loading…
Reference in a new issue