385 lines
8.8 KiB
Go
385 lines
8.8 KiB
Go
package miner
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"parasocial/internal/auth"
|
|
"parasocial/internal/twitch"
|
|
)
|
|
|
|
const defaultWatchInterval = 20 * time.Second
|
|
|
|
// Service is the Twitch capability surface the miner needs.
|
|
type Service interface {
|
|
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)
|
|
PlaybackAccessToken(context.Context, string) (*twitch.PlaybackToken, error)
|
|
TouchPlayback(context.Context, string, *twitch.PlaybackToken) error
|
|
SendMinuteWatched(context.Context, string, twitch.MinuteWatchedPayload) error
|
|
}
|
|
|
|
// PubSubSyncer reconciles the live PubSub subscriptions the miner needs.
|
|
type PubSubSyncer interface {
|
|
Sync(context.Context, string, string, []string) error
|
|
Close() error
|
|
}
|
|
|
|
// Manager owns background channel points mining state for the current authenticated user.
|
|
type Manager struct {
|
|
ctx context.Context
|
|
service Service
|
|
pubsub PubSubSyncer
|
|
watchInterval time.Duration
|
|
sleep func(context.Context, time.Duration) error
|
|
|
|
mu sync.Mutex
|
|
auth *auth.State
|
|
viewer *twitch.Viewer
|
|
order []string
|
|
entries map[string]*streamerState
|
|
|
|
watchStarted bool
|
|
watchCancel context.CancelFunc
|
|
}
|
|
|
|
type streamerState struct {
|
|
ConfigLogin string
|
|
Login string
|
|
ChannelID string
|
|
Live bool
|
|
ChannelPoints int
|
|
SpadeURL string
|
|
Metadata *twitch.StreamMetadata
|
|
LastWatchAt time.Time
|
|
|
|
seeded bool
|
|
seeding bool
|
|
refreshing bool
|
|
}
|
|
|
|
// NewManager constructs one background miner manager.
|
|
func NewManager(ctx context.Context, service Service, pubsub PubSubSyncer) *Manager {
|
|
manager := &Manager{
|
|
ctx: ctx,
|
|
service: service,
|
|
pubsub: pubsub,
|
|
watchInterval: defaultWatchInterval,
|
|
sleep: sleepContext,
|
|
entries: make(map[string]*streamerState),
|
|
}
|
|
if manager.pubsub == nil {
|
|
manager.pubsub = NewPubSubClient(ctx, manager.handlePubSubEvent)
|
|
}
|
|
return manager
|
|
}
|
|
|
|
// Sync reconciles the miner against the current resolved streamer snapshot.
|
|
func (m *Manager) Sync(ctx context.Context, state *auth.State, viewer *twitch.Viewer, entries []twitch.StreamerEntry) {
|
|
if state == nil || viewer == nil {
|
|
return
|
|
}
|
|
|
|
type seedTarget struct {
|
|
configLogin string
|
|
}
|
|
|
|
var (
|
|
channelIDs []string
|
|
seedList []seedTarget
|
|
)
|
|
|
|
m.mu.Lock()
|
|
m.auth = state
|
|
m.viewer = viewer
|
|
m.order = m.order[:0]
|
|
|
|
nextEntries := make(map[string]*streamerState, len(entries))
|
|
for _, entry := range entries {
|
|
m.order = append(m.order, entry.ConfigLogin)
|
|
if entry.Status != twitch.StreamerReady || entry.ChannelID == "" || entry.Login == "" {
|
|
continue
|
|
}
|
|
|
|
channelIDs = append(channelIDs, entry.ChannelID)
|
|
current, ok := m.entries[entry.ConfigLogin]
|
|
if !ok || current.ChannelID != entry.ChannelID || current.Login != entry.Login {
|
|
current = &streamerState{
|
|
ConfigLogin: entry.ConfigLogin,
|
|
Login: entry.Login,
|
|
ChannelID: entry.ChannelID,
|
|
}
|
|
seedList = append(seedList, seedTarget{configLogin: entry.ConfigLogin})
|
|
}
|
|
|
|
current.ConfigLogin = entry.ConfigLogin
|
|
current.Login = entry.Login
|
|
current.ChannelID = entry.ChannelID
|
|
current.Live = entry.Live
|
|
nextEntries[entry.ConfigLogin] = current
|
|
}
|
|
|
|
m.entries = nextEntries
|
|
m.startWatchLoopLocked()
|
|
m.mu.Unlock()
|
|
|
|
_ = m.pubsub.Sync(ctx, viewer.ID, state.AccessToken, channelIDs)
|
|
for _, target := range seedList {
|
|
m.scheduleSeed(target.configLogin)
|
|
}
|
|
}
|
|
|
|
// Close stops background work and closes the PubSub connection.
|
|
func (m *Manager) Close() {
|
|
m.mu.Lock()
|
|
cancel := m.watchCancel
|
|
m.watchCancel = nil
|
|
m.mu.Unlock()
|
|
|
|
if cancel != nil {
|
|
cancel()
|
|
}
|
|
if m.pubsub != nil {
|
|
_ = m.pubsub.Close()
|
|
}
|
|
}
|
|
|
|
func (m *Manager) startWatchLoopLocked() {
|
|
if m.watchStarted {
|
|
return
|
|
}
|
|
ctx, cancel := context.WithCancel(m.ctx)
|
|
m.watchStarted = true
|
|
m.watchCancel = cancel
|
|
go m.watchLoop(ctx)
|
|
}
|
|
|
|
func (m *Manager) scheduleSeed(configLogin string) {
|
|
m.mu.Lock()
|
|
state, ok := m.entries[configLogin]
|
|
if !ok || state.seeding {
|
|
m.mu.Unlock()
|
|
return
|
|
}
|
|
state.seeding = true
|
|
login := state.Login
|
|
channelID := state.ChannelID
|
|
live := state.Live
|
|
m.mu.Unlock()
|
|
|
|
go func() {
|
|
defer m.finishSeeding(configLogin)
|
|
|
|
channelPoints, err := m.service.LoadChannelPointsContext(m.ctx, login)
|
|
if err == nil {
|
|
m.mu.Lock()
|
|
if current, ok := m.entries[configLogin]; ok && current.ChannelID == channelID {
|
|
current.ChannelPoints = channelPoints.Balance
|
|
current.seeded = true
|
|
}
|
|
m.mu.Unlock()
|
|
if channelPoints.ClaimID != "" {
|
|
_ = m.service.ClaimCommunityPoints(m.ctx, channelID, channelPoints.ClaimID)
|
|
}
|
|
}
|
|
|
|
if live {
|
|
m.scheduleRefresh(configLogin)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (m *Manager) finishSeeding(configLogin string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if state, ok := m.entries[configLogin]; ok {
|
|
state.seeding = false
|
|
}
|
|
}
|
|
|
|
func (m *Manager) scheduleRefresh(configLogin string) {
|
|
m.mu.Lock()
|
|
state, ok := m.entries[configLogin]
|
|
if !ok || state.refreshing {
|
|
m.mu.Unlock()
|
|
return
|
|
}
|
|
state.refreshing = true
|
|
login := state.Login
|
|
channelID := state.ChannelID
|
|
m.mu.Unlock()
|
|
|
|
go func() {
|
|
defer m.finishRefresh(configLogin)
|
|
|
|
spadeURL, err := m.service.FetchSpadeURL(m.ctx, login)
|
|
if err != nil {
|
|
return
|
|
}
|
|
metadata, err := m.service.StreamMetadata(m.ctx, login)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if current, ok := m.entries[configLogin]; ok && current.ChannelID == channelID {
|
|
current.SpadeURL = spadeURL
|
|
current.Metadata = metadata
|
|
current.Live = true
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (m *Manager) finishRefresh(configLogin string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if state, ok := m.entries[configLogin]; ok {
|
|
state.refreshing = false
|
|
}
|
|
}
|
|
|
|
func (m *Manager) watchLoop(ctx context.Context) {
|
|
for {
|
|
if err := m.watchOnce(ctx); err != nil {
|
|
if err == context.Canceled {
|
|
return
|
|
}
|
|
}
|
|
if err := m.sleep(ctx, m.watchInterval); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *Manager) watchOnce(ctx context.Context) error {
|
|
type candidate struct {
|
|
configLogin string
|
|
login string
|
|
channelID string
|
|
spadeURL string
|
|
metadata *twitch.StreamMetadata
|
|
}
|
|
|
|
m.mu.Lock()
|
|
viewer := m.viewer
|
|
candidates := make([]candidate, 0, 2)
|
|
for _, configLogin := range m.order {
|
|
state, ok := m.entries[configLogin]
|
|
if !ok || !state.Live || state.Login == "" || state.ChannelID == "" {
|
|
continue
|
|
}
|
|
candidates = append(candidates, candidate{
|
|
configLogin: configLogin,
|
|
login: state.Login,
|
|
channelID: state.ChannelID,
|
|
spadeURL: state.SpadeURL,
|
|
metadata: state.Metadata,
|
|
})
|
|
if len(candidates) == 2 {
|
|
break
|
|
}
|
|
}
|
|
m.mu.Unlock()
|
|
|
|
if viewer == nil || len(candidates) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, candidate := range candidates {
|
|
if candidate.spadeURL == "" || candidate.metadata == nil {
|
|
m.scheduleRefresh(candidate.configLogin)
|
|
continue
|
|
}
|
|
|
|
token, err := m.service.PlaybackAccessToken(ctx, candidate.login)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if err := m.service.TouchPlayback(ctx, candidate.login, token); err != nil {
|
|
continue
|
|
}
|
|
|
|
payload := twitch.BuildMinuteWatchedPayload(viewer.ID, candidate.channelID, candidate.login, candidate.metadata)
|
|
if err := m.service.SendMinuteWatched(ctx, candidate.spadeURL, payload); err != nil {
|
|
continue
|
|
}
|
|
|
|
m.mu.Lock()
|
|
if state, ok := m.entries[candidate.configLogin]; ok && state.ChannelID == candidate.channelID {
|
|
state.LastWatchAt = time.Now()
|
|
}
|
|
m.mu.Unlock()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Manager) handlePubSubEvent(event Event) {
|
|
configLogin, state := m.streamerForChannel(event.ChannelID)
|
|
if state == nil {
|
|
return
|
|
}
|
|
|
|
switch event.MessageType {
|
|
case "points-earned", "points-spent":
|
|
m.mu.Lock()
|
|
if current, ok := m.entries[configLogin]; ok {
|
|
current.ChannelPoints = event.Balance
|
|
}
|
|
m.mu.Unlock()
|
|
case "claim-available":
|
|
if event.ClaimID != "" {
|
|
_ = m.service.ClaimCommunityPoints(m.ctx, state.ChannelID, event.ClaimID)
|
|
}
|
|
case "stream-up":
|
|
m.mu.Lock()
|
|
if current, ok := m.entries[configLogin]; ok {
|
|
current.Live = true
|
|
}
|
|
m.mu.Unlock()
|
|
m.scheduleRefresh(configLogin)
|
|
case "stream-down":
|
|
m.mu.Lock()
|
|
if current, ok := m.entries[configLogin]; ok {
|
|
current.Live = false
|
|
}
|
|
m.mu.Unlock()
|
|
case "viewcount":
|
|
if !state.Live {
|
|
m.mu.Lock()
|
|
if current, ok := m.entries[configLogin]; ok {
|
|
current.Live = true
|
|
}
|
|
m.mu.Unlock()
|
|
m.scheduleRefresh(configLogin)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *Manager) streamerForChannel(channelID string) (string, *streamerState) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for configLogin, state := range m.entries {
|
|
if state.ChannelID == channelID {
|
|
copy := *state
|
|
return configLogin, ©
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
func sleepContext(ctx context.Context, duration time.Duration) error {
|
|
timer := time.NewTimer(duration)
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
return nil
|
|
}
|
|
}
|