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