rawr 0.1.0

Rust API Wrapper for Reddit
Documentation
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
use traits::{Votable, Created, Editable, Content, Commentable, Stickable, Lockable, Flairable,
             Reportable, Visible, Distinguishable};
use structures::comment_list::{CommentList, CommentStream};
use structures::user::User;
use structures::subreddit::Subreddit;
use structures::listing::Listing;
use client::RedditClient;
use responses::listing;
use responses::{FlairChoice, FlairSelectorResponse};
use errors::APIError;

/// Structure representing a link post or self post (a submission) on Reddit.
pub struct Submission<'a> {
    data: listing::Submission,
    client: &'a RedditClient,
}

impl<'a> Eq for Submission<'a> {}

impl<'a> PartialEq for Submission<'a> {
    fn eq(&self, other: &Submission) -> bool {
        self.name() == other.name()
    }
}


impl<'a> Votable for Submission<'a> {
    fn score(&self) -> i64 {
        self.data.score
    }

    fn likes(&self) -> Option<bool> {
        self.data.likes
    }

    fn upvote(&self) -> Result<(), APIError> {
        self.vote(1)
    }

    fn downvote(&self) -> Result<(), APIError> {
        self.vote(-1)
    }

    fn cancel_vote(&self) -> Result<(), APIError> {
        self.vote(0)
    }
}

impl<'a> Created for Submission<'a> {
    fn created(&self) -> i64 {
        self.data.created
    }

    fn created_utc(&self) -> i64 {
        self.data.created_utc
    }
}

impl<'a> Editable for Submission<'a> {
    fn edited(&self) -> bool {
        match self.data.edited.as_boolean() {
            Some(edited) => edited,
            None => true,
        }
    }

    fn edited_time(&self) -> Option<i64> {
        self.data.edited.as_i64()
    }

    fn edit(&mut self, text: &str) -> Result<(), APIError> {
        let body = format!("api_type=json&text={}&thing_id={}", self.client.url_escape(text.to_owned()), self.data.name);
        let res = self.client.post_success("/api/editusertext", &body, false);
        if let Ok(()) = res {
            // TODO: should we update selftext_html?
            self.data.selftext = text.to_owned();
        }
        res
    }

    fn body(&self) -> Option<String> {
        let self_text = self.data.selftext.to_owned();
        if self_text.is_empty() {
            None
        } else {
            Some(self_text)
        }
    }

    fn body_html(&self) -> Option<String> {
        self.data.selftext_html.to_owned()
    }
}

impl<'a> Content for Submission<'a> {
    fn author(&self) -> User {
        User::new(self.client, &self.data.author)
    }

    fn author_flair_text(&self) -> Option<String> {
        self.data.author_flair_text.to_owned()
    }

    fn author_flair_css(&self) -> Option<String> {
        self.data.author_flair_css_class.to_owned()
    }

    fn subreddit(&self) -> Subreddit {
        Subreddit::create_new(self.client, &self.data.subreddit)
    }

    fn delete(self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        self.client.post_success("/api/del", &body, false)
    }

    fn name(&self) -> &str {
        &self.data.name
    }
}

impl<'a> Commentable<'a> for Submission<'a> {
    fn reply_count(&self) -> u64 {
        self.data.num_comments
    }

    fn reply(&self, text: &str) -> Result<(), APIError> {
        // TODO: url escape properly
        let body = format!("api_type=json&text={}&thing_id={}", self.client.url_escape(text.to_owned()), self.name());
        self.client.post_success("/api/comment", &body, false)
    }

    fn replies(self) -> Result<CommentList<'a>, APIError> {
        // TODO: sort type
        let url = format!("/comments/{}", self.data.id);
        self.client.get_json::<listing::CommentResponse>(&url, false)
            .and_then(|res| Ok(CommentList::new(self.client, self.data.name.to_owned(), self.data.name.to_owned(), res.1.data.children)))
    }
}

impl<'a> Submission<'a> {
    /// Internal method. Get submissions from a listing instead (see `Subreddit.hot()` etc.)
    pub fn new(client: &RedditClient, data: listing::Submission) -> Submission {
        Submission {
            client: client,
            data: data
        }
    }

    /// Returns a `CommentStream` that fetches the latest comments in an infinite loop and returns
    /// it from the iterator. Comments will be ordered from oldest to newest, with up to 5 comments
    /// that exist being yielded at a time. This will poll the API every 5 seconds for updates.
    /// # Examples
    /// ```rust,no_run
    /// use rawr::prelude::*;
    /// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
    /// let sub = client.subreddit("all");
    /// let mut listing = sub.hot(ListingOptions::default()).expect("Could not fetch listing!");
    /// let post = listing.nth(0).unwrap();
    /// for comment in post.reply_stream() {
    ///     println!("New comment received!");
    /// }
    pub fn reply_stream(self) -> CommentStream<'a> {
        CommentStream::new(self.client, self.data.name, self.data.id)
    }

    /// The title of the post (as an &str). All link and self posts have a title, and any post
    /// flairs are not included in this.
    pub fn title(&self) -> &str {
        &self.data.title
    }

    /// This is `true` if the post is a self post, and `false` if it is a link post.
    pub fn is_self_post(&self) -> bool {
        self.data.is_self
    }

    /// Gets the URL linked to by this link post (or `None`, if this is a self post)
    pub fn link_url(&self) -> Option<String> {
        self.data.url.to_owned()
    }

    /// Returns `true` if the post is marked NSFW (over 18).
    pub fn nsfw(&self) -> bool {
        self.data.over_18
    }

    /// Sets the post as NSFW (over 18) if you have the correct privileges (owner of the post or
    /// moderator) **and** the subreddit allows NSFW posts.
    pub fn mark_nsfw(&mut self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        let res = self.client.post_success("/api/marknsfw", &body, false);

        if let Ok(_) = res {
            self.data.over_18 = true;
        }

        res
    }

    /// Sets the post as **not** NSFW (over 18).
    pub fn unmark_nsfw(&mut self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        let res = self.client.post_success("/api/unmarknsfw", &body, false);

        if let Ok(_) = res {
            self.data.over_18 = false;
        }

        res
    }

    fn vote(&self, dir: i8) -> Result<(), APIError> {
        let body = format!("dir={}&id={}", dir, self.data.name);
        self.client.post_success("/api/vote", &body, false)
    }
}

impl<'a> Stickable for Submission<'a> {
    /// This is `true` if the post is stickied (an announcement post).
    fn stickied(&self) -> bool {
        self.data.stickied
    }

    fn stick(&mut self) -> Result<(), APIError> {
        let body = format!("api_type=json&id={}&state=true", self.data.name);
        let res = self.client.post_success("/api/set_subreddit_sticky", &body, false);

        if let Ok(_) = res {
            self.data.stickied = true;
        }

        res
    }

    fn unstick(&mut self) -> Result<(), APIError> {
        let body = format!("api_type=json&id={}&state=false", self.data.name);
        let res = self.client.post_success("/api/set_subreddit_sticky", &body, false);

        if let Ok(_) = res {
            self.data.stickied = false;
        }

        res
    }
}

impl<'a> Lockable for Submission<'a>  {
    fn locked(&self) -> bool {
        self.data.locked
    }

    fn lock(&mut self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        let res = self.client.post_success("/api/lock", &body, false);

        if let Ok(_) = res {
            self.data.locked = true;
        }

        res
    }

    fn unlock(&mut self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        let res = self.client.post_success("/api/unlock", &body, false);

        if let Ok(_) = res {
            self.data.locked = false;
        }

        res
    }
}

impl<'a> Reportable for Submission<'a> {
    fn report(&self, reason: &str) -> Result<(), APIError> {
        let body = format!("api_type=json&thing_id={}&reason={}", self.data.name, self.client.url_escape(reason.to_owned()));
        self.client.post_success("/api/report", &body, false)
    }

    fn report_count(&self) -> Option<u64> {
        self.data.num_reports.to_owned()
    }
}

impl<'a> Distinguishable for Submission<'a> {
    fn distinguished(&self) -> Option<String> {
        self.data.distinguished.to_owned()
    }

    fn distinguish(&mut self) -> Result<(), APIError> {
        let body = format!("api_type=json&how=yes&id={}", self.data.name);
        let res = self.client.post_success("/api/distinguish", &body, false);
        if let Ok(()) = res {
            self.data.distinguished = Some(String::from("moderator"));
        }
        res
    }

    fn undistinguish(&mut self) -> Result<(), APIError> {
        let body = format!("api_type=json&how=no&id={}", self.data.name);
        let res = self.client.post_success("/api/distinguish", &body, false);
        if let Ok(()) = res {
            self.data.distinguished = None;
        }
        res
    }
}

impl<'a> Flairable for Submission<'a> {
    fn get_flair_text(&self) -> Option<String> {
        self.data.link_flair_text.to_owned()
    }

    fn get_flair_css(&self) -> Option<String> {
        self.data.link_flair_css_class.to_owned()
    }

    fn flair_options(&self) -> Result<FlairList, APIError> {
        let body = format!("link={}", self.data.name);
        let url = format!("/r/{}/api/flairselector", self.data.subreddit);
        self.client.post_json::<FlairSelectorResponse>(&url, &body, false)
            .and_then(|res| Ok(FlairList::new(res.choices)))
    }

    fn flair(&self, template: &str) -> Result<(), APIError> {
        let body = format!("api_type=json&link={}&flair_template_id={}", self.data.name, template);
        let url = format!("/r/{}/api/selectflair", self.data.subreddit);
        self.client.post_success(&url, &body, false)
    }
}

impl<'a> Visible for Submission<'a> {
    fn hidden(&self) -> bool {
        self.data.hidden
    }

    fn hide(&mut self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        let res = self.client.post_success("/api/hide", &body, false);

        if let Ok(_) = res {
            self.data.hidden = true;
        }

        res
    }

    fn show(&mut self) -> Result<(), APIError> {
        let body = format!("id={}", self.data.name);
        let res = self.client.post_success("/api/unhide", &body, false);

        if let Ok(_) = res {
            self.data.hidden = false;
        }

        res
    }
}

/// A list of flairs that can be assigned to a post. To access the complete list, use
/// `FlairList.flairs`, which is a list of `FlairChoice` objects.
pub struct FlairList {
    /// The list of flairs available.
    pub flairs: Vec<FlairChoice>
}

impl FlairList {
    /// Creates a `FlairList` from a vector of `FlairChoice` objects.
    pub fn new(choices: Vec<FlairChoice>) -> FlairList {
        FlairList {
            flairs: choices
        }
    }

    /// Finds the flair with the specified text, consuming the `FlairList`.
    /// # Examples
    /// ```rust,no_run
    /// use rawr::client::RedditClient;
    /// use rawr::auth::PasswordAuthenticator;
    /// use rawr::options::ListingOptions;
    /// use rawr::traits::Flairable;
    /// let client = RedditClient::new("rawr", PasswordAuthenticator::new("a", "b", "c", "d"));
    /// let sub = client.subreddit("learnprogramming");
    /// let post = sub.hot(ListingOptions::default()).unwrap().next().unwrap();
    /// // NOTE: this would 403 unless you are a moderator or the creator of the post.
    /// let tutorial_flair = post.flair_options().unwrap().find_text("tutorial").unwrap();
    /// post.flair(&tutorial_flair);
    /// ```
    pub fn find_text(self, text: &str) -> Option<String> {
        for flair in self.flairs {
            if flair.flair_text == text {
                return Some(flair.flair_template_id);
            }
        }

        None
    }

}

/// A lazy object representing a submission. Used by the `Client.get_by_id()` method until the
/// data is specified by the user (we don't know if they want the `Submission` or `CommentList`
/// yet). The `LazySubmission` object is consumed when performing either of these actions.
pub struct LazySubmission<'a> {
    id: String,
    client: &'a RedditClient
}

impl<'a> LazySubmission<'a> {
    /// Internal method. Use `Client.get_by_id()` instead.
    pub fn new(client: &'a RedditClient, id: &str) -> LazySubmission<'a> {
        LazySubmission {
            client: client,
            id: id.to_owned()
        }
    }

    /// Fetches the `Submission` with this ID, in order to access post title, body, link and
    /// creation time.
    pub fn get(self) -> Result<Submission<'a>, APIError> {
        let url = format!("/by_id/{}", self.id);
        let listing = self.client.get_json::<listing::Listing>(&url, false)
            .and_then(|res| Ok(Listing::new(self.client, url, res.data)));
        Ok(try!(listing).nth(0).unwrap())
    }

    /// Fetches a `CommentList` with replies to this submission.
    pub fn replies(self) -> Result<CommentList<'a>, APIError> {
        let url = format!("/comments/{}", self.id.split('_').nth(1).unwrap());
        self.client.get_json::<listing::CommentResponse>(&url, false)
            .and_then(|res| Ok(CommentList::new(self.client, self.id.to_owned(), self.id.to_owned(), res.1.data.children)))
    }
}