amagi 0.1.4

Rust SDK, CLI, and Web API service skeleton for multi-platform social web adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use std::borrow::Cow;

use reqwest::Url;
use serde_json::{Value, json};

use crate::error::AppError;

const TWITTER_GRAPHQL_BASE_URL: &str = "https://x.com/i/api/graphql";
const TWITTER_GUEST_ACTIVATE_URL: &str = "https://api.twitter.com/1.1/guest/activate.json";

const USER_BY_SCREEN_NAME_OPERATION_ID: &str = "IGgvgiOx4QZndDHuD3x9TQ";
const USER_BY_REST_ID_OPERATION_ID: &str = "VQfQ9wwYdk6j_u2O4vt64Q";
const USER_TWEETS_OPERATION_ID: &str = "FOlovQsiHGDls3c0Q_HaSQ";
const USER_TWEETS_AND_REPLIES_OPERATION_ID: &str = "EJTxTKSH-byy7X46AhtKeA";
const USER_MEDIA_OPERATION_ID: &str = "SjiAp7wyuCUBkKAJJObU8w";
const FOLLOWERS_OPERATION_ID: &str = "-FpGYzBsUxUOecYYfso0yA";
const FOLLOWING_OPERATION_ID: &str = "UCFedrkjMz7PeEAWCWhqFw";
const LIKES_OPERATION_ID: &str = "jTVU5QdKqziEc4GsLNvhMQ";
const BOOKMARKS_OPERATION_ID: &str = "-LGfdImKeQz0xS_jjUwzlA";
const HOME_LATEST_TIMELINE_OPERATION_ID: &str = "_qO7FJzShSKYWi9gtboE6A";
const HOME_TIMELINE_OPERATION_ID: &str = "V7xdnRnvW6a8vIsMr9xK7A";
const SEARCH_TIMELINE_OPERATION_ID: &str = "GcXk9vN_d1jUfHNqLacXQA";
const USER_SEARCH_TIMELINE_OPERATION_ID: &str = "M1jEez78PEfVfbQLvlWMvQ";
const TWEET_RESULT_BY_REST_ID_OPERATION_ID: &str = "aFvUsJm2c-oDkJV75blV6g";
const TWEET_DETAIL_OPERATION_ID: &str = "97JF30KziU00483E_8elBA";
const FAVORITERS_OPERATION_ID: &str = "b3OrdeHDQfb9zRMC0fV3bw";
const RETWEETERS_OPERATION_ID: &str = "wfglZEC0MRgBdxMa_1a5YQ";
const AUDIO_SPACE_BY_ID_OPERATION_ID: &str = "rR7CQrr8kxb6fatlUaB61Q";

/// Public Twitter/X API URL builder.
#[derive(Debug, Clone)]
#[doc(alias = "twitterApiUrls")]
pub struct TwitterApiUrls {
    graphql_base_url: Cow<'static, str>,
    guest_activate_url: Cow<'static, str>,
}

impl Default for TwitterApiUrls {
    fn default() -> Self {
        Self::new()
    }
}

impl TwitterApiUrls {
    /// Create a URL builder using the default X web endpoints.
    pub fn new() -> Self {
        Self {
            graphql_base_url: Cow::Borrowed(TWITTER_GRAPHQL_BASE_URL),
            guest_activate_url: Cow::Borrowed(TWITTER_GUEST_ACTIVATE_URL),
        }
    }

    /// Create a URL builder with explicit upstream overrides.
    pub fn with_base_urls(
        graphql_base_url: impl Into<Cow<'static, str>>,
        guest_activate_url: impl Into<Cow<'static, str>>,
    ) -> Self {
        Self {
            graphql_base_url: graphql_base_url.into(),
            guest_activate_url: guest_activate_url.into(),
        }
    }

    /// Return the guest-token activation URL.
    pub fn guest_activate(&self) -> &str {
        self.guest_activate_url.as_ref()
    }

    /// Build the user-profile URL for one screen name.
    #[doc(alias = "getUserProfile")]
    pub fn user_profile(&self, screen_name: &str) -> Result<String, AppError> {
        self.build_graphql_url(
            USER_BY_SCREEN_NAME_OPERATION_ID,
            "UserByScreenName",
            &json!({
                "screen_name": screen_name,
                "withGrokTranslatedBio": false,
            }),
            &current_user_profile_features(),
            Some(&current_user_profile_field_toggles()),
        )
    }

    /// Build the user-profile URL for one user rest id.
    #[doc(alias = "getUserProfileById")]
    pub fn user_profile_by_id(&self, user_id: &str) -> Result<String, AppError> {
        self.build_graphql_url(
            USER_BY_REST_ID_OPERATION_ID,
            "UserByRestId",
            &json!({
                "userId": user_id,
            }),
            &current_user_profile_features(),
            None,
        )
    }

    /// Build the user-timeline URL for one user id.
    #[doc(alias = "getUserTimeline")]
    pub fn user_timeline(
        &self,
        user_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            USER_TWEETS_OPERATION_ID,
            "UserTweets",
            &json!({
                "userId": user_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": true,
                "withQuickPromoteEligibilityTweetFields": true,
                "withVoice": true,
            }),
            &current_user_timeline_features(),
            Some(&current_timeline_field_toggles()),
        )
    }

    /// Build the user-replies URL for one user id.
    #[doc(alias = "getUserReplies")]
    pub fn user_replies(
        &self,
        user_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            USER_TWEETS_AND_REPLIES_OPERATION_ID,
            "UserTweetsAndReplies",
            &json!({
                "userId": user_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": true,
                "withCommunity": true,
                "withVoice": true,
            }),
            &current_user_timeline_features(),
            Some(&current_timeline_field_toggles()),
        )
    }

    /// Build the user-media URL for one user id.
    #[doc(alias = "getUserMedia")]
    pub fn user_media(
        &self,
        user_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        let upstream_count = normalize_user_media_count(count);
        self.build_graphql_url(
            USER_MEDIA_OPERATION_ID,
            "UserMedia",
            &json!({
                "userId": user_id,
                "count": upstream_count,
                "cursor": cursor,
                "includePromotedContent": false,
                "withClientEventToken": false,
                "withBirdwatchNotes": false,
                "withVoice": true,
            }),
            &current_user_timeline_features(),
            Some(&current_timeline_field_toggles()),
        )
    }

    /// Build the followers URL for one user id.
    #[doc(alias = "getUserFollowers")]
    pub fn user_followers(
        &self,
        user_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            FOLLOWERS_OPERATION_ID,
            "Followers",
            &json!({
                "userId": user_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": false,
                "withGrokTranslatedBio": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the following URL for one user id.
    #[doc(alias = "getUserFollowing")]
    pub fn user_following(
        &self,
        user_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            FOLLOWING_OPERATION_ID,
            "Following",
            &json!({
                "userId": user_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": false,
                "withGrokTranslatedBio": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the authenticated user-likes URL for one user id.
    #[doc(alias = "getUserLikes")]
    pub fn user_likes(
        &self,
        user_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            LIKES_OPERATION_ID,
            "Likes",
            &json!({
                "userId": user_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": false,
                "withClientEventToken": false,
                "withBirdwatchNotes": false,
                "withVoice": true,
            }),
            &current_user_timeline_features(),
            Some(&current_timeline_field_toggles()),
        )
    }

    /// Build the bookmarks URL for the authenticated user.
    #[doc(alias = "getUserBookmarks")]
    pub fn user_bookmarks(
        &self,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            BOOKMARKS_OPERATION_ID,
            "Bookmarks",
            &json!({
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the followed-feed URL for the authenticated user.
    #[doc(alias = "getFollowedFeed")]
    pub fn user_followed(
        &self,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            HOME_LATEST_TIMELINE_OPERATION_ID,
            "HomeLatestTimeline",
            &json!({
                "count": count.unwrap_or(35),
                "cursor": cursor,
                "includePromotedContent": false,
                "latestControlAvailable": true,
                "withCommunity": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the recommended-feed URL for the authenticated user.
    #[doc(alias = "getRecommendedFeed")]
    pub fn user_recommended(
        &self,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            HOME_TIMELINE_OPERATION_ID,
            "HomeTimeline",
            &json!({
                "count": count.unwrap_or(35),
                "cursor": cursor,
                "includePromotedContent": false,
                "latestControlAvailable": true,
                "withCommunity": false,
                "seenTweetIds": [],
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the tweet-search URL for one raw query.
    #[doc(alias = "searchTweets")]
    pub fn search_tweets(
        &self,
        raw_query: &str,
        search_product: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            SEARCH_TIMELINE_OPERATION_ID,
            "SearchTimeline",
            &json!({
                "rawQuery": raw_query,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "querySource": "typed_query",
                "product": search_product,
                "withGrokTranslatedBio": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the user-search URL for one raw query.
    #[doc(alias = "searchUsers")]
    pub fn search_users(
        &self,
        raw_query: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            USER_SEARCH_TIMELINE_OPERATION_ID,
            "SearchTimeline",
            &json!({
                "rawQuery": raw_query,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "querySource": "typed_query",
                "product": "People",
                "withGrokTranslatedBio": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the tweet-detail URL for one tweet id.
    #[doc(alias = "getTweetDetail")]
    pub fn tweet_detail(&self, tweet_id: &str) -> Result<String, AppError> {
        self.build_graphql_url(
            TWEET_RESULT_BY_REST_ID_OPERATION_ID,
            "TweetResultByRestId",
            &json!({
                "tweetId": tweet_id,
                "referrer": "home",
                "with_rux_injections": false,
                "includePromotedContent": false,
                "withCommunity": false,
                "withQuickPromoteEligibilityTweetFields": false,
                "withBirdwatchNotes": false,
                "withVoice": false,
                "withV2Timeline": false,
            }),
            &json!({
                "creator_subscriptions_tweet_preview_api_enabled": true,
                "premium_content_api_read_enabled": false,
                "communities_web_enable_tweet_community_results_fetch": true,
                "c9s_tweet_anatomy_moderator_badge_enabled": true,
                "responsive_web_grok_analyze_button_fetch_trends_enabled": false,
                "responsive_web_grok_analyze_post_followups_enabled": false,
                "responsive_web_jetfuel_frame": false,
                "responsive_web_grok_share_attachment_enabled": true,
                "articles_preview_enabled": true,
                "responsive_web_edit_tweet_api_enabled": true,
                "graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
                "view_counts_everywhere_api_enabled": true,
                "longform_notetweets_consumption_enabled": true,
                "responsive_web_twitter_article_tweet_consumption_enabled": true,
                "tweet_awards_web_tipping_enabled": false,
                "responsive_web_grok_show_grok_translated_post": false,
                "responsive_web_grok_analysis_button_from_backend": false,
                "creator_subscriptions_quote_tweet_preview_enabled": false,
                "freedom_of_speech_not_reach_fetch_enabled": true,
                "standardized_nudges_misinfo": true,
                "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
                "longform_notetweets_rich_text_read_enabled": true,
                "longform_notetweets_inline_media_enabled": true,
                "profile_label_improvements_pcf_label_in_post_enabled": true,
                "rweb_tipjar_consumption_enabled": true,
                "verified_phone_label_enabled": true,
                "responsive_web_grok_image_annotation_enabled": true,
                "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
                "responsive_web_graphql_timeline_navigation_enabled": true,
                "responsive_web_enhance_cards_enabled": false,
            }),
            None,
        )
    }

    /// Build the tweet-replies URL for one tweet id.
    #[doc(alias = "getTweetReplies")]
    pub fn tweet_replies(
        &self,
        tweet_id: &str,
        cursor: Option<&str>,
        ranking_mode: &str,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            TWEET_DETAIL_OPERATION_ID,
            "TweetDetail",
            &json!({
                "focalTweetId": tweet_id,
                "cursor": cursor,
                "referrer": "tweet",
                "with_rux_injections": false,
                "rankingMode": ranking_mode,
                "includePromotedContent": true,
                "withCommunity": true,
                "withQuickPromoteEligibilityTweetFields": true,
                "withBirdwatchNotes": true,
                "withVoice": true,
            }),
            &current_user_timeline_features(),
            Some(&current_tweet_replies_field_toggles()),
        )
    }

    /// Build the tweet-likers URL for one tweet id.
    #[doc(alias = "getTweetLikers")]
    pub fn tweet_likers(
        &self,
        tweet_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            FAVORITERS_OPERATION_ID,
            "Favoriters",
            &json!({
                "tweetId": tweet_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "enableRanking": false,
                "includePromotedContent": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the tweet-retweeters URL for one tweet id.
    #[doc(alias = "getTweetRetweeters")]
    pub fn tweet_retweeters(
        &self,
        tweet_id: &str,
        count: Option<u32>,
        cursor: Option<&str>,
    ) -> Result<String, AppError> {
        self.build_graphql_url(
            RETWEETERS_OPERATION_ID,
            "Retweeters",
            &json!({
                "tweetId": tweet_id,
                "count": count.unwrap_or(20),
                "cursor": cursor,
                "includePromotedContent": false,
            }),
            &current_user_timeline_features(),
            None,
        )
    }

    /// Build the space-detail URL for one space id.
    #[doc(alias = "getSpaceDetail")]
    pub fn space_detail(&self, space_id: &str) -> Result<String, AppError> {
        self.build_graphql_url(
            AUDIO_SPACE_BY_ID_OPERATION_ID,
            "AudioSpaceById",
            &json!({
                "id": space_id,
                "isMetatagsQuery": false,
                "withReplays": true,
                "withListeners": false,
            }),
            &json!({
                "spaces_2022_h2_spaces_communities": true,
                "spaces_2022_h2_clipping": true,
                "creator_subscriptions_tweet_preview_api_enabled": true,
                "profile_label_improvements_pcf_label_in_post_enabled": true,
                "responsive_web_profile_redirect_enabled": false,
                "rweb_tipjar_consumption_enabled": false,
                "verified_phone_label_enabled": false,
                "premium_content_api_read_enabled": false,
                "communities_web_enable_tweet_community_results_fetch": true,
                "c9s_tweet_anatomy_moderator_badge_enabled": true,
                "responsive_web_grok_analyze_button_fetch_trends_enabled": false,
                "responsive_web_grok_analyze_post_followups_enabled": true,
                "responsive_web_jetfuel_frame": true,
                "responsive_web_grok_share_attachment_enabled": true,
                "responsive_web_grok_annotations_enabled": false,
                "articles_preview_enabled": true,
                "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
                "responsive_web_edit_tweet_api_enabled": true,
                "graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
                "view_counts_everywhere_api_enabled": true,
                "longform_notetweets_consumption_enabled": true,
                "responsive_web_twitter_article_tweet_consumption_enabled": true,
                "tweet_awards_web_tipping_enabled": false,
                "responsive_web_grok_show_grok_translated_post": false,
                "responsive_web_grok_analysis_button_from_backend": true,
                "post_ctas_fetch_enabled": true,
                "creator_subscriptions_quote_tweet_preview_enabled": false,
                "freedom_of_speech_not_reach_fetch_enabled": true,
                "standardized_nudges_misinfo": true,
                "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
                "longform_notetweets_rich_text_read_enabled": true,
                "longform_notetweets_inline_media_enabled": true,
                "responsive_web_grok_image_annotation_enabled": true,
                "responsive_web_grok_imagine_annotation_enabled": true,
                "responsive_web_graphql_timeline_navigation_enabled": true,
                "responsive_web_grok_community_note_auto_translation_is_enabled": false,
                "responsive_web_enhance_cards_enabled": false,
            }),
            None,
        )
    }

    fn build_graphql_url(
        &self,
        operation_id: &str,
        operation_name: &str,
        variables: &Value,
        features: &Value,
        field_toggles: Option<&Value>,
    ) -> Result<String, AppError> {
        let variables = compact_graphql_value(variables.clone());
        let features = compact_graphql_value(features.clone());
        let field_toggles = field_toggles.cloned().map(compact_graphql_value);
        let mut url = Url::parse(&format!(
            "{}/{operation_id}/{operation_name}",
            self.graphql_base_url.as_ref()
        ))
        .map_err(|error| AppError::InvalidRequestConfig(format!("invalid twitter url: {error}")))?;
        {
            let mut query = url.query_pairs_mut();
            query.append_pair("variables", &variables.to_string());
            query.append_pair("features", &features.to_string());

            if let Some(field_toggles) = field_toggles.as_ref() {
                query.append_pair("fieldToggles", &field_toggles.to_string());
            }
        }

        Ok(url.to_string())
    }
}

fn current_user_profile_features() -> Value {
    json!({
        "hidden_profile_subscriptions_enabled": true,
        "profile_label_improvements_pcf_label_in_post_enabled": true,
        "responsive_web_profile_redirect_enabled": false,
        "rweb_tipjar_consumption_enabled": false,
        "verified_phone_label_enabled": false,
        "subscriptions_verification_info_is_identity_verified_enabled": true,
        "subscriptions_verification_info_verified_since_enabled": true,
        "highlights_tweets_tab_ui_enabled": true,
        "responsive_web_twitter_article_notes_tab_enabled": true,
        "subscriptions_feature_can_gift_premium": true,
        "creator_subscriptions_tweet_preview_api_enabled": true,
        "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
        "responsive_web_graphql_timeline_navigation_enabled": true,
    })
}

fn current_user_profile_field_toggles() -> Value {
    json!({
        "withPayments": false,
        "withAuxiliaryUserLabels": true,
    })
}

fn current_user_timeline_features() -> Value {
    json!({
        "rweb_video_screen_enabled": false,
        "profile_label_improvements_pcf_label_in_post_enabled": true,
        "responsive_web_profile_redirect_enabled": false,
        "rweb_tipjar_consumption_enabled": false,
        "verified_phone_label_enabled": false,
        "creator_subscriptions_tweet_preview_api_enabled": true,
        "responsive_web_graphql_timeline_navigation_enabled": true,
        "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
        "premium_content_api_read_enabled": false,
        "communities_web_enable_tweet_community_results_fetch": true,
        "c9s_tweet_anatomy_moderator_badge_enabled": true,
        "responsive_web_grok_analyze_button_fetch_trends_enabled": false,
        "responsive_web_grok_analyze_post_followups_enabled": true,
        "responsive_web_jetfuel_frame": true,
        "responsive_web_grok_share_attachment_enabled": true,
        "responsive_web_grok_annotations_enabled": true,
        "articles_preview_enabled": true,
        "responsive_web_edit_tweet_api_enabled": true,
        "graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
        "view_counts_everywhere_api_enabled": true,
        "longform_notetweets_consumption_enabled": true,
        "responsive_web_twitter_article_tweet_consumption_enabled": true,
        "content_disclosure_indicator_enabled": true,
        "content_disclosure_ai_generated_indicator_enabled": true,
        "responsive_web_grok_show_grok_translated_post": false,
        "responsive_web_grok_analysis_button_from_backend": true,
        "post_ctas_fetch_enabled": false,
        "freedom_of_speech_not_reach_fetch_enabled": true,
        "standardized_nudges_misinfo": true,
        "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
        "longform_notetweets_rich_text_read_enabled": true,
        "longform_notetweets_inline_media_enabled": false,
        "responsive_web_grok_image_annotation_enabled": true,
        "responsive_web_grok_imagine_annotation_enabled": true,
        "responsive_web_grok_community_note_auto_translation_is_enabled": false,
        "responsive_web_enhance_cards_enabled": false,
    })
}

fn current_timeline_field_toggles() -> Value {
    json!({
        "withArticlePlainText": false,
    })
}

fn current_tweet_replies_field_toggles() -> Value {
    json!({
        "withArticleRichContentState": true,
        "withArticlePlainText": false,
        "withGrokAnalyze": false,
        "withDisallowedReplyControls": false,
    })
}

fn normalize_user_media_count(count: Option<u32>) -> u32 {
    count.unwrap_or(20).max(2)
}

fn compact_graphql_value(mut value: Value) -> Value {
    strip_null_fields(&mut value);
    value
}

fn strip_null_fields(value: &mut Value) {
    match value {
        Value::Object(map) => {
            for child in map.values_mut() {
                strip_null_fields(child);
            }
            map.retain(|_, child| !child.is_null());
        }
        Value::Array(items) => {
            for item in items {
                strip_null_fields(item);
            }
        }
        _ => {}
    }
}

/// Create a public Twitter/X URL builder.
#[doc(alias = "createTwitterApiUrls")]
pub fn create_twitter_api_urls() -> TwitterApiUrls {
    TwitterApiUrls::new()
}