From 05ed8e15a0f65f5949e3832562f43d44f8a314cf Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 14 Jan 2022 10:16:44 +0100 Subject: [PATCH] Support for browsing seed projects Eg. /seeds/seed.cloudhead.io/projects/:urn Signed-off-by: Alexis Sellier --- src/api.ts | 2 + src/base/projects/Browser.svelte | 3 + src/base/projects/ProjectContentRoutes.svelte | 7 +- src/base/projects/Routes.svelte | 6 +- src/base/projects/View.svelte | 22 ++++--- src/base/projects/Widget.svelte | 2 + src/base/registrations/View.svelte | 4 +- src/base/registrations/registrar.ts | 36 +++++----- src/base/seeds/Seed.ts | 66 +++++++++++-------- src/base/seeds/View.svelte | 20 +++--- src/config.ts | 3 +- src/profile.ts | 2 +- src/project.ts | 14 +++- src/utils.ts | 5 +- 14 files changed, 110 insertions(+), 82 deletions(-) diff --git a/src/api.ts b/src/api.ts index 443fbb339..1f6726826 100644 --- a/src/api.ts +++ b/src/api.ts @@ -21,6 +21,8 @@ export async function get( const isLocalhost = /^0.0.0.0$/.test(base); const protocol = isLocalhost ? "http://" : "https://"; + path = path.startsWith("/") ? path.slice(1) : path; + const baseUrl = path ? `${protocol}${base}:${port}/v1/${path}` : `${protocol}${base}:${port}`; diff --git a/src/base/projects/Browser.svelte b/src/base/projects/Browser.svelte index 642c19a63..baa35e8b4 100644 --- a/src/base/projects/Browser.svelte +++ b/src/base/projects/Browser.svelte @@ -21,6 +21,7 @@ export let config: Config; export let org: string | null = null; export let user: string | null = null; + export let seed: string | null = null; export let tree: proj.Tree; export let project: proj.Info; export let branches: [string, string][]; @@ -78,6 +79,8 @@ navigate(proj.path({ urn, org, revision, path })); } else if (user) { navigate(proj.path({ urn, user, revision, path })); + } else if (seed) { + navigate(proj.path({ urn, seed, revision, path })); } else { navigate(proj.path({ urn, revision, path })); } diff --git a/src/base/projects/ProjectContentRoutes.svelte b/src/base/projects/ProjectContentRoutes.svelte index 0c04a6f2f..1486fe73f 100644 --- a/src/base/projects/ProjectContentRoutes.svelte +++ b/src/base/projects/ProjectContentRoutes.svelte @@ -11,6 +11,7 @@ export let org: string; export let tree: Tree; export let user: string; + export let seed: string; export let branches: [string, string][]; export let content: ProjectContent; export let revision: string; @@ -20,21 +21,21 @@ - - - - - + + + + diff --git a/src/base/projects/View.svelte b/src/base/projects/View.svelte index a8966fb10..bd76b6f93 100644 --- a/src/base/projects/View.svelte +++ b/src/base/projects/View.svelte @@ -9,6 +9,7 @@ import type { Info } from '@app/project'; import { formatOrg } from '@app/utils'; import { getOid } from '@app/project'; + import { Seed } from '@app/base/seeds/Seed'; import Header from '@app/base/projects/Header.svelte'; import ProjectContentRoutes from '@app/base/projects/ProjectContentRoutes.svelte'; @@ -16,6 +17,7 @@ export let urn: string; export let org = ""; export let user = ""; + export let seed = ""; export let config: Config; let parentName = formatOrg(org || user, config); @@ -24,16 +26,19 @@ let revision: string; let content: proj.ProjectContent; let path: string; - let getProject = new Promise(resolve => { + let getProject = new Promise<{ profile?: Profile; seed?: Seed } | null>(resolve => { if (org) { - Profile.get(org, ProfileType.Project, config).then(p => resolve(p)); + Profile.get(org, ProfileType.Project, config).then(p => resolve({ profile: p })); } else if (user) { - Profile.get(user, ProfileType.Project, config).then(p => resolve(p)); + Profile.get(user, ProfileType.Project, config).then(p => resolve({ profile: p })); + } else if (seed) { + Seed.get(config.withSeed({ host: seed })).then(s => resolve({ seed: s })); } else { resolve(null); } - }).then(async (profile) => { - const seed = profile?.seed; + }).then(async (result) => { + const profile = result?.profile; + const seed = profile?.seed ?? result?.seed; const cfg = seed ? config.withSeed(seed) : config; const info = await proj.getInfo(urn, cfg); projectInfo = info; @@ -65,7 +70,7 @@ } function updateRouteParams({ detail: newParams }: { detail: { path: string; revision: string; content: proj.ProjectContent } }) { - let newLocation = proj.path({ urn, user, org, content: newParams.content, revision: newParams.revision, path: newParams.path }); + let newLocation = proj.path({ urn, user, org, seed, content: newParams.content, revision: newParams.revision, path: newParams.path }); if (newLocation !== window.location.pathname) { navigate(newLocation); } @@ -75,8 +80,7 @@ } const back = () => window.history.back(); - // React to changes to the project commit. We have to manually - // set the URL as well, to match the current commit. + $: projectRoot = proj.path({ urn, user, org }); @@ -169,7 +173,7 @@ branches={result.branches} profile={result.profile} on:routeParamsChange={updateRouteParams} /> - r.status == "fulfilled" && r.value ? r.value : undefined); - return { - resolver, - profile: { - name, - url, - avatar, - seed: new Seed( - config, - seedHost, - seedId, - seedGit, - seedApi, - ), - anchorsAccount, - address, - twitter, - github, - }, + const profile: EnsProfile = { + name, + url, + avatar, + anchorsAccount, + address, + twitter, + github, }; + + if (seedHost && seedId) { + profile.seed = new Seed(seedHost, seedId, seedGit, seedApi); + } + + return { resolver, profile }; } export async function getAvatar(name: string, config: Config, resolver?: EnsResolver | null): Promise { @@ -142,7 +138,7 @@ export async function getSeed(name: string, config: Config, resolver?: EnsResolv resolver.getText('eth.radicle.seed.api'), ]); - return new Seed(config, host, id, git, api); + return new Seed(host, id, git, api); } export function registrar(config: Config): ethers.Contract { diff --git a/src/base/seeds/Seed.ts b/src/base/seeds/Seed.ts index c43e301ca..16ef0922f 100644 --- a/src/base/seeds/Seed.ts +++ b/src/base/seeds/Seed.ts @@ -1,46 +1,58 @@ import * as api from '@app/api'; import type { Config } from '@app/config'; -import { getInfo, getProjects } from '@app/project'; -import type { Info, Project } from "@app/project"; +import * as proj from '@app/project'; +import type { Project } from "@app/project"; import { isDomain } from '@app/utils'; - -export interface SeedInfo { - version: string; -} +import { assert } from '@app/error'; export class Seed { - host?: string; - id?: string; + host: string; + id: string; + + api?: string; git?: string; - api?: string; - config: Config; + version?: string; + + constructor(host: string, id: string, git?: string, api?: string) { + assert(isDomain(host)); + assert(/^[a-z0-9]+$/.test(id)); + + this.host = host; + this.id = id; - constructor(config: Config, host?: string, id?: string, git?: string, api?: string) { - if (id && /^[a-z0-9]+$/.test(id)) { - this.id = id; - } - if (host && isDomain(host)) { - this.host = host; - } if (api && isDomain(api)) { this.api = api; } if (git && isDomain(git)) { this.git = git; } - this.config = config.withSeed(this); } - async getInfo(): Promise { - return api.get(``, {}, this.config); + + static async getPeer(config: Config): Promise<{ id: string }> { + return api.get("/peer", {}, config); } - async getPeer(): Promise<{ id: string }> { - return api.get("peer", {}, this.config); + + static async getProject(urn: string, config: Config): Promise { + return proj.getInfo(urn, config); } - async getProject(urn: string): Promise { - return getInfo(urn, this.config); - } - async getProjects(): Promise { - const result = await getProjects(this.config); + + static async getProjects(config: Config): Promise { + const result = await proj.getProjects(config); return result.map((project: any) => ({ ...project, id: project.urn })); } + + static async get(config: Config): Promise { + assert(config.seed.api.host); + + const [info, peer] = await Promise.all([ + api.get("/", {}, config), + Seed.getPeer(config), + ]); + + return { + host: config.seed.api.host, + id: peer.id, + version: info.version, + }; + } } diff --git a/src/base/seeds/View.svelte b/src/base/seeds/View.svelte index 71b15729b..a98ed9780 100644 --- a/src/base/seeds/View.svelte +++ b/src/base/seeds/View.svelte @@ -8,7 +8,7 @@ export let config: Config; export let seedAddress: string; - const seed = new Seed(config, seedAddress); + config = config.withSeed({ host: seedAddress }); - {seed.host} + {seedAddress} -{#await seed.getInfo()} +{#await Seed.get(config)}
@@ -83,7 +83,7 @@
- {seed.host} + {seedAddress}
@@ -92,17 +92,15 @@
Seed
- {#if info.version === "0.2.0" && seed.host} - {#await seed.getPeer() then peer} - - {/await} + {#if info.version === "0.2.0" && info.host} + {:else}
N/A
{/if}
API Port
-
{seed.config.seed.api.port}
+
{config.seed.api.port}
Version
@@ -111,11 +109,11 @@
{#if info.version === "0.2.0"} - {#await seed.getProjects() then projects} + {#await Seed.getProjects(config) then projects}
{#each projects as project}
- +
{/each}
diff --git a/src/config.ts b/src/config.ts index 38594fcf2..d4905d832 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,7 +7,6 @@ import { Core } from '@self.id/core'; import WalletConnect from "@walletconnect/client"; import config from "@app/config.json"; import { WalletConnectSigner } from "./WalletConnectSigner"; -import type { Seed } from '@app/base/seeds/Seed'; declare global { interface Window { @@ -138,7 +137,7 @@ export class Config { } // Return the config with an overwritten seed URL. - withSeed(seed: Seed): Config { + withSeed(seed: { host: string; id?: string; api?: string; git?: string }): Config { const cfg = {} as Config; Object.assign(cfg, this); diff --git a/src/profile.ts b/src/profile.ts index fcf8f4a4d..474afe9a4 100644 --- a/src/profile.ts +++ b/src/profile.ts @@ -75,7 +75,7 @@ export class Profile { } get seedId(): string | undefined { - return this.profile?.ens?.seed.id; + return this.profile?.ens?.seed?.id; } get seed(): Seed | undefined { diff --git a/src/project.ts b/src/project.ts index 09aee87e4..253da7a2d 100644 --- a/src/project.ts +++ b/src/project.ts @@ -138,15 +138,25 @@ export async function getReadme( } export function path( - opts: { urn: string; org?: string; content?: ProjectContent; user?: string; revision?: string; path?: string } + opts: { + urn: string; + org?: string; + user?: string; + seed?: string; + content?: ProjectContent; + revision?: string; + path?: string; + } ): string { - const { urn, org, user, content, revision, path } = opts; + const { urn, org, user, seed, content, revision, path } = opts; const result = []; if (org) { result.push("orgs", org); } else if (user) { result.push("users", user); + } else if (seed) { + result.push("seeds", seed); } result.push("projects", urn); diff --git a/src/utils.ts b/src/utils.ts index 446af8104..e05f56d3f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -243,9 +243,8 @@ export async function querySubgraph( const json = await response.json(); if (json.errors) { - for (const e of json.errors) { - console.error("querySubgraph:", e.message); - } + console.error("querySubgraph:", json.errors); + if (retries > 0) querySubgraph(url, query, variables, retries - 1); else return null; }