2015-07-20 23:11:24 -04:00
|
|
|
/*
|
2016-01-06 23:17:56 -05:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-07-20 23:11:24 -04:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2015-09-22 14:09:23 -04:00
|
|
|
var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg');
|
2016-02-20 08:36:48 -05:00
|
|
|
var ContentRepo = require("matrix-js-sdk").ContentRepo;
|
2015-09-22 14:09:23 -04:00
|
|
|
var Modal = require('matrix-react-sdk/lib/Modal');
|
2016-05-18 10:01:36 -04:00
|
|
|
var sdk = require('matrix-react-sdk');
|
2015-09-22 14:09:23 -04:00
|
|
|
var dis = require('matrix-react-sdk/lib/dispatcher');
|
2016-02-22 04:35:11 -05:00
|
|
|
var GeminiScrollbar = require('react-gemini-scrollbar');
|
2015-07-20 23:11:24 -04:00
|
|
|
|
2016-02-20 08:36:48 -05:00
|
|
|
var linkify = require('linkifyjs');
|
2016-02-20 18:54:47 -05:00
|
|
|
var linkifyString = require('linkifyjs/string');
|
2016-02-20 08:36:48 -05:00
|
|
|
var linkifyMatrix = require('matrix-react-sdk/lib/linkify-matrix');
|
2016-02-20 18:54:47 -05:00
|
|
|
var sanitizeHtml = require('sanitize-html');
|
2016-02-20 08:36:48 -05:00
|
|
|
|
|
|
|
linkifyMatrix(linkify);
|
|
|
|
|
2015-07-20 23:11:24 -04:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'RoomDirectory',
|
|
|
|
|
2016-09-15 12:20:13 -04:00
|
|
|
propTypes: {
|
|
|
|
config: React.PropTypes.object,
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
networks: [],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-20 23:11:24 -04:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
publicRooms: [],
|
|
|
|
roomAlias: '',
|
2015-07-27 17:31:24 -04:00
|
|
|
loading: true,
|
2016-09-15 10:18:12 -04:00
|
|
|
filterByNetwork: null,
|
2015-07-20 23:11:24 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-15 13:29:57 -04:00
|
|
|
componentWillMount: function() {
|
2016-09-15 12:20:13 -04:00
|
|
|
// precompile Regexps
|
|
|
|
this.networkPatterns = {};
|
|
|
|
if (this.props.config.networkPatterns) {
|
|
|
|
for (const network of Object.keys(this.props.config.networkPatterns)) {
|
|
|
|
this.networkPatterns[network] = new RegExp(this.props.config.networkPatterns[network]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 13:29:57 -04:00
|
|
|
// dis.dispatch({
|
|
|
|
// action: 'ui_opacity',
|
|
|
|
// sideOpacity: 0.3,
|
|
|
|
// middleOpacity: 0.3,
|
|
|
|
// });
|
|
|
|
},
|
|
|
|
|
2015-07-20 23:11:24 -04:00
|
|
|
componentDidMount: function() {
|
2016-06-21 11:47:40 -04:00
|
|
|
this.getPublicRooms();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
// dis.dispatch({
|
|
|
|
// action: 'ui_opacity',
|
|
|
|
// sideOpacity: 1.0,
|
|
|
|
// middleOpacity: 1.0,
|
|
|
|
// });
|
|
|
|
},
|
|
|
|
|
|
|
|
getPublicRooms: function() {
|
2015-07-20 23:11:24 -04:00
|
|
|
var self = this;
|
|
|
|
MatrixClientPeg.get().publicRooms(function (err, data) {
|
|
|
|
if (err) {
|
2016-03-15 14:38:24 -04:00
|
|
|
self.setState({ loading: false });
|
2015-07-20 23:11:24 -04:00
|
|
|
console.error("Failed to get publicRooms: %s", JSON.stringify(err));
|
2015-11-30 09:11:28 -05:00
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2015-07-20 23:11:24 -04:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Failed to get public room list",
|
|
|
|
description: err.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.setState({
|
2015-07-27 17:31:24 -04:00
|
|
|
publicRooms: data.chunk,
|
|
|
|
loading: false,
|
2015-07-20 23:11:24 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-06-22 09:52:55 -04:00
|
|
|
/**
|
|
|
|
* A limited interface for removing rooms from the directory.
|
|
|
|
* Will set the room to not be publicly visible and delete the
|
|
|
|
* default alias. In the long term, it would be better to allow
|
|
|
|
* HS admins to do this through the RoomSettings interface, but
|
|
|
|
* this needs SPEC-417.
|
|
|
|
*/
|
|
|
|
removeFromDirectory: function(room) {
|
|
|
|
var alias = get_display_alias_for_room(room);
|
|
|
|
var name = room.name || alias || "Unnamed room";
|
2016-04-15 13:29:57 -04:00
|
|
|
|
2016-06-21 11:47:40 -04:00
|
|
|
var QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2016-06-22 09:52:55 -04:00
|
|
|
|
2016-06-23 06:12:25 -04:00
|
|
|
var desc;
|
|
|
|
if (alias) {
|
|
|
|
desc = `Delete the room alias '${alias}' and remove '${name}' from the directory?`;
|
|
|
|
} else {
|
|
|
|
desc = `Remove '${name}' from the directory?`;
|
|
|
|
}
|
|
|
|
|
2016-06-21 11:47:40 -04:00
|
|
|
Modal.createDialog(QuestionDialog, {
|
2016-06-22 09:52:55 -04:00
|
|
|
title: "Remove from Directory",
|
2016-06-23 06:12:25 -04:00
|
|
|
description: desc,
|
2016-06-21 11:47:40 -04:00
|
|
|
onFinished: (should_delete) => {
|
2016-06-22 09:52:55 -04:00
|
|
|
if (!should_delete) return;
|
|
|
|
|
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
var modal = Modal.createDialog(Loader);
|
|
|
|
var step = `remove '${name}' from the directory.`;
|
|
|
|
|
|
|
|
MatrixClientPeg.get().setRoomDirectoryVisibility(room.room_id, 'private').then(() => {
|
2016-06-23 06:12:25 -04:00
|
|
|
if (!alias) return;
|
2016-06-22 09:52:55 -04:00
|
|
|
step = 'delete the alias.';
|
2016-06-22 11:20:06 -04:00
|
|
|
return MatrixClientPeg.get().deleteAlias(alias);
|
2016-06-22 09:52:55 -04:00
|
|
|
}).done(() => {
|
|
|
|
modal.close();
|
|
|
|
this.getPublicRooms();
|
|
|
|
}, function(err) {
|
|
|
|
modal.close();
|
2016-06-22 11:20:06 -04:00
|
|
|
this.getPublicRooms();
|
2016-06-22 09:52:55 -04:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Failed to "+step,
|
|
|
|
description: err.toString()
|
2016-06-21 11:47:40 -04:00
|
|
|
});
|
2016-06-22 09:52:55 -04:00
|
|
|
});
|
2016-03-02 09:24:00 -05:00
|
|
|
}
|
2016-06-21 11:47:40 -04:00
|
|
|
});
|
2016-04-15 13:29:57 -04:00
|
|
|
},
|
|
|
|
|
2016-06-23 05:11:46 -04:00
|
|
|
onRoomClicked: function(room, ev) {
|
2016-06-21 11:47:40 -04:00
|
|
|
if (ev.shiftKey) {
|
|
|
|
ev.preventDefault();
|
2016-06-22 09:52:55 -04:00
|
|
|
this.removeFromDirectory(room);
|
2016-06-23 05:11:46 -04:00
|
|
|
} else {
|
|
|
|
this.showRoom(room);
|
2016-03-02 09:24:00 -05:00
|
|
|
}
|
2016-06-23 05:11:46 -04:00
|
|
|
},
|
2016-06-21 11:47:40 -04:00
|
|
|
|
2016-09-15 10:18:12 -04:00
|
|
|
onNetworkChange: function(network) {
|
|
|
|
this.setState({
|
|
|
|
filterByNetwork: network,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-06-23 05:11:46 -04:00
|
|
|
showRoomAlias: function(alias) {
|
|
|
|
this.showRoom(null, alias);
|
|
|
|
},
|
|
|
|
|
|
|
|
showRoom: function(room, room_alias) {
|
|
|
|
var payload = {action: 'view_room'};
|
2016-03-02 09:24:00 -05:00
|
|
|
if (room) {
|
2016-06-23 05:11:46 -04:00
|
|
|
// Don't let the user view a room they won't be able to either
|
|
|
|
// peek or join: fail earlier so they don't have to click back
|
|
|
|
// to the directory.
|
2016-04-13 08:33:16 -04:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
if (!room.world_readable && !room.guest_can_join) {
|
|
|
|
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
|
|
|
|
Modal.createDialog(NeedToRegisterDialog, {
|
|
|
|
title: "Failed to join the room",
|
|
|
|
description: "This room is inaccessible to guests. You may be able to join if you register."
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 05:11:46 -04:00
|
|
|
if (!room_alias) {
|
|
|
|
room_alias = get_display_alias_for_room(room);
|
|
|
|
}
|
2016-06-22 09:52:55 -04:00
|
|
|
|
2016-06-23 05:11:46 -04:00
|
|
|
payload.oob_data = {
|
2016-03-02 09:24:00 -05:00
|
|
|
avatarUrl: room.avatar_url,
|
|
|
|
// XXX: This logic is duplicated from the JS SDK which
|
|
|
|
// would normally decide what the name is.
|
2016-06-23 05:11:46 -04:00
|
|
|
name: room.name || room_alias || "Unnamed room",
|
2016-03-02 09:24:00 -05:00
|
|
|
};
|
|
|
|
}
|
2016-06-14 08:02:34 -04:00
|
|
|
// It's not really possible to join Matrix rooms by ID because the HS has no way to know
|
|
|
|
// which servers to start querying. However, there's no other way to join rooms in
|
|
|
|
// this list without aliases at present, so if roomAlias isn't set here we have no
|
|
|
|
// choice but to supply the ID.
|
2016-06-22 09:52:55 -04:00
|
|
|
if (room_alias) {
|
|
|
|
payload.room_alias = room_alias;
|
2016-06-14 08:02:34 -04:00
|
|
|
} else {
|
2016-06-22 09:52:55 -04:00
|
|
|
payload.room_id = room.room_id;
|
2016-06-14 08:02:34 -04:00
|
|
|
}
|
|
|
|
dis.dispatch(payload);
|
2015-07-20 23:11:24 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getRows: function(filter) {
|
2016-02-20 08:36:48 -05:00
|
|
|
var BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
|
|
|
|
|
2015-07-20 23:11:24 -04:00
|
|
|
if (!this.state.publicRooms) return [];
|
|
|
|
|
2016-09-15 10:18:12 -04:00
|
|
|
var rooms = this.state.publicRooms.filter((a) => {
|
2015-07-20 23:11:24 -04:00
|
|
|
// FIXME: if incrementally typing, keep narrowing down the search set
|
2015-07-27 17:31:24 -04:00
|
|
|
// incrementally rather than starting over each time.
|
2016-09-15 10:18:12 -04:00
|
|
|
if (this.state.filterByNetwork) {
|
|
|
|
if (this._networkForRoom(a) != this.state.filterByNetwork) return false;
|
|
|
|
}
|
|
|
|
|
2016-04-20 07:29:32 -04:00
|
|
|
return (((a.name && a.name.toLowerCase().search(filter.toLowerCase()) >= 0) ||
|
|
|
|
(a.aliases && a.aliases[0].toLowerCase().search(filter.toLowerCase()) >= 0)) &&
|
2016-03-23 07:26:18 -04:00
|
|
|
a.num_joined_members > 0);
|
2015-07-20 23:11:24 -04:00
|
|
|
}).sort(function(a,b) {
|
2015-07-23 13:24:34 -04:00
|
|
|
return a.num_joined_members - b.num_joined_members;
|
2015-07-20 23:11:24 -04:00
|
|
|
});
|
|
|
|
var rows = [];
|
|
|
|
var self = this;
|
2016-01-18 12:37:13 -05:00
|
|
|
var guestRead, guestJoin, perms;
|
2015-07-20 23:11:24 -04:00
|
|
|
for (var i = 0; i < rooms.length; i++) {
|
2016-06-22 09:52:55 -04:00
|
|
|
var name = rooms[i].name || get_display_alias_for_room(rooms[i]) || "Unnamed room";
|
2016-01-07 09:43:12 -05:00
|
|
|
guestRead = null;
|
|
|
|
guestJoin = null;
|
|
|
|
|
|
|
|
if (rooms[i].world_readable) {
|
|
|
|
guestRead = (
|
2016-02-20 08:36:48 -05:00
|
|
|
<div className="mx_RoomDirectory_perm">World readable</div>
|
2016-01-07 09:43:12 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (rooms[i].guest_can_join) {
|
|
|
|
guestJoin = (
|
2016-02-20 08:36:48 -05:00
|
|
|
<div className="mx_RoomDirectory_perm">Guests can join</div>
|
2016-01-07 09:43:12 -05:00
|
|
|
);
|
2016-01-18 12:37:13 -05:00
|
|
|
}
|
2016-03-15 14:38:24 -04:00
|
|
|
|
2016-01-18 12:37:13 -05:00
|
|
|
perms = null;
|
|
|
|
if (guestRead || guestJoin) {
|
2016-02-20 08:36:48 -05:00
|
|
|
perms = <div className="mx_RoomDirectory_perms">{guestRead} {guestJoin}</div>;
|
2016-01-07 09:43:12 -05:00
|
|
|
}
|
|
|
|
|
2016-02-20 18:54:47 -05:00
|
|
|
var topic = rooms[i].topic || '';
|
|
|
|
topic = linkifyString(sanitizeHtml(topic));
|
|
|
|
|
2015-07-20 23:11:24 -04:00
|
|
|
rows.unshift(
|
2016-06-21 11:47:40 -04:00
|
|
|
<tr key={ rooms[i].room_id }
|
2016-06-23 05:11:46 -04:00
|
|
|
onClick={self.onRoomClicked.bind(self, rooms[i])}
|
2016-06-21 11:47:40 -04:00
|
|
|
// cancel onMouseDown otherwise shift-clicking highlights text
|
|
|
|
onMouseDown={(ev) => {ev.preventDefault();}}
|
|
|
|
>
|
2016-02-20 08:36:48 -05:00
|
|
|
<td className="mx_RoomDirectory_roomAvatar">
|
|
|
|
<BaseAvatar width={24} height={24} resizeMethod='crop'
|
|
|
|
name={ name } idName={ name }
|
|
|
|
url={ ContentRepo.getHttpUriForMxc(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
rooms[i].avatar_url, 24, 24, "crop") } />
|
|
|
|
</td>
|
|
|
|
<td className="mx_RoomDirectory_roomDescription">
|
|
|
|
<div className="mx_RoomDirectory_name">{ name }</div>
|
|
|
|
{ perms }
|
2016-02-20 18:54:47 -05:00
|
|
|
<div className="mx_RoomDirectory_topic"
|
|
|
|
onClick={ function(e) { e.stopPropagation() } }
|
|
|
|
dangerouslySetInnerHTML={{ __html: topic }}/>
|
2016-06-22 09:52:55 -04:00
|
|
|
<div className="mx_RoomDirectory_alias">{ get_display_alias_for_room(rooms[i]) }</div>
|
2016-02-20 08:36:48 -05:00
|
|
|
</td>
|
|
|
|
<td className="mx_RoomDirectory_roomMemberCount">
|
|
|
|
{ rooms[i].num_joined_members }
|
|
|
|
</td>
|
|
|
|
</tr>
|
2015-07-20 23:11:24 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyUp: function(ev) {
|
|
|
|
this.forceUpdate();
|
2015-11-09 18:54:10 -05:00
|
|
|
this.setState({ roomAlias : this.refs.roomAlias.value })
|
2015-07-20 23:11:24 -04:00
|
|
|
if (ev.key == "Enter") {
|
2016-06-23 05:11:46 -04:00
|
|
|
this.showRoomAlias(this.refs.roomAlias.value);
|
2015-07-20 23:11:24 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-15 10:18:12 -04:00
|
|
|
/**
|
|
|
|
* Terrible temporary function that guess what network a public room
|
|
|
|
* entry is in, until synapse is able to tell us
|
|
|
|
*/
|
|
|
|
_networkForRoom(room) {
|
|
|
|
if (room.aliases) {
|
|
|
|
for (const alias of room.aliases) {
|
2016-09-15 12:20:13 -04:00
|
|
|
for (const network of Object.keys(this.networkPatterns)) {
|
|
|
|
if (this.networkPatterns[network].test(alias)) return network;
|
2016-09-15 10:18:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'matrix:matrix_org';
|
|
|
|
},
|
|
|
|
|
2015-07-20 23:11:24 -04:00
|
|
|
render: function() {
|
2015-07-27 17:31:24 -04:00
|
|
|
if (this.state.loading) {
|
2016-03-15 14:38:24 -04:00
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
2015-07-27 17:31:24 -04:00
|
|
|
return (
|
|
|
|
<div className="mx_RoomDirectory">
|
|
|
|
<Loader />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-15 06:29:27 -04:00
|
|
|
const SimpleRoomHeader = sdk.getComponent('rooms.SimpleRoomHeader');
|
|
|
|
const NetworkDropdown = sdk.getComponent('directory.NetworkDropdown');
|
2015-07-20 23:11:24 -04:00
|
|
|
return (
|
|
|
|
<div className="mx_RoomDirectory">
|
2016-03-29 18:25:26 -04:00
|
|
|
<SimpleRoomHeader title="Directory" />
|
2015-07-20 23:11:24 -04:00
|
|
|
<div className="mx_RoomDirectory_list">
|
2016-09-15 06:29:27 -04:00
|
|
|
<div className="mx_RoomDirectory_listheader">
|
|
|
|
<input ref="roomAlias" placeholder="Join a room (e.g. #foo:domain.com)" className="mx_RoomDirectory_input" size="64" onKeyUp={ this.onKeyUp }/>
|
2016-09-15 12:20:13 -04:00
|
|
|
<NetworkDropdown config={this.props.config} onNetworkChange={this.onNetworkChange} />
|
2016-09-15 06:29:27 -04:00
|
|
|
</div>
|
2016-07-27 06:41:27 -04:00
|
|
|
<GeminiScrollbar className="mx_RoomDirectory_tableWrapper">
|
2016-02-20 08:36:48 -05:00
|
|
|
<table ref="directory_table" className="mx_RoomDirectory_table">
|
|
|
|
<tbody>
|
|
|
|
{ this.getRows(this.state.roomAlias) }
|
|
|
|
</tbody>
|
2015-07-23 13:24:34 -04:00
|
|
|
</table>
|
2016-02-22 04:35:11 -05:00
|
|
|
</GeminiScrollbar>
|
2015-07-20 23:11:24 -04:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-06-22 09:52:55 -04:00
|
|
|
|
|
|
|
// Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom
|
|
|
|
// but works with the objects we get from the public room list
|
|
|
|
function get_display_alias_for_room(room) {
|
|
|
|
return room.canonical_alias || (room.aliases ? room.aliases[0] : "");
|
|
|
|
}
|