aboutsummaryrefslogtreecommitdiff
path: root/src/commands/fav.rs
blob: 5c125354508da3e93f0c1ce48f1a044c220891d7 (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
use tw;
use ::Queryer;

use tw::TweetId;

use commands::Command;

static FAV_TWEET_URL: &str = "https://api.twitter.com/1.1/favorites/create.json";
static UNFAV_TWEET_URL: &str = "https://api.twitter.com/1.1/favorites/destroy.json";

pub static UNFAV: Command = Command {
    keyword: "unfav",
    params: 1,
    exec: unfav,
    param_str: " <tweet_id>",
    help_str: "Unfavorite a tweet."
};

fn unfav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
    let maybe_id = TweetId::parse(line.to_owned());
    match maybe_id {
        Ok(twid) => {
            if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self
                let result = match tweeter.current_profile() {
                    Some(user_profile) => queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id), &tweeter.app_key, &user_profile.creds),
                    None => Err("No logged in user to unfav from".to_owned())
                };
                match result {
                    Ok(_) => (),
                    Err(e) => tweeter.display_info.status(e)
                }
            } else {
                tweeter.display_info.status(format!("No tweet for id: {:?}", twid));
            }
        }
        Err(e) => {
            tweeter.display_info.status(format!("Invalid id: {}", e));
        }
    }
}

pub static FAV: Command = Command {
    keyword: "fav",
    params: 1,
    exec: fav,
    param_str: "<tweet_id>",
    help_str: "Favorite a tweet."
};

fn fav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
    let maybe_id = TweetId::parse(line.to_owned());
    match maybe_id {
        Ok(twid) => {
            // tweeter.to_twitter_tweet_id(twid)...
            if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self
                let result = match tweeter.current_profile() {
                    Some(user_profile) => queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id), &tweeter.app_key, &user_profile.creds),
                    None => Err("No logged in user to fav from".to_owned())
                };
                match result {
                    Ok(_) => (),
                    Err(e) => tweeter.display_info.status(e)
                }
            } else {
                tweeter.display_info.status(format!("No tweet for id: {:?}", twid));
            }
        }
        Err(e) => {
            tweeter.display_info.status(format!("Invalid id: {}", e));
        }
    }
}