Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use 馃浕 pickup from api #2310

Merged
merged 6 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/api/src/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,23 @@ export function toPSAStatus(status) {
if (pinInfos.some((i) => i.status === 'pin_queued')) return 'queued'
return 'failed'
}

/**
* @param {import('@nftstorage/ipfs-cluster').API.StatusResponse} status
* @returns {import('./utils/db-client.js').definitions["pin"]["status"]} status
*/
export function toDBPinStatus(status) {
const pinInfos = Object.values(status.peerMap)
if (pinInfos.some((i) => i.status === 'pinned')) return 'Pinned'
if (pinInfos.some((i) => i.status === 'pinning')) return 'Pinning'
if (pinInfos.some((i) => i.status === 'pin_queued')) return 'PinQueued'
return 'PinError'
}

/**
* @param {string} cid
* @param {import("@nftstorage/ipfs-cluster").API.StatusOptions} [options]
*/
export function status(cid, options) {
return client.status(cid, options)
}
6 changes: 6 additions & 0 deletions packages/api/src/routes/pins-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export async function pinsAdd(event, ctx) {
})

const upload = await db.createUpload({
pins: [
{
status: 'PinQueued',
service: 'ElasticIpfs', // via pickup
},
],
Comment on lines +54 to +59
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do this to override the default pin info that createUpload would set if we didn't provide this. New pins via pickup are provided via E-IPFS.

type: 'Remote',
content_cid: cid.contentCid,
source_cid: cid.sourceCid,
Expand Down
19 changes: 17 additions & 2 deletions packages/api/src/routes/pins-get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkAuth, validate } from '../utils/auth.js'
import * as cluster from '../cluster.js'
import { checkAuth } from '../utils/auth.js'
import { toPinsResponse } from '../utils/db-transforms.js'
import { JSONResponse } from '../utils/json-response.js'
import { parseCidPinning } from '../utils/utils.js'
Expand All @@ -21,7 +22,7 @@ export async function pinsGet(event, ctx) {
)
}

const upload = await db.getUpload(cid.sourceCid, user.id)
let upload = await db.getUpload(cid.sourceCid, user.id)

if (!upload) {
return new JSONResponse(
Expand All @@ -30,5 +31,19 @@ export async function pinsGet(event, ctx) {
)
}

// check if the status has changed upstream
const status = upload.content.pin[0].status
if (status === 'Pinning' || status === 'PinQueued') {
const res = await cluster.status(cid.sourceCid)
const newStatus = cluster.toDBPinStatus(res)
if (status !== newStatus) {
await ctx.db.updatePinStatus(upload.content_cid, {
service: 'ElasticIpfs',
status: newStatus,
})
upload = (await db.getUpload(cid.sourceCid, user.id)) ?? upload
}
}

return new JSONResponse(toPinsResponse(upload))
}
6 changes: 6 additions & 0 deletions packages/api/src/routes/pins-replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export async function pinsReplace(event, ctx) {
})

const upload = await db.createUpload({
pins: [
{
status: 'PinQueued',
service: 'ElasticIpfs', // via pickup
},
],
type: 'Remote',
content_cid: cid.contentCid,
source_cid: cid.sourceCid,
Expand Down