1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00

db: fix contact method cleaning daemons

don't think there's a way to negate a query with badgerhold, so i can't
do "delete(not (where JellyfinID in <ExistingUsers>))", and the old
    method of rebuilding the store is no longer possible.
This commit is contained in:
Harvey Tindall 2023-06-24 18:38:52 +01:00
parent 833be688ac
commit a470d77938
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2

View File

@ -7,85 +7,53 @@ import "time"
// the user cache is fresh. // the user cache is fresh.
func (app *appContext) clearEmails() { func (app *appContext) clearEmails() {
app.debug.Println("Housekeeping: removing unused email addresses") app.debug.Println("Housekeeping: removing unused email addresses")
users, status, err := app.jf.GetUsers(false) emails := app.storage.GetEmails()
if status != 200 || err != nil || len(users) == 0 { for _, email := range emails {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err) _, status, err := app.jf.UserByID(email.JellyfinID, false)
return if status == 200 && err != nil {
} continue
// Rebuild email storage to from existing users to reduce time complexity
emails := emailStore{}
app.storage.emailsLock.Lock()
for _, user := range users {
if email, ok := app.storage.GetEmailsKey(user.ID); ok {
emails[user.ID] = email
} }
app.storage.DeleteEmailsKey(email.JellyfinID)
} }
app.storage.emails = emails
app.storage.storeEmails()
app.storage.emailsLock.Unlock()
} }
// clearDiscord does the same as clearEmails, but for Discord Users. // clearDiscord does the same as clearEmails, but for Discord Users.
func (app *appContext) clearDiscord() { func (app *appContext) clearDiscord() {
app.debug.Println("Housekeeping: removing unused Discord IDs") app.debug.Println("Housekeeping: removing unused Discord IDs")
users, status, err := app.jf.GetUsers(false) discordUsers := app.storage.GetDiscord()
if status != 200 || err != nil || len(users) == 0 { for _, discordUser := range discordUsers {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err) _, status, err := app.jf.UserByID(discordUser.JellyfinID, false)
return if status == 200 && err != nil {
} continue
// Rebuild discord storage to from existing users to reduce time complexity
dcUsers := discordStore{}
app.storage.discordLock.Lock()
for _, user := range users {
if dcUser, ok := app.storage.GetDiscordKey(user.ID); ok {
dcUsers[user.ID] = dcUser
} }
app.storage.DeleteDiscordKey(discordUser.JellyfinID)
} }
app.storage.discord = dcUsers
app.storage.storeDiscordUsers()
app.storage.discordLock.Unlock()
} }
// clearMatrix does the same as clearEmails, but for Matrix Users. // clearMatrix does the same as clearEmails, but for Matrix Users.
func (app *appContext) clearMatrix() { func (app *appContext) clearMatrix() {
app.debug.Println("Housekeeping: removing unused Matrix IDs") app.debug.Println("Housekeeping: removing unused Matrix IDs")
users, status, err := app.jf.GetUsers(false) matrixUsers := app.storage.GetMatrix()
if status != 200 || err != nil || len(users) == 0 { for _, matrixUser := range matrixUsers {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err) _, status, err := app.jf.UserByID(matrixUser.JellyfinID, false)
return if status == 200 && err != nil {
} continue
// Rebuild matrix storage to from existing users to reduce time complexity
mxUsers := matrixStore{}
app.storage.matrixLock.Lock()
for _, user := range users {
if mxUser, ok := app.storage.GetMatrixKey(user.ID); ok {
mxUsers[user.ID] = mxUser
} }
app.storage.DeleteMatrixKey(matrixUser.JellyfinID)
} }
app.storage.matrix = mxUsers
app.storage.storeMatrixUsers()
app.storage.matrixLock.Unlock()
} }
// clearTelegram does the same as clearEmails, but for Telegram Users. // clearTelegram does the same as clearEmails, but for Telegram Users.
func (app *appContext) clearTelegram() { func (app *appContext) clearTelegram() {
app.debug.Println("Housekeeping: removing unused Telegram IDs") app.debug.Println("Housekeeping: removing unused Telegram IDs")
users, status, err := app.jf.GetUsers(false) telegramUsers := app.storage.GetTelegram()
if status != 200 || err != nil || len(users) == 0 { for _, telegramUser := range telegramUsers {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err) _, status, err := app.jf.UserByID(telegramUser.JellyfinID, false)
return if status == 200 && err != nil {
} continue
// Rebuild telegram storage to from existing users to reduce time complexity
tgUsers := telegramStore{}
app.storage.telegramLock.Lock()
for _, user := range users {
if tgUser, ok := app.storage.GetTelegramKey(user.ID); ok {
tgUsers[user.ID] = tgUser
} }
app.storage.DeleteTelegramKey(telegramUser.JellyfinID)
} }
app.storage.telegram = tgUsers
app.storage.storeTelegramUsers()
app.storage.telegramLock.Unlock()
} }
// https://bbengfort.github.io/snippets/2016/06/26/background-work-goroutines-timer.html THANKS // https://bbengfort.github.io/snippets/2016/06/26/background-work-goroutines-timer.html THANKS