mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-11-09 20:00:12 +00:00
Harvey Tindall
54e4a51a7f
shared "newUser" method is now "NewUserPostVerification", and is shared between all routes which create a jellyfin account. The new "NewUserFromInvite", "NewUserFromAdmin" and "NewUserFromConfirmationKey" are smaller as a result. Discord, Telegram, and Matrix now implement the "ContactMethodLinker" and "ContactMethodUser" interfaces, meaning code is shared a lot between them in the NewUser methods, and the specifics are now in their own files. Ombi/Jellyseerr similarly implement a simpler interface "ThirdPartyService", which simply has ImportUser and AddContactMethod routes. Note these new interface methods are only used for user creation as of yet, but could likely be used in other places.
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/hrfee/jfa-go/jellyseerr"
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
|
)
|
|
|
|
func (app *appContext) SynchronizeJellyseerrUser(jfID string) {
|
|
user, imported, err := app.js.GetOrImportUser(jfID)
|
|
if err != nil {
|
|
app.debug.Printf(lm.FailedImportUser, lm.Jellyseerr, jfID, err)
|
|
return
|
|
}
|
|
if imported {
|
|
app.debug.Printf(lm.ImportJellyseerrUser, jfID, user.ID)
|
|
}
|
|
notif, err := app.js.GetNotificationPreferencesByID(user.ID)
|
|
if err != nil {
|
|
app.debug.Printf(lm.FailedGetJellyseerrNotificationPrefs, jfID, err)
|
|
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 {
|
|
app.err.Printf(lm.FailedSetEmailAddress, lm.Jellyseerr, jfID, err)
|
|
} 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 {
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (app *appContext) SynchronizeJellyseerrUsers() {
|
|
users, status, err := app.jf.GetUsers(false)
|
|
if err != nil || status != 200 {
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
|
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
|
|
}
|