2023-12-17 17:03:11 +00:00
|
|
|
use std::collections::HashMap;
|
2023-12-19 21:08:23 +00:00
|
|
|
use lemmy_api_common::community::{ListCommunities, ListCommunitiesResponse};
|
|
|
|
use lemmy_api_common::lemmy_db_views::structs::PostView;
|
2023-12-17 17:03:11 +00:00
|
|
|
use lemmy_api_common::person::{Login, LoginResponse};
|
2023-12-19 21:08:23 +00:00
|
|
|
use lemmy_api_common::post::{CreatePost, FeaturePost, GetPosts, GetPostsResponse};
|
2023-12-17 17:03:11 +00:00
|
|
|
use lemmy_api_common::sensitive::Sensitive;
|
2023-12-19 21:08:23 +00:00
|
|
|
use lemmy_db_schema::newtypes::{CommunityId, PostId};
|
|
|
|
use lemmy_db_schema::{ListingType, PostFeatureType};
|
2023-12-17 17:03:11 +00:00
|
|
|
use reqwest::StatusCode;
|
2023-12-29 13:35:07 +00:00
|
|
|
use crate::config::Config;
|
2023-12-29 23:23:01 +00:00
|
|
|
use crate::{HTTP_CLIENT, write_error};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
pub(crate) struct Lemmy {
|
|
|
|
jwt_token: Sensitive<String>,
|
|
|
|
instance: String,
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn login(config: &Config) -> Result<Lemmy, ()> {
|
2023-12-17 17:03:11 +00:00
|
|
|
let login_params = Login {
|
2023-12-29 23:23:01 +00:00
|
|
|
username_or_email: config.get_username(),
|
|
|
|
password: config.get_password(),
|
2023-12-17 17:03:11 +00:00
|
|
|
totp_2fa_token: None,
|
|
|
|
};
|
|
|
|
|
2023-12-17 21:29:18 +00:00
|
|
|
let response = match HTTP_CLIENT
|
2023-12-29 23:23:01 +00:00
|
|
|
.post(config.instance.to_owned() + "/api/v3/user/login")
|
2023-12-17 17:03:11 +00:00
|
|
|
.json(&login_params)
|
|
|
|
.send()
|
2023-12-17 21:29:18 +00:00
|
|
|
.await {
|
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
match response.status() {
|
|
|
|
StatusCode::OK => {
|
|
|
|
let data: LoginResponse = response.json().await.expect("Successful Login Request should return JSON");
|
|
|
|
match data.jwt {
|
|
|
|
Some(token) => Ok(Lemmy {
|
|
|
|
jwt_token: token.clone(),
|
2023-12-29 23:23:01 +00:00
|
|
|
instance: config.instance.to_owned(),
|
2023-12-17 17:03:11 +00:00
|
|
|
}),
|
2023-12-29 23:23:01 +00:00
|
|
|
None => {
|
|
|
|
let err_msg = "Login did not return JWT token. Are the credentials valid?".to_owned();
|
|
|
|
write_error(err_msg);
|
2023-12-29 23:53:00 +00:00
|
|
|
Err(())
|
2023-12-29 23:23:01 +00:00
|
|
|
}
|
2023-12-17 17:03:11 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 23:23:01 +00:00
|
|
|
status => {
|
|
|
|
let err_msg = format!("Unexpected HTTP Status '{}' during Login", status);
|
|
|
|
write_error(err_msg);
|
2023-12-29 23:53:00 +00:00
|
|
|
Err(())
|
2023-12-29 23:23:01 +00:00
|
|
|
}
|
2023-12-17 17:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lemmy {
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
|
2023-12-17 21:29:18 +00:00
|
|
|
let response = match HTTP_CLIENT
|
2023-12-17 17:03:11 +00:00
|
|
|
.post(format!("{}/api/v3/post", &self.instance))
|
|
|
|
.bearer_auth(&self.jwt_token.to_string())
|
|
|
|
.json(&post)
|
|
|
|
.send()
|
2023-12-17 21:29:18 +00:00
|
|
|
.await {
|
|
|
|
Ok(data) => {
|
|
|
|
match data.text().await {
|
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
2023-12-19 21:08:23 +00:00
|
|
|
let json_data = match serde_json::from_str::<HashMap<&str, PostView>>(&response) {
|
2023-12-17 21:29:18 +00:00
|
|
|
Ok(mut data) => data.remove("post_view").expect("Element should be present"),
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
Ok(json_data.post.id)
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
|
2023-12-17 21:29:18 +00:00
|
|
|
let response = match HTTP_CLIENT
|
2023-12-17 17:03:11 +00:00
|
|
|
.post(format!("{}/api/v3/post/feature", &self.instance))
|
|
|
|
.bearer_auth(&self.jwt_token.to_string())
|
|
|
|
.json(¶ms)
|
|
|
|
.send()
|
2023-12-17 21:29:18 +00:00
|
|
|
.await {
|
|
|
|
Ok(data) => {
|
|
|
|
match data.text().await {
|
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
|
|
|
|
2023-12-19 21:08:23 +00:00
|
|
|
let json_data = match serde_json::from_str::<HashMap<&str, PostView>>(&response) {
|
2023-12-17 21:29:18 +00:00
|
|
|
Ok(mut data) => data.remove("post_view").expect("Element should be present"),
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
Ok(json_data)
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
|
2023-12-19 21:08:23 +00:00
|
|
|
let pin_params = FeaturePost {
|
2023-12-17 17:03:11 +00:00
|
|
|
post_id,
|
|
|
|
featured: false,
|
|
|
|
feature_type: location,
|
|
|
|
};
|
|
|
|
self.feature(pin_params).await
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
|
2023-12-19 21:08:23 +00:00
|
|
|
let pin_params = FeaturePost {
|
2023-12-17 17:03:11 +00:00
|
|
|
post_id,
|
|
|
|
featured: true,
|
|
|
|
feature_type: location,
|
|
|
|
};
|
|
|
|
self.feature(pin_params).await
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<PostView>, ()> {
|
2023-12-17 17:03:11 +00:00
|
|
|
let list_params = GetPosts {
|
|
|
|
community_id: Some(community),
|
|
|
|
type_: Some(ListingType::Local),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2023-12-17 21:29:18 +00:00
|
|
|
let response = match HTTP_CLIENT
|
2023-12-17 17:03:11 +00:00
|
|
|
.get(format!("{}/api/v3/post/list", &self.instance))
|
|
|
|
.bearer_auth(&self.jwt_token.to_string())
|
|
|
|
.query(&list_params)
|
|
|
|
.send()
|
2023-12-17 21:29:18 +00:00
|
|
|
.await {
|
|
|
|
Ok(data) => {
|
|
|
|
match data.text().await {
|
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
2023-12-19 21:08:23 +00:00
|
|
|
let json_data: GetPostsResponse = match serde_json::from_str(&response) {
|
2023-12-17 21:29:18 +00:00
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
Ok(json_data.posts.iter().filter(|post| {
|
|
|
|
post.post.featured_community
|
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<PostView>, ()> {
|
2023-12-17 17:03:11 +00:00
|
|
|
let list_params = GetPosts {
|
|
|
|
type_: Some(ListingType::Local),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2023-12-17 21:29:18 +00:00
|
|
|
let response = match HTTP_CLIENT
|
2023-12-17 17:03:11 +00:00
|
|
|
.get(format!("{}/api/v3/post/list", &self.instance))
|
|
|
|
.bearer_auth(&self.jwt_token.to_string())
|
|
|
|
.query(&list_params)
|
|
|
|
.send()
|
2023-12-17 21:29:18 +00:00
|
|
|
.await {
|
|
|
|
Ok(data) => {
|
|
|
|
match data.text().await {
|
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
2023-12-19 21:08:23 +00:00
|
|
|
let json_data: GetPostsResponse = match serde_json::from_str(&response) {
|
2023-12-17 21:29:18 +00:00
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
Ok(json_data.posts.iter().filter(|post| {
|
|
|
|
post.post.featured_local
|
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:23:01 +00:00
|
|
|
pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, ()> {
|
2023-12-17 17:03:11 +00:00
|
|
|
let list_params = ListCommunities {
|
|
|
|
type_: Some(ListingType::Local),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2023-12-17 21:29:18 +00:00
|
|
|
let response = match HTTP_CLIENT
|
2023-12-17 17:03:11 +00:00
|
|
|
.get(format!("{}/api/v3/community/list", &self.instance))
|
|
|
|
.bearer_auth(&self.jwt_token.to_string())
|
|
|
|
.query(&list_params)
|
|
|
|
.send()
|
2023-12-17 21:29:18 +00:00
|
|
|
.await {
|
|
|
|
Ok(data) => {
|
|
|
|
match data.text().await {
|
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
2023-12-19 21:08:23 +00:00
|
|
|
let json_data: ListCommunitiesResponse = match serde_json::from_str(&response) {
|
2023-12-17 21:29:18 +00:00
|
|
|
Ok(data) => data,
|
2023-12-29 23:23:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_msg = format!("{e}");
|
|
|
|
write_error(err_msg);
|
|
|
|
return Err(())
|
|
|
|
}
|
2023-12-17 21:29:18 +00:00
|
|
|
};
|
2023-12-17 17:03:11 +00:00
|
|
|
|
|
|
|
let mut communities: HashMap<String, CommunityId> = HashMap::new();
|
|
|
|
for community_view in json_data.communities {
|
|
|
|
let community = community_view.community;
|
|
|
|
communities.insert(community.name, community.id);
|
|
|
|
}
|
|
|
|
|
2023-12-17 19:35:37 +00:00
|
|
|
Ok(communities)
|
2023-12-17 17:03:11 +00:00
|
|
|
}
|
|
|
|
}
|