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
//! # User
//! A read-only module to read data from for a specific user.
//!
//! # Usage
//! ```no_run
//! use roux::User;
//! use roux::util::FeedOption;
//! #[cfg(feature = "async")]
//! use tokio;
//!
//! #[cfg_attr(feature = "async", tokio::main)]
//! #[maybe_async::maybe_async]
//! async fn main() {
//!     let user = User::new("kasuporo");
//!     // Now you are able to:
//!
//!     // Get overview
//!     let overview = user.overview(None).await;
//!
//!     // Get submitted posts.
//!     let submitted = user.submitted(None).await;
//!
//!     // Get comments.
//!     let comments = user.comments(None).await;
//! }
//! ```

extern crate serde_json;

use crate::client::Client;
use crate::util::{FeedOption, RouxError};

use crate::models::{About, Comments, Overview, Submissions};

/// User.
pub struct User {
    /// User's name.
    pub user: String,
    client: Client,
}

impl User {
    /// Create a new `User` instance.
    pub fn new(user: &str) -> User {
        User {
            user: user.to_owned(),
            client: Client::new(),
        }
    }

    /// Get user's overview.
    #[maybe_async::maybe_async]
    pub async fn overview(&self, options: Option<FeedOption>) -> Result<Overview, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/overview/.json", self.user);

        if let Some(options) = options {
            options.build_url(url);
        }

        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<Overview>()
            .await?)
    }

    /// Get user's submitted posts.
    #[maybe_async::maybe_async]
    pub async fn submitted(&self, options: Option<FeedOption>) -> Result<Submissions, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/submitted/.json", self.user);

        if let Some(options) = options {
            options.build_url(url);
        }

        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<Submissions>()
            .await?)
    }

    /// Get user's submitted comments.
    #[maybe_async::maybe_async]
    pub async fn comments(&self, options: Option<FeedOption>) -> Result<Comments, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/comments/.json", self.user);

        if let Some(options) = options {
            options.build_url(url);
        }

        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<Comments>()
            .await?)
    }

    /// Get user's about page
    #[maybe_async::maybe_async]
    pub async fn about(&self, options: Option<FeedOption>) -> Result<About, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/about/.json", self.user);

        if let Some(options) = options {
            options.build_url(url);
        }

        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<About>()
            .await?)
    }
}

#[cfg(test)]
mod tests {
    use super::User;
    use crate::util::FeedOption;

    #[maybe_async::test(feature = "blocking", async(not(feature = "blocking"), tokio::test))]
    async fn test_no_auth() {
        let user = User::new("beneater");

        // Test overview
        let overview = user.overview(None).await;
        assert!(overview.is_ok());

        // Test submitted
        let submitted = user.submitted(None).await;
        assert!(submitted.is_ok());

        // Test comments
        let comments = user.comments(None).await;
        assert!(comments.is_ok());

        // Test about
        let about = user.about(None).await;
        assert!(about.is_ok());

        // Test feed options
        let after = comments.unwrap().data.after.unwrap();
        let after_options = FeedOption::new().after(&after);
        let next_comments = user.comments(Some(after_options)).await;
        assert!(next_comments.is_ok());
    }
}