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