2019-06-09 04:37:02 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-26 14:28:50 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-16 23:33:52 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-05 19:21:35 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-06-09 04:37:02 +02:00
|
|
|
)
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
// UpdateLabel updates label properties.
|
|
|
|
//
|
2020-05-23 20:58:58 +02:00
|
|
|
// PUT /api/v1/labels/:uid
|
2020-06-25 14:54:04 +02:00
|
|
|
func UpdateLabel(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.PUT("/labels/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-02-02 02:00:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Label
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortBadRequest(c)
|
2020-02-02 02:00:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-05-23 20:58:58 +02:00
|
|
|
m, err := query.LabelByUID(id)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrLabelNotFound)
|
2020-02-02 02:00:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
m.SetName(f.LabelName)
|
2020-05-08 15:41:01 +02:00
|
|
|
entity.Db().Save(&m)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
event.SuccessMsg(i18n.MsgLabelSaved)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
2024-01-07 12:25:56 +01:00
|
|
|
PublishLabelEvent(StatusUpdated, id, c)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
// LikeLabel flags a label as favorite.
|
|
|
|
//
|
2024-01-08 14:53:39 +01:00
|
|
|
// The request parameters are:
|
|
|
|
//
|
|
|
|
// - uid: string Label UID
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
2024-01-07 12:25:56 +01:00
|
|
|
// POST /api/v1/labels/:uid/like
|
2020-06-25 14:54:04 +02:00
|
|
|
func LikeLabel(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.POST("/labels/:uid/like", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-05-23 20:58:58 +02:00
|
|
|
label, err := query.LabelByUID(id)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UpperFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
if err := label.Update("LabelFavorite", true); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-05-30 14:52:47 +02:00
|
|
|
return
|
|
|
|
}
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if label.LabelPriority < 0 {
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-07 12:25:56 +01:00
|
|
|
PublishLabelEvent(StatusUpdated, id, c)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2019-06-09 04:37:02 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
// DislikeLabel removes the favorite flag from a label.
|
|
|
|
//
|
2024-01-08 14:53:39 +01:00
|
|
|
// The request parameters are:
|
|
|
|
//
|
|
|
|
// - uid: string Label UID
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
2024-01-07 12:25:56 +01:00
|
|
|
// DELETE /api/v1/labels/:uid/like
|
2020-06-25 14:54:04 +02:00
|
|
|
func DislikeLabel(router *gin.RouterGroup) {
|
2020-05-23 20:58:58 +02:00
|
|
|
router.DELETE("/labels/:uid/like", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceLabels, acl.ActionUpdate)
|
2020-06-25 14:54:04 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2019-11-12 05:49:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
id := clean.UID(c.Param("uid"))
|
2020-05-23 20:58:58 +02:00
|
|
|
label, err := query.LabelByUID(id)
|
2019-06-09 04:37:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": txt.UpperFirst(err.Error())})
|
2019-06-09 04:37:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
if err := label.Update("LabelFavorite", false); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2020-05-30 14:52:47 +02:00
|
|
|
return
|
|
|
|
}
|
2019-06-09 04:37:02 +02:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
if label.LabelPriority < 0 {
|
|
|
|
event.Publish("count.labels", event.Data{
|
|
|
|
"count": -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-07 12:25:56 +01:00
|
|
|
PublishLabelEvent(StatusUpdated, id, c)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2019-06-09 04:37:02 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|