preview view model

This commit is contained in:
Bruno Windels 2020-11-27 21:28:54 +01:00
parent 7a6efbcf90
commit ead94695f1
7 changed files with 107 additions and 16 deletions

View File

@ -24,7 +24,7 @@ const EVENT_WITH_ROOMALIAS_PATTERN = /^[#]([^:]*):(.+)\/\$([^:]+):(.+)$/;
const USERID_PATTERN = /^@([^:]+):(.+)$/; const USERID_PATTERN = /^@([^:]+):(.+)$/;
const GROUPID_PATTERN = /^\+([^:]+):(.+)$/; const GROUPID_PATTERN = /^\+([^:]+):(.+)$/;
export const IdentifierKind = createEnum( const IdentifierKind = createEnum(
"RoomId", "RoomId",
"RoomAlias", "RoomAlias",
"UserId", "UserId",
@ -60,6 +60,9 @@ function orderedUnique(array) {
export class Link { export class Link {
static parseFragment(fragment) { static parseFragment(fragment) {
if (!fragment) {
return null;
}
let [identifier, queryParams] = fragment.split("?"); let [identifier, queryParams] = fragment.split("?");
let viaServers = []; let viaServers = [];
@ -117,7 +120,7 @@ export class Link {
const localPart = matches[1]; const localPart = matches[1];
return new Link(viaServers, IdentifierKind.GroupId, localPart, server); return new Link(viaServers, IdentifierKind.GroupId, localPart, server);
} }
return; return null;
} }
constructor(viaServers, identifierKind, localPart, server, messageLocalPart = null, messageServer = null) { constructor(viaServers, identifierKind, localPart, server, messageLocalPart = null, messageServer = null) {

36
src/RootViewModel.js Normal file
View File

@ -0,0 +1,36 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {Link} from "./Link.js";
import {ViewModel} from "./ViewModel.js";
import {PreviewViewModel} from "./preview/PreviewViewModel.js";
export class RootViewModel extends ViewModel {
constructor(request, hash) {
super();
this._request = request;
this.link = Link.parseFragment(hash);
this.previewViewModel = null;
}
load() {
if (this.link) {
this.previewViewModel = new PreviewViewModel(this._request, this.link);
this.emitChange();
this.previewViewModel.load();
}
}
}

View File

@ -1,16 +1,10 @@
import {xhrRequest} from "./utils/xhr.js"; import {xhrRequest} from "./utils/xhr.js";
import {validateHomeServer} from "./matrix/HomeServer.js"; import {RootViewModel} from "./RootViewModel.js";
import {Link, LinkKind} from "./Link.js";
export async function main() { export async function main(container) {
const link = Link.parseFragment(location.hash); const vm = new RootViewModel(xhrRequest, location.hash);
if (!link) { vm.load();
throw new Error("bad link"); window.__rootvm = vm;
} // const view = new RootView(vm);
const hs = await validateHomeServer(xhrRequest, link.servers[0]); // container.appendChild(view.mount());
if (link.kind === LinkKind.User) {
const profile = await hs.getUserProfile(link.identifier);
const imageURL = hs.mxcUrlThumbnail(profile.avatar_url, 64, 64, "crop");
console.log(imageURL);
}
} }

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export async function validateHomeServer(request, baseURL) { export async function resolveServer(request, baseURL) {
if (!baseURL.startsWith("http://") && !baseURL.startsWith("https://")) { if (!baseURL.startsWith("http://") && !baseURL.startsWith("https://")) {
baseURL = `https://${baseURL}`; baseURL = `https://${baseURL}`;
} }

View File

@ -0,0 +1,58 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {LinkKind} from "../Link.js";
import {ViewModel} from "../ViewModel.js";
import {resolveServer} from "./HomeServer.js";
export class PreviewViewModel extends ViewModel {
constructor(request, link) {
super();
this._link = link;
this._request = request;
this.loading = false;
this.name = null;
this.avatarURL = null;
}
async load() {
this.loading = true;
this.emitChange();
for (const server of this._link.servers) {
try {
const homeserver = await resolveServer(this._request, server);
switch (this._link.kind) {
case LinkKind.User:
await this._loadUserPreview(homeserver, this._link.identifier);
}
// assume we're done if nothing threw
break;
} catch (err) {
continue;
}
}
this.loading = false;
this.emitChange();
}
async _loadUserPreview(homeserver, userId) {
const profile = await homeserver.getUserProfile(userId);
this.name = profile.displayname || userId;
this.avatarURL = profile.avatar_url ?
homeserver.mxcUrlThumbnail(profile.avatar_url, 64, 64, "crop") :
null;
}
}