2024-07-31 13:24:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hrfee/jfa-go/jellyseerr"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2024-07-31 13:24:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (app *appContext) SynchronizeJellyseerrUser(jfID string) {
|
|
|
|
user, imported, err := app.js.GetOrImportUser(jfID)
|
|
|
|
if err != nil {
|
2024-08-03 20:23:59 +00:00
|
|
|
app.debug.Printf(lm.FailedImportUser, lm.Jellyseerr, jfID, err)
|
2024-07-31 13:24:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if imported {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Printf(lm.ImportJellyseerrUser, jfID, user.ID)
|
2024-07-31 13:24:02 +00:00
|
|
|
}
|
|
|
|
notif, err := app.js.GetNotificationPreferencesByID(user.ID)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Printf(lm.FailedGetJellyseerrNotificationPrefs, jfID, err)
|
2024-07-31 13:24:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contactMethods := map[jellyseerr.NotificationsField]any{}
|
|
|
|
email, ok := app.storage.GetEmailsKey(jfID)
|
|
|
|
if ok && email.Addr != "" && user.Email != email.Addr {
|
|
|
|
err = app.js.ModifyMainUserSettings(jfID, jellyseerr.MainUserSettings{Email: email.Addr})
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedSetEmailAddress, lm.Jellyseerr, jfID, err)
|
2024-07-31 13:24:02 +00:00
|
|
|
} else {
|
|
|
|
contactMethods[jellyseerr.FieldEmailEnabled] = email.Contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if discordEnabled {
|
|
|
|
dcUser, ok := app.storage.GetDiscordKey(jfID)
|
|
|
|
if ok && dcUser.ID != "" && notif.DiscordID != dcUser.ID {
|
|
|
|
contactMethods[jellyseerr.FieldDiscord] = dcUser.ID
|
|
|
|
contactMethods[jellyseerr.FieldDiscordEnabled] = dcUser.Contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if telegramEnabled {
|
|
|
|
tgUser, ok := app.storage.GetTelegramKey(jfID)
|
|
|
|
chatID, _ := strconv.ParseInt(notif.TelegramChatID, 10, 64)
|
|
|
|
if ok && tgUser.ChatID != 0 && chatID != tgUser.ChatID {
|
|
|
|
u, _ := app.storage.GetTelegramKey(jfID)
|
|
|
|
contactMethods[jellyseerr.FieldTelegram] = u.ChatID
|
|
|
|
contactMethods[jellyseerr.FieldTelegramEnabled] = tgUser.Contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(contactMethods) != 0 {
|
|
|
|
err := app.js.ModifyNotifications(jfID, contactMethods)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
2024-07-31 13:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) SynchronizeJellyseerrUsers() {
|
2024-08-06 13:48:31 +00:00
|
|
|
users, err := app.jf.GetUsers(false)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2024-07-31 13:24:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// I'm sure Jellyseerr can handle it,
|
|
|
|
// but past issues with the Jellyfin db scare me from
|
|
|
|
// running these concurrently. W/e, its a bg task anyway.
|
|
|
|
for _, user := range users {
|
|
|
|
app.SynchronizeJellyseerrUser(user.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newJellyseerrDaemon(interval time.Duration, app *appContext) *GenericDaemon {
|
|
|
|
d := NewGenericDaemon(interval, app,
|
|
|
|
func(app *appContext) {
|
|
|
|
app.SynchronizeJellyseerrUsers()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
d.Name("Jellyseerr import daemon")
|
|
|
|
return d
|
|
|
|
}
|