xmaster 1.6.0

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
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
use chrono::{Datelike, Timelike, Utc};
use rand::Rng;
use rusqlite::{params, Connection, OptionalExtension};
use serde::Serialize;
use std::path::PathBuf;
use std::sync::Arc;

use crate::config::config_dir;
use crate::context::AppContext;
use crate::errors::XmasterError;
use crate::intel::preflight::{self, AnalyzeContext};
use crate::intel::store::IntelStore;
use crate::providers::xapi::XApi;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
pub struct ScheduledPost {
    pub id: String,
    pub content: String,
    pub scheduled_at: i64,
    pub timezone: String,
    pub status: String,
    pub content_type: String,
    pub created_at: i64,
    pub fired_at: Option<i64>,
    pub tweet_id: Option<String>,
    pub retry_count: i32,
    pub last_error: Option<String>,
    pub preflight_score: Option<i32>,
    pub auto_scheduled: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct FireResult {
    pub fired: u32,
    pub failed: u32,
    pub missed: u32,
    pub posts: Vec<FiredPost>,
}

#[derive(Debug, Clone, Serialize)]
pub struct FiredPost {
    pub id: String,
    pub content_preview: String,
    pub status: String,
    pub tweet_id: Option<String>,
    pub error: Option<String>,
}

// ---------------------------------------------------------------------------
// PostScheduler
// ---------------------------------------------------------------------------

pub struct PostScheduler {
    conn: Connection,
}

impl PostScheduler {
    /// Open (or create) the scheduler database at `~/.config/xmaster/xmaster.db`.
    pub fn open() -> Result<Self, XmasterError> {
        let dir = config_dir();
        std::fs::create_dir_all(&dir).ok();
        let db_path: PathBuf = dir.join("xmaster.db");
        let conn = Connection::open(db_path).map_err(|e| XmasterError::Config(e.to_string()))?;
        conn.pragma_update(None, "journal_mode", "wal").ok();
        conn.pragma_update(None, "busy_timeout", 5000).ok();
        conn.pragma_update(None, "synchronous", "NORMAL").ok();
        let sched = Self { conn };
        sched.init_tables()?;
        Ok(sched)
    }

    fn init_tables(&self) -> Result<(), XmasterError> {
        self.conn
            .execute_batch(
                "
            CREATE TABLE IF NOT EXISTS scheduled_posts (
                id              TEXT PRIMARY KEY,
                content         TEXT NOT NULL,
                scheduled_at    INTEGER NOT NULL,
                timezone        TEXT NOT NULL DEFAULT 'UTC',
                status          TEXT NOT NULL DEFAULT 'pending',
                content_type    TEXT NOT NULL DEFAULT 'text',
                reply_to_id     TEXT,
                quote_id        TEXT,
                media_paths     TEXT,
                created_at      INTEGER NOT NULL,
                fired_at        INTEGER,
                tweet_id        TEXT,
                retry_count     INTEGER NOT NULL DEFAULT 0,
                last_error      TEXT,
                preflight_score INTEGER,
                auto_scheduled  INTEGER NOT NULL DEFAULT 0
            );
            CREATE INDEX IF NOT EXISTS idx_scheduled_status
                ON scheduled_posts(status, scheduled_at);
            ",
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;
        Ok(())
    }

    // -- writes --------------------------------------------------------------

    /// Schedule a new post.
    #[allow(clippy::too_many_arguments)]
    pub fn add(
        &self,
        content: &str,
        scheduled_at_utc: i64,
        timezone: &str,
        content_type: &str,
        reply_to: Option<&str>,
        quote: Option<&str>,
        media: Option<&[String]>,
        auto_scheduled: bool,
    ) -> Result<ScheduledPost, XmasterError> {
        let now = Utc::now().timestamp();
        let id = generate_id(now);

        // Run preflight analysis
        let pf = preflight::analyze(content, &AnalyzeContext::default());
        let score = pf.score as i32;

        let media_json = media.map(|m| serde_json::to_string(m).unwrap_or_default());

        self.conn
            .execute(
                "INSERT INTO scheduled_posts
                    (id, content, scheduled_at, timezone, status, content_type,
                     reply_to_id, quote_id, media_paths, created_at,
                     preflight_score, auto_scheduled)
                 VALUES (?1,?2,?3,?4,'pending',?5,?6,?7,?8,?9,?10,?11)",
                params![
                    id,
                    content,
                    scheduled_at_utc,
                    timezone,
                    content_type,
                    reply_to,
                    quote,
                    media_json,
                    now,
                    score,
                    auto_scheduled as i32,
                ],
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        Ok(ScheduledPost {
            id,
            content: content.to_string(),
            scheduled_at: scheduled_at_utc,
            timezone: timezone.to_string(),
            status: "pending".to_string(),
            content_type: content_type.to_string(),
            created_at: now,
            fired_at: None,
            tweet_id: None,
            retry_count: 0,
            last_error: None,
            preflight_score: Some(score),
            auto_scheduled,
        })
    }

    // -- reads ---------------------------------------------------------------

    /// List scheduled posts, optionally filtered by status.
    pub fn list(&self, status_filter: Option<&str>) -> Result<Vec<ScheduledPost>, XmasterError> {
        let (sql, use_filter) = match status_filter {
            Some(_) => (
                "SELECT id, content, scheduled_at, timezone, status, content_type,
                        created_at, fired_at, tweet_id, retry_count, last_error,
                        preflight_score, auto_scheduled
                 FROM scheduled_posts WHERE status = ?1
                 ORDER BY scheduled_at ASC",
                true,
            ),
            None => (
                "SELECT id, content, scheduled_at, timezone, status, content_type,
                        created_at, fired_at, tweet_id, retry_count, last_error,
                        preflight_score, auto_scheduled
                 FROM scheduled_posts
                 ORDER BY scheduled_at ASC",
                false,
            ),
        };

        let mut stmt = self
            .conn
            .prepare(sql)
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        let map_row = |row: &rusqlite::Row| -> rusqlite::Result<ScheduledPost> {
            let auto_int: i32 = row.get(12)?;
            Ok(ScheduledPost {
                id: row.get(0)?,
                content: row.get(1)?,
                scheduled_at: row.get(2)?,
                timezone: row.get(3)?,
                status: row.get(4)?,
                content_type: row.get(5)?,
                created_at: row.get(6)?,
                fired_at: row.get(7)?,
                tweet_id: row.get(8)?,
                retry_count: row.get(9)?,
                last_error: row.get(10)?,
                preflight_score: row.get(11)?,
                auto_scheduled: auto_int != 0,
            })
        };

        let results: Vec<ScheduledPost> = if use_filter {
            stmt.query_map(params![status_filter.unwrap()], map_row)
                .map_err(|e| XmasterError::Config(e.to_string()))?
                .collect::<Result<Vec<_>, _>>()
                .map_err(|e| XmasterError::Config(e.to_string()))?
        } else {
            stmt.query_map([], map_row)
                .map_err(|e| XmasterError::Config(e.to_string()))?
                .collect::<Result<Vec<_>, _>>()
                .map_err(|e| XmasterError::Config(e.to_string()))?
        };

        Ok(results)
    }

    // -- mutations ------------------------------------------------------------

    /// Cancel a pending scheduled post.
    pub fn cancel(&self, id: &str) -> Result<(), XmasterError> {
        let changed = self
            .conn
            .execute(
                "UPDATE scheduled_posts SET status = 'cancelled'
                 WHERE id = ?1 AND status = 'pending'",
                params![id],
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        if changed == 0 {
            return Err(XmasterError::NotFound(format!(
                "No pending post with id '{id}' (may already be sent or cancelled)"
            )));
        }
        Ok(())
    }

    /// Reschedule a pending or failed post to a new time.
    pub fn reschedule(&self, id: &str, new_time: i64) -> Result<(), XmasterError> {
        let changed = self
            .conn
            .execute(
                "UPDATE scheduled_posts SET scheduled_at = ?1
                 WHERE id = ?2 AND status IN ('pending', 'failed')",
                params![new_time, id],
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        if changed == 0 {
            return Err(XmasterError::NotFound(format!(
                "No pending/failed post with id '{id}'"
            )));
        }
        Ok(())
    }

    // -- fire engine ----------------------------------------------------------

    /// Fire all due posts. The core scheduling engine.
    ///
    /// - Posts past the grace window are marked as missed/failed.
    /// - Posts within the window are posted via XApi.
    /// - On post failure, retry_count is incremented; >= 3 retries marks as failed.
    pub async fn fire(
        &self,
        ctx: Arc<AppContext>,
        grace_minutes: i64,
    ) -> Result<FireResult, XmasterError> {
        let now = Utc::now().timestamp();
        let grace_cutoff = now - (grace_minutes * 60);

        // Phase 1: Atomically claim due posts (prevents duplicate posts from concurrent runs)
        self.conn
            .execute(
                "UPDATE scheduled_posts SET status = 'firing'
                 WHERE status = 'pending' AND scheduled_at <= ?1",
                params![now],
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        // Phase 2: Fetch claimed posts
        let mut stmt = self
            .conn
            .prepare(
                "SELECT id, content, scheduled_at, reply_to_id, quote_id, media_paths, retry_count
                 FROM scheduled_posts
                 WHERE status = 'firing'",
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        type DuePost = (String, String, i64, Option<String>, Option<String>, Option<String>, i32);
        let due_posts: Vec<DuePost> =
            stmt.query_map([], |row| {
                Ok((
                    row.get(0)?,
                    row.get(1)?,
                    row.get(2)?,
                    row.get(3)?,
                    row.get(4)?,
                    row.get(5)?,
                    row.get(6)?,
                ))
            })
            .map_err(|e| XmasterError::Config(e.to_string()))?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| XmasterError::Config(e.to_string()))?;

        let is_premium = ctx.config.account.premium;
        let api = XApi::new(ctx);
        let mut result = FireResult {
            fired: 0,
            failed: 0,
            missed: 0,
            posts: Vec::new(),
        };

        for (id, content, scheduled_at, reply_to, quote, media_json, retry_count) in &due_posts {
            let preview: String = content.chars().take(80).collect();

            // Missed the grace window
            if *scheduled_at < grace_cutoff {
                self.mark_failed(id, "Missed schedule window")?;
                result.missed += 1;
                result.posts.push(FiredPost {
                    id: id.clone(),
                    content_preview: preview,
                    status: "missed".to_string(),
                    tweet_id: None,
                    error: Some("Missed schedule window".to_string()),
                });
                continue;
            }

            // Upload media files and collect media IDs (stored as file paths, not IDs)
            let media_upload = if let Some(paths_json) = media_json {
                let paths: Vec<String> = serde_json::from_str(paths_json).unwrap_or_default();
                if !paths.is_empty() {
                    let mut ids = Vec::new();
                    let mut upload_err = None;
                    for path in &paths {
                        match api.upload_media(path).await {
                            Ok(mid) => ids.push(mid),
                            Err(e) => {
                                upload_err = Some(e);
                                break;
                            }
                        }
                    }
                    if let Some(e) = upload_err {
                        Err(e)
                    } else {
                        Ok(Some(ids))
                    }
                } else {
                    Ok(None)
                }
            } else {
                Ok(None)
            };

            let media_ids = match media_upload {
                Ok(ids) => ids,
                Err(e) => {
                    self.mark_failed(id, &format!("Media upload failed: {e}"))?;
                    result.failed += 1;
                    result.posts.push(FiredPost {
                        id: id.clone(),
                        content_preview: preview,
                        status: "failed".to_string(),
                        tweet_id: None,
                        error: Some(format!("Media upload failed: {e}")),
                    });
                    continue;
                }
            };

            // Attempt to post
            let post_result = api
                .create_tweet(
                    content,
                    reply_to.as_deref(),
                    quote.as_deref(),
                    media_ids.as_deref(),
                    None,
                    None,
                )
                .await;

            match post_result {
                Ok(tweet) => {
                    let fire_ts = Utc::now().timestamp();
                    self.conn
                        .execute(
                            "UPDATE scheduled_posts
                             SET status = 'sent', fired_at = ?1, tweet_id = ?2
                             WHERE id = ?3",
                            params![fire_ts, tweet.id, id],
                        )
                        .map_err(|e| XmasterError::Config(e.to_string()))?;

                    // Record to intel store with context-aware analysis
                    if let Ok(store) = IntelStore::open() {
                        let is_reply = reply_to.is_some();
                        let is_quote = quote.is_some();
                        let has_media = media_ids.as_ref().map_or(false, |m| !m.is_empty());
                        let mode = if is_reply {
                            preflight::PostMode::Reply
                        } else if is_quote {
                            preflight::PostMode::Quote
                        } else {
                            preflight::PostMode::Standalone
                        };
                        let analyze_ctx = AnalyzeContext {
                            mode: Some(mode),
                            has_media,
                            premium: is_premium,
                            ..AnalyzeContext::default()
                        };
                        let pf = preflight::analyze(content, &analyze_ctx);
                        let analysis_json = serde_json::to_string(&pf).ok();
                        let content_type = if is_reply { "reply" } else if is_quote { "quote" } else { "scheduled" };
                        let _ = store.record_published_post(
                            &tweet.id,
                            content,
                            content_type,
                            reply_to.as_deref(),
                            quote.as_deref(),
                            Some(pf.score as f64),
                            analysis_json.as_deref(),
                            Some(id),
                        );
                        // Track reply style for scheduled replies
                        if is_reply {
                            if let Some(ref target_id) = reply_to {
                                let style = crate::intel::store::IntelStore::classify_reply_style(content);
                                let _ = store.log_reply(target_id, None, None, None, &tweet.id, Some(&style));
                            }
                        }
                    }

                    result.fired += 1;
                    result.posts.push(FiredPost {
                        id: id.clone(),
                        content_preview: preview,
                        status: "sent".to_string(),
                        tweet_id: Some(tweet.id),
                        error: None,
                    });
                }
                Err(e) => {
                    let new_retry = retry_count + 1;
                    let err_msg = e.to_string();

                    if new_retry >= 3 {
                        self.conn
                            .execute(
                                "UPDATE scheduled_posts
                                 SET status = 'failed', retry_count = ?1, last_error = ?2
                                 WHERE id = ?3",
                                params![new_retry, err_msg, id],
                            )
                            .map_err(|e| XmasterError::Config(e.to_string()))?;
                        result.failed += 1;
                    } else {
                        self.conn
                            .execute(
                                "UPDATE scheduled_posts
                                 SET status = 'pending', retry_count = ?1, last_error = ?2
                                 WHERE id = ?3",
                                params![new_retry, err_msg, id],
                            )
                            .map_err(|e| XmasterError::Config(e.to_string()))?;
                        result.failed += 1;
                    }

                    result.posts.push(FiredPost {
                        id: id.clone(),
                        content_preview: preview,
                        status: if new_retry >= 3 {
                            "failed".to_string()
                        } else {
                            "retry".to_string()
                        },
                        tweet_id: None,
                        error: Some(err_msg),
                    });
                }
            }
        }

        Ok(result)
    }

    /// Find the best auto-schedule time based on historical engagement data.
    /// Queries timing_stats for the best performing hour and returns the next
    /// future occurrence of that hour as a Unix timestamp.
    pub fn get_best_auto_time(&self) -> Option<i64> {
        let row: Option<(i32, i32)> = self
            .conn
            .query_row(
                "SELECT day_of_week, hour_of_day
                 FROM timing_stats
                 WHERE content_type = 'all' AND sample_count >= 2
                 ORDER BY avg_engagement_rate DESC
                 LIMIT 1",
                [],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .optional()
            .ok()
            .flatten();

        let (best_dow, best_hour) = row?;

        // Find the next occurrence of this day/hour in UTC
        let now = Utc::now();
        let current_dow = now.weekday().num_days_from_monday() as i32; // 0=Mon
        let current_hour = now.hour() as i32;

        // Days until the target day-of-week
        let mut days_ahead = best_dow - current_dow;
        if days_ahead < 0 || (days_ahead == 0 && current_hour >= best_hour) {
            days_ahead += 7;
        }

        let target = now
            .date_naive()
            .and_hms_opt(best_hour as u32, 0, 0)?;
        let target_date = target + chrono::Duration::days(days_ahead as i64);
        Some(target_date.and_utc().timestamp())
    }

    // -- internal helpers -----------------------------------------------------

    fn mark_failed(&self, id: &str, error: &str) -> Result<(), XmasterError> {
        self.conn
            .execute(
                "UPDATE scheduled_posts SET status = 'failed', last_error = ?1 WHERE id = ?2",
                params![error, id],
            )
            .map_err(|e| XmasterError::Config(e.to_string()))?;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn generate_id(timestamp: i64) -> String {
    let mut rng = rand::thread_rng();
    let chars: Vec<char> = "abcdefghijklmnopqrstuvwxyz0123456789".chars().collect();
    let suffix: String = (0..4).map(|_| chars[rng.gen_range(0..chars.len())]).collect();
    format!("sched_{timestamp}_{suffix}")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn in_memory_scheduler() -> PostScheduler {
        let conn = Connection::open_in_memory().unwrap();
        let sched = PostScheduler { conn };
        sched.init_tables().unwrap();
        sched
    }

    #[test]
    fn add_and_list() {
        let sched = in_memory_scheduler();
        let future_ts = Utc::now().timestamp() + 3600;

        let post = sched
            .add("Hello world!", future_ts, "UTC", "text", None, None, None, false)
            .unwrap();

        assert!(post.id.starts_with("sched_"));
        assert_eq!(post.status, "pending");
        assert!(post.preflight_score.is_some());

        let all = sched.list(None).unwrap();
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].content, "Hello world!");

        let pending = sched.list(Some("pending")).unwrap();
        assert_eq!(pending.len(), 1);

        let sent = sched.list(Some("sent")).unwrap();
        assert!(sent.is_empty());
    }

    #[test]
    fn cancel_pending() {
        let sched = in_memory_scheduler();
        let future_ts = Utc::now().timestamp() + 3600;

        let post = sched
            .add("Cancel me", future_ts, "UTC", "text", None, None, None, false)
            .unwrap();

        sched.cancel(&post.id).unwrap();

        let pending = sched.list(Some("pending")).unwrap();
        assert!(pending.is_empty());

        let cancelled = sched.list(Some("cancelled")).unwrap();
        assert_eq!(cancelled.len(), 1);
    }

    #[test]
    fn cancel_nonexistent_fails() {
        let sched = in_memory_scheduler();
        let result = sched.cancel("sched_fake_id");
        assert!(result.is_err());
    }

    #[test]
    fn reschedule_updates_time() {
        let sched = in_memory_scheduler();
        let t1 = Utc::now().timestamp() + 3600;
        let t2 = t1 + 7200;

        let post = sched
            .add("Reschedule me", t1, "UTC", "text", None, None, None, false)
            .unwrap();

        sched.reschedule(&post.id, t2).unwrap();

        let all = sched.list(None).unwrap();
        assert_eq!(all[0].scheduled_at, t2);
    }

    #[test]
    fn id_format() {
        let id = generate_id(1700000000);
        assert!(id.starts_with("sched_1700000000_"));
        assert_eq!(id.len(), "sched_1700000000_".len() + 4);
    }
}