2015-07-21 06:11:24 +03:00
|
|
|
/*
|
2016-01-07 07:17:56 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-07-21 06:11:24 +03: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 21:09:23 +03:00
|
|
|
var MatrixClientPeg = require('matrix-react-sdk/lib/MatrixClientPeg');
|
2016-02-20 16:36:48 +03:00
|
|
|
var ContentRepo = require("matrix-js-sdk").ContentRepo;
|
2015-09-22 21:09:23 +03:00
|
|
|
var Modal = require('matrix-react-sdk/lib/Modal');
|
2015-09-22 20:49:04 +03:00
|
|
|
var sdk = require('matrix-react-sdk')
|
2015-09-22 21:09:23 +03:00
|
|
|
var dis = require('matrix-react-sdk/lib/dispatcher');
|
2016-02-22 12:35:11 +03:00
|
|
|
var GeminiScrollbar = require('react-gemini-scrollbar');
|
2015-07-21 06:11:24 +03:00
|
|
|
|
2016-02-20 16:36:48 +03:00
|
|
|
var linkify = require('linkifyjs');
|
2016-02-21 02:54:47 +03:00
|
|
|
var linkifyString = require('linkifyjs/string');
|
2016-02-20 16:36:48 +03:00
|
|
|
var linkifyMatrix = require('matrix-react-sdk/lib/linkify-matrix');
|
2016-02-21 02:54:47 +03:00
|
|
|
var sanitizeHtml = require('sanitize-html');
|
2016-02-20 16:36:48 +03:00
|
|
|
|
|
|
|
linkifyMatrix(linkify);
|
|
|
|
|
2015-07-21 06:11:24 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'RoomDirectory',
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
publicRooms: [],
|
|
|
|
roomAlias: '',
|
2015-07-28 00:31:24 +03:00
|
|
|
loading: true,
|
2015-07-21 06:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-15 20:29:57 +03:00
|
|
|
componentWillMount: function() {
|
|
|
|
// dis.dispatch({
|
|
|
|
// action: 'ui_opacity',
|
|
|
|
// sideOpacity: 0.3,
|
|
|
|
// middleOpacity: 0.3,
|
|
|
|
// });
|
|
|
|
},
|
|
|
|
|
2015-07-21 06:11:24 +03:00
|
|
|
componentDidMount: function() {
|
2016-06-21 18:47:40 +03:00
|
|
|
this.getPublicRooms();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
// dis.dispatch({
|
|
|
|
// action: 'ui_opacity',
|
|
|
|
// sideOpacity: 1.0,
|
|
|
|
// middleOpacity: 1.0,
|
|
|
|
// });
|
|
|
|
},
|
|
|
|
|
|
|
|
getPublicRooms: function() {
|
2015-07-21 06:11:24 +03:00
|
|
|
var self = this;
|
|
|
|
MatrixClientPeg.get().publicRooms(function (err, data) {
|
|
|
|
if (err) {
|
2016-03-15 21:38:24 +03:00
|
|
|
self.setState({ loading: false });
|
2015-07-21 06:11:24 +03:00
|
|
|
console.error("Failed to get publicRooms: %s", JSON.stringify(err));
|
2015-11-30 17:11:28 +03:00
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2015-07-21 06:11:24 +03:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Failed to get public room list",
|
|
|
|
description: err.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.setState({
|
2015-07-28 00:31:24 +03:00
|
|
|
publicRooms: data.chunk,
|
|
|
|
loading: false,
|
2015-07-21 06:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-06-21 18:47:40 +03:00
|
|
|
deleteAliasClicked: function(roomAlias) {
|
|
|
|
var QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(QuestionDialog, {
|
|
|
|
title: "Delete Alias",
|
|
|
|
description: `Are you sure you want to remove the alias '${roomAlias}'?`,
|
|
|
|
onFinished: (should_delete) => {
|
|
|
|
if (should_delete) {
|
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
var modal = Modal.createDialog(Loader);
|
|
|
|
|
|
|
|
MatrixClientPeg.get().deleteAlias(roomAlias).done(() => {
|
|
|
|
modal.close();
|
|
|
|
this.getPublicRooms();
|
|
|
|
}, function(err) {
|
|
|
|
modal.close();
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
title: "Failed to delete alias",
|
|
|
|
description: err.toString()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-04-15 20:29:57 +03:00
|
|
|
},
|
|
|
|
|
2016-06-21 18:47:40 +03:00
|
|
|
showRoom: function(roomId, roomAlias, ev) {
|
|
|
|
if (ev.shiftKey) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.deleteAliasClicked(roomAlias);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:34:31 +03:00
|
|
|
// extract the metadata from the publicRooms structure to pass
|
|
|
|
// as out-of-band data to view_room, because we get information
|
|
|
|
// here that we can't get other than by joining the room in some
|
|
|
|
// cases.
|
2016-03-02 17:24:00 +03:00
|
|
|
var room;
|
2016-06-09 19:13:02 +03:00
|
|
|
if (roomId) {
|
2016-06-09 18:41:01 +03:00
|
|
|
for (var i = 0; i < this.state.publicRooms.length; ++i) {
|
2016-06-09 19:13:02 +03:00
|
|
|
if (this.state.publicRooms[i].room_id == roomId) {
|
2016-06-09 18:41:01 +03:00
|
|
|
room = this.state.publicRooms[i];
|
|
|
|
break;
|
|
|
|
}
|
2016-03-02 17:24:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var oob_data = {};
|
|
|
|
if (room) {
|
2016-04-13 15:33:16 +03: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-03-02 17:24:00 +03:00
|
|
|
oob_data = {
|
|
|
|
avatarUrl: room.avatar_url,
|
|
|
|
// XXX: This logic is duplicated from the JS SDK which
|
|
|
|
// would normally decide what the name is.
|
2016-03-23 14:38:17 +03:00
|
|
|
name: room.name || room.canonical_alias || (room.aliases ? room.aliases[0] : "Unnamed room"),
|
2016-03-02 17:24:00 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:02:34 +03:00
|
|
|
var payload = {
|
2016-03-02 17:24:00 +03:00
|
|
|
oob_data: oob_data,
|
2016-06-10 17:13:41 +03:00
|
|
|
action: 'view_room',
|
2016-06-14 15:02:34 +03: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.
|
|
|
|
if (roomAlias) {
|
|
|
|
payload.room_alias = roomAlias;
|
|
|
|
} else {
|
|
|
|
payload.room_id = roomId;
|
|
|
|
}
|
|
|
|
dis.dispatch(payload);
|
2015-07-21 06:11:24 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getRows: function(filter) {
|
2016-02-20 16:36:48 +03:00
|
|
|
var BaseAvatar = sdk.getComponent('avatars.BaseAvatar');
|
|
|
|
|
2015-07-21 06:11:24 +03:00
|
|
|
if (!this.state.publicRooms) return [];
|
|
|
|
|
|
|
|
var rooms = this.state.publicRooms.filter(function(a) {
|
|
|
|
// FIXME: if incrementally typing, keep narrowing down the search set
|
2015-07-28 00:31:24 +03:00
|
|
|
// incrementally rather than starting over each time.
|
2016-04-20 14:29:32 +03:00
|
|
|
return (((a.name && a.name.toLowerCase().search(filter.toLowerCase()) >= 0) ||
|
|
|
|
(a.aliases && a.aliases[0].toLowerCase().search(filter.toLowerCase()) >= 0)) &&
|
2016-03-23 14:26:18 +03:00
|
|
|
a.num_joined_members > 0);
|
2015-07-21 06:11:24 +03:00
|
|
|
}).sort(function(a,b) {
|
2015-07-23 20:24:34 +03:00
|
|
|
return a.num_joined_members - b.num_joined_members;
|
2015-07-21 06:11:24 +03:00
|
|
|
});
|
|
|
|
var rows = [];
|
|
|
|
var self = this;
|
2016-01-18 20:37:13 +03:00
|
|
|
var guestRead, guestJoin, perms;
|
2015-07-21 06:11:24 +03:00
|
|
|
for (var i = 0; i < rooms.length; i++) {
|
2016-03-23 14:50:38 +03:00
|
|
|
var alias = rooms[i].canonical_alias || (rooms[i].aliases ? rooms[i].aliases[0] : "");
|
|
|
|
var name = rooms[i].name || alias || "Unnamed room";
|
2016-01-07 17:43:12 +03:00
|
|
|
guestRead = null;
|
|
|
|
guestJoin = null;
|
|
|
|
|
|
|
|
if (rooms[i].world_readable) {
|
|
|
|
guestRead = (
|
2016-02-20 16:36:48 +03:00
|
|
|
<div className="mx_RoomDirectory_perm">World readable</div>
|
2016-01-07 17:43:12 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (rooms[i].guest_can_join) {
|
|
|
|
guestJoin = (
|
2016-02-20 16:36:48 +03:00
|
|
|
<div className="mx_RoomDirectory_perm">Guests can join</div>
|
2016-01-07 17:43:12 +03:00
|
|
|
);
|
2016-01-18 20:37:13 +03:00
|
|
|
}
|
2016-03-15 21:38:24 +03:00
|
|
|
|
2016-01-18 20:37:13 +03:00
|
|
|
perms = null;
|
|
|
|
if (guestRead || guestJoin) {
|
2016-02-20 16:36:48 +03:00
|
|
|
perms = <div className="mx_RoomDirectory_perms">{guestRead} {guestJoin}</div>;
|
2016-01-07 17:43:12 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 02:54:47 +03:00
|
|
|
var topic = rooms[i].topic || '';
|
|
|
|
topic = linkifyString(sanitizeHtml(topic));
|
|
|
|
|
2015-07-21 06:11:24 +03:00
|
|
|
rows.unshift(
|
2016-06-21 18:47:40 +03:00
|
|
|
<tr key={ rooms[i].room_id }
|
|
|
|
onClick={self.showRoom.bind(null, rooms[i].room_id, alias)}
|
|
|
|
// cancel onMouseDown otherwise shift-clicking highlights text
|
|
|
|
onMouseDown={(ev) => {ev.preventDefault();}}
|
|
|
|
>
|
2016-02-20 16:36:48 +03: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-21 02:54:47 +03:00
|
|
|
<div className="mx_RoomDirectory_topic"
|
|
|
|
onClick={ function(e) { e.stopPropagation() } }
|
|
|
|
dangerouslySetInnerHTML={{ __html: topic }}/>
|
2016-03-23 14:26:18 +03:00
|
|
|
<div className="mx_RoomDirectory_alias">{ alias }</div>
|
2016-02-20 16:36:48 +03:00
|
|
|
</td>
|
|
|
|
<td className="mx_RoomDirectory_roomMemberCount">
|
|
|
|
{ rooms[i].num_joined_members }
|
|
|
|
</td>
|
|
|
|
</tr>
|
2015-07-21 06:11:24 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyUp: function(ev) {
|
|
|
|
this.forceUpdate();
|
2015-11-10 02:54:10 +03:00
|
|
|
this.setState({ roomAlias : this.refs.roomAlias.value })
|
2015-07-21 06:11:24 +03:00
|
|
|
if (ev.key == "Enter") {
|
2016-06-09 19:13:02 +03:00
|
|
|
this.showRoom(null, this.refs.roomAlias.value);
|
2015-07-21 06:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2015-07-28 00:31:24 +03:00
|
|
|
if (this.state.loading) {
|
2016-03-15 21:38:24 +03:00
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
2015-07-28 00:31:24 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_RoomDirectory">
|
|
|
|
<Loader />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-29 18:45:24 +03:00
|
|
|
var SimpleRoomHeader = sdk.getComponent('rooms.SimpleRoomHeader');
|
2015-07-21 06:11:24 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_RoomDirectory">
|
2016-03-30 01:25:26 +03:00
|
|
|
<SimpleRoomHeader title="Directory" />
|
2015-07-21 06:11:24 +03:00
|
|
|
<div className="mx_RoomDirectory_list">
|
|
|
|
<input ref="roomAlias" placeholder="Join a room (e.g. #foo:domain.com)" className="mx_RoomDirectory_input" size="64" onKeyUp={ this.onKeyUp }/>
|
2016-04-20 14:29:32 +03:00
|
|
|
<GeminiScrollbar className="mx_RoomDirectory_tableWrapper"
|
|
|
|
relayoutOnUpdate={false} >
|
2016-02-20 16:36:48 +03:00
|
|
|
<table ref="directory_table" className="mx_RoomDirectory_table">
|
|
|
|
<tbody>
|
|
|
|
{ this.getRows(this.state.roomAlias) }
|
|
|
|
</tbody>
|
2015-07-23 20:24:34 +03:00
|
|
|
</table>
|
2016-02-22 12:35:11 +03:00
|
|
|
</GeminiScrollbar>
|
2015-07-21 06:11:24 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|