1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::{
    my_plex::{MyPlexAccount, MyPlexApiErrorResponse},
    InternalHttpApi,
};
use reqwest::StatusCode;
use std::collections::HashMap;

#[derive(Debug, Deserialize)]
#[cfg_attr(test, serde(deny_unknown_fields))]
#[serde(rename_all = "camelCase")]
pub struct Privacy {
    opt_out_playback: bool,
    opt_out_library_stats: bool,
    domain: String,
    base_url: String,
    metrics: Vec<Metric>,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(test, serde(deny_unknown_fields))]
struct Metric {
    event: String,
    status: String,
}

const PRIVACY_URL: &str = "api/v2/user/privacy";

impl MyPlexAccount {
    /// Returns current privacy settings, see [Privacy Preferences on plex.tv](https://www.plex.tv/about/privacy-legal/privacy-preferences/#opd).
    pub fn get_privacy(&self) -> crate::Result<Privacy> {
        let mut response = self.get(PRIVACY_URL)?;
        if response.status() == StatusCode::OK {
            let p: Privacy = response.json()?;
            Ok(p)
        } else {
            let err: MyPlexApiErrorResponse = response.json()?;
            Err(crate::error::PlexApiError::from(err))
        }
    }

    /// Changes privacy settings, see [Privacy Preferences on plex.tv](https://www.plex.tv/about/privacy-legal/privacy-preferences/#opd).
    pub fn set_privacy(
        &self,
        opt_out_playback: bool,
        opt_out_library_stats: bool,
    ) -> crate::Result<()> {
        let mut params = HashMap::new();
        params.insert("optOutPlayback", if opt_out_playback { "1" } else { "0" });
        params.insert(
            "optOutLibraryStats",
            if opt_out_library_stats { "1" } else { "0" },
        );
        self.put_form(PRIVACY_URL, &params)?;
        Ok(())
    }
}