basic handling of event and group preview

This commit is contained in:
Bruno Windels 2020-12-04 16:38:10 +01:00
parent a705621dd5
commit 920a95296c
3 changed files with 39 additions and 58 deletions

View file

@ -19,9 +19,8 @@ import {orderedUnique} from "./utils/unique.js";
const ROOMALIAS_PATTERN = /^#([^:]*):(.+)$/; const ROOMALIAS_PATTERN = /^#([^:]*):(.+)$/;
const ROOMID_PATTERN = /^!([^:]*):(.+)$/; const ROOMID_PATTERN = /^!([^:]*):(.+)$/;
const EVENT_WITH_ROOMID_PATTERN = /^[!]([^:]*):(.+)\/\$([^:]+):(.+)$/;
const EVENT_WITH_ROOMALIAS_PATTERN = /^[#]([^:]*):(.+)\/\$([^:]+):(.+)$/;
const USERID_PATTERN = /^@([^:]+):(.+)$/; const USERID_PATTERN = /^@([^:]+):(.+)$/;
const EVENTID_PATTERN = /^$([^:]+):(.+)$/;
const GROUPID_PATTERN = /^\+([^:]+):(.+)$/; const GROUPID_PATTERN = /^\+([^:]+):(.+)$/;
export const IdentifierKind = createEnum( export const IdentifierKind = createEnum(
@ -71,7 +70,7 @@ export class Link {
if (!fragment) { if (!fragment) {
return null; return null;
} }
let [identifier, queryParams] = fragment.split("?"); let [linkStr, queryParams] = fragment.split("?");
let viaServers = []; let viaServers = [];
if (queryParams) { if (queryParams) {
@ -81,29 +80,13 @@ export class Link {
.map(([,value]) => value); .map(([,value]) => value);
} }
if (identifier.startsWith("#/")) { if (linkStr.startsWith("#/")) {
identifier = identifier.substr(2); linkStr = linkStr.substr(2);
} }
let kind; const [identifier, eventId] = linkStr.split("/");
let matches; 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); matches = USERID_PATTERN.exec(identifier);
if (matches) { if (matches) {
const server = matches[2]; const server = matches[2];
@ -114,13 +97,13 @@ export class Link {
if (matches) { if (matches) {
const server = matches[2]; const server = matches[2];
const localPart = matches[1]; 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); matches = ROOMID_PATTERN.exec(identifier);
if (matches) { if (matches) {
const server = matches[2]; const server = matches[2];
const localPart = matches[1]; 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); matches = GROUPID_PATTERN.exec(identifier);
if (matches) { if (matches) {
@ -131,16 +114,13 @@ export class Link {
return null; return null;
} }
constructor(viaServers, identifierKind, localPart, server, messageLocalPart = null, messageServer = null) { constructor(viaServers, identifierKind, localPart, server, eventId) {
const servers = [server]; const servers = [server];
if (messageServer) {
servers.push(messageServer);
}
servers.push(...viaServers); servers.push(...viaServers);
this.servers = orderedUnique(servers); this.servers = orderedUnique(servers);
this.identifierKind = identifierKind; this.identifierKind = identifierKind;
this.identifier = `${asPrefix(identifierKind)}${localPart}:${server}`; this.identifier = `${asPrefix(identifierKind)}${localPart}:${server}`;
this.eventId = messageLocalPart ? `$${messageLocalPart}:${messageServer}` : null; this.eventId = eventId;
} }
get kind() { get kind() {

View file

@ -47,10 +47,6 @@ export class HomeServer {
return body; return body;
} }
getGroupProfile(groupId) {
//`/_matrix/client/r0/groups/${groupId}/profile`
}
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

@ -37,32 +37,37 @@ export class PreviewViewModel extends ViewModel {
} }
async load() { async load() {
this.loading = true; const {kind} = this._link;
this.emitChange(); const supportsPreview = kind === LinkKind.User || kind === LinkKind.Room || kind === LinkKind.Event;
for (const server of this._consentedServers) { if (supportsPreview) {
try { this.loading = true;
const homeserver = await resolveServer(this.request, server); this.emitChange();
switch (this._link.kind) { for (const server of this._consentedServers) {
case LinkKind.User: try {
await this._loadUserPreview(homeserver, this._link.identifier); const homeserver = await resolveServer(this.request, server);
break; switch (this._link.kind) {
case LinkKind.Room: case LinkKind.User:
await this._loadRoomPreview(homeserver, this._link); await this._loadUserPreview(homeserver, this._link.identifier);
break; break;
} case LinkKind.Room:
// assume we're done if nothing threw case LinkKind.Event:
this.domain = server; await this._loadRoomPreview(homeserver, this._link);
this.loading = false; break;
this.emitChange(); }
return; // assume we're done if nothing threw
} catch (err) { this.domain = server;
continue; this.loading = false;
} this.emitChange();
} return;
} catch (err) {
continue;
}
}
}
this._setNoPreview(this._link);
this.loading = false; 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.domain = this._consentedServers[this._consentedServers.length - 1];
this.failed = true; this.failed = true;
} }