element-web/src/controllers/organisms/RoomList.js

141 lines
4.7 KiB
JavaScript
Raw Normal View History

/*
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';
var React = require("react");
var MatrixClientPeg = require("../../MatrixClientPeg");
var RoomListSorter = require("../../RoomListSorter");
2015-06-12 16:59:33 +03:00
var ComponentBroker = require('../../ComponentBroker');
2015-09-11 18:55:48 +03:00
var ConferenceHandler = require("../../ConferenceHandler");
2015-06-12 16:59:33 +03:00
var RoomTile = ComponentBroker.get("molecules/RoomTile");
2015-06-12 16:59:33 +03:00
2015-09-11 18:55:48 +03:00
var HIDE_CONFERENCE_CHANS = true;
module.exports = {
2015-06-12 16:59:33 +03:00
componentWillMount: function() {
var cli = MatrixClientPeg.get();
2015-06-19 18:12:22 +03:00
cli.on("Room", this.onRoom);
2015-06-18 16:35:17 +03:00
cli.on("Room.timeline", this.onRoomTimeline);
2015-06-22 17:56:16 +03:00
cli.on("Room.name", this.onRoomName);
2015-06-18 16:35:17 +03:00
var rooms = this.getRoomList();
2015-06-18 16:35:17 +03:00
this.setState({
roomList: rooms,
2015-06-18 16:35:17 +03:00
activityMap: {}
});
},
componentWillUnmount: function() {
if (MatrixClientPeg.get()) {
2015-06-19 18:12:22 +03:00
MatrixClientPeg.get().removeListener("Room", this.onRoom);
2015-06-18 16:35:17 +03:00
MatrixClientPeg.get().removeListener("Room.timeline", this.onRoomTimeline);
2015-06-22 17:56:16 +03:00
MatrixClientPeg.get().removeListener("Room.name", this.onRoomName);
2015-06-18 16:35:17 +03:00
}
},
2015-06-12 16:59:33 +03:00
componentWillReceiveProps: function(newProps) {
this.state.activityMap[newProps.selectedRoom] = undefined;
2015-06-18 16:35:17 +03:00
this.setState({
activityMap: this.state.activityMap
});
},
2015-06-19 18:12:22 +03:00
onRoom: function(room) {
this.refreshRoomList();
2015-06-19 18:12:22 +03:00
},
2015-06-18 16:35:17 +03:00
onRoomTimeline: function(ev, room, toStartOfTimeline) {
if (toStartOfTimeline) return;
2015-06-29 18:48:13 +03:00
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;
}
// 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;
2015-06-29 18:48:13 +03:00
}
this.setState(newState);
2015-06-22 17:56:16 +03:00
},
onRoomName: function(room) {
this.refreshRoomList();
},
refreshRoomList: function() {
var rooms = this.getRoomList();
2015-06-22 17:56:16 +03:00
this.setState({
roomList: rooms
2015-06-18 16:35:17 +03:00
});
2015-06-12 16:59:33 +03:00
},
2015-07-08 20:28:30 +03:00
getRoomList: function() {
return RoomListSorter.mostRecentActivityFirst(
MatrixClientPeg.get().getRooms().filter(function(room) {
var member = room.getMember(MatrixClientPeg.get().credentials.userId);
2015-09-11 18:55:48 +03:00
var shouldShowRoom = (
member && (member.membership == "join" || member.membership == "invite")
);
// hiding conf rooms only ever toggles shouldShowRoom to false
if (shouldShowRoom && HIDE_CONFERENCE_CHANS) {
// we want to hide the 1:1 conf<->user room and not the group chat
var joinedMembers = room.getJoinedMembers();
if (joinedMembers.length === 2) {
var otherMember = joinedMembers.filter(function(m) {
return m.userId !== member.userId
})[0];
if (ConferenceHandler.isConferenceUser(otherMember)) {
// console.log("Hiding conference 1:1 room %s", room.roomId);
2015-09-11 18:55:48 +03:00
shouldShowRoom = false;
}
}
}
return shouldShowRoom;
})
);
},
2015-06-12 16:59:33 +03:00
makeRoomTiles: function() {
2015-07-17 23:59:48 +03:00
var self = this;
2015-06-12 16:59:33 +03:00
return this.state.roomList.map(function(room) {
2015-07-17 23:59:48 +03:00
var selected = room.roomId == self.props.selectedRoom;
2015-06-12 16:59:33 +03:00
return (
2015-06-18 16:35:17 +03:00
<RoomTile
room={room}
key={room.roomId}
selected={selected}
2015-07-17 23:59:48 +03:00
unread={self.state.activityMap[room.roomId] === 1}
highlight={self.state.activityMap[room.roomId] === 2}
2015-06-18 16:35:17 +03:00
/>
2015-06-12 16:59:33 +03:00
);
});
},
};