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
use std::fmt;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::sync::{Arc};

use reqwest::{Body, Client, Response};
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use serde::{de, Deserialize, Deserializer};
use serde::de::DeserializeOwned;

use crate::auth::Authenticator;
use crate::message::Inbox;
use crate::responses::subreddit::Subreddits;
use crate::responses::user::Users;
use crate::subreddit::Subreddit;
use crate::user::User;
use crate::utils::error::APIError;
use crate::utils::options::FeedOption;
use tokio::sync::{Mutex, MutexGuard};

/// This is who you are. This is your identity and you access point to the Reddit API

pub struct Me {
    auth: Arc<Mutex<Box<dyn Authenticator+ Send>>>,
    client: Client,
    user_agent: String,
    pub oauth: bool
}

impl Me {
    /// Logs into Reddit and Returns a Me
    pub async fn login(
        auth: Arc<Mutex<Box<dyn Authenticator+ Send>>>,
        user_agent: String,
    ) -> Result<Me, APIError> {
        let client = Client::new();
        let arc = auth.clone();
        let mut guard = arc.lock().await;
        let b = guard.oauth();
        let _x = guard.login(&client, &user_agent).await;
        Ok(Me {
            auth,
            client,
            user_agent,
            oauth:b
        })
    }
    /// Gets the authenticator. Internal use
    pub async fn get_authenticator(&self) -> MutexGuard<'_, Box<dyn Authenticator + 'static+ Send>> {
        self.auth.lock().await
    }
    /// Creates a subreddit object. However, this will not tell you if the user exists.
    pub fn subreddit(&self, name: String) -> Subreddit {
        Subreddit { me: self, name }
    }
    /// Inbox
    pub fn inbox(&self) -> Inbox {
        Inbox { me: self }
    }
    /// Creates a user object. However, this will not tell you if the user exists.
    pub fn user(&self, name: String) -> User {
        User { me: self, name }
    }
    /// Makes a get request with Reqwest response
    pub async fn get(&self, url: &str, oauth: bool) -> Result<Response, reqwest::Error> {
        let mut guard = self.get_authenticator().await;
        if guard.needs_token_refresh() {
            guard.login(&self.client, self.user_agent.as_str()).await;
        }
        let string = self.build_url(url, oauth);
        let mut headers = HeaderMap::new();
        headers.insert(
            USER_AGENT,
            HeaderValue::from_str(&*self.user_agent).unwrap(),
        );
        guard.headers(&mut headers);
        self.client.get(string).headers(headers).send().await
    }
    /// Makes a post request with Reqwest response
    pub async fn post(
        &self,
        url: &str,
        oauth: bool,
        body: Body,
    ) -> Result<Response, reqwest::Error> {
        let mut guard = self.get_authenticator().await;
        if guard.needs_token_refresh() {
            guard.login(&self.client, self.user_agent.as_str()).await;
        }
        let string = self.build_url(url, oauth);
        let mut headers = HeaderMap::new();
        headers.insert(
            USER_AGENT,
            HeaderValue::from_str(&*self.user_agent).unwrap(),
        );
        guard.headers(&mut headers);
        self.client
            .post(string)
            .body(body)
            .headers(headers)
            .send()
            .await
    }
    /// Makes a get request with JSON response
    pub async fn get_json<T: DeserializeOwned>(
        &self,
        url: &str,
        oauth: bool,
    ) -> Result<T, APIError> {
        let x = self.get(url, oauth).await;
        return Me::respond::<T>(x).await;
    }
    /// Makes a post request with JSON response
    pub async fn post_json<T: DeserializeOwned>(
        &self,
        url: &str,
        oauth: bool,
        body: Body,
    ) -> Result<T, APIError> {
        let x = self.post(url, oauth, body).await;
        return Me::respond::<T>(x).await;
    }
    /// Builds a URL
    pub fn build_url(&self, dest: &str, oauth_required: bool) -> String {
        let stem = if oauth_required {
            "https://oauth.reddit.com"
        } else {
            "https://api.reddit.com"
        };
        format!("{}{}", stem, dest)
    }
    /// Handles a Response from Reqwest mainly for internal use
    pub async fn respond<T: DeserializeOwned>(
        result: Result<Response, reqwest::Error>,
    ) -> Result<T, APIError> {
        if let Ok(response) = result {
            let code = response.status();
            if !code.is_success() {
                return Err(APIError::HTTPError(code));
            }

            let value = response.json::<T>().await;
            if let Ok(about) = value {
                return Ok(about);
            } else if let Err(response) = value {
                return Err(APIError::from(response));
            }
        } else if let Err(response) = result {
            return Err(APIError::from(response));
        }
        return Err(APIError::ExhaustedListing);
    }
    /// Searches Reddit for subreddits
    pub async fn search_subreddits(
        &self,
        name: String,
        limit: Option<u64>,
        feed: Option<FeedOption>,
    ) -> Result<Subreddits, APIError> {
        let mut url = format!("https://www.reddit.com/subreddits/search.json?q={}", name);
        if let Some(options) = feed {
            url.push_str(options.url().as_str());
        }
        if let Some(limit) = limit {
            url.push_str(&mut format!("&limit={}", limit));
        }
        self.get_json::<Subreddits>(&*url, false).await
    }
    /// Searches Reddit for Users
    pub async fn search_users(
        &self,
        name: String,
        limit: Option<u64>,
        feed: Option<FeedOption>,
    ) -> Result<Users, APIError> {
        let mut url = format!("https://www.reddit.com/users/search.json?q={}", name);
        if let Some(options) = feed {
            url.push_str(options.url().as_str());
        }
        if let Some(limit) = limit {
            url.push_str(&mut format!("&limit={}", limit));
        }
        self.get_json::<Users>(&*url, false).await
    }
}

#[derive(Debug)]
pub enum RedditType {
    Comment,
    Account,
    Link,
    Message,
    Subreddit,
    Award,
}

impl<'de> Deserialize<'de> for RedditType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        RedditType::from_str(s.as_str()).map_err(de::Error::custom)
    }
}

impl RedditType {
    pub fn get_id(&self) -> String {
        return match self {
            RedditType::Comment => "t1".to_string(),
            RedditType::Account => "t2".to_string(),
            RedditType::Link => "t3".to_string(),
            RedditType::Message => "t4".to_string(),
            RedditType::Subreddit => "t5".to_string(),
            RedditType::Award => "t6".to_string(),
        };
    }
}

impl FromStr for RedditType {
    type Err = APIError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "t1" => Ok(RedditType::Comment),
            "t2" => Ok(RedditType::Account),
            "t3" => Ok(RedditType::Link),
            "t4" => Ok(RedditType::Message),
            "t5" => Ok(RedditType::Subreddit),
            "t6" => Ok(RedditType::Message),
            _ => Err(APIError::Custom("Invalid RedditType".to_string())),
        }
    }
}

pub struct FullName {
    pub reddit_type: RedditType,
    pub id: String,
}

impl<'de> Deserialize<'de> for FullName {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        FullName::from_str(s.as_str()).map_err(de::Error::custom)
    }
}

impl FromStr for FullName {
    type Err = APIError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let split = s.split("_").collect::<Vec<&str>>();
        if split.len() == 1 {
            // Yes, it is always a good time to make a monty python joke.
            return Err(APIError::Custom("Then shalt thou count to two, no more, no less. Two shall be the number thou shalt count, and the number of the counting shall be two.".to_string()));
        }
        return Ok(FullName {
            reddit_type: RedditType::from_str(split.get(0).unwrap()).unwrap(),
            id: split.get(1).unwrap().to_string(),
        });
    }
}

impl Display for FullName {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}_{}", self.reddit_type.get_id(), self.id)
    }
}