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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
mod auth;
pub(crate) mod claim_token;
mod devices;
mod privacy;
mod resources;
mod users;
mod webhooks;

use crate::{http::base_headers, HasPlexHeaders, PlexApiError};
use chrono::DateTime;
use chrono::Utc;
use reqwest::header::HeaderMap;
use serde_repr::Deserialize_repr;
use std::collections::HashMap;

#[derive(Deserialize, Debug)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
#[serde(rename_all = "camelCase")]
struct SubscriptionSummary {
    active: bool,
    #[serde(default)]
    subscribed_at: Option<DateTime<Utc>>,
    status: String,
    payment_service: String,
    plan: String,
    features: Vec<String>,
}

#[derive(Deserialize_repr, Debug)]
#[repr(u8)]
enum AutoSelectSubtitleMode {
    ManuallySelected = 0,
    ShownWithForeignAudio = 1,
    AlwaysEnabled = 2,
}

#[derive(Deserialize, Debug)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
#[serde(rename_all = "camelCase")]
struct Profile {
    auto_select_audio: bool,
    auto_select_subtitle: AutoSelectSubtitleMode,
    #[serde(deserialize_with = "serde_aux::prelude::deserialize_bool_from_anything")]
    default_subtitle_accessibility: bool,
    #[serde(deserialize_with = "serde_aux::prelude::deserialize_bool_from_anything")]
    default_subtitle_forced: bool,
    default_audio_language: String,
    default_subtitle_language: String,
}

#[derive(Deserialize, Debug)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
#[serde(rename_all = "camelCase")]
struct Subscription {
    id: Option<i32>,
    mode: String,
    state: String,
    renews_at: Option<DateTime<Utc>>,
    ends_at: Option<DateTime<Utc>>,
    #[serde(rename = "type")]
    subscription_type: Option<String>,
    transfer: bool,
}

#[derive(Deserialize, Debug)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
struct Service {
    identifier: String,
    endpoint: String,
    token: Option<String>,
    secret: Option<String>,
    status: String,
}

#[derive(Deserialize, Debug)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
#[serde(rename_all = "camelCase")]
pub struct MyPlexAccount {
    id: i32,
    uuid: String,
    username: String,
    title: String,
    email: String,
    thumb: String,
    locale: Option<String>,
    email_only_auth: bool,
    has_password: bool,
    cloud_sync_device: Option<String>,
    auth_token: String,
    mailing_list_status: String,
    mailing_list_active: bool,
    scrobble_types: String,
    pin: String,
    subscription: SubscriptionSummary,
    subscription_description: String,
    restricted: bool,
    home: bool,
    guest: bool,
    queue_email: String,
    queue_uid: HashMap<String, String>,
    home_size: i32,
    max_home_size: i32,
    certificate_version: i32,
    #[serde(with = "chrono::serde::ts_seconds")]
    remember_expires_at: DateTime<Utc>,
    profile: Profile,
    entitlements: Vec<String>,
    roles: Vec<String>,
    subscriptions: Vec<Subscription>,
    services: Vec<Service>,
    protected: bool,
    country: String,
    home_admin: bool,
    trials: Vec<String>,
    ads_consent: bool,
    #[serde(with = "chrono::serde::ts_seconds")]
    ads_consent_set_at: DateTime<Utc>,
    #[serde(with = "chrono::serde::ts_seconds")]
    ads_consent_reminder_at: DateTime<Utc>,
}

#[derive(Deserialize, Debug, Clone)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
struct MyPlexApiError {
    code: i32,
    message: String,
}

#[derive(Deserialize, Debug, Clone)]
#[cfg_attr(all(test, feature = "test_new_attributes"), serde(deny_unknown_fields))]
struct MyPlexApiErrorResponse {
    errors: Vec<MyPlexApiError>,
}

impl From<MyPlexApiErrorResponse> for PlexApiError {
    fn from(r: MyPlexApiErrorResponse) -> Self {
        let mut errors = vec![];
        for err in r.errors {
            errors.push(PlexApiError::MyPlexApiError {
                code: err.code,
                message: err.message,
            })
        }

        PlexApiError::MyPlexErrorResponse { errors }
    }
}

pub trait HasMyPlexToken {
    fn get_auth_token(&self) -> &str;
    fn set_auth_token(&mut self, auth_token: &str);
}

impl<T> HasPlexHeaders for T
where
    T: HasMyPlexToken,
{
    fn headers(&self) -> HeaderMap {
        let mut headers = base_headers();

        if !self.get_auth_token().is_empty() {
            headers.insert("X-Plex-Token", self.get_auth_token().parse().unwrap());
        }

        headers.insert("Accept", "application/json".parse().unwrap());

        headers
    }
}

impl HasMyPlexToken for MyPlexAccount {
    /// Returns authentication token for current account.
    fn get_auth_token(&self) -> &str {
        &self.auth_token
    }

    /// Sets authentication token for current account.
    fn set_auth_token(&mut self, auth_token: &str) {
        self.auth_token = String::from(auth_token);
    }
}

impl MyPlexAccount {
    /// Return username which was used to log in to MyPlex.
    pub fn get_username(&self) -> String {
        self.username.clone()
    }
}

impl crate::HasBaseUrl for MyPlexAccount {
    fn get_base_url(&self) -> &str {
        "https://plex.tv/"
    }
}