Skip to content

Commit

Permalink
Merge pull request #280 from desci-labs/improve-api
Browse files Browse the repository at this point in the history
Improve user collaborated nodes retrieval API
  • Loading branch information
kadamidev authored Apr 16, 2024
2 parents 75a67a1 + 10e0419 commit 2395429
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions desci-server/src/services/Contributors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { format } from 'path';

import { IpldUrl, ResearchObjectV1Dpid } from '@desci-labs/desci-models';
import { Node, NodeContribution, User } from '@prisma/client';
import ShortUniqueId from 'short-unique-id';

Expand All @@ -8,6 +9,8 @@ import { logger as parentLogger } from '../logger.js';
import { getIndexedResearchObjects } from '../theGraph.js';
import { formatOrcidString, hexToCid } from '../utils.js';

import { getManifestByCid } from './data/processing.js';

type ContributorId = string;

export type NodeContributorMap = Record<ContributorId, NodeContributor>;
Expand All @@ -24,7 +27,15 @@ export interface NodeContributorAuthed extends NodeContributor {
orcid?: string;
}

export type UserContribution = { uuid: string; manifestCid: string };
export type UserContribution = {
uuid: string;
manifestCid: string;
title?: string;
versions: number;
coverImageCid?: string | IpldUrl;
dpid?: ResearchObjectV1Dpid;
publishDate: string;
};

export type Contribution = {
nodeUuid: string;
Expand Down Expand Up @@ -184,16 +195,28 @@ class ContributorService {
const nodeUuids = contributions.map((contribution) => contribution.node.uuid);
// Filter out for published works
const { researchObjects } = await getIndexedResearchObjects(nodeUuids);
const NodesWithManifestCids = researchObjects.map((ro) => {
// convert hex string to integer
const nodeUuidInt = Buffer.from(ro.id.substring(2), 'hex');
// convert integer to hex
const nodeUuid = nodeUuidInt.toString('base64url');

return { uuid: nodeUuid, manifestCid: hexToCid(ro.recentCid) };
});
const filledContributions = await Promise.all(
researchObjects.map(async (ro) => {
// convert hex string to integer
const nodeUuidInt = Buffer.from(ro.id.substring(2), 'hex');
// convert integer to hex
const nodeUuid = nodeUuidInt.toString('base64url');
const manifestCid = hexToCid(ro.recentCid);
const latestManifest = await getManifestByCid(manifestCid);

return {
uuid: nodeUuid,
manifestCid,
title: latestManifest.title,
versions: ro.versions.length,
coverImageCid: latestManifest.coverImage,
dpid: latestManifest.dpid,
publishDate: ro.versions[0].time,
};
}),
);
// debugger;
return NodesWithManifestCids || [];
return filledContributions || [];
}

/**
Expand Down

0 comments on commit 2395429

Please sign in to comment.