Skip to content

Commit c12839d

Browse files
authored
Merge pull request element-hq#1680 from vector-im/dbkr/delete_alias
Add ability to delete an alias from room directory
2 parents eca453e + c35c9f7 commit c12839d

File tree

1 file changed

+91
-44
lines changed

1 file changed

+91
-44
lines changed

src/components/structures/RoomDirectory.js

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ module.exports = React.createClass({
5252
},
5353

5454
componentDidMount: function() {
55+
this.getPublicRooms();
56+
},
57+
58+
componentWillUnmount: function() {
59+
// dis.dispatch({
60+
// action: 'ui_opacity',
61+
// sideOpacity: 1.0,
62+
// middleOpacity: 1.0,
63+
// });
64+
},
65+
66+
getPublicRooms: function() {
5567
var self = this;
5668
MatrixClientPeg.get().publicRooms(function (err, data) {
5769
if (err) {
@@ -68,54 +80,80 @@ module.exports = React.createClass({
6880
publicRooms: data.chunk,
6981
loading: false,
7082
});
71-
self.forceUpdate();
7283
}
7384
});
7485
},
7586

76-
componentWillUnmount: function() {
77-
// dis.dispatch({
78-
// action: 'ui_opacity',
79-
// sideOpacity: 1.0,
80-
// middleOpacity: 1.0,
81-
// });
82-
},
87+
/**
88+
* A limited interface for removing rooms from the directory.
89+
* Will set the room to not be publicly visible and delete the
90+
* default alias. In the long term, it would be better to allow
91+
* HS admins to do this through the RoomSettings interface, but
92+
* this needs SPEC-417.
93+
*/
94+
removeFromDirectory: function(room) {
95+
var alias = get_display_alias_for_room(room);
96+
var name = room.name || alias || "Unnamed room";
8397

84-
showRoom: function(roomId, roomAlias) {
85-
// extract the metadata from the publicRooms structure to pass
86-
// as out-of-band data to view_room, because we get information
87-
// here that we can't get other than by joining the room in some
88-
// cases.
89-
var room;
90-
if (roomId) {
91-
for (var i = 0; i < this.state.publicRooms.length; ++i) {
92-
if (this.state.publicRooms[i].room_id == roomId) {
93-
room = this.state.publicRooms[i];
94-
break;
95-
}
98+
var QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
99+
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
100+
101+
Modal.createDialog(QuestionDialog, {
102+
title: "Remove from Directory",
103+
description: `Delete the room alias '${alias}' and remove '${name}' from the directory?`,
104+
onFinished: (should_delete) => {
105+
if (!should_delete) return;
106+
107+
var Loader = sdk.getComponent("elements.Spinner");
108+
var modal = Modal.createDialog(Loader);
109+
var step = `remove '${name}' from the directory.`;
110+
111+
MatrixClientPeg.get().setRoomDirectoryVisibility(room.room_id, 'private').then(() => {
112+
step = 'delete the alias.';
113+
return MatrixClientPeg.get().deleteAlias(alias);
114+
}).done(() => {
115+
modal.close();
116+
this.getPublicRooms();
117+
}, function(err) {
118+
modal.close();
119+
this.getPublicRooms();
120+
Modal.createDialog(ErrorDialog, {
121+
title: "Failed to "+step,
122+
description: err.toString()
123+
});
124+
});
96125
}
126+
});
127+
},
128+
129+
showRoom: function(room, ev) {
130+
if (ev.shiftKey) {
131+
ev.preventDefault();
132+
this.removeFromDirectory(room);
133+
return;
97134
}
135+
98136
var oob_data = {};
99-
if (room) {
100-
if (MatrixClientPeg.get().isGuest()) {
101-
if (!room.world_readable && !room.guest_can_join) {
102-
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
103-
Modal.createDialog(NeedToRegisterDialog, {
104-
title: "Failed to join the room",
105-
description: "This room is inaccessible to guests. You may be able to join if you register."
106-
});
107-
return;
108-
}
137+
if (MatrixClientPeg.get().isGuest()) {
138+
if (!room.world_readable && !room.guest_can_join) {
139+
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
140+
Modal.createDialog(NeedToRegisterDialog, {
141+
title: "Failed to join the room",
142+
description: "This room is inaccessible to guests. You may be able to join if you register."
143+
});
144+
return;
109145
}
110-
111-
oob_data = {
112-
avatarUrl: room.avatar_url,
113-
// XXX: This logic is duplicated from the JS SDK which
114-
// would normally decide what the name is.
115-
name: room.name || room.canonical_alias || (room.aliases ? room.aliases[0] : "Unnamed room"),
116-
};
117146
}
118147

148+
var room_alias = get_display_alias_for_room(room);
149+
150+
oob_data = {
151+
avatarUrl: room.avatar_url,
152+
// XXX: This logic is duplicated from the JS SDK which
153+
// would normally decide what the name is.
154+
name: room.name || room_alias || "Unnamed room",
155+
};
156+
119157
var payload = {
120158
oob_data: oob_data,
121159
action: 'view_room',
@@ -124,10 +162,10 @@ module.exports = React.createClass({
124162
// which servers to start querying. However, there's no other way to join rooms in
125163
// this list without aliases at present, so if roomAlias isn't set here we have no
126164
// choice but to supply the ID.
127-
if (roomAlias) {
128-
payload.room_alias = roomAlias;
165+
if (room_alias) {
166+
payload.room_alias = room_alias;
129167
} else {
130-
payload.room_id = roomId;
168+
payload.room_id = room.room_id;
131169
}
132170
dis.dispatch(payload);
133171
},
@@ -150,8 +188,7 @@ module.exports = React.createClass({
150188
var self = this;
151189
var guestRead, guestJoin, perms;
152190
for (var i = 0; i < rooms.length; i++) {
153-
var alias = rooms[i].canonical_alias || (rooms[i].aliases ? rooms[i].aliases[0] : "");
154-
var name = rooms[i].name || alias || "Unnamed room";
191+
var name = rooms[i].name || get_display_alias_for_room(rooms[i]) || "Unnamed room";
155192
guestRead = null;
156193
guestJoin = null;
157194

@@ -175,7 +212,11 @@ module.exports = React.createClass({
175212
topic = linkifyString(sanitizeHtml(topic));
176213

177214
rows.unshift(
178-
<tr key={ rooms[i].room_id } onClick={self.showRoom.bind(null, rooms[i].room_id, alias)}>
215+
<tr key={ rooms[i].room_id }
216+
onClick={self.showRoom.bind(null, rooms[i])}
217+
// cancel onMouseDown otherwise shift-clicking highlights text
218+
onMouseDown={(ev) => {ev.preventDefault();}}
219+
>
179220
<td className="mx_RoomDirectory_roomAvatar">
180221
<BaseAvatar width={24} height={24} resizeMethod='crop'
181222
name={ name } idName={ name }
@@ -189,7 +230,7 @@ module.exports = React.createClass({
189230
<div className="mx_RoomDirectory_topic"
190231
onClick={ function(e) { e.stopPropagation() } }
191232
dangerouslySetInnerHTML={{ __html: topic }}/>
192-
<div className="mx_RoomDirectory_alias">{ alias }</div>
233+
<div className="mx_RoomDirectory_alias">{ get_display_alias_for_room(rooms[i]) }</div>
193234
</td>
194235
<td className="mx_RoomDirectory_roomMemberCount">
195236
{ rooms[i].num_joined_members }
@@ -237,3 +278,9 @@ module.exports = React.createClass({
237278
);
238279
}
239280
});
281+
282+
// Similar to matrix-react-sdk's MatrixTools.getDisplayAliasForRoom
283+
// but works with the objects we get from the public room list
284+
function get_display_alias_for_room(room) {
285+
return room.canonical_alias || (room.aliases ? room.aliases[0] : "");
286+
}

0 commit comments

Comments
 (0)