/
reactions.go
93 lines (72 loc) Β· 2.1 KB
/
reactions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/get"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/react"
)
// LikePhoto flags a photo as favorite.
//
// POST /api/v1/photos/:uid/like
func LikePhoto(router *gin.RouterGroup) {
router.POST("/photos/:uid/like", func(c *gin.Context) {
s := AuthAny(c, acl.ResourcePhotos, acl.Permissions{acl.ActionUpdate, acl.ActionReact})
if s.Abort(c) {
return
}
id := clean.UID(c.Param("uid"))
m, err := query.PhotoByUID(id)
if err != nil {
AbortEntityNotFound(c)
return
}
if get.Config().Experimental() && acl.Rules.Allow(acl.ResourcePhotos, s.UserRole(), acl.ActionReact) {
logWarn("react", m.React(s.User(), react.Find("love")))
}
if acl.Rules.Allow(acl.ResourcePhotos, s.UserRole(), acl.ActionUpdate) {
err = m.SetFavorite(true)
if err != nil {
log.Errorf("photo: %s", err.Error())
AbortSaveFailed(c)
return
}
SavePhotoAsYaml(m)
PublishPhotoEvent(StatusUpdated, id, c)
}
c.JSON(http.StatusOK, gin.H{"photo": m})
})
}
// DislikePhoto removes the favorite flags from a photo.
//
// DELETE /api/v1/photos/:uid/like
func DislikePhoto(router *gin.RouterGroup) {
router.DELETE("/photos/:uid/like", func(c *gin.Context) {
s := AuthAny(c, acl.ResourcePhotos, acl.Permissions{acl.ActionUpdate, acl.ActionReact})
if s.Abort(c) {
return
}
id := clean.UID(c.Param("uid"))
m, err := query.PhotoByUID(id)
if err != nil {
AbortEntityNotFound(c)
return
}
if get.Config().Experimental() && acl.Rules.Allow(acl.ResourcePhotos, s.UserRole(), acl.ActionReact) {
logWarn("react", m.UnReact(s.User()))
}
if acl.Rules.Allow(acl.ResourcePhotos, s.UserRole(), acl.ActionUpdate) {
err = m.SetFavorite(false)
if err != nil {
log.Errorf("photo: %s", err.Error())
AbortSaveFailed(c)
return
}
SavePhotoAsYaml(m)
PublishPhotoEvent(StatusUpdated, id, c)
}
c.JSON(http.StatusOK, gin.H{"photo": m})
})
}