2021-09-02 16:12:31 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-06 20:06:52 +02:00
|
|
|
|
2021-09-02 16:12:31 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2021-09-17 14:26:12 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2021-09-02 16:12:31 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2021-12-09 02:33:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-09-02 16:12:31 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetSubject returns a subject as JSON.
|
|
|
|
//
|
|
|
|
// GET /api/v1/subjects/:uid
|
|
|
|
func GetSubject(router *gin.RouterGroup) {
|
|
|
|
router.GET("/subjects/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePeople, acl.ActionView)
|
2021-09-02 16:12:31 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2021-09-02 16:12:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if subj := entity.FindSubject(clean.UID(c.Param("uid"))); subj == nil {
|
2021-09-02 16:12:31 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, subj)
|
|
|
|
}
|
2021-09-17 14:26:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSubject updates subject properties.
|
|
|
|
//
|
|
|
|
// PUT /api/v1/subjects/:uid
|
|
|
|
func UpdateSubject(router *gin.RouterGroup) {
|
|
|
|
router.PUT("/subjects/:uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
if err := mutex.UpdatePeople.Start(); err != nil {
|
2021-12-09 02:33:41 +01:00
|
|
|
AbortBusy(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
defer mutex.UpdatePeople.Stop()
|
2021-12-09 02:33:41 +01:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
|
2021-09-17 14:26:12 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2021-09-17 14:26:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2021-09-17 14:26:12 +02:00
|
|
|
m := entity.FindSubject(uid)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:06:52 +02:00
|
|
|
// Initialize form.
|
|
|
|
f, err := form.NewSubject(*m)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("subject: %s (new form)", err)
|
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
} else if err := c.BindJSON(&f); err != nil {
|
|
|
|
log.Errorf("subject: %s (update form)", err)
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:27:17 +02:00
|
|
|
// Update subject from form values.
|
|
|
|
if changed, err := m.SaveForm(f); err != nil {
|
2021-09-29 20:09:34 +02:00
|
|
|
log.Errorf("subject: %s", err)
|
|
|
|
AbortSaveFailed(c)
|
2021-09-17 14:26:12 +02:00
|
|
|
return
|
2021-10-06 15:27:17 +02:00
|
|
|
} else if changed {
|
|
|
|
if m.IsPerson() {
|
|
|
|
event.SuccessMsg(i18n.MsgPersonSaved)
|
|
|
|
} else {
|
|
|
|
event.SuccessMsg(i18n.MsgSubjectSaved)
|
|
|
|
}
|
2021-09-18 15:32:39 +02:00
|
|
|
}
|
2021-09-17 14:26:12 +02:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LikeSubject flags a subject as favorite.
|
|
|
|
//
|
|
|
|
// POST /api/v1/subjects/:uid/like
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string Subject UID
|
2021-09-17 14:26:12 +02:00
|
|
|
func LikeSubject(router *gin.RouterGroup) {
|
|
|
|
router.POST("/subjects/:uid/like", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
|
2021-09-17 14:26:12 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2021-09-17 14:26:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2021-09-17 14:26:12 +02:00
|
|
|
subj := entity.FindSubject(uid)
|
|
|
|
|
|
|
|
if subj == nil {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := subj.Update("SubjFavorite", true); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2021-09-17 14:26:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
PublishSubjectEvent(EntityUpdated, uid, c)
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DislikeSubject removes the favorite flag from a subject.
|
|
|
|
//
|
|
|
|
// DELETE /api/v1/subjects/:uid/like
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string Subject UID
|
2021-09-17 14:26:12 +02:00
|
|
|
func DislikeSubject(router *gin.RouterGroup) {
|
|
|
|
router.DELETE("/subjects/:uid/like", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
|
2021-09-17 14:26:12 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2021-09-17 14:26:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
uid := clean.UID(c.Param("uid"))
|
2021-09-17 14:26:12 +02:00
|
|
|
subj := entity.FindSubject(uid)
|
|
|
|
|
|
|
|
if subj == nil {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := subj.Update("SubjFavorite", false); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
|
2021-09-17 14:26:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
PublishSubjectEvent(EntityUpdated, uid, c)
|
2021-09-02 16:12:31 +02:00
|
|
|
|
2021-09-17 14:26:12 +02:00
|
|
|
c.JSON(http.StatusOK, http.Response{})
|
2021-09-02 16:12:31 +02:00
|
|
|
})
|
|
|
|
}
|