2023-02-01 15:11:10 +00:00
|
|
|
package main
|
|
|
|
|
2023-10-19 14:04:31 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-08-29 13:30:00 +00:00
|
|
|
"github.com/dgraph-io/badger/v4"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-10-19 14:04:31 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2023-10-23 11:49:28 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
2023-10-19 14:04:31 +00:00
|
|
|
)
|
2023-02-01 15:11:10 +00:00
|
|
|
|
|
|
|
// clearEmails removes stored emails for users which no longer exist.
|
|
|
|
// meant to be called with other such housekeeping functions, so assumes
|
|
|
|
// the user cache is fresh.
|
|
|
|
func (app *appContext) clearEmails() {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingEmail)
|
2023-06-24 17:38:52 +00:00
|
|
|
emails := app.storage.GetEmails()
|
|
|
|
for _, email := range emails {
|
2024-08-06 13:48:31 +00:00
|
|
|
_, err := app.jf.UserByID(email.JellyfinID, false)
|
2023-10-19 14:04:31 +00:00
|
|
|
// Make sure the user doesn't exist, and no other error has occured
|
|
|
|
switch err.(type) {
|
|
|
|
case mediabrowser.ErrUserNotFound:
|
|
|
|
app.storage.DeleteEmailsKey(email.JellyfinID)
|
|
|
|
default:
|
2023-06-24 17:38:52 +00:00
|
|
|
continue
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearDiscord does the same as clearEmails, but for Discord Users.
|
|
|
|
func (app *appContext) clearDiscord() {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingDiscord)
|
2023-06-24 17:38:52 +00:00
|
|
|
discordUsers := app.storage.GetDiscord()
|
|
|
|
for _, discordUser := range discordUsers {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(discordUser.JellyfinID, false)
|
2023-10-19 14:04:31 +00:00
|
|
|
// Make sure the user doesn't exist, and no other error has occured
|
|
|
|
switch err.(type) {
|
|
|
|
case mediabrowser.ErrUserNotFound:
|
2024-08-04 14:37:01 +00:00
|
|
|
// Remove role in case their account was deleted oustide of jfa-go
|
|
|
|
app.discord.RemoveRole(discordUser.MethodID().(string))
|
2023-10-19 14:04:31 +00:00
|
|
|
app.storage.DeleteDiscordKey(discordUser.JellyfinID)
|
|
|
|
default:
|
2024-08-04 14:48:01 +00:00
|
|
|
if user.Policy.IsDisabled {
|
|
|
|
app.discord.RemoveRole(discordUser.MethodID().(string))
|
|
|
|
}
|
2023-06-24 17:38:52 +00:00
|
|
|
continue
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearMatrix does the same as clearEmails, but for Matrix Users.
|
|
|
|
func (app *appContext) clearMatrix() {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingMatrix)
|
2023-06-24 17:38:52 +00:00
|
|
|
matrixUsers := app.storage.GetMatrix()
|
|
|
|
for _, matrixUser := range matrixUsers {
|
2024-08-06 13:48:31 +00:00
|
|
|
_, err := app.jf.UserByID(matrixUser.JellyfinID, false)
|
2023-10-19 14:04:31 +00:00
|
|
|
// Make sure the user doesn't exist, and no other error has occured
|
|
|
|
switch err.(type) {
|
|
|
|
case mediabrowser.ErrUserNotFound:
|
|
|
|
app.storage.DeleteMatrixKey(matrixUser.JellyfinID)
|
|
|
|
default:
|
2023-06-24 17:38:52 +00:00
|
|
|
continue
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearTelegram does the same as clearEmails, but for Telegram Users.
|
|
|
|
func (app *appContext) clearTelegram() {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingTelegram)
|
2023-06-24 17:38:52 +00:00
|
|
|
telegramUsers := app.storage.GetTelegram()
|
|
|
|
for _, telegramUser := range telegramUsers {
|
2024-08-06 13:48:31 +00:00
|
|
|
_, err := app.jf.UserByID(telegramUser.JellyfinID, false)
|
2023-10-19 14:04:31 +00:00
|
|
|
// Make sure the user doesn't exist, and no other error has occured
|
|
|
|
switch err.(type) {
|
|
|
|
case mediabrowser.ErrUserNotFound:
|
|
|
|
app.storage.DeleteTelegramKey(telegramUser.JellyfinID)
|
|
|
|
default:
|
2023-06-24 17:38:52 +00:00
|
|
|
continue
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 20:18:16 +00:00
|
|
|
func (app *appContext) clearPWRCaptchas() {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingCaptcha)
|
2023-12-23 20:18:16 +00:00
|
|
|
captchas := map[string]Captcha{}
|
|
|
|
for k, capt := range app.pwrCaptchas {
|
|
|
|
if capt.Generated.Add(CAPTCHA_VALIDITY * time.Second).After(time.Now()) {
|
|
|
|
captchas[k] = capt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.pwrCaptchas = captchas
|
|
|
|
}
|
|
|
|
|
2023-10-23 11:49:28 +00:00
|
|
|
func (app *appContext) clearActivities() {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingActivity)
|
2023-10-23 11:49:28 +00:00
|
|
|
keepCount := app.config.Section("activity_log").Key("keep_n_records").MustInt(1000)
|
|
|
|
maxAgeDays := app.config.Section("activity_log").Key("delete_after_days").MustInt(90)
|
|
|
|
minAge := time.Now().AddDate(0, 0, -maxAgeDays)
|
|
|
|
err := error(nil)
|
|
|
|
errorSource := 0
|
|
|
|
if maxAgeDays != 0 {
|
|
|
|
err = app.storage.db.DeleteMatching(&Activity{}, badgerhold.Where("Time").Lt(minAge))
|
|
|
|
}
|
|
|
|
if err == nil && keepCount != 0 {
|
|
|
|
// app.debug.Printf("Keeping %d records", keepCount)
|
|
|
|
err = app.storage.db.DeleteMatching(&Activity{}, (&badgerhold.Query{}).Reverse().SortBy("Time").Skip(keepCount))
|
|
|
|
if err != nil {
|
|
|
|
errorSource = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == badger.ErrTxnTooBig {
|
2024-08-03 20:23:59 +00:00
|
|
|
app.debug.Printf(lm.ActivityLogTxnTooBig)
|
2023-10-23 11:49:28 +00:00
|
|
|
list := []Activity{}
|
|
|
|
if errorSource == 0 {
|
|
|
|
app.storage.db.Find(&list, badgerhold.Where("Time").Lt(minAge))
|
|
|
|
} else {
|
|
|
|
app.storage.db.Find(&list, (&badgerhold.Query{}).Reverse().SortBy("Time").Skip(keepCount))
|
|
|
|
}
|
|
|
|
for _, record := range list {
|
|
|
|
app.storage.DeleteActivityKey(record.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:24:02 +00:00
|
|
|
func newHousekeepingDaemon(interval time.Duration, app *appContext) *GenericDaemon {
|
|
|
|
d := NewGenericDaemon(interval, app,
|
2023-10-23 11:49:28 +00:00
|
|
|
func(app *appContext) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.HousekeepingInvites)
|
2023-10-23 11:49:28 +00:00
|
|
|
app.checkInvites()
|
|
|
|
},
|
|
|
|
func(app *appContext) { app.clearActivities() },
|
2024-07-31 13:24:02 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 18:30:14 +00:00
|
|
|
d.Name("Housekeeping")
|
2023-02-01 15:11:10 +00:00
|
|
|
|
|
|
|
clearEmail := app.config.Section("email").Key("require_unique").MustBool(false)
|
2024-08-10 18:30:14 +00:00
|
|
|
clearDiscord := discordEnabled && (app.config.Section("discord").Key("require_unique").MustBool(false) || app.config.Section("discord").Key("disable_enable_role").MustBool(false))
|
|
|
|
clearTelegram := telegramEnabled && (app.config.Section("telegram").Key("require_unique").MustBool(false))
|
|
|
|
clearMatrix := matrixEnabled && (app.config.Section("matrix").Key("require_unique").MustBool(false))
|
2023-12-23 20:18:16 +00:00
|
|
|
clearPWR := app.config.Section("captcha").Key("enabled").MustBool(false) && !app.config.Section("captcha").Key("recaptcha").MustBool(false)
|
2023-02-01 15:11:10 +00:00
|
|
|
|
|
|
|
if clearEmail || clearDiscord || clearTelegram || clearMatrix {
|
2024-07-31 13:24:02 +00:00
|
|
|
d.appendJobs(func(app *appContext) { app.jf.CacheExpiry = time.Now() })
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if clearEmail {
|
2024-07-31 13:24:02 +00:00
|
|
|
d.appendJobs(func(app *appContext) { app.clearEmails() })
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
if clearDiscord {
|
2024-07-31 13:24:02 +00:00
|
|
|
d.appendJobs(func(app *appContext) { app.clearDiscord() })
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
if clearTelegram {
|
2024-07-31 13:24:02 +00:00
|
|
|
d.appendJobs(func(app *appContext) { app.clearTelegram() })
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
|
|
|
if clearMatrix {
|
2024-07-31 13:24:02 +00:00
|
|
|
d.appendJobs(func(app *appContext) { app.clearMatrix() })
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
2023-12-23 20:18:16 +00:00
|
|
|
if clearPWR {
|
2024-07-31 13:24:02 +00:00
|
|
|
d.appendJobs(func(app *appContext) { app.clearPWRCaptchas() })
|
2023-12-23 20:18:16 +00:00
|
|
|
}
|
2023-02-01 15:11:10 +00:00
|
|
|
|
2024-07-31 13:24:02 +00:00
|
|
|
return d
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|