]> git.openfl.eu Git - chat-scroll.git/commitdiff
change auth method
authorFl_GUI <flor.guilini@hotmail.com>
Sun, 15 Dec 2024 15:01:42 +0000 (16:01 +0100)
committerFl_GUI <flor.guilini@hotmail.com>
Sun, 15 Dec 2024 15:01:42 +0000 (16:01 +0100)
decorations/badges.go
decorations/emotes.go
main.go
messages/client.go
model/model.go
status/create.go
status/twitchconn/twitchConnection.go
status/twitchlogin/twitchLogin.go [deleted file]
todo
token/validate.go

index 9dab742a870bfd174a76a31998b02f17dee5e44a..dd6a32719c576d6a37610381be3cda6f710668d2 100644 (file)
@@ -27,10 +27,14 @@ func GetBadge(badgeName string) *image.Image {
        return &badge
 }
 
-var modelValid = model.Application.Twitch.Valid
+var tokenState = model.Application.Twitch.TokenState
 
 func downloadBadges() {
-       if val, _ := modelValid.Get(); !val {
+       if val, _ := tokenState.Get(); val != model.Valid {
+               return
+       }
+
+       if len(badgeMap) != 0 {
                return
        }
 
@@ -86,5 +90,5 @@ func saveBadge(name string, url string) {
 
 func init() {
        badgeMap = make(map[string]image.Image)
-       modelValid.AddListener(binding.NewDataListener(downloadBadges))
+       tokenState.AddListener(binding.NewDataListener(downloadBadges))
 }
index 1a59242fc0d98165e3480f795c391eb8d446f946..9bf89abb93f51ea1ca5b2f529b91e3ca9558e86e 100644 (file)
@@ -10,6 +10,7 @@ import (
        "sync"
 
        "fyne.io/fyne/v2/data/binding"
+       "go.openfl.eu/chat-scroller/model"
        "go.openfl.eu/chat-scroller/token"
 )
 
@@ -32,7 +33,11 @@ func GetEmote(emoteId string) *image.Image {
 }
 
 func downloadEmotes() {
-       if val, _ := modelValid.Get(); !val {
+       if val, _ := tokenState.Get(); val != model.Valid {
+               return
+       }
+
+       if len(emoteMap) != 0 {
                return
        }
 
@@ -95,5 +100,5 @@ func saveEmoteId(id string, template string) *image.Image {
 
 func init() {
        emoteMap = make(map[string]image.Image)
-       modelValid.AddListener(binding.NewDataListener(downloadEmotes))
+       tokenState.AddListener(binding.NewDataListener(downloadEmotes))
 }
diff --git a/main.go b/main.go
index e97144fb7483132893f7a228960e43a28b8febd8..dd9502fc62dfb9058039aeca5e6f79db6e7f5ce4 100644 (file)
--- a/main.go
+++ b/main.go
@@ -7,12 +7,16 @@ import (
        "runtime/pprof"
 
        "fyne.io/fyne/v2/app"
+       "fyne.io/fyne/v2/data/binding"
        "fyne.io/fyne/v2/theme"
+       "go.openfl.eu/chat-scroller/model"
        "go.openfl.eu/chat-scroller/status"
+       "go.openfl.eu/chat-scroller/token"
 
        // for init purposes
        _ "go.openfl.eu/chat-scroller/decorations"
        _ "go.openfl.eu/chat-scroller/messages"
+       _ "go.openfl.eu/chat-scroller/model"
        _ "go.openfl.eu/chat-scroller/token"
 )
 
@@ -35,6 +39,9 @@ func main() {
 
        scroller := app.New()
 
+       // this listener will trigger on initialization
+       model.Application.Twitch.TokenState.AddListener(binding.NewDataListener(func() { token.Authenticate(&scroller) }))
+
        scroller.Settings().SetTheme(theme.LightTheme())
 
        go status.CreateSetupWindow(scroller)
index f52da2c5be7179a15c87a11c3caa18e447146f02..b577527ff04fe93b434232c9f44ecfd99cde6f91 100644 (file)
@@ -14,7 +14,7 @@ import (
        "go.openfl.eu/chat-scroller/token"
 )
 
-var valid = model.Application.Twitch.Valid
+var state = model.Application.Twitch.TokenState
 
 var client = twitch.NewChatClient()
 
@@ -29,8 +29,8 @@ func startClient() {
                core.DebugLogger = os.Stdout
        }
 
-       val, _ := valid.Get()
-       if !val {
+       val, _ := state.Get()
+       if val != model.Valid {
                return
        }
        if generate {
@@ -74,5 +74,5 @@ func generateFake() {
 }
 
 func init() {
-       valid.AddListener(fynepatch.NewGoroutineDataListener(startClient))
+       state.AddListener(fynepatch.NewGoroutineDataListener(startClient))
 }
index ab023f8123774152076db31eb64dcb55bde17b1a..519d7c084e62c10b99c628e4482e0dbc46ad557e 100644 (file)
@@ -11,8 +11,8 @@ type Model struct {
 }
 
 type TwitchModel struct {
-       Valid binding.Bool
-       Token binding.String
+       TokenState binding.Untyped
+       Token      binding.String
 }
 
 var Application Model
@@ -20,15 +20,16 @@ var Application Model
 func init() {
        {
                tw := &Application.Twitch
-               tw.Valid = binding.NewBool()
+               tw.TokenState = binding.NewUntyped()
+               tw.TokenState.Set(Absent)
                tw.Token = binding.NewString()
        }
 
-       debug := false
+       debug := true
        if debug {
-               Application.Twitch.Valid.AddListener(binding.NewDataListener(func() {
-                       fmt.Print("token valid: ")
-                       fmt.Println(Application.Twitch.Valid.Get())
+               Application.Twitch.TokenState.AddListener(binding.NewDataListener(func() {
+                       fmt.Print("token state: ")
+                       fmt.Println(Application.Twitch.TokenState.Get())
                }))
                Application.Twitch.Token.AddListener(binding.NewDataListener(func() {
                        fmt.Print("access token: ")
index 1b413d6f16692f7f94c5d7b19af7c4656f575bec..5fbf979d912ed8d18695242068b8132905d631fb 100644 (file)
@@ -8,7 +8,6 @@ import (
        "go.openfl.eu/chat-scroller/chat"
        "go.openfl.eu/chat-scroller/model"
        "go.openfl.eu/chat-scroller/status/twitchconn"
-       "go.openfl.eu/chat-scroller/status/twitchlogin"
 )
 
 type windows struct {
@@ -18,7 +17,7 @@ type windows struct {
 
 var cscrWindows windows
 
-var valid = model.Application.Twitch.Valid
+var state = model.Application.Twitch.TokenState
 
 // UI items
 var (
@@ -31,8 +30,8 @@ func spawnChatWindow(app fyne.App) {
 }
 
 func ableSpawnButton() {
-       val, _ := valid.Get()
-       if val {
+       val, _ := state.Get()
+       if val == model.Valid {
                spawnButton.Enable()
        } else {
                spawnButton.Disable()
@@ -43,7 +42,6 @@ func CreateSetupWindow(app fyne.App) {
 
        // init but after app is created
        createSpinner()
-       twitchlogin.CreateContent()
        twitchconn.CreateContent()
 
        // own creations
@@ -58,10 +56,9 @@ func CreateSetupWindow(app fyne.App) {
        statusWindow.SetContent(container.NewVBox(
                //spinner,
                twitchconn.Content,
-               twitchlogin.Content,
                spawnButton,
        ))
        statusWindow.Show()
 
-       valid.AddListener(binding.NewDataListener(ableSpawnButton))
+       state.AddListener(binding.NewDataListener(ableSpawnButton))
 }
index 1df940fc9e8e551d92dcabfd52502ca254faec64..b9ccc90149816c6c3e89b9c5ec69b57e258673bf 100644 (file)
@@ -10,7 +10,7 @@ import (
 
 var Content fyne.CanvasObject
 
-var valid = model.Application.Twitch.Valid
+var tokenState = model.Application.Twitch.TokenState
 var token = model.Application.Twitch.Token
 
 // UI elements
@@ -30,11 +30,14 @@ func showContent() {
 }
 
 func updateLabel() {
-       val, _ := valid.Get()
-       if val {
+       state, _ := tokenState.Get()
+       switch state {
+       case model.Valid:
                tokenLabel.Text = "access token available"
-       } else {
+       case model.Invalid:
                tokenLabel.Text = "access token invalid"
+       case model.Absent:
+               tokenLabel.Text = "Please log in"
        }
 
        Content.Refresh()
@@ -44,8 +47,7 @@ func CreateContent() {
        Content = container.NewMax(
                tokenLabel,
        )
-       Content.Hide()
 
-       token.AddListener(binding.NewDataListener(showContent))
-       valid.AddListener(binding.NewDataListener(updateLabel))
+       //token.AddListener(binding.NewDataListener(showContent))
+       tokenState.AddListener(binding.NewDataListener(updateLabel))
 }
diff --git a/status/twitchlogin/twitchLogin.go b/status/twitchlogin/twitchLogin.go
deleted file mode 100644 (file)
index acae1b4..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-package twitchlogin
-
-import (
-       "net/url"
-       "sync"
-
-       "fyne.io/fyne/v2"
-       "fyne.io/fyne/v2/container"
-       "fyne.io/fyne/v2/data/binding"
-       "fyne.io/fyne/v2/widget"
-       "go.openfl.eu/chat-scroller/model"
-       "go.openfl.eu/twitch-auth/device"
-)
-
-var Content fyne.CanvasObject
-var tokenChannel <-chan device.AuthResponse
-var readingTokens = sync.Mutex{}
-
-var valid = model.Application.Twitch.Valid
-
-func login() {
-       if !readingTokens.TryLock() {
-               return
-       }
-       defer readingTokens.Unlock()
-
-       // TODO closing channel when/how?
-       tokenChannel = device.Authenticate(&device.Config{
-               "9mcopb33ssgli53u6cor8ou2pvyb0g",
-               []string{"user:read:chat", "user:write:chat", "chat:read"},
-       })
-
-       app := fyne.CurrentApp()
-
-       for token := range tokenChannel {
-               if token.Err == nil {
-                       model.Application.Twitch.Token.Set(token.AccessCode)
-               } else {
-                       valid.Set(false)
-                       err := token.Err
-                       validating, ok := err.(device.AuthorizationPendingError)
-                       if ok {
-                               u, err := url.Parse(validating.Url)
-                               if err != nil {
-                                       panic(err)
-                               }
-                               err = app.OpenURL(u)
-                               if err != nil {
-                                       panic(err)
-                               }
-                       }
-               }
-       }
-
-}
-
-func CreateContent() {
-       label := widget.NewLabel("hi")
-       loginButton := widget.NewButton("login to twitch", func() {
-               go login()
-       })
-       Content = container.NewVBox(label, loginButton)
-
-       valid.AddListener(binding.NewDataListener(func() {
-               val, _ := valid.Get()
-               if val {
-                       Content.Hide()
-               } else {
-                       Content.Show()
-               }
-
-               Content.Refresh()
-       }))
-}
diff --git a/todo b/todo
index 86a3293347bb5087f25c15b33c911ad329e82246..4bdbb4daf8dc058a7e36ccdfa7a7c7a8bba546e4 100644 (file)
--- a/todo
+++ b/todo
@@ -1,6 +1,6 @@
-#TODO
+--TODO
 
-auth again?
-persistence
+better emotes showing
 chat settings
-log levels
+persistence
+log levels?
index a5c16453935db6a30bd2a6037953e39c5df95d57..4a0f1102577d407d8eb76db7503feccaa4f9b730 100644 (file)
@@ -30,7 +30,7 @@ func validate(token string, alreadyLocked bool) {
        // token error
        if resp.StatusCode == http.StatusUnauthorized {
                fmt.Println("invalid token")
-               model.Application.Twitch.Valid.Set(false)
+               model.Application.Twitch.TokenState.Set(model.Invalid)
                return
        }
 
@@ -52,7 +52,7 @@ func validate(token string, alreadyLocked bool) {
                fmt.Printf("couldn't decode validation response: %s\n", err)
        }
 
-       model.Application.Twitch.Valid.Set(true)
+       model.Application.Twitch.TokenState.Set(model.Valid)
 }
 
 func init() {