Removes the TUI in favor of event based logging #15

Merged
Neshura merged 17 commits from remove_tui into main 2023-12-30 00:28:47 +00:00
Showing only changes of commit c0bff03120 - Show all commits

View file

@ -8,49 +8,31 @@ use lemmy_db_schema::newtypes::{CommunityId, PostId};
use lemmy_db_schema::{ListingType, PostFeatureType}; use lemmy_db_schema::{ListingType, PostFeatureType};
use reqwest::StatusCode; use reqwest::StatusCode;
use crate::config::Config; use crate::config::Config;
use crate::HTTP_CLIENT; use crate::{HTTP_CLIENT, write_error};
pub(crate) struct Credentials {
username: String,
password: String
}
impl Credentials {
pub(crate) fn get_username(&self) -> Sensitive<String> {
Sensitive::new(self.username.clone())
}
pub(crate) fn get_password(&self) -> Sensitive<String> {
Sensitive::new(self.password.clone())
}
pub(crate) fn set_credentials(config: &Config) -> Self {
Self {
username: config.username.clone(),
password: config.password.clone(),
}
}
}
pub(crate) struct Lemmy { pub(crate) struct Lemmy {
jwt_token: Sensitive<String>, jwt_token: Sensitive<String>,
instance: String, instance: String,
} }
pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result<Lemmy, String> { pub(crate) async fn login(config: &Config) -> Result<Lemmy, ()> {
let login_params = Login { let login_params = Login {
username_or_email: credentials.get_username(), username_or_email: config.get_username(),
password: credentials.get_password(), password: config.get_password(),
totp_2fa_token: None, totp_2fa_token: None,
}; };
let response = match HTTP_CLIENT 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) .json(&login_params)
.send() .send()
.await { .await {
Ok(data) => data, 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() { match response.status() {
@ -59,17 +41,25 @@ pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result<L
match data.jwt { match data.jwt {
Some(token) => Ok(Lemmy { Some(token) => Ok(Lemmy {
jwt_token: token.clone(), 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 { impl Lemmy {
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, String> { pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
let response = match HTTP_CLIENT let response = match HTTP_CLIENT
.post(format!("{}/api/v3/post", &self.instance)) .post(format!("{}/api/v3/post", &self.instance))
.bearer_auth(&self.jwt_token.to_string()) .bearer_auth(&self.jwt_token.to_string())
@ -79,21 +69,33 @@ impl Lemmy {
Ok(data) => { Ok(data) => {
match data.text().await { match data.text().await {
Ok(data) => data, 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::<HashMap<&str, PostView>>(&response) { let json_data = match serde_json::from_str::<HashMap<&str, PostView>>(&response) {
Ok(mut data) => data.remove("post_view").expect("Element should be present"), 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) Ok(json_data.post.id)
} }
async fn feature(&self, params: FeaturePost) -> Result<PostView, String> { async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
let response = match HTTP_CLIENT let response = match HTTP_CLIENT
.post(format!("{}/api/v3/post/feature", &self.instance)) .post(format!("{}/api/v3/post/feature", &self.instance))
.bearer_auth(&self.jwt_token.to_string()) .bearer_auth(&self.jwt_token.to_string())
@ -103,21 +105,33 @@ impl Lemmy {
Ok(data) => { Ok(data) => {
match data.text().await { match data.text().await {
Ok(data) => data, 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::<HashMap<&str, PostView>>(&response) { let json_data = match serde_json::from_str::<HashMap<&str, PostView>>(&response) {
Ok(mut data) => data.remove("post_view").expect("Element should be present"), 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) Ok(json_data)
} }
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, String> { pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
let pin_params = FeaturePost { let pin_params = FeaturePost {
post_id, post_id,
featured: false, featured: false,
@ -126,7 +140,7 @@ impl Lemmy {
self.feature(pin_params).await self.feature(pin_params).await
} }
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, String> { pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
let pin_params = FeaturePost { let pin_params = FeaturePost {
post_id, post_id,
featured: true, featured: true,
@ -135,7 +149,7 @@ impl Lemmy {
self.feature(pin_params).await self.feature(pin_params).await
} }
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<PostView>, String> { pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<PostView>, ()> {
let list_params = GetPosts { let list_params = GetPosts {
community_id: Some(community), community_id: Some(community),
type_: Some(ListingType::Local), type_: Some(ListingType::Local),
@ -151,15 +165,27 @@ impl Lemmy {
Ok(data) => { Ok(data) => {
match data.text().await { match data.text().await {
Ok(data) => data, 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) { let json_data: GetPostsResponse = match serde_json::from_str(&response) {
Ok(data) => data, 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| { Ok(json_data.posts.iter().filter(|post| {
@ -170,7 +196,7 @@ impl Lemmy {
) )
} }
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<PostView>, String> { pub(crate) async fn get_local_pinned(&self) -> Result<Vec<PostView>, ()> {
let list_params = GetPosts { let list_params = GetPosts {
type_: Some(ListingType::Local), type_: Some(ListingType::Local),
..Default::default() ..Default::default()
@ -185,15 +211,27 @@ impl Lemmy {
Ok(data) => { Ok(data) => {
match data.text().await { match data.text().await {
Ok(data) => data, 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) { let json_data: GetPostsResponse = match serde_json::from_str(&response) {
Ok(data) => data, 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| { Ok(json_data.posts.iter().filter(|post| {
@ -204,7 +242,7 @@ impl Lemmy {
) )
} }
pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, String> { pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, ()> {
let list_params = ListCommunities { let list_params = ListCommunities {
type_: Some(ListingType::Local), type_: Some(ListingType::Local),
..Default::default() ..Default::default()
@ -219,15 +257,27 @@ impl Lemmy {
Ok(data) => { Ok(data) => {
match data.text().await { match data.text().await {
Ok(data) => data, 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) { let json_data: ListCommunitiesResponse = match serde_json::from_str(&response) {
Ok(data) => data, 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<String, CommunityId> = HashMap::new(); let mut communities: HashMap<String, CommunityId> = HashMap::new();