Skip to content

Initial MSC3266 implementation with tweaks for displaying Spaces #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
build
*.tar.gz
/.idea
4 changes: 4 additions & 0 deletions css/preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
height: 64px;
}

.PreviewView .mxSpace .avatar {
border-radius: 12px;
}

.PreviewView .defaultAvatar {
width: 64px;
height: 64px;
Expand Down
1 change: 1 addition & 0 deletions scripts/serve-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ const server = http.createServer(function onRequest (req, res) {

// Listen
server.listen(5000);
console.log("Listening on port 5000");
29 changes: 22 additions & 7 deletions src/preview/HomeServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ export async function resolveServer(request, baseURL) {
baseURL = `https://${baseURL}`;
}
{
const {status, body} = await request(`${baseURL}/.well-known/matrix/client`, {method: "GET"}).response();
if (status === 200) {
const proposedBaseURL = body?.['m.homeserver']?.base_url;
if (typeof proposedBaseURL === "string") {
baseURL = noTrailingSlash(proposedBaseURL);
}
}
try {
const {status, body} = await request(`${baseURL}/.well-known/matrix/client`, {method: "GET"}).response();
if (status === 200) {
const proposedBaseURL = body?.['m.homeserver']?.base_url;
if (typeof proposedBaseURL === "string") {
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();
Expand All @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion src/preview/PreviewView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 17 additions & 8 deletions src/preview/PreviewViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class PreviewViewModel extends ViewModel {
this.topic = null;
this.domain = null;
this.failed = false;
this.isSpaceRoom = false;
}

async load() {
Expand Down Expand Up @@ -88,21 +89,29 @@ 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") :
null;
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;
}
Expand Down