2.5 KiB · text History 0463cca
//! Which players to scrobble. Port of scropple-android's `AppFilter`, keyed
//! on the MPRIS player name (`org.mpris.MediaPlayer2.<player>[.instance…]`)
//! rather than an Android package.
//!
//! MPRIS doesn't say whether a player is playing music or a video, so, as on
//! Android, it's by player: `deny` always wins; with `allow` set, only those.

use std::collections::BTreeSet;

/// Players never scrobbled unless configured otherwise: browsers (every
/// YouTube video would scrobble), KDE Connect and GSConnect (they relay the
/// phone's media, which Scropple on the phone already scrobbles), and
/// playerctld (a proxy that re-announces other players).
pub const DEFAULT_DENY: &[&str] = &[
    "firefox",
    "chromium",
    "chrome",
    "google-chrome",
    "brave",
    "vivaldi",
    "opera",
    "microsoft-edge",
    "librewolf",
    "plasma-browser-integration",
    "kdeconnect",
    "gsconnect",
    "playerctld",
];

#[derive(Debug, Clone)]
pub struct Filter {
    deny: BTreeSet<String>,
    allow: Option<BTreeSet<String>>,
}

impl Default for Filter {
    fn default() -> Self {
        Self::new(DEFAULT_DENY.iter().map(|s| s.to_string()).collect(), None)
    }
}

impl Filter {
    pub fn new(deny: Vec<String>, allow: Option<Vec<String>>) -> Self {
        let norm = |v: Vec<String>| {
            v.into_iter()
                .map(|s| s.to_lowercase())
                .collect::<BTreeSet<_>>()
        };
        Self {
            deny: norm(deny),
            allow: allow.map(norm),
        }
    }

    pub fn allows(&self, player: &str) -> bool {
        let player = player.to_lowercase();
        if self.deny.contains(&player) {
            return false;
        }
        self.allow
            .as_ref()
            .is_none_or(|allow| allow.contains(&player))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn browsers_and_relays_are_denied_by_default() {
        let f = Filter::default();
        for p in ["firefox", "chromium", "kdeconnect", "playerctld", "Firefox"] {
            assert!(!f.allows(p), "{p}");
        }
        for p in ["spotify", "strawberry", "mpv", "vlc"] {
            assert!(f.allows(p), "{p}");
        }
    }

    #[test]
    fn allow_list_mode_and_deny_wins() {
        let f = Filter::new(
            vec!["spotify".into()],
            Some(vec!["spotify".into(), "strawberry".into()]),
        );
        assert!(f.allows("strawberry"));
        assert!(!f.allows("spotify"));
        assert!(!f.allows("mpv"));
    }
}