Skip to content

Commit

Permalink
add releases to getPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarinas committed Oct 12, 2023
1 parent c215091 commit a62d848
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 153 deletions.
115 changes: 45 additions & 70 deletions modules/client/src/internal/client/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
PluginRepo,
PluginRepoBuildMetadata,
PluginRepoListItem,
PluginRepoRelease,
PluginRepoReleaseMetadata,
PluginSortBy,
PluginUpdateProposalInValidityCause,
Expand Down Expand Up @@ -97,8 +96,6 @@ import {
toDaoDetails,
toDaoListItem,
toPluginRepo,
toPluginRepoListItem,
toPluginRepoRelease,
toTokenTransfer,
} from "../utils";
import { isAddress } from "@ethersproject/address";
Expand Down Expand Up @@ -126,6 +123,7 @@ import {
DaoAction,
DaoCreationError,
DecodedApplyUpdateParams,
EmptyMultiUriError,
FailedDepositError,
findLog,
getNamedTypesFromMetadata,
Expand Down Expand Up @@ -967,6 +965,47 @@ export class ClientMethods extends ClientCore implements IClientMethods {
toTokenTransfer(transfer),
);
}
private async getMetadata(
ipfsUri: string,
) {
const metadataCid = resolveIpfsCid(ipfsUri);
const stringMetadata = await this.ipfs.fetchString(metadataCid);
const resolvedMetadata = JSON.parse(stringMetadata);
return resolvedMetadata;
}

private async getPluginRepo(
pluginRepo: SubgraphPluginRepo,
): Promise<PluginRepo> {
let releaseMetadata: PluginRepoReleaseMetadata;
// releases are ordered son the index 0 will be the latest
const releaseIpfsUri = pluginRepo?.releases[0]?.metadata;
try {
releaseMetadata = await this.getMetadata(releaseIpfsUri);
} catch (err) {
releaseMetadata = UNAVAILABLE_RELEASE_METADATA;
if (err instanceof InvalidCidError) {
releaseMetadata = UNSUPPORTED_RELEASE_METADATA_LINK;
} else if (err instanceof EmptyMultiUriError) {
releaseMetadata = EMPTY_RELEASE_METADATA_LINK;
}
}

let buildMetadata: PluginRepoBuildMetadata;
// builds are ordered son the index 0 will be the latest
const buildIpfsUri = pluginRepo?.releases[0]?.builds[0]?.metadata;
try {
buildMetadata = await this.getMetadata(buildIpfsUri);
} catch (err) {
buildMetadata = UNAVAILABLE_BUILD_METADATA;
if (err instanceof InvalidCidError) {
buildMetadata = UNSUPPORTED_BUILD_METADATA_LINK;
} else if (err instanceof EmptyMultiUriError) {
buildMetadata = EMPTY_BUILD_METADATA_LINK;
}
}
return toPluginRepo(pluginRepo, releaseMetadata, buildMetadata);
}

/**
* Retrieves the list of plugins available on the PluginRegistry
Expand Down Expand Up @@ -1017,37 +1056,8 @@ export class ClientMethods extends ClientCore implements IClientMethods {
});
return Promise.all(
pluginRepos.map(
async (
pluginRepo: SubgraphPluginRepoListItem,
): Promise<PluginRepoListItem> => {
let pluginRepoReleases: PluginRepoRelease[] = [];
for (const release of pluginRepo.releases) {
let metadata: PluginRepoReleaseMetadata;
if (!release.metadata) {
metadata = EMPTY_RELEASE_METADATA_LINK;
} else {
try {
const metadataCid = resolveIpfsCid(release.metadata);
// Avoid blocking Promise.all if this individual fetch takes too long
const stringMetadata = await promiseWithTimeout(
this.ipfs.fetchString(metadataCid),
MULTI_FETCH_TIMEOUT,
);
const resolvedMetadata = JSON.parse(stringMetadata);
metadata = resolvedMetadata;
} catch (err) {
metadata = UNAVAILABLE_RELEASE_METADATA;
if (err instanceof InvalidCidError) {
metadata = UNSUPPORTED_RELEASE_METADATA_LINK;
}
}
}
pluginRepoReleases = [
...pluginRepoReleases,
toPluginRepoRelease(release, metadata),
];
}
return toPluginRepoListItem(pluginRepo, pluginRepoReleases);
(pluginRepo: SubgraphPluginRepoListItem) => {
return this.getPluginRepo(pluginRepo);
},
),
);
Expand All @@ -1070,42 +1080,7 @@ export class ClientMethods extends ClientCore implements IClientMethods {
name,
});
// get release metadata
let releaseMetadata: PluginRepoReleaseMetadata;
if (!pluginRepo.releases[0].metadata) {
releaseMetadata = EMPTY_RELEASE_METADATA_LINK;
} else {
try {
const metadataCid = resolveIpfsCid(pluginRepo.releases[0].metadata);
const stringMetadata = await this.ipfs.fetchString(metadataCid);
const resolvedMetadata = JSON.parse(stringMetadata);
releaseMetadata = resolvedMetadata;
} catch (err) {
releaseMetadata = UNAVAILABLE_RELEASE_METADATA;
if (err instanceof InvalidCidError) {
releaseMetadata = UNSUPPORTED_RELEASE_METADATA_LINK;
}
}
}
// get build metadata
let buildMetadata: PluginRepoBuildMetadata;
if (!pluginRepo.releases[0].builds[0].metadata) {
buildMetadata = EMPTY_BUILD_METADATA_LINK;
} else {
try {
const metadataCid = resolveIpfsCid(
pluginRepo.releases[0].builds[0].metadata,
);
const stringMetadata = await this.ipfs.fetchString(metadataCid);
const resolvedMetadata = JSON.parse(stringMetadata);
buildMetadata = resolvedMetadata;
} catch (err) {
buildMetadata = UNAVAILABLE_BUILD_METADATA;
if (err instanceof InvalidCidError) {
buildMetadata = UNSUPPORTED_BUILD_METADATA_LINK;
}
}
}
return toPluginRepo(pluginRepo, releaseMetadata, buildMetadata);
return this.getPluginRepo(pluginRepo);
}
/**
* Returns the protocol version of a contract
Expand Down
10 changes: 6 additions & 4 deletions modules/client/src/internal/graphql-queries/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ query Plugins($where: PluginRepo_filter!, $limit:Int!, $skip: Int!, $direction:
pluginRepos(where: $where, first: $limit, skip: $skip, orderDirection: $direction, orderBy: $sortBy){
id
subdomain
releases{
releases (orderBy: release, orderDirection: desc){
release
metadata
builds{
builds (orderBy: build, orderDirection: desc) {
build
metadata
}
}
}
Expand All @@ -19,11 +20,12 @@ query Plugins($where: PluginRepo_filter!, $limit:Int!, $skip: Int!, $direction:
export const QueryPlugin = gql`
query Plugin($id: ID!) {
pluginRepo(id:$id){
id
subdomain
releases(orderBy: release, orderDirection: desc, first: 1){
releases(orderBy: release, orderDirection: desc){
release
metadata
builds(orderBy: build, orderDirection: desc, first: 1){
builds(orderBy: build, orderDirection: desc){
build
metadata
}
Expand Down
30 changes: 12 additions & 18 deletions modules/client/src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,30 +149,24 @@ export type ContractPermissionWithConditionParams = [
];
export type ContractWithdrawParams = [string, string, BigNumber, string];

export type SubgraphPluginRepoReleaseListItem = {
release: number;
metadata: string;
builds: {
build: number;
}[];
export type SubgraphPluginRepo = {
id: string;
subdomain: string;
releases: SubgraphPluginRepoRelease[];
};

export type SubgraphPluginRepoRelease = SubgraphPluginRepoReleaseListItem & {
builds: {
build: number;
metadata: string;
}[];
export type SubgraphPluginRepoRelease = {
release: number;
metadata: string;
builds: SubgraphPluginRepoBuild[];
};

export type SubgraphPluginRepoListItem = {
id: string;
subdomain: string;
releases: SubgraphPluginRepoReleaseListItem[];
export type SubgraphPluginRepoBuild = {
build: number;
metadata: string;
};

export type SubgraphPluginRepo = SubgraphPluginRepoListItem & {
releases: SubgraphPluginRepoRelease[];
};
export type SubgraphPluginRepoListItem = SubgraphPluginRepo;

export type SubgraphPluginVersion = {
release: {
Expand Down
37 changes: 10 additions & 27 deletions modules/client/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
InstalledPluginListItem,
PluginRepo,
PluginRepoBuildMetadata,
PluginRepoListItem,
PluginRepoRelease,
PluginRepoReleaseMetadata,
RevokePermissionDecodedParams,
RevokePermissionParams,
Expand All @@ -41,8 +39,6 @@ import {
SubgraphNativeTransferListItem,
SubgraphPluginListItem,
SubgraphPluginRepo,
SubgraphPluginRepoListItem,
SubgraphPluginRepoReleaseListItem,
SubgraphTransferListItem,
SubgraphTransferType,
} from "./types";
Expand Down Expand Up @@ -381,27 +377,6 @@ export function toTokenTransfer(transfer: SubgraphTransferListItem): Transfer {
}
}

export function toPluginRepoRelease(
release: SubgraphPluginRepoReleaseListItem,
metadata: PluginRepoReleaseMetadata,
): PluginRepoRelease {
return {
release: release.release,
currentBuild: Math.max(...release.builds.map((build) => build.build)),
metadata,
};
}

export function toPluginRepoListItem(
pluginRepo: SubgraphPluginRepoListItem,
releases: PluginRepoRelease[],
): PluginRepoListItem {
return {
address: pluginRepo.id,
subdomain: pluginRepo.subdomain,
releases,
};
}
export function toPluginRepo(
pluginRepo: SubgraphPluginRepo,
releaseMetadata: PluginRepoReleaseMetadata,
Expand All @@ -410,6 +385,14 @@ export function toPluginRepo(
return {
address: pluginRepo.id,
subdomain: pluginRepo.subdomain,
releases: pluginRepo.releases.map((release) => ({
release: release.release,
metadata: release.metadata,
builds: release.builds.map((build) => ({
build: build.build,
metadata: build.metadata,
})),
})),
current: {
build: {
metadata: buildMetadata,
Expand All @@ -419,8 +402,8 @@ export function toPluginRepo(
},
release: {
metadata: releaseMetadata,
// the subgraph returns only one realease ordered by realease number
// in descending order, this means it's the latest realease
// the subgraph returns only one release ordered by release number
// in descending order, this means it's the latest release
number: pluginRepo.releases?.[0]?.release,
},
},
Expand Down
40 changes: 22 additions & 18 deletions modules/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,36 @@ export type PluginRepoBuildMetadata = {
};
};

export type PluginRepoRelease = {
release: number;
metadata: PluginRepoReleaseMetadata;
currentBuild: number;
export type PluginRepoCurrent = {
build: {
number: number;
metadata: PluginRepoBuildMetadata;
};
release: {
number: number;
metadata: PluginRepoReleaseMetadata;
};
};

export type PluginRepoListItem = {
type PluginRepoBase = {
address: string;
subdomain: string;
current: PluginRepoCurrent
releases: PluginRepoRelease[];
};

export type PluginRepo = {
address: string;
subdomain: string;
current: {
build: {
number: number;
metadata: PluginRepoBuildMetadata;
};
release: {
number: number;
metadata: PluginRepoReleaseMetadata;
};
};
type PluginRepoRelease = {
release: number;
metadata: string;
builds: PluginRepoBuild[];
};

type PluginRepoBuild = {
build: number;
metadata: string;
};
export type PluginRepo = PluginRepoBase;
export type PluginRepoListItem = PluginRepoBase;

/* Deposits */
type DepositBaseParams = {
Expand Down
Loading

0 comments on commit a62d848

Please sign in to comment.