1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +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,86 +7,54 @@ import "time"
// the user cache is fresh.
func (app *appContext) clearEmails() {
app.debug.Println("Housekeeping: removing unused email addresses")
users, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil || len(users) == 0 {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
return
emails := app.storage.GetEmails()
for _, email := range emails {
_, status, err := app.jf.UserByID(email.JellyfinID, false)
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.
func (app *appContext) clearDiscord() {
app.debug.Println("Housekeeping: removing unused Discord IDs")
users, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil || len(users) == 0 {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
return
discordUsers := app.storage.GetDiscord()
for _, discordUser := range discordUsers {
_, status, err := app.jf.UserByID(discordUser.JellyfinID, false)
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.
func (app *appContext) clearMatrix() {
app.debug.Println("Housekeeping: removing unused Matrix IDs")
users, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil || len(users) == 0 {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
return
matrixUsers := app.storage.GetMatrix()
for _, matrixUser := range matrixUsers {
_, status, err := app.jf.UserByID(matrixUser.JellyfinID, false)
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.
func (app *appContext) clearTelegram() {
app.debug.Println("Housekeeping: removing unused Telegram IDs")
users, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil || len(users) == 0 {
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
return
telegramUsers := app.storage.GetTelegram()
for _, telegramUser := range telegramUsers {
_, status, err := app.jf.UserByID(telegramUser.JellyfinID, false)
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