|
| 1 | +use crate::date::WpGmtDateTime; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::collections::HashMap; |
| 4 | +use wp_serde_helper::{ |
| 5 | + deserialize_empty_string_as_none, deserialize_null_as_empty_vec, |
| 6 | + deserialize_string_vec_or_string, deserialize_u64_or_none, |
| 7 | + deserialize_u64_or_none_with_negative_as_none, deserialize_u64_or_none_with_zero_as_none, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug, Serialize, Deserialize, uniffi::Record)] |
| 11 | +pub struct WPComUserInfo { |
| 12 | + /// The user's WP.com ID. |
| 13 | + #[serde(rename = "ID")] |
| 14 | + pub id: u64, |
| 15 | + |
| 16 | + /// The user's display name as set in the `Public display name` field |
| 17 | + pub display_name: String, |
| 18 | + |
| 19 | + /// The user's username as set at account creation. This cannot be changed. |
| 20 | + pub username: String, |
| 21 | + |
| 22 | + /// The user's email address. |
| 23 | + pub email: String, |
| 24 | + |
| 25 | + /// The user's primary blog ID – this is the one that was created when they made their account. |
| 26 | + #[serde(rename = "primary_blog")] |
| 27 | + #[serde(deserialize_with = "deserialize_u64_or_none")] |
| 28 | + pub primary_blog_id: Option<u64>, |
| 29 | + |
| 30 | + /// The user's primary blog URL – this is the one that was created when they made their account. |
| 31 | + pub primary_blog_url: Option<String>, |
| 32 | + |
| 33 | + /// Whether the user's primary blog is a Jetpack blog. |
| 34 | + pub primary_blog_is_jetpack: bool, |
| 35 | + |
| 36 | + /// Whether the user has Jetpack partner access. |
| 37 | + pub has_jetpack_partner_access: bool, |
| 38 | + |
| 39 | + /// The partner types of the partner accounts this user has access to. |
| 40 | + #[serde(default)] |
| 41 | + pub jetpack_partner_types: Vec<String>, |
| 42 | + |
| 43 | + /// The user's preferred language. |
| 44 | + pub language: String, |
| 45 | + |
| 46 | + /// The variant of the user's preferred language. |
| 47 | + #[serde(deserialize_with = "deserialize_empty_string_as_none")] |
| 48 | + pub locale_variant: Option<String>, |
| 49 | + |
| 50 | + /// If the current access token is scoped to a specific Site ID, this field will be set to that Site ID. Otherwise, it will be null. |
| 51 | + #[serde(deserialize_with = "deserialize_u64_or_none_with_zero_as_none")] |
| 52 | + pub token_site_id: Option<u64>, |
| 53 | + |
| 54 | + /// The scopes of the current access token – see https://developer.wordpress.com/docs/api/oauth2/ for a list of possible values. |
| 55 | + #[serde(rename = "token_scope")] |
| 56 | + #[serde(deserialize_with = "deserialize_string_vec_or_string")] |
| 57 | + pub token_scopes: Vec<String>, |
| 58 | + |
| 59 | + /// If the current access token is scoped to a specific Client ID, this field will be set to that Client ID. Otherwise, it will be null. |
| 60 | + #[serde(deserialize_with = "deserialize_u64_or_none_with_negative_as_none")] |
| 61 | + pub token_client_id: Option<u64>, |
| 62 | + |
| 63 | + /// The user's avatar URL as set on WordPress.com or using Gravatar. |
| 64 | + pub avatar_url: Option<String>, |
| 65 | + |
| 66 | + /// The user's Gravatar profile URL. |
| 67 | + pub profile_url: Option<String>, |
| 68 | + |
| 69 | + /// Whether the user's email address has been verified via WordPress.com Connect. |
| 70 | + pub verified: bool, |
| 71 | + |
| 72 | + /// Whether the user's email address has been verified – their ability to perform many actions requires this to be true. |
| 73 | + pub email_verified: bool, |
| 74 | + |
| 75 | + /// The date of the user's account creation. |
| 76 | + #[serde(rename = "date")] |
| 77 | + pub creation_date: WpGmtDateTime, |
| 78 | + |
| 79 | + /// The number of sites the user has access to. |
| 80 | + pub site_count: u64, |
| 81 | + |
| 82 | + /// The number of sites the user has access to that are Jetpack sites. |
| 83 | + pub jetpack_site_count: u64, |
| 84 | + |
| 85 | + /// The number of sites the user has access to that are Atomic sites. |
| 86 | + pub atomic_site_count: u64, |
| 87 | + |
| 88 | + /// The number of sites the user has access to that are visible. |
| 89 | + pub visible_site_count: u64, |
| 90 | + |
| 91 | + /// The number of visible sites the user has access to that are Jetpack sites. |
| 92 | + pub jetpack_visible_site_count: u64, |
| 93 | + |
| 94 | + /// The number of visible sites the user has access to that are Atomic sites. |
| 95 | + pub atomic_visible_site_count: u64, |
| 96 | + |
| 97 | + /// Whether the user has unseen notifications. |
| 98 | + pub has_unseen_notes: bool, |
| 99 | + |
| 100 | + /// The type of the user's newest notification. |
| 101 | + pub newest_note_type: Option<String>, |
| 102 | + |
| 103 | + /// If this is a phone account then the user doesn't have a verified email address |
| 104 | + pub phone_account: bool, |
| 105 | + |
| 106 | + /// Is the user somewhere where Google Workspace can be purchased? |
| 107 | + pub is_valid_google_apps_country: bool, |
| 108 | + |
| 109 | + /// Country code for the user's IP address. |
| 110 | + pub user_ip_country_code: Option<String>, |
| 111 | + |
| 112 | + /// Active social login connections. |
| 113 | + #[serde(deserialize_with = "deserialize_null_as_empty_vec")] |
| 114 | + pub social_login_connections: Vec<WpComSocialLoginConnection>, |
| 115 | + |
| 116 | + /// The name of the social service this account is linked to. |
| 117 | + pub social_signup_service: Option<String>, |
| 118 | + |
| 119 | + /// User's assigned A/B test variations, where the key is the test name and the value is the variation |
| 120 | + pub abtests: HashMap<String, String>, |
| 121 | +} |
| 122 | + |
| 123 | +#[derive(Debug, Serialize, Deserialize, uniffi::Record)] |
| 124 | +pub struct WpComSocialLoginConnection { |
| 125 | + pub service: String, |
| 126 | + pub service_user_email: String, |
| 127 | + pub service_user_id: String, |
| 128 | +} |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +mod tests { |
| 132 | + use rstest::rstest; |
| 133 | + use std::io::Read; |
| 134 | + |
| 135 | + use super::*; |
| 136 | + |
| 137 | + #[rstest] |
| 138 | + #[case("v1.1-me-01.json", 742098)] |
| 139 | + #[case("v1.1-me-02.json", 158350866)] |
| 140 | + // IDs for this test aren't anonymized so that they can be checked later if needed. |
| 141 | + fn test_wpcom_user_info_deserialization( |
| 142 | + #[case] json_file_path: &str, |
| 143 | + #[case] expected_id: u64, |
| 144 | + ) { |
| 145 | + let json = test_json(json_file_path).expect("Failed to read JSON file"); |
| 146 | + let user_info: WPComUserInfo = |
| 147 | + serde_json::from_slice(json.as_slice()).expect("Failed to deserialize user info"); |
| 148 | + assert_eq!(user_info.id, expected_id); |
| 149 | + } |
| 150 | + |
| 151 | + fn test_json(input: &str) -> Result<Vec<u8>, std::io::Error> { |
| 152 | + let mut file_path = std::path::PathBuf::from(env!("CARGO_WORKSPACE_DIR")); |
| 153 | + file_path.push("wp_api"); |
| 154 | + file_path.push("tests"); |
| 155 | + file_path.push("wpcom"); |
| 156 | + file_path.push("me"); |
| 157 | + file_path.push(input); |
| 158 | + |
| 159 | + let mut f = std::fs::File::open(file_path)?; |
| 160 | + let mut buffer = Vec::new(); |
| 161 | + |
| 162 | + // read the whole file |
| 163 | + f.read_to_end(&mut buffer)?; |
| 164 | + |
| 165 | + Ok(buffer) |
| 166 | + } |
| 167 | +} |
0 commit comments