refactor: simplify codebase
This commit is contained in:
+10
-39
@@ -2,18 +2,11 @@ package tui
|
||||
|
||||
import (
|
||||
"parasocial/internal/auth"
|
||||
"parasocial/internal/irc"
|
||||
"parasocial/internal/miner"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
// IRCState describes the current IRC join lifecycle for one streamer row.
|
||||
type IRCState string
|
||||
|
||||
const (
|
||||
IRCPending IRCState = "pending"
|
||||
IRCJoined IRCState = "joined"
|
||||
IRCDisconnected IRCState = "disconnected"
|
||||
)
|
||||
|
||||
// AuthUpdate carries one incremental auth log line or completion result into the TUI.
|
||||
type AuthUpdate struct {
|
||||
Line string
|
||||
@@ -24,36 +17,14 @@ type AuthUpdate struct {
|
||||
|
||||
// StreamerUpdate carries one streamer resolution update into the TUI.
|
||||
type StreamerUpdate struct {
|
||||
Viewer *twitch.Viewer
|
||||
Entry *twitch.StreamerEntry
|
||||
IRC *IRCUpdate
|
||||
Miner *MinerUpdate
|
||||
Index int
|
||||
Err error
|
||||
Done bool
|
||||
}
|
||||
|
||||
// IRCUpdate carries one IRC connection state or log line into the TUI.
|
||||
type IRCUpdate struct {
|
||||
Login string
|
||||
State IRCState
|
||||
Line string
|
||||
}
|
||||
|
||||
// MinerUpdate carries one miner log line into the TUI.
|
||||
type MinerUpdate struct {
|
||||
Login string
|
||||
Line string
|
||||
Status *MinerStatus
|
||||
}
|
||||
|
||||
// MinerStatus carries the current active watch state for one streamer.
|
||||
type MinerStatus struct {
|
||||
Watching bool
|
||||
Reason string
|
||||
WatchedMinutes int
|
||||
WatchStreakMinutes int
|
||||
WatchStreak *int
|
||||
Viewer *twitch.Viewer
|
||||
Entry *twitch.StreamerEntry
|
||||
IRC *irc.Event
|
||||
MinerLog *miner.LogEntry
|
||||
MinerStatus *miner.StatusEntry
|
||||
Index int
|
||||
Err error
|
||||
Done bool
|
||||
}
|
||||
|
||||
type authStartedMsg struct {
|
||||
|
||||
+23
-34
@@ -8,6 +8,8 @@ import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"parasocial/internal/auth"
|
||||
"parasocial/internal/irc"
|
||||
"parasocial/internal/miner"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
@@ -61,7 +63,7 @@ type Model struct {
|
||||
focus panelFocus
|
||||
ircDetails map[string]ircDetail
|
||||
minerDetails map[string][]string
|
||||
minerStatuses map[string]MinerStatus
|
||||
minerStatuses map[string]miner.StatusEntry
|
||||
authViewport viewport.Model
|
||||
ircViewport viewport.Model
|
||||
minerViewport viewport.Model
|
||||
@@ -87,13 +89,13 @@ func New(options Options) Model {
|
||||
mode: authView,
|
||||
ircDetails: make(map[string]ircDetail),
|
||||
minerDetails: make(map[string][]string),
|
||||
minerStatuses: make(map[string]MinerStatus),
|
||||
minerStatuses: make(map[string]miner.StatusEntry),
|
||||
width: defaultViewWidth,
|
||||
height: 24,
|
||||
focus: focusStreamers,
|
||||
authViewport: newAuthViewport(contentWidth(defaultViewWidth), authViewportHeight(24)),
|
||||
ircViewport: newIRCViewport(detailViewportWidth(defaultViewWidth), detailViewportHeight(24, "")),
|
||||
minerViewport: newMinerViewport(
|
||||
authViewport: viewport.New(contentWidth(defaultViewWidth), authViewportHeight(24)),
|
||||
ircViewport: viewport.New(detailViewportWidth(defaultViewWidth), detailViewportHeight(24, "")),
|
||||
minerViewport: viewport.New(
|
||||
detailViewportWidth(defaultViewWidth),
|
||||
detailViewportHeight(24, ""),
|
||||
),
|
||||
@@ -206,7 +208,7 @@ func (m Model) updateAuth(msg AuthUpdate) (tea.Model, tea.Cmd) {
|
||||
m.streamers = loadingEntries(m.streamers)
|
||||
m.ircDetails = make(map[string]ircDetail)
|
||||
m.minerDetails = make(map[string][]string)
|
||||
m.minerStatuses = make(map[string]MinerStatus)
|
||||
m.minerStatuses = make(map[string]miner.StatusEntry)
|
||||
m.selectedConfig = ""
|
||||
m.focus = focusStreamers
|
||||
m.ensureSelection()
|
||||
@@ -233,8 +235,17 @@ func (m Model) updateStreamer(msg StreamerUpdate) (tea.Model, tea.Cmd) {
|
||||
if msg.IRC != nil {
|
||||
m.applyIRCUpdate(*msg.IRC)
|
||||
}
|
||||
if msg.Miner != nil {
|
||||
m.applyMinerUpdate(*msg.Miner)
|
||||
if update := msg.MinerLog; update != nil {
|
||||
if login := normalizeKey(update.Login); login != "" {
|
||||
if line := strings.TrimSpace(update.Line); line != "" {
|
||||
m.minerDetails[login] = appendCappedHistory(m.minerDetails[login], line)
|
||||
}
|
||||
}
|
||||
}
|
||||
if update := msg.MinerStatus; update != nil {
|
||||
if login := normalizeKey(update.Login); login != "" {
|
||||
m.minerStatuses[login] = *update
|
||||
}
|
||||
}
|
||||
if msg.Err != nil {
|
||||
m.resolveErr = msg.Err
|
||||
@@ -251,17 +262,17 @@ func (m Model) updateStreamer(msg StreamerUpdate) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Model) applyIRCUpdate(update IRCUpdate) {
|
||||
login := normalizeKey(update.Login)
|
||||
func (m *Model) applyIRCUpdate(update irc.Event) {
|
||||
login := normalizeKey(update.Streamer)
|
||||
if login == "" {
|
||||
return
|
||||
}
|
||||
|
||||
detail := m.ircDetails[login]
|
||||
switch update.State {
|
||||
case IRCJoined:
|
||||
case irc.StateJoined:
|
||||
detail.joined = true
|
||||
case IRCPending, IRCDisconnected:
|
||||
case irc.StatePending, irc.StateDisconnected:
|
||||
detail.joined = false
|
||||
}
|
||||
if message, ok := formatIRCChatLine(update.Line); ok {
|
||||
@@ -270,20 +281,6 @@ func (m *Model) applyIRCUpdate(update IRCUpdate) {
|
||||
m.ircDetails[login] = detail
|
||||
}
|
||||
|
||||
func (m *Model) applyMinerUpdate(update MinerUpdate) {
|
||||
login := normalizeKey(update.Login)
|
||||
if login == "" {
|
||||
return
|
||||
}
|
||||
if update.Status != nil {
|
||||
m.minerStatuses[login] = *update.Status
|
||||
}
|
||||
line := strings.TrimSpace(update.Line)
|
||||
if line != "" {
|
||||
m.minerDetails[login] = appendCappedHistory(m.minerDetails[login], line)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) ensureSelection() {
|
||||
entries := m.orderedStreamers()
|
||||
if len(entries) == 0 {
|
||||
@@ -482,14 +479,6 @@ func (m Model) visibleDetailTab() detailTab {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) isStreamersFocused() bool {
|
||||
return m.focus == focusStreamers
|
||||
}
|
||||
|
||||
func (m Model) isRightPanelFocused() bool {
|
||||
return m.focus == focusInfo || m.focus == focusIRC || m.focus == focusMiner
|
||||
}
|
||||
|
||||
func isActive(entry twitch.StreamerEntry) bool {
|
||||
return entry.Status == twitch.StreamerReady && entry.Live
|
||||
}
|
||||
|
||||
+38
-42
@@ -8,6 +8,8 @@ import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"parasocial/internal/auth"
|
||||
"parasocial/internal/irc"
|
||||
"parasocial/internal/miner"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
@@ -59,7 +61,7 @@ func TestAuthUpdateAppendsLogLine(t *testing.T) {
|
||||
|
||||
func TestAuthSuccessSwitchesToStreamerViewAndStartsResolution(t *testing.T) {
|
||||
runtime := &fakeModelRuntime{}
|
||||
state := &auth.State{Login: "viewer", UserID: "7"}
|
||||
state := &auth.State{Login: "viewer"}
|
||||
updated, cmd := New(Options{Streamers: []string{"alpha"}, runtime: runtime}).Update(AuthUpdate{State: state, Done: true})
|
||||
if cmd == nil {
|
||||
t.Fatal("expected streamer resolution command")
|
||||
@@ -87,7 +89,7 @@ func TestInitStartsAuthOrResolution(t *testing.T) {
|
||||
t.Fatal("expected auth runtime to start")
|
||||
}
|
||||
|
||||
state := &auth.State{Login: "viewer", UserID: "7"}
|
||||
state := &auth.State{Login: "viewer"}
|
||||
runtime = &fakeModelRuntime{}
|
||||
if _, ok := New(Options{Streamers: []string{"alpha"}, AuthState: state, runtime: runtime}).Init()().(streamerStartedMsg); !ok {
|
||||
t.Fatal("authenticated Init() did not start streamer resolution")
|
||||
@@ -177,13 +179,11 @@ func TestInfoTabShowsWatchStreakMinerStatus(t *testing.T) {
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
Login: "alpha_live",
|
||||
Status: &MinerStatus{
|
||||
Watching: true,
|
||||
Reason: "watchstreak",
|
||||
WatchedMinutes: 4,
|
||||
},
|
||||
updated, _ := model.Update(StreamerUpdate{MinerStatus: &miner.StatusEntry{
|
||||
Login: "alpha_live",
|
||||
Watching: true,
|
||||
Reason: miner.WatchReasonStreak,
|
||||
WatchedMinutes: 4,
|
||||
}})
|
||||
next := updated.(Model)
|
||||
entry, ok := next.selectedEntry()
|
||||
@@ -206,14 +206,12 @@ func TestInfoTabShowsPointsMinerStatus(t *testing.T) {
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
Login: "alpha_live",
|
||||
Status: &MinerStatus{
|
||||
Watching: true,
|
||||
Reason: "points",
|
||||
WatchedMinutes: 6,
|
||||
WatchStreak: &minerStreak,
|
||||
},
|
||||
updated, _ := model.Update(StreamerUpdate{MinerStatus: &miner.StatusEntry{
|
||||
Login: "alpha_live",
|
||||
Watching: true,
|
||||
Reason: miner.WatchReasonPoints,
|
||||
WatchedMinutes: 6,
|
||||
WatchStreak: &minerStreak,
|
||||
}})
|
||||
next := updated.(Model)
|
||||
entry, ok := next.selectedEntry()
|
||||
@@ -233,12 +231,10 @@ func TestInfoTabOmitsMinerStatusWhenInactiveOrUnwatched(t *testing.T) {
|
||||
Status: twitch.StreamerReady,
|
||||
WatchStreak: &streak,
|
||||
})
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
Login: "alpha_live",
|
||||
Status: &MinerStatus{
|
||||
Watching: true,
|
||||
Reason: "points",
|
||||
},
|
||||
updated, _ := model.Update(StreamerUpdate{MinerStatus: &miner.StatusEntry{
|
||||
Login: "alpha_live",
|
||||
Watching: true,
|
||||
Reason: miner.WatchReasonPoints,
|
||||
}})
|
||||
next := updated.(Model)
|
||||
entry, ok := next.selectedEntry()
|
||||
@@ -427,16 +423,16 @@ func TestIRCUpdatesShowJoinedStatusAndFormattedMessages(t *testing.T) {
|
||||
Login: "alpha_live",
|
||||
Live: true,
|
||||
Status: twitch.StreamerReady,
|
||||
}).Update(StreamerUpdate{IRC: &IRCUpdate{Login: "alpha_live", State: IRCJoined}})
|
||||
}).Update(StreamerUpdate{IRC: &irc.Event{Streamer: "alpha_live", State: irc.StateJoined}})
|
||||
next := updated.(Model)
|
||||
|
||||
if !next.ircDetails["alpha_live"].joined {
|
||||
t.Fatal("expected joined detail")
|
||||
}
|
||||
|
||||
updated, _ = next.Update(StreamerUpdate{IRC: &IRCUpdate{
|
||||
Login: "alpha_live",
|
||||
Line: ":someone!someone@someone.tmi.twitch.tv PRIVMSG #alpha_live :hello there",
|
||||
updated, _ = next.Update(StreamerUpdate{IRC: &irc.Event{
|
||||
Streamer: "alpha_live",
|
||||
Line: ":someone!someone@someone.tmi.twitch.tv PRIVMSG #alpha_live :hello there",
|
||||
}})
|
||||
next = updated.(Model)
|
||||
next.focus = focusIRC
|
||||
@@ -482,11 +478,11 @@ func TestIRCUpdatesKeepOnlyLast50ChatMessages(t *testing.T) {
|
||||
Status: twitch.StreamerReady,
|
||||
})
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &IRCUpdate{Login: "alpha_live", State: IRCJoined}})
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &irc.Event{Streamer: "alpha_live", State: irc.StateJoined}})
|
||||
next := updated.(Model)
|
||||
for i := 1; i <= maxIRCMessageHistory+5; i++ {
|
||||
line := fmt.Sprintf(":user%d!user%d@user%d.tmi.twitch.tv PRIVMSG #alpha_live :message %d", i, i, i, i)
|
||||
updated, _ = next.Update(StreamerUpdate{IRC: &IRCUpdate{Login: "alpha_live", Line: line}})
|
||||
updated, _ = next.Update(StreamerUpdate{IRC: &irc.Event{Streamer: "alpha_live", Line: line}})
|
||||
next = updated.(Model)
|
||||
}
|
||||
|
||||
@@ -512,7 +508,7 @@ func TestMinerUpdatesRenderAndKeepOnlyLast50Messages(t *testing.T) {
|
||||
|
||||
next := model
|
||||
for i := 1; i <= maxIRCMessageHistory+5; i++ {
|
||||
updated, _ := next.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
updated, _ := next.Update(StreamerUpdate{MinerLog: &miner.LogEntry{
|
||||
Login: "alpha_live",
|
||||
Line: fmt.Sprintf("miner message %d", i),
|
||||
}})
|
||||
@@ -541,7 +537,7 @@ func TestMinerTabShowsHistoryForOfflineStreamer(t *testing.T) {
|
||||
Status: twitch.StreamerReady,
|
||||
})
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
updated, _ := model.Update(StreamerUpdate{MinerLog: &miner.LogEntry{
|
||||
Login: "alpha_live",
|
||||
Line: "pubsub stream down",
|
||||
}})
|
||||
@@ -560,9 +556,9 @@ func TestIRCUpdatesIgnoreNonChatProtocolLines(t *testing.T) {
|
||||
Status: twitch.StreamerReady,
|
||||
})
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &IRCUpdate{
|
||||
Login: "alpha_live",
|
||||
Line: "Joined #alpha_live as viewer",
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &irc.Event{
|
||||
Streamer: "alpha_live",
|
||||
Line: "Joined #alpha_live as viewer",
|
||||
}})
|
||||
next := updated.(Model)
|
||||
if len(next.ircDetails["alpha_live"].messages) != 0 {
|
||||
@@ -588,9 +584,9 @@ func TestIRCViewportAutoScrollsAtBottom(t *testing.T) {
|
||||
t.Fatal("expected viewport to start at bottom")
|
||||
}
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &IRCUpdate{
|
||||
Login: "alpha_live",
|
||||
Line: ":late!late@late.tmi.twitch.tv PRIVMSG #alpha_live :newest",
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &irc.Event{
|
||||
Streamer: "alpha_live",
|
||||
Line: ":late!late@late.tmi.twitch.tv PRIVMSG #alpha_live :newest",
|
||||
}})
|
||||
next := updated.(Model)
|
||||
if !next.ircViewport.AtBottom() {
|
||||
@@ -613,9 +609,9 @@ func TestIRCViewportPreservesManualScrollPosition(t *testing.T) {
|
||||
model.syncIRCViewport(true)
|
||||
model.ircViewport.GotoTop()
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &IRCUpdate{
|
||||
Login: "alpha_live",
|
||||
Line: ":late!late@late.tmi.twitch.tv PRIVMSG #alpha_live :newest",
|
||||
updated, _ := model.Update(StreamerUpdate{IRC: &irc.Event{
|
||||
Streamer: "alpha_live",
|
||||
Line: ":late!late@late.tmi.twitch.tv PRIVMSG #alpha_live :newest",
|
||||
}})
|
||||
next := updated.(Model)
|
||||
if next.ircViewport.YOffset != 0 {
|
||||
@@ -641,7 +637,7 @@ func TestMinerViewportAutoScrollsAtBottom(t *testing.T) {
|
||||
t.Fatal("expected miner viewport to start at bottom")
|
||||
}
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
updated, _ := model.Update(StreamerUpdate{MinerLog: &miner.LogEntry{
|
||||
Login: "alpha_live",
|
||||
Line: "newest miner event",
|
||||
}})
|
||||
@@ -663,7 +659,7 @@ func TestMinerViewportPreservesManualScrollPosition(t *testing.T) {
|
||||
model.syncMinerViewport(true)
|
||||
model.minerViewport.GotoTop()
|
||||
|
||||
updated, _ := model.Update(StreamerUpdate{Miner: &MinerUpdate{
|
||||
updated, _ := model.Update(StreamerUpdate{MinerLog: &miner.LogEntry{
|
||||
Login: "alpha_live",
|
||||
Line: "newest miner event",
|
||||
}})
|
||||
|
||||
+43
-148
@@ -4,9 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -25,23 +23,16 @@ const streamerRefreshInterval = 5 * time.Minute
|
||||
type Options struct {
|
||||
Streamers []string
|
||||
AuthState *auth.State
|
||||
AuthPath string
|
||||
Input io.Reader
|
||||
Output io.Writer
|
||||
|
||||
runtime modelRuntime
|
||||
initialStreamers []twitch.StreamerEntry
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
type runtime struct {
|
||||
ctx context.Context
|
||||
logins []string
|
||||
httpClient *http.Client
|
||||
authClient *auth.Client
|
||||
authPath string
|
||||
refreshEvery time.Duration
|
||||
sleep func(context.Context, time.Duration) error
|
||||
ctx context.Context
|
||||
logins []string
|
||||
httpClient *http.Client
|
||||
authClient *auth.Client
|
||||
}
|
||||
|
||||
// Run starts the Bubble Tea UI and keeps application orchestration inside the TUI package.
|
||||
@@ -49,11 +40,17 @@ func Run(ctx context.Context, options Options) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
rt := newRuntime(ctx, options)
|
||||
httpClient := &http.Client{Timeout: 30 * time.Second}
|
||||
rt := &runtime{
|
||||
ctx: ctx,
|
||||
logins: append([]string(nil), options.Streamers...),
|
||||
httpClient: httpClient,
|
||||
authClient: auth.NewClient(httpClient),
|
||||
}
|
||||
state := options.AuthState
|
||||
if state == nil {
|
||||
var err error
|
||||
state, err = rt.reuseAuth()
|
||||
state, err = rt.authClient.ReuseAuth(rt.ctx, auth.DefaultPath())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -62,54 +59,19 @@ func Run(ctx context.Context, options Options) error {
|
||||
options.AuthState = state
|
||||
options.runtime = rt
|
||||
|
||||
input := options.Input
|
||||
if input == nil {
|
||||
input = os.Stdin
|
||||
}
|
||||
output := options.Output
|
||||
if output == nil {
|
||||
output = os.Stdout
|
||||
}
|
||||
|
||||
program := tea.NewProgram(
|
||||
New(options),
|
||||
tea.WithContext(ctx),
|
||||
tea.WithInput(input),
|
||||
tea.WithOutput(output),
|
||||
)
|
||||
_, err := program.Run()
|
||||
return err
|
||||
}
|
||||
|
||||
func newRuntime(ctx context.Context, options Options) *runtime {
|
||||
httpClient := options.httpClient
|
||||
if httpClient == nil {
|
||||
httpClient = &http.Client{Timeout: 30 * time.Second}
|
||||
}
|
||||
authPath := options.AuthPath
|
||||
if authPath == "" {
|
||||
authPath = auth.DefaultPath()
|
||||
}
|
||||
return &runtime{
|
||||
ctx: ctx,
|
||||
logins: append([]string(nil), options.Streamers...),
|
||||
httpClient: httpClient,
|
||||
authClient: auth.NewClient(httpClient),
|
||||
authPath: authPath,
|
||||
refreshEvery: streamerRefreshInterval,
|
||||
sleep: sleepContext,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *runtime) reuseAuth() (*auth.State, error) {
|
||||
return r.authClient.ReuseAuth(r.ctx, r.authPath)
|
||||
}
|
||||
|
||||
func (r *runtime) startAuth(ch chan<- AuthUpdate) {
|
||||
go func() {
|
||||
defer close(ch)
|
||||
|
||||
state, err := r.authClient.EnsureAuth(r.ctx, r.authPath, func(line string) {
|
||||
state, err := r.authClient.EnsureAuth(r.ctx, auth.DefaultPath(), func(line string) {
|
||||
ch <- AuthUpdate{Line: strings.TrimRight(line, "\n")}
|
||||
})
|
||||
if err != nil {
|
||||
@@ -128,20 +90,40 @@ func (r *runtime) startAuth(ch chan<- AuthUpdate) {
|
||||
func (r *runtime) startResolve(state *auth.State, ch chan<- StreamerUpdate) {
|
||||
go func() {
|
||||
defer close(ch)
|
||||
ircManager := newIRCManager(ch)
|
||||
ircManager := &irc.Manager{Events: func(event irc.Event) {
|
||||
ch <- StreamerUpdate{IRC: &event}
|
||||
}}
|
||||
defer ircManager.Close()
|
||||
|
||||
service, err := newTwitchService(r.httpClient, state)
|
||||
if err != nil {
|
||||
ch <- StreamerUpdate{Err: err, Done: true}
|
||||
client := &gql.Client{
|
||||
HTTPClient: r.httpClient,
|
||||
Session: gql.Session{
|
||||
AccessToken: state.AccessToken,
|
||||
ClientID: auth.ClientID,
|
||||
DeviceID: state.DeviceID,
|
||||
UserAgent: auth.TVUserAgent,
|
||||
},
|
||||
}
|
||||
if err := client.Validate(); err != nil {
|
||||
ch <- StreamerUpdate{Err: fmt.Errorf("configure graphql session: %w", err), Done: true}
|
||||
return
|
||||
}
|
||||
minerManager := newMinerManager(r.ctx, service, ch)
|
||||
service := &twitch.Service{
|
||||
GQL: client,
|
||||
HTTPClient: r.httpClient,
|
||||
Session: client.Session,
|
||||
}
|
||||
minerManager := miner.NewManager(r.ctx, service, nil, func(entry miner.LogEntry) {
|
||||
ch <- StreamerUpdate{MinerLog: &entry}
|
||||
})
|
||||
minerManager.SetStatusSink(func(entry miner.StatusEntry) {
|
||||
ch <- StreamerUpdate{MinerStatus: &entry}
|
||||
})
|
||||
defer minerManager.Close()
|
||||
|
||||
if err := resolveStreamerEntriesWithSleep(r.ctx, service, state, r.logins, ircManager, minerManager, func(update StreamerUpdate) {
|
||||
ch <- update
|
||||
}, r.refreshEvery, r.sleep); err != nil {
|
||||
}, streamerRefreshInterval, sleepContext); err != nil {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
@@ -158,95 +140,16 @@ type streamerService interface {
|
||||
StreamInfo(context.Context, string) (*twitch.StreamInfo, error)
|
||||
WatchStreak(context.Context, string) (*int, error)
|
||||
PlaybackAccessToken(context.Context, string) (*twitch.PlaybackToken, error)
|
||||
LoadChannelPointsContext(context.Context, string) (*twitch.ChannelPointsContext, error)
|
||||
ClaimCommunityPoints(context.Context, string, string) error
|
||||
StreamMetadata(context.Context, string) (*twitch.StreamMetadata, error)
|
||||
FetchSpadeURL(context.Context, string) (string, error)
|
||||
TouchPlayback(context.Context, string, *twitch.PlaybackToken) error
|
||||
SendMinuteWatched(context.Context, string, twitch.MinuteWatchedPayload) error
|
||||
}
|
||||
|
||||
type ircSyncer interface {
|
||||
Sync(context.Context, string, string, []irc.Target)
|
||||
Sync(context.Context, string, string, []string)
|
||||
}
|
||||
|
||||
type minerSyncer interface {
|
||||
Sync(context.Context, *auth.State, *twitch.Viewer, []twitch.StreamerEntry)
|
||||
}
|
||||
|
||||
func newTwitchService(httpClient *http.Client, state *auth.State) (*twitch.Service, error) {
|
||||
client := &gql.Client{
|
||||
HTTPClient: httpClient,
|
||||
Session: gql.Session{
|
||||
AccessToken: state.AccessToken,
|
||||
ClientID: state.ClientID,
|
||||
DeviceID: state.DeviceID,
|
||||
UserAgent: auth.TVUserAgent(),
|
||||
},
|
||||
}
|
||||
if err := client.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("configure graphql session: %w", err)
|
||||
}
|
||||
return &twitch.Service{
|
||||
GQL: client,
|
||||
HTTPClient: httpClient,
|
||||
Session: client.Session,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newIRCManager(ch chan<- StreamerUpdate) *irc.Manager {
|
||||
return &irc.Manager{
|
||||
Addr: irc.DefaultAddr,
|
||||
Events: func(event irc.Event) {
|
||||
ch <- StreamerUpdate{
|
||||
IRC: &IRCUpdate{
|
||||
Login: event.Streamer,
|
||||
State: IRCState(event.State),
|
||||
Line: event.Line,
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newMinerManager(ctx context.Context, service miner.Service, ch chan<- StreamerUpdate) *miner.Manager {
|
||||
manager := miner.NewManager(ctx, service, nil, newMinerLogSink(ch))
|
||||
manager.SetStatusSink(newMinerStatusSink(ch))
|
||||
return manager
|
||||
}
|
||||
|
||||
func newMinerLogSink(ch chan<- StreamerUpdate) func(miner.LogEntry) {
|
||||
return func(entry miner.LogEntry) {
|
||||
ch <- StreamerUpdate{
|
||||
Miner: &MinerUpdate{
|
||||
Login: entry.Login,
|
||||
Line: entry.Line,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newMinerStatusSink(ch chan<- StreamerUpdate) func(miner.StatusEntry) {
|
||||
return func(entry miner.StatusEntry) {
|
||||
ch <- StreamerUpdate{
|
||||
Miner: &MinerUpdate{
|
||||
Login: entry.Login,
|
||||
Status: &MinerStatus{
|
||||
Watching: entry.Watching,
|
||||
Reason: string(entry.Reason),
|
||||
WatchedMinutes: entry.WatchedMinutes,
|
||||
WatchStreakMinutes: entry.WatchStreakMinutes,
|
||||
WatchStreak: cloneInt(entry.WatchStreak),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resolveStreamerEntries(ctx context.Context, service streamerService, state *auth.State, logins []string, ircSyncer ircSyncer, minerSyncer minerSyncer, send func(StreamerUpdate)) error {
|
||||
return resolveStreamerEntriesWithSleep(ctx, service, state, logins, ircSyncer, minerSyncer, send, streamerRefreshInterval, sleepContext)
|
||||
}
|
||||
|
||||
func resolveStreamerEntriesWithSleep(ctx context.Context, service streamerService, state *auth.State, logins []string, ircSyncer ircSyncer, minerSyncer minerSyncer, send func(StreamerUpdate), interval time.Duration, sleep func(context.Context, time.Duration) error) error {
|
||||
viewer, err := service.CurrentUser(ctx)
|
||||
if err != nil {
|
||||
@@ -348,24 +251,16 @@ func syncRuntimeState(ctx context.Context, ircSyncer ircSyncer, minerSyncer mine
|
||||
}
|
||||
}
|
||||
|
||||
func watchedIRCTargets(entries []twitch.StreamerEntry) []irc.Target {
|
||||
targets := make([]irc.Target, 0, 2)
|
||||
func watchedIRCTargets(entries []twitch.StreamerEntry) []string {
|
||||
targets := make([]string, 0, 2)
|
||||
for _, entry := range entries {
|
||||
if entry.Status != twitch.StreamerReady || !entry.Live || entry.Login == "" {
|
||||
continue
|
||||
}
|
||||
targets = append(targets, irc.Target{Login: entry.Login})
|
||||
targets = append(targets, entry.Login)
|
||||
if len(targets) == 2 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return targets
|
||||
}
|
||||
|
||||
func cloneInt(value *int) *int {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copy := *value
|
||||
return ©
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"time"
|
||||
|
||||
"parasocial/internal/auth"
|
||||
"parasocial/internal/irc"
|
||||
"parasocial/internal/miner"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
@@ -73,40 +71,12 @@ func (f *fakeStreamerService) PlaybackAccessToken(_ context.Context, login strin
|
||||
return &twitch.PlaybackToken{Signature: "sig", Value: "token"}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) LoadChannelPointsContext(context.Context, string) (*twitch.ChannelPointsContext, error) {
|
||||
return &twitch.ChannelPointsContext{Balance: 0}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) ClaimCommunityPoints(context.Context, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) StreamMetadata(context.Context, string) (*twitch.StreamMetadata, error) {
|
||||
return &twitch.StreamMetadata{BroadcastID: "broadcast"}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) FetchSpadeURL(context.Context, string) (string, error) {
|
||||
return "https://spade.test", nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) TouchPlayback(context.Context, string, *twitch.PlaybackToken) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStreamerService) SendMinuteWatched(context.Context, string, twitch.MinuteWatchedPayload) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeIRCSyncer struct {
|
||||
calls [][]string
|
||||
}
|
||||
|
||||
func (f *fakeIRCSyncer) Sync(_ context.Context, _, _ string, targets []irc.Target) {
|
||||
logins := make([]string, 0, len(targets))
|
||||
for _, target := range targets {
|
||||
logins = append(logins, target.Login)
|
||||
}
|
||||
f.calls = append(f.calls, logins)
|
||||
func (f *fakeIRCSyncer) Sync(_ context.Context, _, _ string, targets []string) {
|
||||
f.calls = append(f.calls, append([]string{}, targets...))
|
||||
}
|
||||
|
||||
type fakeMinerSyncer struct {
|
||||
@@ -249,23 +219,6 @@ func TestResolveStreamerEntriesRefreshUpdatesWatchedChannels(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewMinerLogSinkBridgesMinerEntriesIntoStreamerUpdates(t *testing.T) {
|
||||
ch := make(chan StreamerUpdate, 1)
|
||||
|
||||
newMinerLogSink(ch)(miner.LogEntry{
|
||||
Login: "alpha_live",
|
||||
Line: "pubsub points earned: balance=42",
|
||||
})
|
||||
|
||||
update := <-ch
|
||||
if update.Miner == nil {
|
||||
t.Fatal("expected miner update")
|
||||
}
|
||||
if update.Miner.Login != "alpha_live" || update.Miner.Line != "pubsub points earned: balance=42" {
|
||||
t.Fatalf("miner update = %#v", update.Miner)
|
||||
}
|
||||
}
|
||||
|
||||
type serviceOption func(*fakeStreamerService)
|
||||
|
||||
type watchStreakResult struct {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
@@ -55,21 +54,3 @@ var (
|
||||
BorderForeground(accentColor).
|
||||
Foreground(titleColor)
|
||||
)
|
||||
|
||||
func newAuthViewport(width, height int) viewport.Model {
|
||||
vp := viewport.New(width, height)
|
||||
vp.Style = lipgloss.NewStyle()
|
||||
return vp
|
||||
}
|
||||
|
||||
func newIRCViewport(width, height int) viewport.Model {
|
||||
vp := viewport.New(width, height)
|
||||
vp.Style = lipgloss.NewStyle()
|
||||
return vp
|
||||
}
|
||||
|
||||
func newMinerViewport(width, height int) viewport.Model {
|
||||
vp := viewport.New(width, height)
|
||||
vp.Style = lipgloss.NewStyle()
|
||||
return vp
|
||||
}
|
||||
|
||||
+7
-41
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
|
||||
"parasocial/internal/miner"
|
||||
"parasocial/internal/twitch"
|
||||
)
|
||||
|
||||
@@ -39,10 +40,10 @@ func (m Model) renderStreamerView() string {
|
||||
header := m.renderDashboardHeader()
|
||||
leftStyle := panelStyle
|
||||
rightStyle := panelStyle
|
||||
if m.isStreamersFocused() {
|
||||
if m.focus == focusStreamers {
|
||||
leftStyle = focusedPanelStyle
|
||||
}
|
||||
if m.isRightPanelFocused() {
|
||||
if m.focus != focusStreamers {
|
||||
rightStyle = focusedPanelStyle
|
||||
}
|
||||
|
||||
@@ -91,9 +92,9 @@ func (m Model) renderDetailPanel() string {
|
||||
var body string
|
||||
switch m.visibleDetailTab() {
|
||||
case ircTab:
|
||||
body = m.renderIRCTab()
|
||||
body = m.ircViewport.View()
|
||||
case minerTab:
|
||||
body = m.renderMinerTab()
|
||||
body = m.minerViewport.View()
|
||||
default:
|
||||
body = m.renderInfoTab(entry)
|
||||
}
|
||||
@@ -143,14 +144,6 @@ func (m Model) renderInfoTab(entry twitch.StreamerEntry) string {
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (m Model) renderIRCTab() string {
|
||||
return m.ircViewport.View()
|
||||
}
|
||||
|
||||
func (m Model) renderMinerTab() string {
|
||||
return m.minerViewport.View()
|
||||
}
|
||||
|
||||
func (m Model) renderStreamerRows(maxRows int) string {
|
||||
entries := m.visibleStreamers(maxRows)
|
||||
if len(entries) == 0 {
|
||||
@@ -248,9 +241,9 @@ func (m Model) minerWatchingLines(entry twitch.StreamerEntry) []string {
|
||||
}
|
||||
var reason string
|
||||
switch status.Reason {
|
||||
case string(minerWatchReasonStreak):
|
||||
case miner.WatchReasonStreak:
|
||||
reason = "Watching for watchstreak"
|
||||
case string(minerWatchReasonPoints):
|
||||
case miner.WatchReasonPoints:
|
||||
reason = "Watching for points"
|
||||
default:
|
||||
return nil
|
||||
@@ -261,33 +254,6 @@ func (m Model) minerWatchingLines(entry twitch.StreamerEntry) []string {
|
||||
}
|
||||
}
|
||||
|
||||
type minerWatchReason string
|
||||
|
||||
const (
|
||||
minerWatchReasonStreak minerWatchReason = "watchstreak"
|
||||
minerWatchReasonPoints minerWatchReason = "points"
|
||||
)
|
||||
|
||||
func rawStatus(entry twitch.StreamerEntry) string {
|
||||
switch {
|
||||
case entry.Status == twitch.StreamerError:
|
||||
return "error"
|
||||
case entry.Status == twitch.StreamerLoading:
|
||||
return "loading"
|
||||
case entry.Live:
|
||||
return "live"
|
||||
default:
|
||||
return "offline"
|
||||
}
|
||||
}
|
||||
|
||||
func ircSummary(detail ircDetail) string {
|
||||
if detail.joined {
|
||||
return "irc joined"
|
||||
}
|
||||
return "irc idle"
|
||||
}
|
||||
|
||||
func contentWidth(width int) int {
|
||||
if width <= 0 {
|
||||
return defaultViewWidth
|
||||
|
||||
Reference in New Issue
Block a user