xmaster 1.6.6

Enterprise-grade X/Twitter CLI — post, reply, like, retweet, DM, search, and more
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
use crate::cli::parse_tweet_id;
use crate::context::AppContext;
use crate::errors::XmasterError;
use crate::output::{self, OutputFormat, Tableable};
use crate::providers::xapi::{TweetData, TweetLookup, XApi};
use serde::Serialize;
use std::sync::Arc;

const REPLY_ENGAGED_BY_AUTHOR_WEIGHT: f64 = 75.0;
const LIKE_WEIGHT: f64 = 0.5;

#[derive(Debug, Serialize)]
struct EngageInboxResult {
    source_tweet_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    root_metrics: Option<RootMetrics>,
    totals: InboxTotals,
    heuristic: EngagementHeuristic,
    recommendations: Vec<InboxRecommendation>,
    quote_threads: Vec<QuoteThread>,
    suggested_next_commands: Vec<String>,
    notes: Vec<String>,
}

#[derive(Debug, Serialize)]
struct RootMetrics {
    impressions: u64,
    likes: u64,
    retweets: u64,
    replies: u64,
    quotes: u64,
    bookmarks: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    profile_clicks: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    url_clicks: Option<u64>,
}

#[derive(Debug, Serialize)]
struct InboxTotals {
    direct_replies_checked: usize,
    quote_tweets_checked: usize,
    quote_replies_checked: usize,
    recommended_actions: usize,
}

#[derive(Debug, Serialize)]
struct EngagementHeuristic {
    name: String,
    source: String,
    source_url: String,
    reply_engaged_by_author_weight: f64,
    like_weight: f64,
    ratio_to_like: String,
    caveat: String,
}

#[derive(Debug, Clone, Serialize)]
struct InboxRecommendation {
    priority: u8,
    surface: String,
    action: String,
    target_id: String,
    target_url: String,
    author: String,
    likes: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    created_at: Option<String>,
    reason: String,
    text: String,
}

#[derive(Debug, Serialize)]
struct QuoteThread {
    quote_id: String,
    quote_url: String,
    author: String,
    likes: u64,
    impressions: u64,
    text: String,
    replies_checked: usize,
    replies: Vec<QuoteReply>,
}

#[derive(Debug, Serialize)]
struct QuoteReply {
    id: String,
    author: String,
    likes: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    created_at: Option<String>,
    text: String,
}

impl Tableable for EngageInboxResult {
    fn to_table(&self) -> comfy_table::Table {
        let mut table = comfy_table::Table::new();
        table.set_header(vec![
            "Priority", "Surface", "Action", "Target", "Author", "Why", "Text",
        ]);

        if self.recommendations.is_empty() {
            table.add_row(vec![
                "-".into(),
                "none".into(),
                "watch".into(),
                self.source_tweet_id.clone(),
                "-".into(),
                "No direct reply, quote, or quote-comment action found".into(),
                "-".into(),
            ]);
            return table;
        }

        for rec in &self.recommendations {
            table.add_row(vec![
                rec.priority.to_string(),
                rec.surface.clone(),
                rec.action.clone(),
                rec.target_id.clone(),
                rec.author.clone(),
                rec.reason.clone(),
                crate::utils::safe_truncate(&rec.text, 120).to_string(),
            ]);
        }
        table
    }
}

pub async fn execute(
    ctx: Arc<AppContext>,
    format: OutputFormat,
    id: &str,
    count: usize,
    quote_reply_count: usize,
) -> Result<(), XmasterError> {
    let source_tweet_id = parse_tweet_id(id);
    let api = XApi::new(ctx);
    let mut notes = vec![
        "Native reposts do not have their own comments; this checks quote tweets/reposts-with-comment and replies under those quote tweets.".into(),
        "Recommendation priorities are read-only heuristics. The command never posts or drafts replies.".into(),
    ];

    let root_metrics = fetch_root_metrics(&api, &source_tweet_id).await?;
    let self_username = api
        .get_me()
        .await
        .ok()
        .map(|user| user.username.to_lowercase());
    if self_username.is_some() {
        notes.push(
            "Self-authored replies and quote tweets are excluded from recommendations.".into(),
        );
    }

    let direct_replies = match api.get_replies(&source_tweet_id, count).await {
        Ok(replies) => replies,
        Err(err) => {
            notes.push(format!(
                "Could not fetch direct replies for {source_tweet_id}: {err}"
            ));
            Vec::new()
        }
    };

    let quotes = match api.get_tweet_quotes(&source_tweet_id, count).await {
        Ok(quotes) => quotes,
        Err(err) => {
            notes.push(format!(
                "Could not fetch quote tweets for {source_tweet_id}: {err}"
            ));
            Vec::new()
        }
    };

    let mut recommendations = Vec::new();
    for reply in &direct_replies {
        if !is_self_authored(reply, self_username.as_deref()) {
            recommendations.push(recommend_direct_reply(reply));
        }
    }

    let mut quote_threads = Vec::new();
    let mut quote_replies_checked = 0usize;
    for quote in quotes {
        if !is_self_authored(&quote, self_username.as_deref()) {
            recommendations.push(recommend_quote(&quote));
        }

        let quote_replies = if quote_reply_count == 0 {
            Vec::new()
        } else {
            match api.get_replies(&quote.id, quote_reply_count).await {
                Ok(replies) => replies,
                Err(err) => {
                    notes.push(format!(
                        "Could not fetch replies under quote {}: {err}",
                        quote.id
                    ));
                    Vec::new()
                }
            }
        };

        quote_replies_checked += quote_replies.len();
        for reply in &quote_replies {
            if !is_self_authored(reply, self_username.as_deref()) {
                recommendations.push(recommend_quote_reply(reply, &quote));
            }
        }

        quote_threads.push(build_quote_thread(quote, quote_replies));
    }

    recommendations.sort_by(|a, b| {
        b.priority
            .cmp(&a.priority)
            .then_with(|| b.likes.cmp(&a.likes))
            .then_with(|| b.created_at.cmp(&a.created_at))
    });

    let totals = InboxTotals {
        direct_replies_checked: direct_replies.len(),
        quote_tweets_checked: quote_threads.len(),
        quote_replies_checked,
        recommended_actions: recommendations.len(),
    };

    let suggested_next_commands = vec![
        format!("xmaster metrics {source_tweet_id}"),
        format!("xmaster replies {source_tweet_id} --count {count}"),
        format!("xmaster quotes {source_tweet_id} --count {count}"),
    ];

    let result = EngageInboxResult {
        source_tweet_id,
        root_metrics,
        totals,
        heuristic: reply_engaged_by_author_heuristic(),
        recommendations,
        quote_threads,
        suggested_next_commands,
        notes,
    };

    output::render(format, &result, None);
    Ok(())
}

async fn fetch_root_metrics(
    api: &XApi,
    source_tweet_id: &str,
) -> Result<Option<RootMetrics>, XmasterError> {
    let ids = vec![source_tweet_id.to_string()];
    let root = api.get_posts_by_ids(&ids).await?;
    Ok(root.first().map(root_metrics_from_lookup))
}

fn root_metrics_from_lookup(tweet: &TweetLookup) -> RootMetrics {
    let public = tweet.public_metrics.clone().unwrap_or_default();
    let non_public = tweet.non_public_metrics.clone();
    RootMetrics {
        impressions: public.impression_count,
        likes: public.like_count,
        retweets: public.retweet_count,
        replies: public.reply_count,
        quotes: public.quote_count,
        bookmarks: public.bookmark_count,
        profile_clicks: non_public.as_ref().map(|m| m.user_profile_clicks),
        url_clicks: non_public.as_ref().map(|m| m.url_link_clicks),
    }
}

fn recommend_direct_reply(tweet: &TweetData) -> InboxRecommendation {
    let question = is_question(&tweet.text);
    InboxRecommendation {
        priority: if question { 100 } else { 92 },
        surface: "direct_reply".into(),
        action: if question {
            "answer_reply".into()
        } else {
            "reply_back".into()
        },
        target_id: tweet.id.clone(),
        target_url: status_url(&tweet.id),
        author: author(tweet),
        likes: likes(tweet),
        created_at: tweet.created_at.clone(),
        reason: if question {
            "Question on your post; answer fast to create an author reply-back loop".into()
        } else {
            "Direct comment on your post; author reply-back is a high-value engagement signal"
                .into()
        },
        text: tweet.text.clone(),
    }
}

fn recommend_quote(tweet: &TweetData) -> InboxRecommendation {
    InboxRecommendation {
        priority: if is_question(&tweet.text) { 88 } else { 78 },
        surface: "quote_tweet".into(),
        action: "thank_or_extend_quote".into(),
        target_id: tweet.id.clone(),
        target_url: status_url(&tweet.id),
        author: author(tweet),
        likes: likes(tweet),
        created_at: tweet.created_at.clone(),
        reason: "Quote tweet amplified the post; reply lightly or ask one real question".into(),
        text: tweet.text.clone(),
    }
}

fn recommend_quote_reply(reply: &TweetData, quote: &TweetData) -> InboxRecommendation {
    let question = is_question(&reply.text);
    InboxRecommendation {
        priority: if question { 96 } else { 84 },
        surface: "quote_reply".into(),
        action: if question {
            "answer_quote_comment".into()
        } else {
            "join_quote_thread".into()
        },
        target_id: reply.id.clone(),
        target_url: status_url(&reply.id),
        author: author(reply),
        likes: likes(reply),
        created_at: reply.created_at.clone(),
        reason: format!(
            "Comment under @{}'s quote; second-order thread that manual metrics checks often miss",
            quote.author_username.as_deref().unwrap_or("unknown")
        ),
        text: reply.text.clone(),
    }
}

fn build_quote_thread(quote: TweetData, replies: Vec<TweetData>) -> QuoteThread {
    let quote_metrics = quote.public_metrics.clone();
    let quote_id = quote.id;
    let quote_url = status_url(&quote_id);
    QuoteThread {
        quote_id,
        quote_url,
        author: quote.author_username.unwrap_or_else(|| "unknown".into()),
        likes: quote_metrics.as_ref().map(|m| m.like_count).unwrap_or(0),
        impressions: quote_metrics
            .as_ref()
            .map(|m| m.impression_count)
            .unwrap_or(0),
        text: quote.text,
        replies_checked: replies.len(),
        replies: replies
            .into_iter()
            .map(|reply| QuoteReply {
                id: reply.id,
                author: reply.author_username.unwrap_or_else(|| "unknown".into()),
                likes: reply
                    .public_metrics
                    .as_ref()
                    .map(|m| m.like_count)
                    .unwrap_or(0),
                created_at: reply.created_at,
                text: reply.text,
            })
            .collect(),
    }
}

fn likes(tweet: &TweetData) -> u64 {
    tweet
        .public_metrics
        .as_ref()
        .map(|metrics| metrics.like_count)
        .unwrap_or(0)
}

fn author(tweet: &TweetData) -> String {
    tweet
        .author_username
        .as_ref()
        .map(|username| format!("@{username}"))
        .unwrap_or_else(|| "@unknown".into())
}

fn is_question(text: &str) -> bool {
    text.contains('?')
}

fn is_self_authored(tweet: &TweetData, self_username: Option<&str>) -> bool {
    match (&tweet.author_username, self_username) {
        (Some(author), Some(me)) => author.eq_ignore_ascii_case(me),
        _ => false,
    }
}

fn status_url(id: &str) -> String {
    format!("https://x.com/i/status/{id}")
}

fn reply_engaged_by_author_heuristic() -> EngagementHeuristic {
    EngagementHeuristic {
        name: "reply_engaged_by_author".into(),
        source: "Twitter open-source heavy-ranker README, April 5 2023 weights".into(),
        source_url: "https://github.com/twitter/the-algorithm-ml/blob/main/projects/home/recap/README.md".into(),
        reply_engaged_by_author_weight: REPLY_ENGAGED_BY_AUTHOR_WEIGHT,
        like_weight: LIKE_WEIGHT,
        ratio_to_like: format!(
            "~{}x a like",
            (REPLY_ENGAGED_BY_AUTHOR_WEIGHT / LIKE_WEIGHT).round() as u64
        ),
        caveat: "Historical open-model weight, not a guaranteed live X production constant; use it to prioritize replies, not as an exact score.".into(),
    }
}