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
build
*.tar.gz
/.idea

View file

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

View file

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

View file

@ -24,6 +24,7 @@ export async function resolveServer(request, baseURL) {
baseURL = `https://${baseURL}`;
}
{
try {
const {status, body} = await request(`${baseURL}/.well-known/matrix/client`, {method: "GET"}).response();
if (status === 200) {
const proposedBaseURL = body?.['m.homeserver']?.base_url;
@ -31,6 +32,9 @@ export async function resolveServer(request, baseURL) {
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();
@ -52,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") {

View file

@ -51,7 +51,7 @@ class LoadedPreviewView extends TemplateView {
return t.div({className: "defaultAvatar"});
}
});
return t.div([
return t.div({className: vm.isSpaceRoom ? "mxSpace" : undefined}, [
t.div({className: "avatarContainer"}, avatar),
t.h1(vm => vm.name),
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.domain = null;
this.failed = false;
this.isSpaceRoom = false;
}
async load() {
@ -88,6 +89,11 @@ export class PreviewViewModel extends ViewModel {
async _loadRoomPreview(homeserver, link) {
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) {
publicRoom = await homeserver.findPublicRoomById(link.identifier);
} else if (link.identifierKind === IdentifierKind.RoomAlias) {
@ -96,6 +102,8 @@ export class PreviewViewModel extends ViewModel {
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") :
@ -103,6 +111,7 @@ export class PreviewViewModel extends ViewModel {
this.memberCount = publicRoom?.num_joined_members;
this.topic = publicRoom?.topic;
this.identifier = publicRoom?.canonical_alias || link.identifier;
this.isSpaceRoom = publicRoom?.room_type === "m.space";
if (this.identifier === this.name) {
this.identifier = null;
}