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,6 +37,9 @@ export class PreviewViewModel extends ViewModel {
} }
async load() { async load() {
const {kind} = this._link;
const supportsPreview = kind === LinkKind.User || kind === LinkKind.Room || kind === LinkKind.Event;
if (supportsPreview) {
this.loading = true; this.loading = true;
this.emitChange(); this.emitChange();
for (const server of this._consentedServers) { for (const server of this._consentedServers) {
@ -47,6 +50,7 @@ export class PreviewViewModel extends ViewModel {
await this._loadUserPreview(homeserver, this._link.identifier); await this._loadUserPreview(homeserver, this._link.identifier);
break; break;
case LinkKind.Room: case LinkKind.Room:
case LinkKind.Event:
await this._loadRoomPreview(homeserver, this._link); await this._loadRoomPreview(homeserver, this._link);
break; break;
} }
@ -59,10 +63,11 @@ export class PreviewViewModel extends ViewModel {
continue; 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;
} }