From d58fdef362f715d3d6633908d0f6baf4080f25c4 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 8 Jul 2015 18:18:03 +0100 Subject: [PATCH] Order room list by most recent activity --- src/RoomListSorter.js | 31 +++++++++++++++ src/controllers/organisms/RoomList.js | 57 ++++++++++++++++----------- src/controllers/pages/MatrixChat.js | 5 ++- 3 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 src/RoomListSorter.js diff --git a/src/RoomListSorter.js b/src/RoomListSorter.js new file mode 100644 index 000000000..d5d3ab1fb --- /dev/null +++ b/src/RoomListSorter.js @@ -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 +}; diff --git a/src/controllers/organisms/RoomList.js b/src/controllers/organisms/RoomList.js index acc510ee1..87413e18f 100644 --- a/src/controllers/organisms/RoomList.js +++ b/src/controllers/organisms/RoomList.js @@ -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 hl = 1; + 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; + var actions = MatrixClientPeg.get().getPushActionsForEvent(ev); + if (actions && actions.tweaks && actions.tweaks.highlight) { + hl = 2; + } + 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); + + newState.activityMap = amap; + } } - if (!actions.notify) { - return; - } - - // 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 - }); + 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) { diff --git a/src/controllers/pages/MatrixChat.js b/src/controllers/pages/MatrixChat.js index 600d97e9d..94c933a53 100644 --- a/src/controllers/pages/MatrixChat.js +++ b/src/controllers/pages/MatrixChat.js @@ -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'});