diff --git a/src/preview/HomeServer.js b/src/preview/HomeServer.js index a3ae289..6123d92 100644 --- a/src/preview/HomeServer.js +++ b/src/preview/HomeServer.js @@ -56,6 +56,17 @@ export class HomeServer { return body; } + // MSC3266 implementation + async getRoomSummary(roomIdOrAlias, viaServers) { + let query; + if (viaServers.length > 0) { + query = "?" + viaServers.map(server => `via=${encodeURIComponent(server)}`).join('&'); + } + const {body, status} = await this._request(`${this.baseURL}/_matrix/client/unstable/im.nheko.summary/rooms/${encodeURIComponent(roomIdOrAlias)}/summary${query}`).response(); + if (status !== 200) return; + return body; + } + async findPublicRoomById(roomId) { const {body, status} = await this._request(`${this.baseURL}/_matrix/client/r0/directory/list/room/${encodeURIComponent(roomId)}`).response(); if (status !== 200 || body.visibility !== "public") { diff --git a/src/preview/PreviewViewModel.js b/src/preview/PreviewViewModel.js index 50843e6..bc507cc 100644 --- a/src/preview/PreviewViewModel.js +++ b/src/preview/PreviewViewModel.js @@ -88,14 +88,21 @@ export class PreviewViewModel extends ViewModel { async _loadRoomPreview(homeserver, link) { let publicRoom; - if (link.identifierKind === IdentifierKind.RoomId) { - publicRoom = await homeserver.findPublicRoomById(link.identifier); - } else if (link.identifierKind === IdentifierKind.RoomAlias) { - const roomId = await homeserver.getRoomIdFromAlias(link.identifier); - if (roomId) { - publicRoom = await homeserver.findPublicRoomById(roomId); - } - } + if (link.identifierKind === IdentifierKind.RoomId || link.identifierKind === IdentifierKind.RoomAlias) { + publicRoom = await homeserver.getRoomSummary(link.identifier, link.servers); + } + + if (!publicRoom) { + if (link.identifierKind === IdentifierKind.RoomId) { + publicRoom = await homeserver.findPublicRoomById(link.identifier); + } else if (link.identifierKind === IdentifierKind.RoomAlias) { + const roomId = await homeserver.getRoomIdFromAlias(link.identifier); + if (roomId) { + publicRoom = await homeserver.findPublicRoomById(roomId); + } + } + } + this.name = publicRoom?.name || publicRoom?.canonical_alias || link.identifier; this.avatarUrl = publicRoom?.avatar_url ? homeserver.mxcUrlThumbnail(publicRoom.avatar_url, 64, 64, "crop") :