Add endpoint to remove a list background
This commit is contained in:
parent
9d0dcb8d7d
commit
ee436efba3
6 changed files with 195 additions and 20 deletions
|
@ -543,6 +543,7 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
|
||||||
"identifier",
|
"identifier",
|
||||||
"hex_color",
|
"hex_color",
|
||||||
"is_favorite",
|
"is_favorite",
|
||||||
|
"background_file_id",
|
||||||
}
|
}
|
||||||
if list.Description != "" {
|
if list.Description != "" {
|
||||||
colsToUpdate = append(colsToUpdate, "description")
|
colsToUpdate = append(colsToUpdate, "description")
|
||||||
|
|
|
@ -199,6 +199,33 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
|
||||||
return c.JSON(http.StatusOK, list)
|
return c.JSON(http.StatusOK, list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkListBackgroundRights(s *xorm.Session, c echo.Context) (list *models.List, auth web.Auth, err error) {
|
||||||
|
auth, err = auth2.GetAuthFromClaims(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, auth, echo.NewHTTPError(http.StatusBadRequest, "Invalid auth token: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
listID, err := strconv.ParseInt(c.Param("list"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, auth, echo.NewHTTPError(http.StatusBadRequest, "Invalid list ID: "+err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a background for this list exists + Rights
|
||||||
|
list = &models.List{ID: listID}
|
||||||
|
can, _, err := list.CanRead(s, auth)
|
||||||
|
if err != nil {
|
||||||
|
_ = s.Rollback()
|
||||||
|
return nil, auth, handler.HandleHTTPError(err, c)
|
||||||
|
}
|
||||||
|
if !can {
|
||||||
|
_ = s.Rollback()
|
||||||
|
log.Infof("Tried to get list background of list %d while not having the rights for it (User: %v)", listID, auth)
|
||||||
|
return nil, auth, echo.NewHTTPError(http.StatusForbidden)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetListBackground serves a previously set background from a list
|
// GetListBackground serves a previously set background from a list
|
||||||
// It has no knowledge of the provider that was responsible for setting the background.
|
// It has no knowledge of the provider that was responsible for setting the background.
|
||||||
// @Summary Get the list background
|
// @Summary Get the list background
|
||||||
|
@ -214,31 +241,14 @@ func (bp *BackgroundProvider) UploadBackground(c echo.Context) error {
|
||||||
// @Router /lists/{id}/background [get]
|
// @Router /lists/{id}/background [get]
|
||||||
func GetListBackground(c echo.Context) error {
|
func GetListBackground(c echo.Context) error {
|
||||||
|
|
||||||
auth, err := auth2.GetAuthFromClaims(c)
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid auth token: "+err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
listID, err := strconv.ParseInt(c.Param("list"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid list ID: "+err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
s := db.NewSession()
|
s := db.NewSession()
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
// Check if a background for this list exists + Rights
|
list, _, err := checkListBackgroundRights(s, c)
|
||||||
list := &models.List{ID: listID}
|
|
||||||
can, _, err := list.CanRead(s, auth)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = s.Rollback()
|
return err
|
||||||
return handler.HandleHTTPError(err, c)
|
|
||||||
}
|
|
||||||
if !can {
|
|
||||||
_ = s.Rollback()
|
|
||||||
log.Infof("Tried to get list background of list %d while not having the rights for it (User: %v)", listID, auth)
|
|
||||||
return echo.NewHTTPError(http.StatusForbidden)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if list.BackgroundFileID == 0 {
|
if list.BackgroundFileID == 0 {
|
||||||
_ = s.Rollback()
|
_ = s.Rollback()
|
||||||
return echo.NotFoundHandler(c)
|
return echo.NotFoundHandler(c)
|
||||||
|
@ -266,3 +276,34 @@ func GetListBackground(c echo.Context) error {
|
||||||
// Serve the file
|
// Serve the file
|
||||||
return c.Stream(http.StatusOK, "image/jpg", bgFile.File)
|
return c.Stream(http.StatusOK, "image/jpg", bgFile.File)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveListBackground removes a list background, no matter the background provider
|
||||||
|
// @Summary Remove a list background
|
||||||
|
// @Description Removes a previously set list background, regardless of the list provider used to set the background. It does not throw an error if the list does not have a background.
|
||||||
|
// @tags list
|
||||||
|
// @Produce json
|
||||||
|
// @Param id path int true "List ID"
|
||||||
|
// @Security JWTKeyAuth
|
||||||
|
// @Success 200 {object} models.List "The list"
|
||||||
|
// @Failure 403 {object} models.Message "No access to this list."
|
||||||
|
// @Failure 404 {object} models.Message "The list does not exist."
|
||||||
|
// @Failure 500 {object} models.Message "Internal error"
|
||||||
|
// @Router /lists/{id}/background [delete]
|
||||||
|
func RemoveListBackground(c echo.Context) error {
|
||||||
|
s := db.NewSession()
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
list, auth, err := checkListBackgroundRights(s, c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
list.BackgroundFileID = 0
|
||||||
|
list.BackgroundInformation = nil
|
||||||
|
err = list.Update(s, auth)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, list)
|
||||||
|
}
|
||||||
|
|
|
@ -576,6 +576,7 @@ func registerAPIRoutes(a *echo.Group) {
|
||||||
// List Backgrounds
|
// List Backgrounds
|
||||||
if config.BackgroundsEnabled.GetBool() {
|
if config.BackgroundsEnabled.GetBool() {
|
||||||
a.GET("/lists/:list/background", backgroundHandler.GetListBackground)
|
a.GET("/lists/:list/background", backgroundHandler.GetListBackground)
|
||||||
|
a.DELETE("/lists/:list/background", backgroundHandler.RemoveListBackground)
|
||||||
if config.BackgroundsUploadEnabled.GetBool() {
|
if config.BackgroundsUploadEnabled.GetBool() {
|
||||||
uploadBackgroundProvider := &backgroundHandler.BackgroundProvider{
|
uploadBackgroundProvider := &backgroundHandler.BackgroundProvider{
|
||||||
Provider: func() background.Provider {
|
Provider: func() background.Provider {
|
||||||
|
|
|
@ -1063,6 +1063,56 @@ var doc = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"JWTKeyAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Removes a previously set list background, regardless of the list provider used to set the background. It does not throw an error if the list does not have a background.",
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"list"
|
||||||
|
],
|
||||||
|
"summary": "Remove a list background",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "integer",
|
||||||
|
"description": "List ID",
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "The list",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.List"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"403": {
|
||||||
|
"description": "No access to this list.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "The list does not exist.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Internal error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/lists/{id}/backgrounds/unsplash": {
|
"/lists/{id}/backgrounds/unsplash": {
|
||||||
|
|
|
@ -1046,6 +1046,56 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"JWTKeyAuth": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Removes a previously set list background, regardless of the list provider used to set the background. It does not throw an error if the list does not have a background.",
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"list"
|
||||||
|
],
|
||||||
|
"summary": "Remove a list background",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "integer",
|
||||||
|
"description": "List ID",
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "The list",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.List"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"403": {
|
||||||
|
"description": "No access to this list.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "The list does not exist.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Internal error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/models.Message"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/lists/{id}/backgrounds/unsplash": {
|
"/lists/{id}/backgrounds/unsplash": {
|
||||||
|
|
|
@ -1835,6 +1835,38 @@ paths:
|
||||||
tags:
|
tags:
|
||||||
- task
|
- task
|
||||||
/lists/{id}/background:
|
/lists/{id}/background:
|
||||||
|
delete:
|
||||||
|
description: Removes a previously set list background, regardless of the list provider used to set the background. It does not throw an error if the list does not have a background.
|
||||||
|
parameters:
|
||||||
|
- description: List ID
|
||||||
|
in: path
|
||||||
|
name: id
|
||||||
|
required: true
|
||||||
|
type: integer
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: The list
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/models.List'
|
||||||
|
"403":
|
||||||
|
description: No access to this list.
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/models.Message'
|
||||||
|
"404":
|
||||||
|
description: The list does not exist.
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/models.Message'
|
||||||
|
"500":
|
||||||
|
description: Internal error
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/models.Message'
|
||||||
|
security:
|
||||||
|
- JWTKeyAuth: []
|
||||||
|
summary: Remove a list background
|
||||||
|
tags:
|
||||||
|
- list
|
||||||
get:
|
get:
|
||||||
description: Get the list background of a specific list. **Returns json on error.**
|
description: Get the list background of a specific list. **Returns json on error.**
|
||||||
parameters:
|
parameters:
|
||||||
|
|
Loading…
Reference in a new issue