Move lemmy module out of subdir, port logging from tui to println/journald
Some checks failed
Run Tests on Code / run-tests (push) Failing after 18s

This commit is contained in:
Neshura 2023-12-30 00:23:01 +01:00
parent ecc05a2812
commit c0bff03120
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C

View file

@ -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<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(),
}
}
}
use crate::{HTTP_CLIENT, write_error};
pub(crate) struct Lemmy {
jwt_token: Sensitive<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 {
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<L
match data.jwt {
Some(token) => 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<PostId, String> {
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
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::<HashMap<&str, PostView>>(&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<PostView, String> {
async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
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::<HashMap<&str, PostView>>(&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<PostView, String> {
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
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<PostView, String> {
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
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<Vec<PostView>, String> {
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<PostView>, ()> {
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<Vec<PostView>, String> {
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<PostView>, ()> {
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<HashMap<String, CommunityId>, String> {
pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, ()> {
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<String, CommunityId> = HashMap::new();