feat(scrobbler): add per-user scrobble filter (#5964)

* feat(scrobbler): add scrobble_filter column to user

* feat(scrobbler): validate scrobble filter criteria on user save

* refactor(persistence): make smart playlist join helpers package-level

* feat(scrobbler): add MediaFileRepository.MatchesCriteria

* feat(scrobbler): filter external scrobbles with per-user criteria

* feat(ui): add scrobble filter field to user form

* fix(scrobbler): default scrobble_filter to empty string for existing users

* refactor(scrobbler): also gate playback reports on the scrobble filter

Playback reports carry the same track metadata to plugin scrobblers, so a
filtered track leaked through that third dispatch path. Skip the filter
evaluation entirely when no scrobbler is active.

* refactor(persistence): move criteria join building into criteria_sql.go

The join set a criteria needs was decided in criteria_sql.go but built in
smart_playlist_repository.go, so both callers had to pair the two by hand.

* refactor(persistence): unexport smartPlaylistCriteria methods

The type never leaves the package, so the exported names advertised an API
that callers outside persistence could never reach. Also disambiguates
where/orderBy from squirrel's SelectBuilder methods of the same name.

* fix(ui): cap the scrobble filter field width

fullWidth stretched it across the whole page next to 256px inputs. Bounded
at 40em, with two rows and a resize handle so JSON rules stay readable.

* refactor(ui): move scrobble filter input in UserEdit component

* feat(ui): add pt-BR translations for the scrobble filter

* fix(scrobbler): take the filter verdict before incPlay

incPlay mutates play counts and dates a filter can test on, so evaluating at
dispatch time let one play decide differently on either side of the increment:
a track could be scrobbled despite matching, or lose only its stopped report
and strand presence plugins. Reject limit/offset too, rather than silently
ignoring part of a rule copied from a smart playlist.

* fix(scrobbler): filter the report from an expired session

The expiry callback runs with a stub user carrying no filter, so evaluating
there always returned false and leaked the track to plugin scrobblers. That is
the normal path for clients that never send stopped, such as legacy Subsonic
now-playing. Carry the last verdict on the session instead.

* refactor(scrobbler): skip the now-playing enqueue instead of threading the verdict

Queuing an entry only to drop it at dispatch also cancelled a pending
announcement for the previous, unfiltered track, since the queue is keyed by
player and a new entry replaces the old one.

* fix(scrobbler): evaluate the filter regardless of active scrobblers

The verdict is stored on the session and dispatched at expiry, so skipping
evaluation when no scrobbler was active let a plugin enabled mid-session
receive a filtered track. The empty-filter guard above already gives servers
without scrobbling the same free path, so the shortcut only ever applied to
users who had a filter set.
This commit is contained in:
Deluan Quintão 2026-08-15 16:10:53 -04:00 • committed by GitHub
commit 82fde00ecc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 590 additions and 112 deletions

View file

@ -155,11 +155,13 @@
"currentPassword": "Current Password",
"newPassword": "New Password",
"token": "Token",
"libraries": "Libraries"
"libraries": "Libraries",
"scrobbleFilter": "Scrobble filter"
},
"helperTexts": {
"name": "Changes to your name will only be reflected on next login",
"libraries": "Select specific libraries for this user, or leave empty to use default libraries"
"libraries": "Select specific libraries for this user, or leave empty to use default libraries",
"scrobbleFilter": "Songs matching these smart playlist rules are not sent to Last.fm, ListenBrainz or scrobbler plugins. Uses the same JSON syntax and behavior as smart playlists. Example: {\"all\":[{\"lt\":{\"rating\":4}}]}. Leave empty to scrobble everything. Local play counts are not affected."
},
"notifications": {
"created": "User created",
@ -167,7 +169,8 @@
"deleted": "User deleted"
},
"validation": {
"librariesRequired": "At least one library must be selected for non-admin users"
"librariesRequired": "At least one library must be selected for non-admin users",
"invalidScrobbleFilter": "Must be valid smart playlist rules. Limit, offset and refresh delay are not supported."
},
"message": {
"listenBrainzToken": "Enter your ListenBrainz user token.",

View file

@ -172,6 +172,17 @@ const UserEdit = (props) => {
</FormDataConsumer>
)}
<TextInput
source="scrobbleFilter"
multiline
minRows={2}
fullWidth
spellCheck={false}
style={{ maxWidth: '40em' }}
inputProps={{ style: { resize: 'vertical' } }}
helperText={translate('resources.user.helperTexts.scrobbleFilter')}
/>
<DateField variant="body1" source="lastLoginAt" showTime />
<DateField variant="body1" source="lastAccessAt" showTime />
<DateField variant="body1" source="updatedAt" showTime />

View file

@ -127,6 +127,12 @@ describe('<UserEdit />', () => {
expect(screen.getByTestId('date-field-createdAt')).toBeInTheDocument()
})
it('should render the scrobble filter input', () => {
render(<UserEdit id="user1" permissions="admin" />)
expect(screen.getByTestId('text-input-scrobbleFilter')).toBeInTheDocument()
})
it('should not render username input for non-admin users', () => {
render(<UserEdit id="user1" permissions="user" />)

View file

@ -15,5 +15,15 @@ export const validateUserForm = (values, translate) => {
}
}
if (values.scrobbleFilter && values.scrobbleFilter.trim() !== '') {
try {
JSON.parse(values.scrobbleFilter)
} catch {
errors.scrobbleFilter = translate(
'resources.user.validation.invalidScrobbleFilter',
)
}
}
return errors
}

View file

@ -67,4 +67,27 @@ describe('User Validation Utilities', () => {
)
})
})
describe('scrobbleFilter validation', () => {
it('accepts an empty filter', () => {
const errors = validateUserForm({ isAdmin: true }, mockTranslate)
expect(errors.scrobbleFilter).toBeUndefined()
})
it('accepts valid JSON', () => {
const errors = validateUserForm(
{ isAdmin: true, scrobbleFilter: '{"all":[{"lt":{"rating":4}}]}' },
mockTranslate,
)
expect(errors.scrobbleFilter).toBeUndefined()
})
it('rejects malformed JSON', () => {
const errors = validateUserForm(
{ isAdmin: true, scrobbleFilter: '{broken' },
mockTranslate,
)
expect(errors.scrobbleFilter).toEqual(
'resources.user.validation.invalidScrobbleFilter',
)
})
})
})