From b3929fef74a0289cd30acea2e4d2a0773013c96c Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 Dec 2021 15:54:09 +0100 Subject: [PATCH] Add stale anchors feature Signed-off-by: Sebastian Martinez --- src/base/projects/Browser.svelte | 57 ++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/src/base/projects/Browser.svelte b/src/base/projects/Browser.svelte index e30bb2278..62acd1a3b 100644 --- a/src/base/projects/Browser.svelte +++ b/src/base/projects/Browser.svelte @@ -2,9 +2,9 @@ import { navigate } from 'svelte-routing'; import type { Config } from '@app/config'; import type { Profile } from '@app/profile'; + import { ethers } from "ethers"; import * as proj from '@app/project'; import Loading from '@app/Loading.svelte'; - import { Org } from '@app/base/orgs/Org'; import * as utils from '@app/utils'; import Tree from './Tree.svelte'; @@ -87,10 +87,36 @@ mobileFileTree = !mobileFileTree; }; + const GetAllAnchors = ` + query GetAllAnchors($project: Bytes!, $org: ID!) { + anchors(orderBy: timestamp, orderDirection: desc, where: { objectId: $project, org: $org }) { + multihash + timestamp + } + } + `; + + interface AnchorObject { + timestamp: number; + commit: string; + multihash: string; + } + + async function getAllAnchors(anchors: string | null, urn: string): Promise { + if (! anchors) { + return null; + } + const unpadded = utils.decodeRadicleId(urn); + const id = ethers.utils.hexZeroPad(unpadded, 32); + const allAnchors = await utils.querySubgraph(config.orgs.subgraph, GetAllAnchors, { project: id, org: anchors }); + return allAnchors.anchors + .map((anchor: AnchorObject) => utils.formatProjectHash(ethers.utils.arrayify(anchor.multihash))); + } + // This is reactive to respond to path changes that don't originate from this // component, eg. when using the browser's "back" button. $: getBlob = loadBlob(path); - $: getAnchor = anchors ? Org.getAnchor(anchors, urn, config) : null; + $: getAnchor = anchors ? getAllAnchors(anchors, urn) : null; $: loadingPath = state.status == Status.Loading ? state.path : null; @@ -343,21 +369,30 @@ {#await getAnchor} {:then anchor} - {#if anchor === commit} - {#if commit === project.head} + {#if anchor} + + {#if commit == anchor[0] && commit === project.head} - anchored 🔒 + latest 🔐 - {:else} + + {:else if commit == anchor[0] && commit !== project.head} navigateBrowser(project.head)}> - anchored 🔒 + latest 🔐 + + + {:else if anchor?.includes(commit)} + navigateBrowser(anchor[0])}> + stale 🔒 + + + {:else} + navigateBrowser(anchor[0])}> + not anchored 🔓 {/if} - {:else if anchor} - navigateBrowser(anchor)}> - not anchored 🔓 - {:else} + not anchored 🔓