refactor: remove login form handler from ui package

This commit is contained in:
ayntgl 2022-01-10 17:01:38 +04:00
parent 1bc81111e8
commit 2f2d0571d8
4 changed files with 69 additions and 72 deletions

59
main.go
View file

@ -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 {

View file

@ -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(),

View file

@ -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)
}
}

View file

@ -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
}