From 2f2d0571d8b0a014a7f4f439adc9fb5ffd6f092e Mon Sep 17 00:00:00 2001 From: ayntgl Date: Mon, 10 Jan 2022 17:01:38 +0400 Subject: [PATCH] refactor: remove login form handler from ui package --- main.go | 59 ++++++++++++++++++++++++++++++++++++++++--- ui/app.go | 2 -- ui/handlers.go | 68 ++++++-------------------------------------------- ui/views.go | 12 +++++---- 4 files changed, 69 insertions(+), 72 deletions(-) diff --git a/main.go b/main.go index 829a05e..82349c3 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,9 @@ package main import ( "os" + "github.com/ayntgl/discordo/discord" "github.com/ayntgl/discordo/ui" + "github.com/rivo/tview" "github.com/zalando/go-keyring" ) @@ -26,11 +28,60 @@ func main() { SetRoot(ui.NewMainFlex(app), true). SetFocus(app.GuildsList) } else { - ui.NewLoginForm(app, func() { - ui.OnLoginFormLoginButtonSelected(app) - }, false) + loginForm := ui.NewLoginForm(false) + loginForm.AddButton("Login", func() { + email := loginForm.GetFormItem(0).(*tview.InputField).GetText() + password := loginForm.GetFormItem(1).(*tview.InputField).GetText() + if email == "" || password == "" { + return + } - app.SetRoot(app.LoginForm, true) + // Login using the email and password + lr, err := discord.Login(app.Session, email, password) + if err != nil { + panic(err) + } + + if lr.Token != "" && !lr.MFA { + err = app.Connect(lr.Token) + if err != nil { + panic(err) + } + + app. + SetRoot(ui.NewMainFlex(app), true). + SetFocus(app.GuildsList) + + go keyring.Set("discordo", "token", lr.Token) + } else { + // The account has MFA enabled, reattempt login with MFA code and ticket. + mfaLoginForm := ui.NewLoginForm(true) + mfaLoginForm.AddButton("Login", func() { + mfaCode := loginForm.GetFormItem(0).(*tview.InputField).GetText() + if mfaCode == "" { + return + } + + lr, err = discord.TOTP(app.Session, mfaCode, lr.Ticket) + if err != nil { + panic(err) + } + + err = app.Connect(lr.Token) + if err != nil { + panic(err) + } + + app. + SetRoot(ui.NewMainFlex(app), true). + SetFocus(app.GuildsList) + + go keyring.Set("discordo", "token", lr.Token) + }) + } + }) + + app.SetRoot(loginForm, true) } if err := app.EnableMouse(app.Config.General.Mouse).Run(); err != nil { diff --git a/ui/app.go b/ui/app.go index b128d96..5ce202b 100644 --- a/ui/app.go +++ b/ui/app.go @@ -13,7 +13,6 @@ import ( type App struct { *tview.Application - LoginForm *tview.Form GuildsList *tview.List ChannelsTreeView *tview.TreeView MessagesTextView *tview.TextView @@ -29,7 +28,6 @@ func NewApp() *App { s, _ := discordgo.New() return &App{ Application: tview.NewApplication(), - LoginForm: tview.NewForm(), GuildsList: tview.NewList(), ChannelsTreeView: tview.NewTreeView(), MessagesTextView: tview.NewTextView(), diff --git a/ui/handlers.go b/ui/handlers.go index e561613..4d43853 100644 --- a/ui/handlers.go +++ b/ui/handlers.go @@ -6,10 +6,9 @@ import ( "github.com/atotto/clipboard" "github.com/ayntgl/discordgo" - util "github.com/ayntgl/discordo/discord" + "github.com/ayntgl/discordo/discord" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" - "github.com/zalando/go-keyring" ) func onAppInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey { @@ -194,9 +193,9 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey return nil } - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0]) if m.ReferencedMessage != nil { - app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID) + app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID) app.MessagesTextView. Highlight(m.ReferencedMessage.ID). ScrollToHighlight() @@ -209,7 +208,7 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey return nil } - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0]) app.MessageInputField.SetTitle("Replying to " + m.Author.String()) app.SetFocus(app.MessageInputField) return nil @@ -219,7 +218,7 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey return nil } - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0]) app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String()) app.SetFocus(app.MessageInputField) return nil @@ -229,7 +228,7 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey return nil } - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0]) err := clipboard.WriteAll(m.Content) if err != nil { return nil @@ -259,7 +258,7 @@ func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKe } if len(app.MessagesTextView.GetHighlights()) != 0 { - _, m := util.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0]) + _, m := discord.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0]) d := &discordgo.MessageSend{ Content: t, Reference: m.Reference(), @@ -299,56 +298,3 @@ func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKe return e } - -func OnLoginFormLoginButtonSelected(app *App) { - email := app.LoginForm.GetFormItem(0).(*tview.InputField).GetText() - password := app.LoginForm.GetFormItem(1).(*tview.InputField).GetText() - if email == "" || password == "" { - return - } - - // Login using the email and password - lr, err := util.Login(app.Session, email, password) - if err != nil { - panic(err) - } - - if lr.Token != "" && !lr.MFA { - app. - SetRoot(NewMainFlex(app), true). - SetFocus(app.GuildsList) - - err = app.Connect(lr.Token) - if err != nil { - panic(err) - } - - go keyring.Set("discordo", "token", lr.Token) - } else if lr.MFA { - // The account has MFA enabled, reattempt login with code and ticket. - NewLoginForm(app, func() { - code := app.LoginForm.GetFormItem(0).(*tview.InputField).GetText() - if code == "" { - return - } - - lr, err = util.TOTP(app.Session, code, lr.Ticket) - if err != nil { - panic(err) - } - - app. - SetRoot(NewMainFlex(app), true). - SetFocus(app.GuildsList) - - err = app.Connect(lr.Token) - if err != nil { - panic(err) - } - - go keyring.Set("discordo", "token", lr.Token) - }, true) - - app.SetRoot(app.LoginForm, true) - } -} diff --git a/ui/views.go b/ui/views.go index 19f52c1..c1df5a4 100644 --- a/ui/views.go +++ b/ui/views.go @@ -68,18 +68,20 @@ func NewMainFlex(app *App) *tview.Flex { AddItem(rightFlex, 0, 4, false) } -func NewLoginForm(app *App, onLoginFormLoginButtonSelected func(), mfa bool) { - app.LoginForm. - AddButton("Login", onLoginFormLoginButtonSelected). +func NewLoginForm(mfa bool) *tview.Form { + loginForm := tview.NewForm() + loginForm. SetButtonsAlign(tview.AlignCenter). SetBorder(true). SetBorderPadding(0, 0, 1, 0) if mfa { - app.LoginForm.AddPasswordField("Code", "", 0, 0, nil) + loginForm.AddPasswordField("MFA Code (optional)", "", 0, 0, nil) } else { - app.LoginForm. + loginForm. AddInputField("Email", "", 0, nil, nil). AddPasswordField("Password", "", 0, 0, nil) } + + return loginForm }