mastodon_async_entities/
account.rs

1//! A module containing everything relating to a account returned from the api.
2
3use serde::{
4    de::{self, Deserializer, Unexpected},
5    Deserialize, Serialize,
6};
7use std::{fmt::Display, path::PathBuf};
8use time::{serde::iso8601, OffsetDateTime};
9
10/// A struct representing an Account.
11#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
12pub struct Account {
13    /// Equals `username` for local users, includes `@domain` for remote ones.
14    pub acct: String,
15    /// URL to the avatar image
16    pub avatar: String,
17    /// URL to the avatar static image (gif)
18    pub avatar_static: String,
19    /// The time the account was created.
20    #[serde(with = "iso8601")]
21    pub created_at: OffsetDateTime,
22    /// The account's display name.
23    pub display_name: String,
24    /// The number of followers for the account.
25    pub followers_count: u64,
26    /// The number of accounts the given account is following.
27    pub following_count: u64,
28    /// URL to the header image.
29    pub header: String,
30    /// URL to the header static image (gif).
31    pub header_static: String,
32    /// The ID of the account.
33    pub id: AccountId,
34    /// Boolean for when the account cannot be followed without waiting for
35    /// approval first.
36    pub locked: bool,
37    /// Biography of user.
38    pub note: String,
39    /// The number of statuses the account has made.
40    pub statuses_count: u64,
41    /// URL of the user's profile page (can be remote).
42    pub url: String,
43    /// The username of the account.
44    pub username: String,
45    /// An extra attribute given from `verify_credentials` giving defaults about
46    /// a user
47    pub source: Option<Source>,
48    /// If the owner decided to switch accounts, new account is in
49    /// this attribute
50    pub moved: Option<Box<Account>>,
51    /// List of profile metadata fields
52    pub fields: Option<Vec<MetadataField>>,
53    /// Boolean indicating whether this account is a bot or not
54    pub bot: Option<bool>,
55}
56
57/// Wrapper type for a account ID string
58#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
59#[serde(transparent)]
60pub struct AccountId(String);
61
62impl AsRef<str> for AccountId {
63    fn as_ref(&self) -> &str {
64        &self.0
65    }
66}
67
68impl AccountId {
69    pub fn new(value: impl Into<String>) -> Self {
70        Self(value.into())
71    }
72}
73
74impl Display for AccountId {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        write!(f, "{}", self.0)
77    }
78}
79
80static_assertions::assert_not_impl_any!(
81    AccountId: PartialEq<crate::attachment::AttachmentId>,
82    PartialEq<crate::filter::FilterId>,
83    PartialEq<crate::list::ListId>,
84    PartialEq<crate::mention::MentionId>,
85    PartialEq<crate::notification::NotificationId>,
86    PartialEq<crate::relationship::RelationshipId>,
87    PartialEq<crate::push::SubscriptionId>,
88    PartialEq<crate::report::ReportId>,
89    PartialEq<crate::status::StatusId>,
90);
91
92/// A single name: value pair from a user's profile
93#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
94pub struct MetadataField {
95    /// name part of metadata
96    pub name: String,
97    /// value part of metadata
98    pub value: String,
99}
100
101impl MetadataField {
102    pub fn new(name: &str, value: &str) -> MetadataField {
103        MetadataField {
104            name: name.into(),
105            value: value.into(),
106        }
107    }
108}
109
110/// An extra object given from `verify_credentials` giving defaults about a user
111#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
112pub struct Source {
113    privacy: Option<crate::visibility::Visibility>,
114    #[serde(deserialize_with = "string_or_bool")]
115    sensitive: bool,
116    note: Option<String>,
117    fields: Option<Vec<MetadataField>>,
118}
119
120fn string_or_bool<'de, D: Deserializer<'de>>(val: D) -> ::std::result::Result<bool, D::Error> {
121    #[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
122    #[serde(untagged)]
123    pub enum BoolOrString {
124        Bool(bool),
125        Str(String),
126    }
127
128    Ok(match BoolOrString::deserialize(val)? {
129        BoolOrString::Bool(b) => b,
130        BoolOrString::Str(ref s) => {
131            if s == "true" {
132                true
133            } else if s == "false" {
134                false
135            } else {
136                return Err(de::Error::invalid_value(
137                    Unexpected::Str(s),
138                    &"true or false",
139                ));
140            }
141        }
142    })
143}
144
145#[derive(Debug, Default, Clone, Serialize, PartialEq, Eq)]
146pub struct UpdateSource {
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub privacy: Option<crate::visibility::Visibility>,
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub sensitive: Option<bool>,
151}
152
153#[derive(Debug, Default, Serialize, PartialEq, Eq)]
154pub struct Credentials {
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub display_name: Option<String>,
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub note: Option<String>,
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub avatar: Option<PathBuf>,
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub header: Option<PathBuf>,
163    #[serde(skip_serializing_if = "Option::is_none")]
164    pub source: Option<UpdateSource>,
165    #[serde(serialize_with = "fields_attributes_ser::ser")]
166    pub fields_attributes: Vec<MetadataField>,
167}
168
169mod fields_attributes_ser {
170    use super::*;
171    use serde::ser::{SerializeMap, Serializer};
172    pub fn ser<S>(attrs: &Vec<MetadataField>, serializer: S) -> Result<S::Ok, S::Error>
173    where
174        S: Serializer,
175    {
176        let mut map = serializer.serialize_map(Some(attrs.len()))?;
177        for (i, field) in attrs.iter().enumerate() {
178            map.serialize_entry(&i, &field)?;
179        }
180        map.end()
181    }
182}