photoprism/internal/api/api_event.go
Michael Mayer f8e0615cc8 Auth: Ensure backwards compatibility for existing API clients #808 #3943
These changes ensure that the new (SHA256) session ID is returned in the
"session_id" field, so that developers have time to update their client
implementations to use the new "access_token" field.

Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-07 12:25:56 +01:00

63 lines
2.1 KiB
Go

package api
import (
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/search"
)
// Event represents an api event type.
type Event string
const (
StatusCreated Event = "created"
StatusUpdated Event = "updated"
StatusDeleted Event = "deleted"
StatusSuccess Event = "success"
)
// String returns the event type as string.
func (ev Event) String() string {
return string(ev)
}
// PublishPhotoEvent publishes updated photo data after changes have been made.
func PublishPhotoEvent(ev Event, uid string, c *gin.Context) {
if result, _, err := search.Photos(form.SearchPhotos{UID: uid, Merged: true}); err != nil {
event.AuditErr([]string{ClientIP(c), "session %s", "%s photo %s", "%s"}, AuthToken(c), string(ev), uid, err)
} else {
event.PublishEntities("photos", string(ev), result)
}
}
// PublishAlbumEvent publishes updated album data after changes have been made.
func PublishAlbumEvent(ev Event, uid string, c *gin.Context) {
f := form.SearchAlbums{UID: uid}
if result, err := search.Albums(f); err != nil {
event.AuditErr([]string{ClientIP(c), "session %s", "%s album %s", "%s"}, AuthToken(c), string(ev), uid, err)
} else {
event.PublishEntities("albums", string(ev), result)
}
}
// PublishLabelEvent publishes updated label data after changes have been made.
func PublishLabelEvent(ev Event, uid string, c *gin.Context) {
f := form.SearchLabels{UID: uid}
if result, err := search.Labels(f); err != nil {
event.AuditErr([]string{ClientIP(c), "session %s", "%s label %s", "%s"}, AuthToken(c), string(ev), uid, err)
} else {
event.PublishEntities("labels", string(ev), result)
}
}
// PublishSubjectEvent publishes updated subject data after changes have been made.
func PublishSubjectEvent(ev Event, uid string, c *gin.Context) {
f := form.SearchSubjects{UID: uid}
if result, err := search.Subjects(f); err != nil {
event.AuditErr([]string{ClientIP(c), "session %s", "%s subject %s", "%s"}, AuthToken(c), string(ev), uid, err)
} else {
event.PublishEntities("subjects", string(ev), result)
}
}