From 7f11549337a72627579a073bf29a63d644384f31 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 25 Jan 2022 17:01:18 +0000 Subject: [PATCH] ombi: broken discord/telegram linking doesn't work due to https://github.com/ombi-app/ombi/issues/3484 committing to save progress. --- api.go | 51 +++++++++++++++++++++++++++++++++++++-------------- ombi/ombi.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/api.go b/api.go index e923c0f..14dda40 100644 --- a/api.go +++ b/api.go @@ -513,9 +513,11 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc } } id := user.ID + var profile Profile if invite.Profile != "" { app.debug.Printf("Applying settings from profile \"%s\"", invite.Profile) - profile, ok := app.storage.profiles[invite.Profile] + var ok bool + profile, ok = app.storage.profiles[invite.Profile] if !ok { profile = app.storage.profiles["Default"] } @@ -536,19 +538,6 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc app.err.Printf("%s: Failed to set configuration template (%d): %v", req.Code, status, err) } } - if app.config.Section("ombi").Key("enabled").MustBool(false) { - if profile.Ombi != nil && len(profile.Ombi) != 0 { - errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, profile.Ombi) - if err != nil || code != 200 { - app.info.Printf("Failed to create Ombi user (%d): %s", code, err) - app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", ")) - } else { - app.info.Println("Created Ombi user") - } - } else { - app.debug.Printf("Skipping Ombi: Profile \"%s\" was empty", invite.Profile) - } - } } // if app.config.Section("password_resets").Key("enabled").MustBool(false) { if req.Email != "" { @@ -598,6 +587,40 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc app.telegram.verifiedTokens = app.telegram.verifiedTokens[:len(app.telegram.verifiedTokens)-1] } } + if invite.Profile != "" && app.config.Section("ombi").Key("enabled").MustBool(false) { + if profile.Ombi != nil && len(profile.Ombi) != 0 { + template := profile.Ombi + errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, template) + if err != nil || code != 200 { + app.info.Printf("Failed to create Ombi user (%d): %s", code, err) + app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", ")) + } else { + app.info.Println("Created Ombi user") + if (discordEnabled && discordVerified) || (telegramEnabled && telegramTokenIndex != -1) { + ombiUser, status, err := app.getOmbiUser(id) + if status != 200 || err != nil { + app.err.Printf("Failed to get Ombi user (%d): %v", status, err) + } else { + dID := "" + tUser := "" + if discordEnabled && discordVerified { + dID = discordUser.ID + } + if telegramEnabled && telegramTokenIndex != -1 { + tUser = app.storage.telegram[user.ID].Username + } + resp, status, err := app.ombi.SetNotificationPrefs(ombiUser, dID, tUser) + if !(status == 200 || status == 204) || err != nil { + app.err.Printf("Failed to link Telegram/Discord to Ombi (%d): %v", status, err) + app.debug.Printf("Response: %v", resp) + } + } + } + } + } else { + app.debug.Printf("Skipping Ombi: Profile \"%s\" was empty", invite.Profile) + } + } if matrixVerified { matrixUser.Contact = req.MatrixContact delete(app.matrix.tokens, req.MatrixPIN) diff --git a/ombi/ombi.go b/ombi/ombi.go index 9ebdbfd..839e402 100644 --- a/ombi/ombi.go +++ b/ombi/ombi.go @@ -13,6 +13,11 @@ import ( "github.com/hrfee/jfa-go/common" ) +const ( + NotifAgentDiscord = 1 + NotifAgentTelegram = 4 +) + // Ombi represents a running Ombi instance. type Ombi struct { server, key string @@ -81,7 +86,7 @@ func (ombi *Ombi) getJSON(url string, params map[string]string) (string, int, er } // does a POST and optionally returns response as string. Returns a string instead of an io.reader bcs i couldn't get it working otherwise. -func (ombi *Ombi) send(mode string, url string, data map[string]interface{}, response bool) (string, int, error) { +func (ombi *Ombi) send(mode string, url string, data interface{}, response bool) (string, int, error) { responseText := "" params, _ := json.Marshal(data) req, _ := http.NewRequest(mode, url, bytes.NewBuffer(params)) @@ -220,3 +225,29 @@ func (ombi *Ombi) NewUser(username, password, email string, template map[string] ombi.cacheExpiry = time.Now() return nil, code, err } + +type NotificationPref struct { + Agent int `json:"agent"` + UserID string `json:"userId"` + Value string `json:"value"` + Enabled bool `json:"enabled"` +} + +func (ombi *Ombi) SetNotificationPrefs(user map[string]interface{}, discordID, telegramUser string) (result string, code int, err error) { + fmt.Printf("%+v\n", user) + url := fmt.Sprintf("%s/api/v1/Identity", ombi.server) + id := user["id"].(string) + var data []NotificationPref + if discordID != "" { + data = []NotificationPref{NotificationPref{NotifAgentDiscord, id, discordID, true}} + } + if telegramUser != "" { + data = append(data, NotificationPref{NotifAgentTelegram, id, telegramUser, true}) + } + if _, ok := user["user"]; !ok { + user["user"] = map[string]interface{}{} + } + user["user"].(map[string]interface{})["userNotificationPreferences"] = data + result, code, err = ombi.send("PUT", url, user, true) + return +}