diff --git a/api/v1/api.go b/api/v1/api.go index d79707ae..ef175a92 100644 --- a/api/v1/api.go +++ b/api/v1/api.go @@ -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)) diff --git a/api/v1/handlers.go b/api/v1/handlers.go index f25a457a..e926c051 100644 --- a/api/v1/handlers.go +++ b/api/v1/handlers.go @@ -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 @@ -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) } diff --git a/docs/docs.go b/docs/docs.go index 90f819a5..b2cb976a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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", @@ -3554,13 +3602,15 @@ const docTemplate = `{ "pinning", "pinned", "failed", - "queued" + "queued", + "offloaded" ], "x-enum-varnames": [ "PinningStatusPinning", "PinningStatusPinned", "PinningStatusFailed", - "PinningStatusQueued" + "PinningStatusQueued", + "PinningStatusOffloaded" ] }, "util.ContentAddResponse": { diff --git a/docs/swagger.json b/docs/swagger.json index f9be18ab..10db5eb3 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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", @@ -3547,13 +3595,15 @@ "pinning", "pinned", "failed", - "queued" + "queued", + "offloaded" ], "x-enum-varnames": [ "PinningStatusPinning", "PinningStatusPinned", "PinningStatusFailed", - "PinningStatusQueued" + "PinningStatusQueued", + "PinningStatusOffloaded" ] }, "util.ContentAddResponse": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 48cc7cb8..2815a01b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -187,12 +187,14 @@ definitions: - pinned - failed - queued + - offloaded type: string x-enum-varnames: - PinningStatusPinning - PinningStatusPinned - PinningStatusFailed - PinningStatusQueued + - PinningStatusOffloaded util.ContentAddResponse: properties: cid: @@ -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 diff --git a/pinner/types/types.go b/pinner/types/types.go index 96738418..70758400 100644 --- a/pinner/types/types.go +++ b/pinner/types/types.go @@ -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 { @@ -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 } diff --git a/util/content.go b/util/content.go index 1696613e..d2386ec5 100644 --- a/util/content.go +++ b/util/content.go @@ -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 {