Merge pull request #219 from matrix-org/t3chguy/msc3266

This commit is contained in:
Michael Telatynski 2021-08-13 17:00:19 +01:00 committed by GitHub
commit c25a9dae4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 828 additions and 798 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules node_modules
build build
*.tar.gz *.tar.gz
/.idea

View file

@ -22,6 +22,10 @@
height: 64px; height: 64px;
} }
.PreviewView .mxSpace .avatar {
border-radius: 12px;
}
.PreviewView .defaultAvatar { .PreviewView .defaultAvatar {
width: 64px; width: 64px;
height: 64px; height: 64px;

View file

@ -45,3 +45,4 @@ const server = http.createServer(function onRequest (req, res) {
// Listen // Listen
server.listen(5000); server.listen(5000);
console.log("Listening on port 5000");

View file

@ -24,6 +24,7 @@ export async function resolveServer(request, baseURL) {
baseURL = `https://${baseURL}`; baseURL = `https://${baseURL}`;
} }
{ {
try {
const {status, body} = await request(`${baseURL}/.well-known/matrix/client`, {method: "GET"}).response(); const {status, body} = await request(`${baseURL}/.well-known/matrix/client`, {method: "GET"}).response();
if (status === 200) { if (status === 200) {
const proposedBaseURL = body?.['m.homeserver']?.base_url; const proposedBaseURL = body?.['m.homeserver']?.base_url;
@ -31,6 +32,9 @@ export async function resolveServer(request, baseURL) {
baseURL = noTrailingSlash(proposedBaseURL); baseURL = noTrailingSlash(proposedBaseURL);
} }
} }
} catch (e) {
console.warn("Failed to fetch ${baseURL}/.well-known/matrix/client", e);
}
} }
{ {
const {status} = await request(`${baseURL}/_matrix/client/versions`, {method: "GET"}).response(); const {status} = await request(`${baseURL}/_matrix/client/versions`, {method: "GET"}).response();
@ -52,6 +56,17 @@ export class HomeServer {
return body; 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) { async findPublicRoomById(roomId) {
const {body, status} = await this._request(`${this.baseURL}/_matrix/client/r0/directory/list/room/${encodeURIComponent(roomId)}`).response(); const {body, status} = await this._request(`${this.baseURL}/_matrix/client/r0/directory/list/room/${encodeURIComponent(roomId)}`).response();
if (status !== 200 || body.visibility !== "public") { if (status !== 200 || body.visibility !== "public") {

View file

@ -51,7 +51,7 @@ class LoadedPreviewView extends TemplateView {
return t.div({className: "defaultAvatar"}); return t.div({className: "defaultAvatar"});
} }
}); });
return t.div([ return t.div({className: vm.isSpaceRoom ? "mxSpace" : undefined}, [
t.div({className: "avatarContainer"}, avatar), t.div({className: "avatarContainer"}, avatar),
t.h1(vm => vm.name), t.h1(vm => vm.name),
t.p({className: {identifier: true, hidden: vm => !vm.identifier}}, vm => vm.identifier), t.p({className: {identifier: true, hidden: vm => !vm.identifier}}, vm => vm.identifier),

View file

@ -34,6 +34,7 @@ export class PreviewViewModel extends ViewModel {
this.topic = null; this.topic = null;
this.domain = null; this.domain = null;
this.failed = false; this.failed = false;
this.isSpaceRoom = false;
} }
async load() { async load() {
@ -88,6 +89,11 @@ export class PreviewViewModel extends ViewModel {
async _loadRoomPreview(homeserver, link) { async _loadRoomPreview(homeserver, link) {
let publicRoom; let publicRoom;
if (link.identifierKind === IdentifierKind.RoomId || link.identifierKind === IdentifierKind.RoomAlias) {
publicRoom = await homeserver.getRoomSummary(link.identifier, link.servers);
}
if (!publicRoom) {
if (link.identifierKind === IdentifierKind.RoomId) { if (link.identifierKind === IdentifierKind.RoomId) {
publicRoom = await homeserver.findPublicRoomById(link.identifier); publicRoom = await homeserver.findPublicRoomById(link.identifier);
} else if (link.identifierKind === IdentifierKind.RoomAlias) { } else if (link.identifierKind === IdentifierKind.RoomAlias) {
@ -96,6 +102,8 @@ export class PreviewViewModel extends ViewModel {
publicRoom = await homeserver.findPublicRoomById(roomId); publicRoom = await homeserver.findPublicRoomById(roomId);
} }
} }
}
this.name = publicRoom?.name || publicRoom?.canonical_alias || link.identifier; this.name = publicRoom?.name || publicRoom?.canonical_alias || link.identifier;
this.avatarUrl = publicRoom?.avatar_url ? this.avatarUrl = publicRoom?.avatar_url ?
homeserver.mxcUrlThumbnail(publicRoom.avatar_url, 64, 64, "crop") : homeserver.mxcUrlThumbnail(publicRoom.avatar_url, 64, 64, "crop") :
@ -103,6 +111,7 @@ export class PreviewViewModel extends ViewModel {
this.memberCount = publicRoom?.num_joined_members; this.memberCount = publicRoom?.num_joined_members;
this.topic = publicRoom?.topic; this.topic = publicRoom?.topic;
this.identifier = publicRoom?.canonical_alias || link.identifier; this.identifier = publicRoom?.canonical_alias || link.identifier;
this.isSpaceRoom = publicRoom?.room_type === "m.space";
if (this.identifier === this.name) { if (this.identifier === this.name) {
this.identifier = null; this.identifier = null;
} }