aboutsummaryrefslogtreecommitdiff
path: root/src/commands/thread.rs
blob: 71d0e5213a1f1b19b5b5c40d88a5428ad49b3c9c (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
use tw;
use ::Queryer;
use ::display;

use tw::TweetId;

use commands::Command;

pub static FORGET_THREAD: Command = Command {
    keyword: "forget",
    params: 1,
    exec: forget,
    param_str: " <name>",
    help_str: "Discard thread known by <name>. Entirely local to the client."
};

fn forget(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) {
    tweeter.forget_thread(line.trim().to_string());
    tweeter.display_info.status(format!("Ok! Forgot thread {}", line.trim().to_string()));
}

pub static REMEMBER_THREAD: Command = Command {
    keyword: "remember",
    params: 2,
    exec: remember,
    param_str: " <tweet_id> <name>",
    help_str: "Remember the thread tipped by <tweet_id> as  <name>. Entirely local to the client."
};

fn remember(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) {
    let mut text: String = line.trim().to_string();
    if let Some(id_end_idx) = text.find(" ") {
        let name_bare = text.split_off(id_end_idx + 1);
        let name = name_bare.trim();
        let id_str = text.trim();
        if name.len() > 0 {
            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()) {
                        tweeter.set_thread(name.to_string(), twete.internal_id);
                        tweeter.display_info.status(format!("Ok! Recorded {:?} as thread {}", twid, name));
                    } else {
                        tweeter.display_info.status(format!("No tweet for id: {:?}", twid));
                    }
                }
                Err(e) => {
                    tweeter.display_info.status(format!("Invalid id: {}", e));
                }
            }
        }
    }
}

pub static LIST_THREADS: Command = Command {
    keyword: "ls_threads",
    params: 0,
    exec: ls_threads,
    param_str: "",
    help_str: "Show all known (local) threads"
};

fn ls_threads(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) {
    let threads: Vec<String> = tweeter.threads().collect::<Vec<&String>>().into_iter().map(|x| x.to_owned()).collect::<Vec<String>>();
    for k in threads {
        let latest_inner_id = tweeter.latest_in_thread(k.to_owned()).unwrap().to_owned();
        tweeter.display_info.recv(display::Infos::TweetWithContext(TweetId::Bare(latest_inner_id), format!("Thread: {}", k)))
    }
}