Skip to main content

twapi_v2/api/
get_2_users_id_mentions.rs

1use crate::fields::{
2    media_fields::MediaFields, place_fields::PlaceFields, poll_fields::PollFields,
3    tweet_fields::TweetFields, user_fields::UserFields,
4};
5use crate::responses::{errors::Errors, includes::Includes, meta::Meta, tweets::Tweets};
6use crate::{
7    api::{Authentication, TwapiOptions, apply_options, execute_twitter, make_url},
8    error::Error,
9    headers::Headers,
10};
11use chrono::prelude::*;
12use itertools::Itertools;
13use reqwest::RequestBuilder;
14use serde::{Deserialize, Serialize};
15use std::collections::HashSet;
16
17const URL: &str = "/2/users/:id/mentions";
18
19#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
20pub enum Expansions {
21    #[serde(rename = "article.cover_media")]
22    #[default]
23    ArticleCoverMedia,
24    #[serde(rename = "article.media_entities")]
25    ArticleMediaEntities,
26    #[serde(rename = "attachments.media_keys")]
27    AttachmentsMediaKeys,
28    #[serde(rename = "attachments.media_source_tweet")]
29    AttachmentsMediaSourceTweet,
30    #[serde(rename = "attachments.poll_ids")]
31    AttachmentsPollIds,
32    #[serde(rename = "author_id")]
33    AuthorId,
34    #[serde(rename = "edit_history_tweet_ids")]
35    EditHistoryTweetIds,
36    #[serde(rename = "entities.mentions.username")]
37    EntitiesMentionsUsername,
38    #[serde(rename = "geo.place_id")]
39    GeoPlaceId,
40    #[serde(rename = "in_reply_to_user_id")]
41    InReplyToUserId,
42    #[serde(rename = "entities.note.mentions.username")]
43    EntitiesNoteMentionsUsername,
44    #[serde(rename = "referenced_tweets.id")]
45    ReferencedTweetsId,
46    #[serde(rename = "referenced_tweets.id.author_id")]
47    ReferencedTweetsIdAuthorId,
48}
49
50impl Expansions {
51    pub fn all() -> HashSet<Self> {
52        let mut result = HashSet::new();
53        result.insert(Self::ArticleCoverMedia);
54        result.insert(Self::ArticleMediaEntities);
55        result.insert(Self::AttachmentsMediaKeys);
56        result.insert(Self::AttachmentsMediaSourceTweet);
57        result.insert(Self::AttachmentsPollIds);
58        result.insert(Self::AuthorId);
59        result.insert(Self::EditHistoryTweetIds);
60        result.insert(Self::EntitiesMentionsUsername);
61        result.insert(Self::GeoPlaceId);
62        result.insert(Self::InReplyToUserId);
63        result.insert(Self::EntitiesNoteMentionsUsername);
64        result.insert(Self::ReferencedTweetsId);
65        result.insert(Self::ReferencedTweetsIdAuthorId);
66        result
67    }
68}
69
70impl std::fmt::Display for Expansions {
71    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72        match self {
73            Self::ArticleCoverMedia => write!(f, "article.cover_media"),
74            Self::ArticleMediaEntities => write!(f, "article.media_entities"),
75            Self::AttachmentsMediaKeys => write!(f, "attachments.media_keys"),
76            Self::AttachmentsMediaSourceTweet => write!(f, "attachments.media_source_tweet"),
77            Self::AttachmentsPollIds => write!(f, "attachments.poll_ids"),
78            Self::AuthorId => write!(f, "author_id"),
79            Self::EditHistoryTweetIds => write!(f, "edit_history_tweet_ids"),
80            Self::EntitiesMentionsUsername => write!(f, "entities.mentions.username"),
81            Self::GeoPlaceId => write!(f, "geo.place_id"),
82            Self::InReplyToUserId => write!(f, "in_reply_to_user_id"),
83            Self::EntitiesNoteMentionsUsername => write!(f, "entities.note.mentions.username"),
84            Self::ReferencedTweetsId => write!(f, "referenced_tweets.id"),
85            Self::ReferencedTweetsIdAuthorId => write!(f, "referenced_tweets.id.author_id"),
86        }
87    }
88}
89
90#[derive(Debug, Clone, Default)]
91pub struct Api {
92    id: String,
93    end_time: Option<DateTime<Utc>>,
94    expansions: Option<HashSet<Expansions>>,
95    max_results: Option<usize>,
96    media_fields: Option<HashSet<MediaFields>>,
97    pagination_token: Option<String>,
98    place_fields: Option<HashSet<PlaceFields>>,
99    poll_fields: Option<HashSet<PollFields>>,
100    since_id: Option<String>,
101    start_time: Option<DateTime<Utc>>,
102    tweet_fields: Option<HashSet<TweetFields>>,
103    until_id: Option<String>,
104    user_fields: Option<HashSet<UserFields>>,
105    twapi_options: Option<TwapiOptions>,
106}
107
108impl Api {
109    pub fn new(id: &str) -> Self {
110        Self {
111            id: id.to_owned(),
112            ..Default::default()
113        }
114    }
115
116    pub fn all(id: &str) -> Self {
117        Self {
118            id: id.to_owned(),
119            expansions: Some(Expansions::all()),
120            media_fields: Some(MediaFields::all()),
121            place_fields: Some(PlaceFields::all()),
122            poll_fields: Some(PollFields::all()),
123            tweet_fields: Some(TweetFields::organic()),
124            user_fields: Some(UserFields::all()),
125            max_results: Some(100),
126            ..Default::default()
127        }
128    }
129
130    pub fn open(id: &str) -> Self {
131        Self {
132            id: id.to_owned(),
133            expansions: Some(Expansions::all()),
134            media_fields: Some(MediaFields::open()),
135            place_fields: Some(PlaceFields::all()),
136            poll_fields: Some(PollFields::all()),
137            tweet_fields: Some(TweetFields::open()),
138            user_fields: Some(UserFields::all()),
139            max_results: Some(100),
140            ..Default::default()
141        }
142    }
143
144    pub fn end_time(mut self, value: DateTime<Utc>) -> Self {
145        self.end_time = Some(value);
146        self
147    }
148
149    pub fn expansions(mut self, value: HashSet<Expansions>) -> Self {
150        self.expansions = Some(value);
151        self
152    }
153
154    pub fn max_results(mut self, value: usize) -> Self {
155        self.max_results = Some(value);
156        self
157    }
158
159    pub fn media_fields(mut self, value: HashSet<MediaFields>) -> Self {
160        self.media_fields = Some(value);
161        self
162    }
163
164    pub fn pagination_token(mut self, value: &str) -> Self {
165        self.pagination_token = Some(value.to_owned());
166        self
167    }
168
169    pub fn place_fields(mut self, value: HashSet<PlaceFields>) -> Self {
170        self.place_fields = Some(value);
171        self
172    }
173
174    pub fn poll_fields(mut self, value: HashSet<PollFields>) -> Self {
175        self.poll_fields = Some(value);
176        self
177    }
178
179    pub fn since_id(mut self, value: &str) -> Self {
180        self.since_id = Some(value.to_owned());
181        self
182    }
183
184    pub fn start_time(mut self, value: DateTime<Utc>) -> Self {
185        self.start_time = Some(value);
186        self
187    }
188
189    pub fn tweet_fields(mut self, value: HashSet<TweetFields>) -> Self {
190        self.tweet_fields = Some(value);
191        self
192    }
193
194    pub fn until_id(mut self, value: &str) -> Self {
195        self.until_id = Some(value.to_owned());
196        self
197    }
198
199    pub fn user_fields(mut self, value: HashSet<UserFields>) -> Self {
200        self.user_fields = Some(value);
201        self
202    }
203
204    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
205        self.twapi_options = Some(value);
206        self
207    }
208
209    pub fn build(self, authentication: &impl Authentication) -> RequestBuilder {
210        let mut query_parameters = vec![];
211        if let Some(end_time) = self.end_time {
212            query_parameters.push((
213                "end_time",
214                end_time.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
215            ));
216        }
217        if let Some(expansions) = self.expansions {
218            query_parameters.push(("expansions", expansions.iter().join(",")));
219        }
220        if let Some(max_results) = self.max_results {
221            query_parameters.push(("max_results", max_results.to_string()));
222        }
223        if let Some(media_fields) = self.media_fields {
224            query_parameters.push(("media.fields", media_fields.iter().join(",")));
225        }
226        if let Some(pagination_token) = self.pagination_token {
227            query_parameters.push(("pagination_token", pagination_token));
228        }
229        if let Some(place_fields) = self.place_fields {
230            query_parameters.push(("place.fields", place_fields.iter().join(",")));
231        }
232        if let Some(poll_fields) = self.poll_fields {
233            query_parameters.push(("poll.fields", poll_fields.iter().join(",")));
234        }
235        if let Some(since_id) = self.since_id {
236            query_parameters.push(("since_id", since_id));
237        }
238        if let Some(start_time) = self.start_time {
239            query_parameters.push((
240                "start_time",
241                start_time.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
242            ));
243        }
244        if let Some(tweet_fields) = self.tweet_fields {
245            query_parameters.push(("tweet.fields", tweet_fields.iter().join(",")));
246        }
247        if let Some(until_id) = self.until_id {
248            query_parameters.push(("until_id", until_id));
249        }
250        if let Some(user_fields) = self.user_fields {
251            query_parameters.push(("user.fields", user_fields.iter().join(",")));
252        }
253        let client = reqwest::Client::new();
254        let url = make_url(&self.twapi_options, &URL.replace(":id", &self.id));
255        let builder = client.get(&url).query(&query_parameters);
256        authentication.execute(
257            apply_options(builder, &self.twapi_options),
258            "GET",
259            &url,
260            &query_parameters
261                .iter()
262                .map(|it| (it.0, it.1.as_str()))
263                .collect::<Vec<_>>(),
264        )
265    }
266
267    pub async fn execute(
268        self,
269        authentication: &impl Authentication,
270    ) -> Result<(Response, Headers), Error> {
271        execute_twitter(self.build(authentication)).await
272    }
273}
274
275#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
276pub struct Response {
277    #[serde(skip_serializing_if = "Option::is_none")]
278    pub data: Option<Vec<Tweets>>,
279    #[serde(skip_serializing_if = "Option::is_none")]
280    pub errors: Option<Vec<Errors>>,
281    #[serde(skip_serializing_if = "Option::is_none")]
282    pub includes: Option<Includes>,
283    #[serde(skip_serializing_if = "Option::is_none")]
284    pub meta: Option<Meta>,
285    #[serde(flatten)]
286    pub extra: std::collections::HashMap<String, serde_json::Value>,
287}
288
289impl Response {
290    pub fn is_empty_extra(&self) -> bool {
291        let res = self.extra.is_empty()
292            && self
293                .data
294                .as_ref()
295                .map(|it| it.iter().all(|item| item.is_empty_extra()))
296                .unwrap_or(true)
297            && self
298                .errors
299                .as_ref()
300                .map(|it| it.iter().all(|item| item.is_empty_extra()))
301                .unwrap_or(true)
302            && self
303                .includes
304                .as_ref()
305                .map(|it| it.is_empty_extra())
306                .unwrap_or(true)
307            && self
308                .meta
309                .as_ref()
310                .map(|it| it.is_empty_extra())
311                .unwrap_or(true);
312        if !res {
313            println!("Response {:?}", self.extra);
314        }
315        res
316    }
317}