diff --git a/Makefile b/Makefile index 1df417c..d8c347f 100644 --- a/Makefile +++ b/Makefile @@ -248,7 +248,7 @@ $(GO_TARGET): $(COMPDEPS) $(SWAGGER_TARGET) $(GO_SRC) go.mod go.sum $(GOBINARY) mod download $(info Building) mkdir -p build - $(GOBINARY) build $(RACEDETECTOR) -ldflags="$(LDFLAGS)" $(TAGS) -o $(GO_TARGET) + $(GOBINARY) build $(RACEDETECTOR) -ldflags="$(LDFLAGS)" $(TAGS) -o $(GO_TARGET) $(GOBUILDFLAGS) test: $(BUILDDEPS) $(COMPDEPS) $(SWAGGER_TARGET) $(GO_SRC) go.mod go.sum $(GOBINARY) test -ldflags="$(LDFLAGS)" $(TAGS) -p 1 diff --git a/api-ombi.go b/api-ombi.go index 76b3828..ed56c3e 100644 --- a/api-ombi.go +++ b/api-ombi.go @@ -12,17 +12,22 @@ import ( "github.com/hrfee/mediabrowser" ) -func (app *appContext) getOmbiUser(jfID string) (map[string]interface{}, error) { +// getOmbiUser searches for an ombi user given a Jellyfin user ID. It looks for matching username or matching email address. +// If "email"=nil, an email address will be acquired from the DB instead. Passing it manually is useful when changing email address. +func (app *appContext) getOmbiUser(jfID string, email *string) (map[string]interface{}, error) { jfUser, err := app.jf.UserByID(jfID, false) if err != nil { return nil, err } username := jfUser.Name - email := "" - if e, ok := app.storage.GetEmailsKey(jfID); ok { - email = e.Addr + if email == nil { + addr := "" + if e, ok := app.storage.GetEmailsKey(jfID); ok { + addr = e.Addr + } + email = &addr } - user, err := app.ombi.getUser(username, email) + user, err := app.ombi.getUser(username, *email) return user, err } @@ -147,7 +152,7 @@ func (app *appContext) DeleteOmbiProfile(gc *gin.Context) { } type OmbiWrapper struct { - OmbiUserByJfID func(jfID string) (map[string]interface{}, error) + OmbiUserByJfID func(jfID string, email *string) (map[string]interface{}, error) *ombiLib.Ombi } @@ -191,7 +196,7 @@ func (ombi *OmbiWrapper) ImportUser(jellyfinID string, req newUserDTO, profile P } func (ombi *OmbiWrapper) SetContactMethods(jellyfinID string, email *string, discord *DiscordUser, telegram *TelegramUser, contactPrefs *common.ContactPreferences) (err error) { - ombiUser, err := ombi.OmbiUserByJfID(jellyfinID) + ombiUser, err := ombi.OmbiUserByJfID(jellyfinID, email) if err != nil { return } diff --git a/api-userpage.go b/api-userpage.go index 39307f1..89bcaf5 100644 --- a/api-userpage.go +++ b/api-userpage.go @@ -206,14 +206,14 @@ func (app *appContext) confirmMyAction(gc *gin.Context, key string) { app.storage.SetActivityKey(shortuuid.New(), Activity{ Type: ActivityContactLinked, - UserID: gc.GetString("jfId"), + UserID: id, SourceType: ActivityUser, - Source: gc.GetString("jfId"), + Source: id, Value: "email", Time: time.Now(), }, gc, true) - app.info.Printf(lm.UserEmailAdjusted, gc.GetString("jfId")) + app.info.Printf(lm.UserEmailAdjusted, id) gc.Redirect(http.StatusSeeOther, PAGES.MyAccount) return } @@ -270,7 +270,7 @@ func (app *appContext) ModifyMyEmail(gc *gin.Context) { } else if err := app.email.send(msg, req.Email); err != nil { app.err.Printf(lm.FailedSendConfirmationEmail, id, req.Email, err) } else { - app.err.Printf(lm.SentConfirmationEmail, id, req.Email) + app.info.Printf(lm.SentConfirmationEmail, id, req.Email) } return } @@ -716,7 +716,7 @@ func (app *appContext) ChangeMyPassword(gc *gin.Context) { if app.config.Section("ombi").Key("enabled").MustBool(false) { func() { - ombiUser, err := app.getOmbiUser(gc.GetString("jfId")) + ombiUser, err := app.getOmbiUser(gc.GetString("jfId"), nil) if err != nil { app.err.Printf(lm.FailedGetUser, user.Name, lm.Ombi, err) return diff --git a/api-users.go b/api-users.go index 6fad4ed..570af70 100644 --- a/api-users.go +++ b/api-users.go @@ -294,6 +294,7 @@ func (app *appContext) PostNewUserFromInvite(nu NewUserData, req ConfirmationKey } contactPrefs.Email = &(emailStore.Contact) if profile != nil { + // FIXME: Why? profile.ReferralTemplateKey = profile.ReferralTemplateKey } /// Ensures at least one contact method is enabled. @@ -1369,7 +1370,7 @@ func (app *appContext) ApplySettings(gc *gin.Context) { } if ombi != nil { errorString := "" - user, err := app.getOmbiUser(id) + user, err := app.getOmbiUser(id, nil) if err != nil { errorString += fmt.Sprintf("Ombi GetUser: %v ", err) } else { @@ -1439,5 +1440,13 @@ func (app *appContext) GetJFActivitesForUser(gc *gin.Context) { respondBool(400, false, gc) return } - gc.JSON(200, ActivityLogEntriesDTO{Entries: activities}) + out := ActivityLogEntriesDTO{ + Entries: make([]ActivityLogEntryDTO, len(activities)), + } + for i := range activities { + out.Entries[i].ActivityLogEntry = activities[i] + out.Entries[i].Date = activities[i].Date.Unix() + } + app.debug.Printf(lm.GotNEntries, len(activities)) + gc.JSON(200, out) } diff --git a/api.go b/api.go index 53097df..004c796 100644 --- a/api.go +++ b/api.go @@ -201,7 +201,7 @@ func (app *appContext) ResetSetPassword(gc *gin.Context) { respondBool(200, true, gc) return } */ - ombiUser, err := app.getOmbiUser(user.ID) + ombiUser, err := app.getOmbiUser(user.ID, nil) if err != nil { app.err.Printf(lm.FailedGetUser, user.ID, lm.Ombi, err) respondBool(200, true, gc) diff --git a/config.go b/config.go index 300fc86..b58f278 100644 --- a/config.go +++ b/config.go @@ -291,6 +291,7 @@ func NewConfig(configPathOrContents any, dataPath string, logs LoggerSet) (*Conf config.MustSetValue("jellyfin", "cache_timeout", "30") config.MustSetValue("jellyfin", "web_cache_async_timeout", "1") config.MustSetValue("jellyfin", "web_cache_sync_timeout", "10") + config.MustSetValue("jellyfin", "activity_cache_sync_timeout_seconds", "20") LOGIP = config.Section("advanced").Key("log_ips").MustBool(false) LOGIPU = config.Section("advanced").Key("log_ips_users").MustBool(false) diff --git a/config/config-base.yaml b/config/config-base.yaml index e523da6..5051920 100644 --- a/config/config-base.yaml +++ b/config/config-base.yaml @@ -136,6 +136,13 @@ sections: type: number value: 10 description: "Synchronise after cache is this old, and wait for it: The accounts tab may take a little longer to load while it does." + - setting: activity_cache_sync_timeout + name: "Activity cache timeout (minutes)" + requires_restart: true + advanced: true + type: number + value: 0.1 + description: "Synchronise Jellyfin's activity log after cache is this old. It can be pretty low as syncing only pulls new records and so is quick. Note this is unrelated to jfa-go's activity log." - setting: type name: Server type requires_restart: true diff --git a/jf_activity.go b/jf_activity.go index c52c609..2c2c010 100644 --- a/jf_activity.go +++ b/jf_activity.go @@ -11,11 +11,13 @@ import ( ) const ( - // ActivityLimit is the maximum number of ActivityLogEntries to fetch at once. - ActivityLimit = 5000 + // ActivityLimit is the maximum number of ActivityLogEntries to keep in memory. + // The array they are stored in is fixed, so (ActivityLimit*unsafe.Sizeof(mediabrowser.ActivityLogEntry)) + // At writing ActivityLogEntries take up ~160 bytes each, so 1M of memory gives us room for ~6250 records + ActivityLimit int = 1e6 / 160 // If ByUserLimitLength is true, ByUserLengthOrBaseLength is the maximum number of records attached // to a user. - // If false, it is the base amount of entries to allocate for for each user ID, and more will be allocayted as needed. + // If false, it is the base amount of entries to allocate for for each user ID, and more will be allocated as needed. ByUserLengthOrBaseLength = 128 ByUserLimitLength = false ) @@ -113,11 +115,13 @@ func (c *JFActivityCache) ByEntryID(entryID int64) (entry mediabrowser.ActivityL // MaybeSync returns once the cache is in a suitable state to read: // return if cache is fresh, sync if not, or wait if another sync is happening already. func (c *JFActivityCache) MaybeSync() error { + syncTime := time.Now() shouldWaitForSync := time.Now().After(c.LastSync.Add(c.WaitForSyncTimeout)) if !shouldWaitForSync { return nil } + defer func() { fmt.Printf("sync took %v", time.Since(syncTime)) }() syncStatus := make(chan error) @@ -153,7 +157,7 @@ func (c *JFActivityCache) MaybeSync() error { } } if recvLength > 0 { - // Lazy strategy: for user ID maps, each refresh we'll rebuild them. + // Lazy strategy: rebuild user ID maps each time. // Wipe them, and then append each new refresh element as we process them. // Then loop through all the old entries and append them too. for uid := range c.byUserID { diff --git a/logmessages/logmessages.go b/logmessages/logmessages.go index 6c9da27..6aea578 100644 --- a/logmessages/logmessages.go +++ b/logmessages/logmessages.go @@ -333,6 +333,9 @@ const ( // usercache.go CacheRefreshCompleted = "Usercache refreshed, %d in %.2fs (%f.2u/sec)" + + // Other + GotNEntries = "got %d entries" ) const ( diff --git a/main.go b/main.go index 90c6d5d..07444b0 100644 --- a/main.go +++ b/main.go @@ -157,14 +157,6 @@ func generateSecret(length int) (string, error) { } func test(app *appContext) { - app.jf.activity = NewJFActivityCache(app.jf, 10*time.Second) - for { - c, _ := app.jf.activity.ByUserID("9d8c71d1bac04c4c8e69ce3446c61652") - v, _ := app.jf.GetActivityLog(-1, 1, time.Time{}, true) - fmt.Printf("From the source: %+v\nFrom the cache: %+v\nequal: %t\n", v.Items[0], c[0], v.Items[0].ID == c[0].ID) - time.Sleep(5 * time.Second) - } - fmt.Printf("\n\n----\n\n") settings := map[string]any{ "server": app.jf.Server, @@ -453,6 +445,10 @@ func start(asDaemon, firstCall bool) { if err != nil { app.err.Fatalf(lm.FailedAuthJellyfin, server, -1, err) } + app.jf.activity = NewJFActivityCache( + app.jf, + time.Duration(app.config.Section("jellyfin").Key("activity_cache_sync_timeout_seconds").MustInt(20))*time.Second, + ) /*if debugMode { app.jf.Verbose = true }*/ diff --git a/migrations.go b/migrations.go index 6e883b0..692c47b 100644 --- a/migrations.go +++ b/migrations.go @@ -189,7 +189,7 @@ func linkExistingOmbiDiscordTelegram(app *appContext) error { idList[user.JellyfinID] = vals } for jfID, ids := range idList { - ombiUser, err := app.getOmbiUser(jfID) + ombiUser, err := app.getOmbiUser(jfID, nil) if err != nil { app.debug.Printf("Failed to get Ombi user with Discord/Telegram \"%s\"/\"%s\": %v", ids[0], ids[1], err) continue diff --git a/models.go b/models.go index 6defa10..897229d 100644 --- a/models.go +++ b/models.go @@ -517,5 +517,10 @@ type LabelsDTO struct { } type ActivityLogEntriesDTO struct { - Entries []mediabrowser.ActivityLogEntry `json:"entries"` + Entries []ActivityLogEntryDTO `json:"entries"` +} + +type ActivityLogEntryDTO struct { + mediabrowser.ActivityLogEntry + Date int64 `json:"Date"` } diff --git a/ts/modules/accounts.ts b/ts/modules/accounts.ts index ccde9c9..c216363 100644 --- a/ts/modules/accounts.ts +++ b/ts/modules/accounts.ts @@ -31,7 +31,7 @@ enum SelectAllState { All = 1, } -interface User { +interface UserDTO { id: string; name: string; email: string | undefined; @@ -177,7 +177,7 @@ const queries = (): { [field: string]: QueryType } => { }; }; -class user implements User, SearchableItem { +class User implements UserDTO, SearchableItem { private _id = ""; private _row: HTMLTableRowElement; private _check: HTMLInputElement; @@ -691,7 +691,7 @@ class user implements User, SearchableItem { private _checkEvent = () => new CustomEvent("accountCheckEvent", { detail: this.id }); private _uncheckEvent = () => new CustomEvent("accountUncheckEvent", { detail: this.id }); - constructor(user: User) { + constructor(user: UserDTO) { this._row = document.createElement("tr") as HTMLTableRowElement; this._row.classList.add("border-b", "border-dashed", "dark:border-dotted", "dark:border-stone-700"); let innerHTML = ` @@ -897,7 +897,7 @@ class user implements User, SearchableItem { this._row.setAttribute(SearchableItemDataAttribute, v); } - update = (user: User) => { + update = (user: UserDTO) => { this.id = user.id; this.name = user.name; this.email = user.email || ""; @@ -934,7 +934,7 @@ class user implements User, SearchableItem { } interface UsersDTO extends paginatedDTO { - users: User[]; + users: UserDTO[]; } declare interface ExtendExpiryDTO { @@ -1000,8 +1000,8 @@ export class accountsList extends PaginatedList { private _selectAllState: SelectAllState = SelectAllState.None; // private _users: { [id: string]: user }; // private _ordering: string[] = []; - get users(): { [id: string]: user } { - return this._search.items as { [id: string]: user }; + get users(): { [id: string]: User } { + return this._search.items as { [id: string]: User }; } // set users(v: { [id: string]: user }) { this._search.items = v as SearchableItems; } @@ -1362,7 +1362,7 @@ export class accountsList extends PaginatedList { this._columns[headerGetters[i]] = new Column( header, headerGetters[i], - Object.getOwnPropertyDescriptor(user.prototype, headerGetters[i]).get, + Object.getOwnPropertyDescriptor(User.prototype, headerGetters[i]).get, ); } } @@ -1501,8 +1501,8 @@ export class accountsList extends PaginatedList { } }; - add = (u: User) => { - let domAccount = new user(u); + add = (u: UserDTO) => { + let domAccount = new User(u); this.users[u.id] = domAccount; // console.log("after appending lengths:", Object.keys(this.users).length, Object.keys(this._search.items).length); }; @@ -1991,7 +1991,7 @@ export class accountsList extends PaginatedList { sendPWR = () => { addLoader(this._sendPWR); let list = this._collectUsers(); - let manualUser: user; + let manualUser: User; for (let id of list) { let user = this.users[id]; if (!user.lastNotifyMethod() && !user.email) { @@ -2545,7 +2545,7 @@ class Column { } // Sorts the user list. previouslyActive is whether this column was previously sorted by, indicating that the direction should change. - sort = (users: { [id: string]: user }): string[] => { + sort = (users: { [id: string]: User }): string[] => { let userIDs = Object.keys(users); userIDs.sort((a: string, b: string): number => { let av: GetterReturnType = this._getter.call(users[a]); @@ -2560,3 +2560,17 @@ class Column { return userIDs; }; } + +type ActivitySeverity = "Info" | "Debug" | "Warn" | "Error" | "Fatal"; +interface ActivityLogEntryDTO { + Id: number; + Name: string; + Overview: string; + ShortOverview: string; + Type: string; + ItemId: string; + Date: number; + UserId: string; + UserPrimaryImageTag: string; + Severity: ActivitySeverity; +} diff --git a/users.go b/users.go index 772a8a0..eec7c93 100644 --- a/users.go +++ b/users.go @@ -207,9 +207,10 @@ func (app *appContext) SetUserDisabled(user mediabrowser.User, disabled bool) (e } func (app *appContext) DeleteUser(user mediabrowser.User) (err error, deleted bool) { + // FIXME: Add DeleteContactMethod to TPS if app.ombi != nil { var tpUser map[string]any - tpUser, err = app.getOmbiUser(user.ID) + tpUser, err = app.getOmbiUser(user.ID, nil) if err == nil { if id, ok := tpUser["id"]; ok { err = app.ombi.DeleteUser(id.(string)) diff --git a/views.go b/views.go index c65b7b7..3c0a94c 100644 --- a/views.go +++ b/views.go @@ -421,7 +421,7 @@ func (app *appContext) ResetPassword(gc *gin.Context) { app.err.Printf(lm.FailedGetUser, username, lm.Jellyfin, err) return } - ombiUser, err := app.getOmbiUser(jfUser.ID) + ombiUser, err := app.getOmbiUser(jfUser.ID, nil) if err != nil { app.err.Printf(lm.FailedGetUser, username, lm.Ombi, err) return