diff --git a/src/lemmy/mod.rs b/src/lemmy.rs similarity index 61% rename from src/lemmy/mod.rs rename to src/lemmy.rs index 8265942..38148b2 100644 --- a/src/lemmy/mod.rs +++ b/src/lemmy.rs @@ -8,49 +8,31 @@ use lemmy_db_schema::newtypes::{CommunityId, PostId}; use lemmy_db_schema::{ListingType, PostFeatureType}; use reqwest::StatusCode; use crate::config::Config; -use crate::HTTP_CLIENT; - -pub(crate) struct Credentials { - username: String, - password: String -} - -impl Credentials { - pub(crate) fn get_username(&self) -> Sensitive { - Sensitive::new(self.username.clone()) - } - - pub(crate) fn get_password(&self) -> Sensitive { - Sensitive::new(self.password.clone()) - } - - pub(crate) fn set_credentials(config: &Config) -> Self { - Self { - username: config.username.clone(), - password: config.password.clone(), - } - } -} +use crate::{HTTP_CLIENT, write_error}; pub(crate) struct Lemmy { jwt_token: Sensitive, instance: String, } -pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result { +pub(crate) async fn login(config: &Config) -> Result { let login_params = Login { - username_or_email: credentials.get_username(), - password: credentials.get_password(), + username_or_email: config.get_username(), + password: config.get_password(), totp_2fa_token: None, }; let response = match HTTP_CLIENT - .post(instance.to_owned() + "/api/v3/user/login") + .post(config.instance.to_owned() + "/api/v3/user/login") .json(&login_params) .send() .await { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; match response.status() { @@ -59,17 +41,25 @@ pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result Ok(Lemmy { jwt_token: token.clone(), - instance: instance.to_owned(), + instance: config.instance.to_owned(), }), - None => panic!("Login did not return JWT token. Are the credentials valid?") + None => { + let err_msg = "Login did not return JWT token. Are the credentials valid?".to_owned(); + write_error(err_msg); + return Err(()) + } } }, - status => panic!("Unexpected HTTP Status '{}' during Login", status) + status => { + let err_msg = format!("Unexpected HTTP Status '{}' during Login", status); + write_error(err_msg); + return Err(()) + } } } impl Lemmy { - pub(crate) async fn post(&self, post: CreatePost) -> Result { + pub(crate) async fn post(&self, post: CreatePost) -> Result { let response = match HTTP_CLIENT .post(format!("{}/api/v3/post", &self.instance)) .bearer_auth(&self.jwt_token.to_string()) @@ -79,21 +69,33 @@ impl Lemmy { Ok(data) => { match data.text().await { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } } }, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; let json_data = match serde_json::from_str::>(&response) { Ok(mut data) => data.remove("post_view").expect("Element should be present"), - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; Ok(json_data.post.id) } - async fn feature(&self, params: FeaturePost) -> Result { + async fn feature(&self, params: FeaturePost) -> Result { let response = match HTTP_CLIENT .post(format!("{}/api/v3/post/feature", &self.instance)) .bearer_auth(&self.jwt_token.to_string()) @@ -103,21 +105,33 @@ impl Lemmy { Ok(data) => { match data.text().await { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } } }, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; let json_data = match serde_json::from_str::>(&response) { Ok(mut data) => data.remove("post_view").expect("Element should be present"), - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; Ok(json_data) } - pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result { + pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result { let pin_params = FeaturePost { post_id, featured: false, @@ -126,7 +140,7 @@ impl Lemmy { self.feature(pin_params).await } - pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result { + pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result { let pin_params = FeaturePost { post_id, featured: true, @@ -135,7 +149,7 @@ impl Lemmy { self.feature(pin_params).await } - pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result, String> { + pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result, ()> { let list_params = GetPosts { community_id: Some(community), type_: Some(ListingType::Local), @@ -151,15 +165,27 @@ impl Lemmy { Ok(data) => { match data.text().await { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } } }, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; let json_data: GetPostsResponse = match serde_json::from_str(&response) { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; Ok(json_data.posts.iter().filter(|post| { @@ -170,7 +196,7 @@ impl Lemmy { ) } - pub(crate) async fn get_local_pinned(&self) -> Result, String> { + pub(crate) async fn get_local_pinned(&self) -> Result, ()> { let list_params = GetPosts { type_: Some(ListingType::Local), ..Default::default() @@ -185,15 +211,27 @@ impl Lemmy { Ok(data) => { match data.text().await { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } } }, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; let json_data: GetPostsResponse = match serde_json::from_str(&response) { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; Ok(json_data.posts.iter().filter(|post| { @@ -204,7 +242,7 @@ impl Lemmy { ) } - pub(crate) async fn get_communities(&self) -> Result, String> { + pub(crate) async fn get_communities(&self) -> Result, ()> { let list_params = ListCommunities { type_: Some(ListingType::Local), ..Default::default() @@ -219,15 +257,27 @@ impl Lemmy { Ok(data) => { match data.text().await { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } } }, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; let json_data: ListCommunitiesResponse = match serde_json::from_str(&response) { Ok(data) => data, - Err(e) => return Err(format!("{}", e)) + Err(e) => { + let err_msg = format!("{e}"); + write_error(err_msg); + return Err(()) + } }; let mut communities: HashMap = HashMap::new();