From 920a95296cf84567fe631d779bac42c3b803ce2f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 4 Dec 2020 16:38:10 +0100 Subject: [PATCH] basic handling of event and group preview --- src/Link.js | 40 +++++++------------------ src/preview/HomeServer.js | 4 --- src/preview/PreviewViewModel.js | 53 ++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 58 deletions(-) diff --git a/src/Link.js b/src/Link.js index 1ccc4d0..7d7b148 100644 --- a/src/Link.js +++ b/src/Link.js @@ -19,9 +19,8 @@ import {orderedUnique} from "./utils/unique.js"; const ROOMALIAS_PATTERN = /^#([^:]*):(.+)$/; const ROOMID_PATTERN = /^!([^:]*):(.+)$/; -const EVENT_WITH_ROOMID_PATTERN = /^[!]([^:]*):(.+)\/\$([^:]+):(.+)$/; -const EVENT_WITH_ROOMALIAS_PATTERN = /^[#]([^:]*):(.+)\/\$([^:]+):(.+)$/; const USERID_PATTERN = /^@([^:]+):(.+)$/; +const EVENTID_PATTERN = /^$([^:]+):(.+)$/; const GROUPID_PATTERN = /^\+([^:]+):(.+)$/; export const IdentifierKind = createEnum( @@ -71,7 +70,7 @@ export class Link { if (!fragment) { return null; } - let [identifier, queryParams] = fragment.split("?"); + let [linkStr, queryParams] = fragment.split("?"); let viaServers = []; if (queryParams) { @@ -81,29 +80,13 @@ export class Link { .map(([,value]) => value); } - if (identifier.startsWith("#/")) { - identifier = identifier.substr(2); + if (linkStr.startsWith("#/")) { + linkStr = linkStr.substr(2); } - let kind; + const [identifier, eventId] = linkStr.split("/"); + let matches; - // longest first, so they dont get caught by ROOMALIAS_PATTERN and ROOMID_PATTERN - matches = EVENT_WITH_ROOMID_PATTERN.exec(identifier); - if (matches) { - const roomServer = matches[2]; - const messageServer = matches[4]; - const roomLocalPart = matches[1]; - const messageLocalPart = matches[3]; - return new Link(viaServers, IdentifierKind.RoomId, roomLocalPart, roomServer, messageLocalPart, messageServer); - } - matches = EVENT_WITH_ROOMALIAS_PATTERN.exec(identifier); - if (matches) { - const roomServer = matches[2]; - const messageServer = matches[4]; - const roomLocalPart = matches[1]; - const messageLocalPart = matches[3]; - return new Link(viaServers, IdentifierKind.RoomAlias, roomLocalPart, roomServer, messageLocalPart, messageServer); - } matches = USERID_PATTERN.exec(identifier); if (matches) { const server = matches[2]; @@ -114,13 +97,13 @@ export class Link { if (matches) { const server = matches[2]; const localPart = matches[1]; - return new Link(viaServers, IdentifierKind.RoomAlias, localPart, server); + return new Link(viaServers, IdentifierKind.RoomAlias, localPart, server, eventId); } matches = ROOMID_PATTERN.exec(identifier); if (matches) { const server = matches[2]; const localPart = matches[1]; - return new Link(viaServers, IdentifierKind.RoomId, localPart, server); + return new Link(viaServers, IdentifierKind.RoomId, localPart, server, eventId); } matches = GROUPID_PATTERN.exec(identifier); if (matches) { @@ -131,16 +114,13 @@ export class Link { return null; } - constructor(viaServers, identifierKind, localPart, server, messageLocalPart = null, messageServer = null) { + constructor(viaServers, identifierKind, localPart, server, eventId) { const servers = [server]; - if (messageServer) { - servers.push(messageServer); - } servers.push(...viaServers); this.servers = orderedUnique(servers); this.identifierKind = identifierKind; this.identifier = `${asPrefix(identifierKind)}${localPart}:${server}`; - this.eventId = messageLocalPart ? `$${messageLocalPart}:${messageServer}` : null; + this.eventId = eventId; } get kind() { diff --git a/src/preview/HomeServer.js b/src/preview/HomeServer.js index 7736e8c..d0c19dd 100644 --- a/src/preview/HomeServer.js +++ b/src/preview/HomeServer.js @@ -47,10 +47,6 @@ export class HomeServer { return body; } - getGroupProfile(groupId) { - //`/_matrix/client/r0/groups/${groupId}/profile` - } - 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 03c72f4..d025782 100644 --- a/src/preview/PreviewViewModel.js +++ b/src/preview/PreviewViewModel.js @@ -37,32 +37,37 @@ export class PreviewViewModel extends ViewModel { } async load() { - this.loading = true; - this.emitChange(); - for (const server of this._consentedServers) { - try { - const homeserver = await resolveServer(this.request, server); - switch (this._link.kind) { - case LinkKind.User: - await this._loadUserPreview(homeserver, this._link.identifier); - break; - case LinkKind.Room: - await this._loadRoomPreview(homeserver, this._link); - break; - } - // assume we're done if nothing threw - this.domain = server; - this.loading = false; - this.emitChange(); - return; - } catch (err) { - continue; - } - } + const {kind} = this._link; + const supportsPreview = kind === LinkKind.User || kind === LinkKind.Room || kind === LinkKind.Event; + if (supportsPreview) { + this.loading = true; + this.emitChange(); + for (const server of this._consentedServers) { + try { + const homeserver = await resolveServer(this.request, server); + switch (this._link.kind) { + case LinkKind.User: + await this._loadUserPreview(homeserver, this._link.identifier); + break; + case LinkKind.Room: + case LinkKind.Event: + await this._loadRoomPreview(homeserver, this._link); + break; + } + // assume we're done if nothing threw + this.domain = server; + this.loading = false; + this.emitChange(); + return; + } catch (err) { + continue; + } + } + } - this._setNoPreview(this._link); this.loading = false; - if (this._consentedServers.length) { + this._setNoPreview(this._link); + if (this._consentedServers.length && supportsPreview) { this.domain = this._consentedServers[this._consentedServers.length - 1]; this.failed = true; }