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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /content/contents endpoint, include PinningStatus in Content #772

Merged
merged 3 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (s *apiV1) RegisterRoutes(e *echo.Echo) {
content.GET("/by-cid/:cid", s.handleGetContentByCid)
content.GET("/:cont_id", withUser(s.handleGetContent))
content.GET("/stats", withUser(s.handleStats))
content.GET("/contents", withUser(s.handleGetUserContents))
content.GET("/ensure-replication/:datacid", s.handleEnsureReplication)
content.GET("/status/:id", withUser(s.handleContentStatus))
content.GET("/list", withUser(s.handleListContent))
Expand Down
29 changes: 29 additions & 0 deletions api/v1/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ func (s *apiV1) handleStats(c echo.Context, u *util.User) error {
return c.JSON(http.StatusOK, out)
}

// handleGetUserContents godoc
// @Summary Get user contents
// @Description This endpoint is used to get user contents
// @Tags content
// @Param limit query string true "limit"
// @Param offset query string true "offset"
// @Produce json
// @Success 200 {object} string
// @Failure 400 {object} util.HttpError
// @Failure 500 {object} util.HttpError
// @Router /content/contents [get]
func (s *apiV1) handleGetUserContents(c echo.Context, u *util.User) error {
limit, offset, _ := s.getLimitAndOffset(c, 500, 0)

var contents []util.Content
if err := s.DB.Limit(limit).Offset(offset).Order("created_at desc").Find(&contents, "user_id = ? and not aggregate", u.ID).Error; err != nil {
return err
}

for i, c := range contents {
contents[i].PinningStatus = string(pinningtypes.GetContentPinningStatus(c))
}

return c.JSON(http.StatusOK, contents)
}

// handlePeeringPeersAdd godoc
// @Summary Add peers on Peering Service
// @Description This endpoint can be used to add a Peer from the Peering Service
Expand Down Expand Up @@ -3607,6 +3633,9 @@ func (s *apiV1) handleGetStagingZoneContents(c echo.Context, u *util.User) error
if err != nil {
return err
}
for i, c := range contents {
contents[i].PinningStatus = string(pinningtypes.GetContentPinningStatus(c))
}
return c.JSON(http.StatusOK, contents)
}

Expand Down
54 changes: 52 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,54 @@ const docTemplate = `{
}
}
},
"/content/contents": {
"get": {
"description": "This endpoint is used to get user contents",
"produces": [
"application/json"
],
"tags": [
"content"
],
"summary": "Get user contents",
"parameters": [
{
"type": "string",
"description": "limit",
"name": "limit",
"in": "query",
"required": true
},
{
"type": "string",
"description": "offset",
"name": "offset",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/util.HttpError"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/util.HttpError"
}
}
}
}
},
"/content/create": {
"post": {
"description": "This endpoint adds a new content",
Expand Down Expand Up @@ -3554,13 +3602,15 @@ const docTemplate = `{
"pinning",
"pinned",
"failed",
"queued"
"queued",
"offloaded"
],
"x-enum-varnames": [
"PinningStatusPinning",
"PinningStatusPinned",
"PinningStatusFailed",
"PinningStatusQueued"
"PinningStatusQueued",
"PinningStatusOffloaded"
]
},
"util.ContentAddResponse": {
Expand Down
54 changes: 52 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,54 @@
}
}
},
"/content/contents": {
"get": {
"description": "This endpoint is used to get user contents",
"produces": [
"application/json"
],
"tags": [
"content"
],
"summary": "Get user contents",
"parameters": [
{
"type": "string",
"description": "limit",
"name": "limit",
"in": "query",
"required": true
},
{
"type": "string",
"description": "offset",
"name": "offset",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/util.HttpError"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/util.HttpError"
}
}
}
}
},
"/content/create": {
"post": {
"description": "This endpoint adds a new content",
Expand Down Expand Up @@ -3547,13 +3595,15 @@
"pinning",
"pinned",
"failed",
"queued"
"queued",
"offloaded"
],
"x-enum-varnames": [
"PinningStatusPinning",
"PinningStatusPinned",
"PinningStatusFailed",
"PinningStatusQueued"
"PinningStatusQueued",
"PinningStatusOffloaded"
]
},
"util.ContentAddResponse": {
Expand Down
34 changes: 34 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ definitions:
- pinned
- failed
- queued
- offloaded
type: string
x-enum-varnames:
- PinningStatusPinning
- PinningStatusPinned
- PinningStatusFailed
- PinningStatusQueued
- PinningStatusOffloaded
util.ContentAddResponse:
properties:
cid:
Expand Down Expand Up @@ -1046,6 +1048,38 @@ paths:
summary: Get content bandwidth
tags:
- content
/content/contents:
get:
description: This endpoint is used to get user contents
parameters:
- description: limit
in: query
name: limit
required: true
type: string
- description: offset
in: query
name: offset
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/util.HttpError'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/util.HttpError'
summary: Get user contents
tags:
- content
/content/create:
post:
description: This endpoint adds a new content
Expand Down
12 changes: 8 additions & 4 deletions pinner/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ const (
- pinning # pinning in progress; additional info can be returned in info[status_details]
- pinned # pinned successfully
- failed # pinning service was unable to finish pinning operation; additional info can be found in info[status_details]
- offloaded # content has been offloaded
*/
PinningStatusPinning PinningStatus = "pinning"
PinningStatusPinned PinningStatus = "pinned"
PinningStatusFailed PinningStatus = "failed"
PinningStatusQueued PinningStatus = "queued"
PinningStatusPinning PinningStatus = "pinning"
PinningStatusPinned PinningStatus = "pinned"
PinningStatusFailed PinningStatus = "failed"
PinningStatusQueued PinningStatus = "queued"
PinningStatusOffloaded PinningStatus = "offloaded"
)

type IpfsPin struct {
Expand Down Expand Up @@ -50,6 +52,8 @@ func GetContentPinningStatus(cont util.Content) PinningStatus {
status = PinningStatusFailed
} else if cont.Pinning {
status = PinningStatusPinning
} else if cont.Offloaded {
status = PinningStatusOffloaded
}
return status
}
2 changes: 2 additions & 0 deletions util/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type Content struct {
// them (unlike with aggregates)
DagSplit bool `json:"dagSplit"`
SplitFrom uint `json:"splitFrom"`

PinningStatus string `json:"pinningStatus" gorm:"-"`
}

type ContentWithPath struct {
Expand Down