From 2c787b4d46aa4a104321ae73a9262ea035917b48 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 19 Oct 2023 18:14:40 +0100 Subject: [PATCH] activity: log creations --- api-users.go | 28 ++++++++++++++++++++++++++++ storage.go | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/api-users.go b/api-users.go index f735bda..11702f2 100644 --- a/api-users.go +++ b/api-users.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" "github.com/hrfee/mediabrowser" + "github.com/lithammer/shortuuid/v3" "github.com/timshannon/badgerhold/v4" ) @@ -45,6 +46,16 @@ func (app *appContext) NewUserAdmin(gc *gin.Context) { return } id := user.ID + + // Record activity + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityCreation, + UserID: id, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Time: time.Now(), + }) + profile := app.storage.GetDefaultProfile() if req.Profile != "" && req.Profile != "none" { if p, ok := app.storage.GetProfileKey(req.Profile); ok { @@ -303,6 +314,23 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc } id := user.ID + // Record activity + sourceType := ActivityAnon + source := "" + if invite.ReferrerJellyfinID != "" { + sourceType = ActivityUser + source = invite.ReferrerJellyfinID + } + + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityCreation, + UserID: id, + SourceType: sourceType, + Source: source, + InviteCode: invite.Code, + Time: time.Now(), + }) + emailStore := EmailAddress{ Addr: req.Email, Contact: (req.Email != ""), diff --git a/storage.go b/storage.go index 567b477..9534d13 100644 --- a/storage.go +++ b/storage.go @@ -24,14 +24,15 @@ type emailStore map[string]EmailAddress type ActivityType int const ( - ActivityCreation ActivityType = iota // FIXME - ActivityDeletion // FIXME - ActivityDisabled // FIXME - ActivityEnabled // FIXME - ActivityLinked // FIXME - ActivityChangePassword // FIXME - ActivityResetPassword // FIXME - ActivityCreateInvite // FIXME + ActivityCreation ActivityType = iota + ActivityDeletion + ActivityDisabled + ActivityEnabled + ActivityLinked + ActivityChangePassword + ActivityResetPassword + ActivityCreateInvite + ActivityDeleteInvite ) type ActivitySource int @@ -544,6 +545,31 @@ func (st *Storage) DeleteCustomContentKey(k string) { st.db.Delete(k, CustomContent{}) } +// GetActivityKey returns the value stored in the store's key. +func (st *Storage) GetActivityKey(k string) (Activity, bool) { + result := Activity{} + err := st.db.Get(k, &result) + ok := true + if err != nil { + // fmt.Printf("Failed to find custom content: %v\n", err) + ok = false + } + return result, ok +} + +// SetActivityKey stores value v in key k. +func (st *Storage) SetActivityKey(k string, v Activity) { + err := st.db.Upsert(k, v) + if err != nil { + // fmt.Printf("Failed to set custom content: %v\n", err) + } +} + +// DeleteActivityKey deletes value at key k. +func (st *Storage) DeleteActivityKey(k string) { + st.db.Delete(k, Activity{}) +} + type TelegramUser struct { JellyfinID string `badgerhold:"key"` ChatID int64 `badgerhold:"index"`