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
//! This module contains the functions used only by the `start-daemon` subsystem.
//!
//! The flow of the `start-daemon` subsystem is as follows:
//!
//! Initialisation:
//!
//! ```plaintext
//! Options::parse()
//! |> ops::start_daemon::verify()
//! |> ops::AppTokens::read()
//! ```
//!
//! Then, in a loop:
//!
//! ```plaintext
//! init_data
//! |> ops::User::read()
//! |> ops::QueuedTweet::read()
//! |> ops::start_daemon::tweet_indices_to_post()
//! |> ops::start_daemon::find_user_index_for_tweet()
//! |> ops::start_daemon::post_tweet()
//! ```


use self::super::super::util::{TWEET_DATETIME_FORMAT, span_r};
use self::super::{QueuedTweet, User, verify_file};
use self::super::super::Outcome;
use egg_mode::tweet::DraftTweet;
use chrono::{DateTime, Local};
use std::path::PathBuf;
use egg_mode::Token;
use std::io::Write;


/// Verify if, given the current configuration, it's permitted to continue with the subsequent steps of the `start-daemon`
/// subsystem.
///
/// The return value contains either the path to the file containing the global app configuration, the path to the file
/// containing the global users data and the path to the file containing the global queued tweets data or why getting them
/// failed.
///
/// # Examples
///
/// Verifying with everything existing.
///
/// ```
/// # use std::fs::{self, File};
/// # use tweetr::ops::start_daemon;
/// # use std::env::temp_dir;
/// # use tweetr::Outcome;
/// # use std::io::Write;
/// let tf = temp_dir().join("tweetr-doctest").join("ops-start-daemon-verify-0");
/// fs::create_dir_all(&tf).unwrap();
/// File::create(tf.join("app.toml")).unwrap().write(&[]).unwrap();
/// File::create(tf.join("users.toml")).unwrap().write(&[]).unwrap();
/// File::create(tf.join("tweets.toml")).unwrap().write(&[]).unwrap();
///
/// assert_eq!(start_daemon::verify(&("$TEMP/ops-start-daemon-verify-0".to_string(), tf.clone())),
///            Ok((tf.join("app.toml"), tf.join("users.toml"), tf.join("tweets.toml"))));
/// ```
///
/// Verifying with users data nonexistant.
///
/// ```
/// # use std::fs::{self, File};
/// # use tweetr::ops::start_daemon;
/// # use std::env::temp_dir;
/// # use tweetr::Outcome;
/// # use std::io::Write;
/// let tf = temp_dir().join("tweetr-doctest").join("ops-start-daemon-verify-1");
/// fs::create_dir_all(&tf).unwrap();
/// File::create(tf.join("app.toml")).unwrap().write(&[]).unwrap();
/// File::create(tf.join("tweets.toml")).unwrap().write(&[]).unwrap();
///
/// assert_eq!(start_daemon::verify(&("$TEMP/ops-start-daemon-verify-1".to_string(), tf)),
///            Err(Outcome::RequiredFileFromSubsystemNonexistant {
///                subsys: "add-user",
///                fname: "$TEMP/ops-start-daemon-verify-1/users.toml".to_string(),
///            }));
/// ```
pub fn verify(config_dir: &(String, PathBuf)) -> Result<(PathBuf, PathBuf, PathBuf), Outcome> {
    let app = try!(verify_file("app.toml", true, config_dir, false, "init"));
    let users = try!(verify_file("users.toml", true, config_dir, false, "add-user"));
    let tweets = try!(verify_file("tweets.toml", true, config_dir, false, "queue-tweet"));

    Ok((app, users, tweets))
}

/// Get the indices of tweets to post now from the provided batch based on whether thy've been posted already and the current
/// time.
///
/// All returned indices are guaranteed to be valid.
///
/// # Examples
///
/// ```
/// # extern crate tweetr;
/// # extern crate chrono;
/// # use tweetr::ops::{QueuedTweet, start_daemon};
/// # use chrono::{Duration, Local};
/// # fn main() {
/// let now = Local::now();
/// let now = now.with_timezone(now.offset());
///
/// assert_eq!(start_daemon::tweet_indices_to_post(&vec![
///     QueuedTweet {
///         author: "nabijaczleweli".to_string(),
///         time: now + Duration::hours(1),
///         content: "This tweet is not going to be posted (it's too early)".to_string(),
///         time_posted: None,
///         id: None,
///     },
///     QueuedTweet {
///         author: "nabijaczleweli".to_string(),
///         time: now - Duration::hours(1),
///         content: "This tweet is going to be posted".to_string(),
///         time_posted: None,
///         id: None,
///     },
///     QueuedTweet {
///         author: "nabijaczleweli".to_string(),
///         time: now - Duration::hours(1),
///         content: "This tweet is not going to be posted (it already was)".to_string(),
///         time_posted: Some(now - Duration::minutes(30)),
///         id: Some(6908265),
///     },
/// ]), vec![1]);
/// # }
/// ```
pub fn tweet_indices_to_post(tweets: &Vec<QueuedTweet>) -> Vec<usize> {
    let now = Local::now();
    let now = now.with_timezone(now.offset());

    tweets.iter()
        .enumerate()
        .flat_map(|(i, ref t)| if t.id.is_none() && t.time <= now {
            Some(i)
        } else {
            None
        })
        .collect()
}

/// Try to get the index of the user to post the given tweet.
///
/// This will fail iff there's no suitable user.
///
/// The returned index guaranteed to be valid.
///
/// # Examples
///
/// Finding a non-existant user:
///
/// ```
/// # extern crate tweetr;
/// # extern crate chrono;
/// # use tweetr::ops::{QueuedTweet, User, start_daemon};
/// # use chrono::{Duration, Local};
/// # fn main() {
/// let now = Local::now();
/// let now = now.with_timezone(now.offset());
///
/// let tweet = QueuedTweet {
///     author: "nabijaczleweli".to_string(),
///     time: now,
///     content: "dummy".to_string(),
///     time_posted: None,
///     id: None,
/// };
///
/// assert!(start_daemon::find_user_index_for_tweet(&tweet, &vec![]).is_err());
/// assert!(start_daemon::find_user_index_for_tweet(&tweet, &vec![User {
///     name: "danerangLP".to_string(),
///     id: 0x4208142311,
///     access_token_key: "key".to_string(),
///     access_token_secret: "secret".to_string(),
/// }]).is_err());
/// # }
/// ```
///
/// Finding am existing user:
///
/// ```
/// # extern crate tweetr;
/// # extern crate chrono;
/// # use tweetr::ops::{QueuedTweet, User, start_daemon};
/// # use chrono::{Duration, Local};
/// # fn main() {
/// let now = Local::now();
/// let now = now.with_timezone(now.offset());
///
/// assert_eq!(start_daemon::find_user_index_for_tweet(&QueuedTweet {
///     author: "danerangLP".to_string(),
///     time: now,
///     content: "dummy".to_string(),
///     time_posted: None,
///     id: None,
/// }, &vec![User {
///     name: "danerangLP".to_string(),
///     id: 0x4208142311,
///     access_token_key: "key".to_string(),
///     access_token_secret: "secret".to_string(),
/// }]), Ok(0));
/// # }
/// ```
pub fn find_user_index_for_tweet(tweet: &QueuedTweet, users: &Vec<User>) -> Result<usize, Outcome> {
    match users.iter().enumerate().find(|&iu| iu.1.name == tweet.author).map(|iu| iu.0) {
        Some(uid) => Ok(uid),
        None => {
            Err(Outcome::RequiredDataFromSubsystemNonexistant {
                subsys: "add-user",
                desc: format!("add and authorise user with name \"{}\" (required for tweet \"{}\" scheduled for {:?})",
                              tweet.author,
                              tweet.content,
                              tweet.time),
            })
        }
    }
}

/// Post the specified tweet on behalf of the specified user and application, optionally printing progress.
///
/// The tweet is updated with the data returned by the Twitter API.
///
/// # Examples
///
/// ```no_run
/// # extern crate tweetr;
/// # extern crate chrono;
/// # use tweetr::ops::{QueuedTweet, AppTokens, User, start_daemon};
/// # use chrono::{Duration, Local};
/// # fn main() {
/// let now = Local::now();
/// let now = now.with_timezone(now.offset());
///
/// let mut tweet = QueuedTweet {
///     author: "nabijaczleweli".to_string(),
///     time: now,
///     content: "This tweet will be posted, no matter the cost!".to_string(),
///     time_posted: None,
///     id: None,
/// };
///
/// let result = start_daemon::post_tweet(&mut tweet, &User {
///     name: "nabijaczleweli".to_string(),
///     id: 0x81423,
///     access_token_key: "529443-FNlJkpZCE7a4Bbd7f1k65GtgaH7SmHlReWSESD4".to_string(),
///     access_token_secret: "GVQDq88qLtJ45KR6u44A6AljW31JSSippjdipQg6gPYE5".to_string(),
/// }, &AppTokens {
///     key: "qzuqpwr101q4RtK9mDorI9ndm".to_string(),
///     secret: "HW4YG3Kdcap5ovcZ5fZfBJFedKR6GQe9MtZDS9Gm34hXiirkU5".to_string(),
/// }.into(), false, &mut vec![]);
///
/// assert_eq!(result.exit_value(), 0);
/// assert!(tweet.time_posted.is_some());
/// assert!(tweet.id.is_some());
/// # }
/// ```
pub fn post_tweet<'a, W: Write>(tweet: &mut QueuedTweet, on_behalf_of: &User, app: &Token<'a>, verbose: bool, output: &mut W) -> Outcome {
    if verbose {
        write!(output, "Posting tweet scheduled for {:?}...", tweet.time).unwrap();
        output.flush().unwrap();
    }

    match span_r(|| DraftTweet::new(&tweet.content).send(app, &Token::new(&on_behalf_of.access_token_key[..], &on_behalf_of.access_token_secret[..]))) {
        (dur, Ok(resp)) => {
            if verbose {
                writeln!(output, " {}ms", dur.num_milliseconds()).unwrap();
            }

            tweet.time_posted = Some(DateTime::parse_from_str(&resp.response.created_at, TWEET_DATETIME_FORMAT).unwrap());
            tweet.id = Some(resp.response.id);

            writeln!(output,
                     "Posted tweet \"{}\" scheduled for {:?} by {} at {:?} with ID {}",
                     tweet.content,
                     tweet.time,
                     tweet.author,
                     tweet.time_posted.as_ref().unwrap(),
                     resp.response.id)
                .unwrap();

            Outcome::NoError
        }
        (_, Err(e)) => {
            if verbose {
                writeln!(output, " FAILED").unwrap();
            }
            Outcome::TwitterAPIError(e.to_string())
        }
    }
}