mirror of
https://github.com/tronbyt/server.git
synced 2026-11-30 04:02:03 +01:00
Drop app-requested sounds while muted, even during the cooldown
This commit is contained in:
2 files changed
+31
-5
No files matched your search
@@ -44,6 +44,15 @@ func validSoundRequestID(id string) string {
|
||||
return id
|
||||
}
|
||||
|
||||
// rememberSoundRequest records id as the last render.Root(sound_id) handled
|
||||
// for app, so later renders returning the same ID don't play it.
|
||||
func (s *Server) rememberSoundRequest(ctx context.Context, app *data.App, id string) {
|
||||
app.LastSoundRequestID = id
|
||||
if _, err := gorm.G[data.App](s.DB).Where("id = ?", app.ID).Update(ctx, "last_sound_request_id", id); err != nil {
|
||||
slog.Debug("Failed to update last_sound_request_id in DB", "app", app.ID, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// RenderApp consolidates the logic for rendering an app for a device.
|
||||
// It handles config overrides, timezone/locale injection, dwell time, and filters.
|
||||
func (s *Server) RenderApp(ctx context.Context, device *data.Device, app *data.App, src *appSource, configOverrides map[string]any) ([]byte, []string, error) {
|
||||
@@ -358,6 +367,12 @@ func (s *Server) possiblyRender(ctx context.Context, app *data.App, device *data
|
||||
// returning the same ID on later renders without repeat sounds.
|
||||
soundRequestID = validSoundRequestID(renderResult.SoundID)
|
||||
shouldPlay = soundRequestID != "" && soundRequestID != app.LastSoundRequestID
|
||||
if shouldPlay && device.IsMuted() {
|
||||
// Muted: drop the request rather than hold it back (even
|
||||
// during the cooldown), so it can't play after unmuting.
|
||||
s.rememberSoundRequest(ctx, app, soundRequestID)
|
||||
shouldPlay = false
|
||||
}
|
||||
}
|
||||
|
||||
// Debounce: Enforce 30-second cooldown to prevent sound spam from animated apps
|
||||
@@ -376,10 +391,7 @@ func (s *Server) possiblyRender(ctx context.Context, app *data.App, device *data
|
||||
slog.Debug("Failed to update last_sound_at in DB", "app", app.ID, "error", err)
|
||||
}
|
||||
if soundRequestID != "" {
|
||||
app.LastSoundRequestID = soundRequestID
|
||||
if _, err := gorm.G[data.App](s.DB).Where("id = ?", app.ID).Update(ctx, "last_sound_request_id", soundRequestID); err != nil {
|
||||
slog.Debug("Failed to update last_sound_request_id in DB", "app", app.ID, "error", err)
|
||||
}
|
||||
s.rememberSoundRequest(ctx, app, soundRequestID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1139,10 +1139,24 @@ def main(config):
|
||||
assert.Equal(t, "match-1-score-2-0", app.LastSoundRequestID, "held-back ID must not be marked as played")
|
||||
assert.True(t, renderAgain("match-1-score-2-1"), "held-back sound_id should play after the cooldown")
|
||||
|
||||
// While muted, a new sound_id is dropped, not held back, even during the
|
||||
// cooldown, so it doesn't play after unmuting.
|
||||
dev.Muted = true
|
||||
_, err = gorm.G[data.Device](s.DB).Where("id = ?", deviceID).Update(ctx, "pending_sound", "")
|
||||
require.NoError(t, err)
|
||||
recent = time.Now().Add(-5 * time.Second)
|
||||
app.LastSoundAt = &recent
|
||||
app.LastRender = time.Time{}
|
||||
app.Config = data.JSONMap{"sid": "match-1-score-3-1"}
|
||||
require.True(t, s.possiblyRender(ctx, &app, &dev, &user))
|
||||
assert.Equal(t, "match-1-score-3-1", app.LastSoundRequestID, "muted request must be marked as handled")
|
||||
dev.Muted = false
|
||||
assert.False(t, renderAgain("match-1-score-3-1"), "request dropped while muted must not play after unmuting")
|
||||
|
||||
// Other triggers ignore sound_id.
|
||||
app.NotificationSoundTrigger = "on_change"
|
||||
app.LastSoundRequestID = ""
|
||||
renderAgain("match-1-score-3-1")
|
||||
renderAgain("match-1-score-4-1")
|
||||
assert.Empty(t, app.LastSoundRequestID, "on_change must not record sound_id")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user