use crate::{
auth::Auth,
client::{route::Route, Client},
error::Error,
model::{
award::Award, comment::Comment, misc::Fullname, misc::Params, subreddit::Subreddit,
user::User,
},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Link {
pub title: String,
pub author: String,
pub score: i64,
pub num_comments: u64,
pub subreddit: String,
pub name: Fullname,
pub all_awardings: Vec<Award>,
}
impl Link {
pub async fn author<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<User, Error> {
client.user(&self.author).await
}
pub async fn subreddit<T: Auth + Send + Sync>(
&self,
client: &Client<T>,
) -> Result<Subreddit, Error> {
client.subreddit(&self.subreddit).await
}
pub async fn replies<T: Auth + Send + Sync>(
&self,
_client: &Client<T>,
) -> Result<Vec<Comment>, Error> {
todo!()
}
pub async fn reply<T: Auth + Send + Sync>(
&self,
client: &Client<T>,
body: &str,
) -> Result<(), Error> {
client.submit_comment(self.name.as_ref(), body).await
}
pub async fn spoiler<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(Route::Spoiler, &Params::new().add("id", self.name.as_ref()))
.await
.and(Ok(()))
}
pub async fn unspoiler<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(
Route::Unspoiler,
&Params::new().add("id", self.name.as_ref()),
)
.await
.and(Ok(()))
}
pub async fn set_nsfw<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(Route::SetNSFW, &Params::new().add("id", self.name.as_ref()))
.await
.and(Ok(()))
}
pub async fn unset_nsfw<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(
Route::UnsetNSFW,
&Params::new().add("id", self.name.as_ref()),
)
.await
.and(Ok(()))
}
pub async fn lock<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(Route::Lock, &Params::new().add("id", self.name.as_ref()))
.await
.and(Ok(()))
}
pub async fn unlock<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(Route::Unlock, &Params::new().add("id", self.name.as_ref()))
.await
.and(Ok(()))
}
pub async fn follow<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(
Route::Follow,
&Params::new()
.add("id", self.name.as_ref())
.add("follow", "1"),
)
.await
.and(Ok(()))
}
pub async fn unfollow<T: Auth + Send + Sync>(&self, client: &Client<T>) -> Result<(), Error> {
client
.post(
Route::Follow,
&Params::new()
.add("id", self.name.as_ref())
.add("follow", "0"),
)
.await
.and(Ok(()))
}
}