Skip to content

Commit

Permalink
ircbot: make AnnounceUser only announce if the name is different
Browse files Browse the repository at this point in the history
AnnounceUser also won't announce nil values anymore until a timeout
period has been elapsed.

storage/test: add track retrieval test

util: make CallbackTimer.Stop guard against a nil timer
  • Loading branch information
Wessie committed May 26, 2024
1 parent 15e8e2e commit 6e909bc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
33 changes: 27 additions & 6 deletions ircbot/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/rpc"
"github.com/R-a-dio/valkyrie/util"
"github.com/lrstanley/girc"
"github.com/rs/zerolog"
"google.golang.org/grpc"
Expand All @@ -25,11 +26,16 @@ func NewGRPCServer(ctx context.Context, service radio.AnnounceService) (*grpc.Se
}

func NewAnnounceService(cfg config.Config, storage radio.StorageService, bot *Bot) radio.AnnounceService {
return &announceService{
ann := &announceService{
Config: cfg,
Storage: storage,
bot: bot,
}
ann.userTimer = util.NewCallbackTimer(func() {
message := Fmt("Current DJ: {red}None")
ann.bot.c.Cmd.Message(ann.Conf().IRC.MainChannel, message)
})
return ann
}

type announceService struct {
Expand All @@ -40,6 +46,10 @@ type announceService struct {
lastAnnounceSongTime time.Time
lastAnnounceSong radio.Song

userTimer *util.CallbackTimer
userMu sync.Mutex
userLast string

topicTimerMu sync.Mutex
topicTimer *time.Timer
topicLastEdit time.Time
Expand Down Expand Up @@ -237,20 +247,31 @@ func (ann *announceService) AnnounceRequest(ctx context.Context, song radio.Song
}

func (ann *announceService) AnnounceUser(ctx context.Context, user *radio.User) error {
name := "None"
if user.IsValid() {
name = user.DJ.Name
const userDelay = time.Second * 15

if !user.IsValid() {
// only announce no-dj after a period of it being no-dj
ann.userTimer.Start(userDelay)
return nil
}
ann.userTimer.Stop()

ann.userMu.Lock()
defer ann.userMu.Unlock()
if ann.userLast == user.DJ.Name {
return nil
}

message := Fmt("Current DJ: {green}%s", name)
ann.userLast = user.DJ.Name
message := Fmt("Current DJ: {green}%s", user.DJ.Name)
ann.bot.c.Cmd.Message(ann.Conf().IRC.MainChannel, message)

ann.queueChangeTopic(ctx, user)
return nil
}

func (ann *announceService) queueChangeTopic(ctx context.Context, user *radio.User) {
const topicDelay = time.Second * 15
const topicDelay = time.Second * 30

ann.topicTimerMu.Lock()
if ann.topicTimer != nil {
Expand Down
42 changes: 42 additions & 0 deletions storage/test/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ func (suite *Suite) TestSongCreateAndRetrieve(t *testing.T) {
}
}

func (suite *Suite) TestSongCreateAndRetrieveWithTrack(t *testing.T) {
ss := suite.Storage(t).Song(suite.ctx)
ts := suite.Storage(t).Track(suite.ctx)

song := radio.Song{
Metadata: "test-song-create-and-retrieve",
Length: time.Second * 300,
DatabaseTrack: &radio.DatabaseTrack{
Title: "testing title",
Artist: "testing artist",
Album: "testing album",
},
}
song.Hydrate()

tid, err := ts.Insert(song)
if assert.NoError(t, err) {
assert.NotZero(t, tid)
}
song.TrackID = tid

fromHash, err := ss.FromHash(song.Hash)
if assert.NoError(t, err) {
assert.NotNil(t, fromHash)
assert.True(t, song.EqualTo(*fromHash))
assert.Equal(t, song.Length, fromHash.Length)
assert.Equal(t, song.DatabaseTrack, fromHash.DatabaseTrack)
}

fromMetadata, err := ss.FromMetadata(song.Metadata)
if assert.NoError(t, err) {
assert.NotNil(t, fromMetadata)
assert.True(t, song.EqualTo(*fromMetadata))
assert.Equal(t, song.Length, fromMetadata.Length)
assert.Equal(t, song.DatabaseTrack, fromMetadata.DatabaseTrack)
}
}

func (suite *Suite) TestSongLastPlayed(t *testing.T) {
ss := suite.Storage(t).Song(suite.ctx)

Expand Down Expand Up @@ -194,4 +232,8 @@ func (suite *Suite) TestSongFavoritesOf(t *testing.T) {
require.NoError(t, err)
require.Len(t, faves, limit)
require.Equal(t, faveCountExpected, count)

db, err := ss.FavoritesOfDatabase(nick)
require.NoError(t, err)
require.Len(t, db, 0)
}
3 changes: 3 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ func (tc *CallbackTimer) Start(timeout time.Duration) {

// Stop stops the current timer if one exists
func (tc *CallbackTimer) Stop() bool {
if tc == nil {
return true
}
tc.mu.Lock()
defer tc.mu.Unlock()
if tc.timer != nil {
Expand Down

0 comments on commit 6e909bc

Please sign in to comment.