aboutsummaryrefslogtreecommitdiff
path: root/src/commands/view.rs
blob: 15c94b1fe2d4734e2bd8cd0562f629150e1f52ca (plain)
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
use tw;
use ::Queryer;

use tw::TweetId;

use commands::Command;

use display;

pub static VIEW: Command = Command {
    keyword: "view",
    params: 1,
    exec: view,
    param_str: " <tweet_id>",
    help_str: "Display tweet <tweet_id> with a reference URL"
};

fn view(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) {
    match TweetId::parse(line) {
        Ok(twid) => {
            if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) {
                tweeter.display_info.recv(display::Infos::TweetWithContext(
                    TweetId::Twitter(twete.id.to_owned()),
                    format!("link: https://twitter.com/i/web/status/{}", twete.id)
                ));
            } else {
                tweeter.display_info.status(format!("No tweet for id {:?}", twid));
            }
        },
        Err(e) => {
            tweeter.display_info.status(format!("Invalid id {:?}", e));
        }
    }
}

pub static VIEW_THREAD: Command = Command {
    keyword: "view_tr",
    params: 1,
    exec: view_tr,
    param_str: " <tweet_id>",
    help_str: "Display whole thread leading to <tweet_id>, reference URLs for each"
};

fn view_tr(line: String, mut tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
    let mut thread: Vec<TweetId> = Vec::new();
    let maybe_curr_id = TweetId::parse(line);
    match maybe_curr_id {
        Ok(curr_id) => {
            let first_twete = tweeter.fetch_tweet(&curr_id, queryer).map(|x| x.to_owned());
            if first_twete.is_some() {
                thread.push(curr_id);
            }
            let mut maybe_next_id = first_twete.and_then(|x| x.reply_to_tweet.to_owned());
            while let Some(next_id) = maybe_next_id {
                let curr_id = TweetId::Twitter(next_id);
                maybe_next_id = tweeter.fetch_tweet(&curr_id, queryer).and_then(|x| x.reply_to_tweet.to_owned());
                thread.push(curr_id);
            }
        },
        Err(e) => {
            tweeter.display_info.status(format!("Invalid id {:?}", e));
        }
    }

    tweeter.display_info.recv(display::Infos::Thread(thread));
}

pub static VIEW_THREAD_FORWARD: Command = Command {
    keyword: "viewthread+",
    params: 1,
    exec: view_tr_forward,
    param_str: "",
    help_str: "help me make this work"
};

fn view_tr_forward(_line: String, _tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) {
    // first see if we have a thread for the tweet named
    // if we do not, we'll have to mimic a request like 
    // curl 'https://twitter.com/jojonila/status/914383908090691584' \
    //   -H 'accept-encoding: gzip, deflate, br' \
    //   -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
    //     (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36' \
    //   -H 'x-overlay-request: true'
    /*
     * above results in a response:
     * {
     *  "init_data": { ... },
     *  "title": "tweet, but for page title",
     *  ..
     *  "page": "HTML THAT'S JUST DROPPED IN AS A MODAL."
     *  */
}