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