This commit is contained in:
parent
5d708bdb82
commit
b6f5c38e4a
5 changed files with 146 additions and 96 deletions
src
135
src/lemmy.rs
135
src/lemmy.rs
|
@ -15,6 +15,15 @@ use serde::{Deserialize, Serialize};
|
|||
use url::Url;
|
||||
use systemd_journal_logger::connected_to_journal;
|
||||
|
||||
macro_rules! debug {
|
||||
($msg:tt) => {
|
||||
match connected_to_journal() {
|
||||
true => log::debug!("[DEBUG] {}", $msg),
|
||||
false => println!("[DEBUG] {}", $msg),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! error {
|
||||
($msg:tt) => {
|
||||
match connected_to_journal() {
|
||||
|
@ -124,7 +133,7 @@ impl PostInfo {
|
|||
pub(crate) fn get_part_info(&self) -> Option<PartInfo> {
|
||||
self.part
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn get_post_config(&self, series: &SeriesConfig) -> PostConfig {
|
||||
match self.post_type {
|
||||
Some(post_type) => {
|
||||
|
@ -136,10 +145,10 @@ impl PostInfo {
|
|||
None => series.prepub_community.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn get_post_data(&self, series: &SeriesConfig, lemmy: &Lemmy) -> CreatePost {
|
||||
let post_config = self.get_post_config(series);
|
||||
|
||||
|
||||
let post_body = match &post_config.post_body {
|
||||
PostBody::None => None,
|
||||
PostBody::Description => self.get_description(),
|
||||
|
@ -156,7 +165,7 @@ impl PostInfo {
|
|||
honeypot: None,
|
||||
nsfw: None,
|
||||
language_id: Some(LanguageId(37)), // TODO get this id once every few hours per API request, the ordering of IDs suggests that the EN Id might change in the future
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -252,21 +261,33 @@ impl Lemmy {
|
|||
}
|
||||
|
||||
|
||||
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
|
||||
let response: String = self.post_data_json("/api/v3/post", &post).await?;
|
||||
let json_data: PostView = self.parse_json_map(&response).await?;
|
||||
pub(crate) async fn post(&self, post: CreatePost) -> Option<PostId> {
|
||||
let response: String = match self.post_data_json("/api/v3/post", &post).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
let json_data: PostView = match self.parse_json_map(&response).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
Ok(json_data.post.id)
|
||||
Some(json_data.post.id)
|
||||
}
|
||||
|
||||
async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
|
||||
let response: String = self.post_data_json("/api/v3/post/feature", ¶ms).await?;
|
||||
let json_data: PostView = self.parse_json_map(&response).await?;
|
||||
async fn feature(&self, params: FeaturePost) -> Option<PostView> {
|
||||
let response: String = match self.post_data_json("/api/v3/post/feature", ¶ms).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
let json_data: PostView = match self.parse_json_map(&response).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
Ok(json_data)
|
||||
Some(json_data)
|
||||
}
|
||||
|
||||
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
|
||||
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Option<PostView> {
|
||||
let pin_params = FeaturePost {
|
||||
post_id,
|
||||
featured: false,
|
||||
|
@ -275,7 +296,7 @@ impl Lemmy {
|
|||
self.feature(pin_params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
|
||||
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Option<PostView> {
|
||||
let pin_params = FeaturePost {
|
||||
post_id,
|
||||
featured: true,
|
||||
|
@ -284,17 +305,23 @@ impl Lemmy {
|
|||
self.feature(pin_params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<PostView>, ()> {
|
||||
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Option<Vec<PostView>> {
|
||||
let list_params = GetPosts {
|
||||
community_id: Some(community),
|
||||
type_: Some(ListingType::Local),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let response: String = self.get_data_query("/api/v3/post/list", &list_params).await?;
|
||||
let json_data: GetPostsResponse = self.parse_json(&response).await?;
|
||||
let response: String = match self.get_data_query("/api/v3/post/list", &list_params).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
let json_data: GetPostsResponse = match self.parse_json(&response).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
Ok(json_data
|
||||
Some(json_data
|
||||
.posts
|
||||
.iter()
|
||||
.filter(|post| post.post.featured_community)
|
||||
|
@ -302,16 +329,22 @@ impl Lemmy {
|
|||
.collect())
|
||||
}
|
||||
|
||||
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<PostView>, ()> {
|
||||
pub(crate) async fn get_local_pinned(&self) -> Option<Vec<PostView>> {
|
||||
let list_params = GetPosts {
|
||||
type_: Some(ListingType::Local),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let response: String = self.get_data_query("/api/v3/post/list", &list_params).await?;
|
||||
let json_data: GetPostsResponse = self.parse_json(&response).await?;
|
||||
let response: String = match self.get_data_query("/api/v3/post/list", &list_params).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
let json_data: GetPostsResponse = match self.parse_json(&response).await {
|
||||
Some(data) => data,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
Ok(json_data
|
||||
Some(json_data
|
||||
.posts
|
||||
.iter()
|
||||
.filter(|post| post.post.featured_local)
|
||||
|
@ -326,18 +359,12 @@ impl Lemmy {
|
|||
};
|
||||
|
||||
let response: String = match self.get_data_query("/api/v3/community/list", &list_params).await {
|
||||
Ok(data) => data,
|
||||
Err(_) => {
|
||||
error!("Unable to extract data from request");
|
||||
return;
|
||||
}
|
||||
Some(data) => data,
|
||||
None => return,
|
||||
};
|
||||
let json_data: ListCommunitiesResponse = match self.parse_json::<ListCommunitiesResponse>(&response).await {
|
||||
Ok(data) => data,
|
||||
Err(_) => {
|
||||
error!("Unable to parse data from json");
|
||||
return;
|
||||
},
|
||||
Some(data) => data,
|
||||
None => return,
|
||||
};
|
||||
|
||||
let mut communities: HashMap<String, CommunityId> = HashMap::new();
|
||||
|
@ -349,7 +376,7 @@ impl Lemmy {
|
|||
self.communities = communities;
|
||||
}
|
||||
|
||||
async fn post_data_json<T: Serialize>(&self, route: &str, json: &T ) -> Result<String,()> {
|
||||
async fn post_data_json<T: Serialize>(&self, route: &str, json: &T ) -> Option<String> {
|
||||
let res = HTTP_CLIENT
|
||||
.post(format!("{}{route}", &self.instance))
|
||||
.bearer_auth(&self.jwt_token.to_string())
|
||||
|
@ -359,7 +386,7 @@ impl Lemmy {
|
|||
self.extract_data(res).await
|
||||
}
|
||||
|
||||
async fn get_data_query<T: Serialize>(&self, route: &str, param: &T ) -> Result<String,()> {
|
||||
async fn get_data_query<T: Serialize>(&self, route: &str, param: &T ) -> Option<String> {
|
||||
let res = HTTP_CLIENT
|
||||
.get(format!("{}{route}", &self.instance))
|
||||
.bearer_auth(&self.jwt_token.to_string())
|
||||
|
@ -369,42 +396,52 @@ impl Lemmy {
|
|||
self.extract_data(res).await
|
||||
}
|
||||
|
||||
async fn extract_data(&self, response: Result<reqwest::Response, reqwest::Error>) -> Result<String,()> {
|
||||
async fn extract_data(&self, response: Result<reqwest::Response, reqwest::Error>) -> Option<String> {
|
||||
match response {
|
||||
Ok(data) => match data.text().await {
|
||||
Ok(data) => Ok(data),
|
||||
Err(e) => {
|
||||
let err_msg = format!("{e}");
|
||||
Ok(data) => {
|
||||
if data.status().is_success() {
|
||||
match data.text().await {
|
||||
Ok(data) => Some(data),
|
||||
Err(e) => {
|
||||
let err_msg = format!("{e}");
|
||||
error!(err_msg);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
let err_msg = format!("HTTP Request failed: {}", data.text().await.unwrap());
|
||||
error!(err_msg);
|
||||
Err(())
|
||||
None
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let err_msg = format!("{e}");
|
||||
error!(err_msg);
|
||||
Err(())
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result<T,()> {
|
||||
async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Option<T> {
|
||||
match serde_json::from_str::<T>(response) {
|
||||
Ok(data) => Ok(data),
|
||||
Ok(data) => Some(data),
|
||||
Err(e) => {
|
||||
let err_msg = format!("{e} while parsing JSON");
|
||||
let err_msg = format!("while parsing JSON: {e} ");
|
||||
error!(err_msg);
|
||||
Err(())
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn parse_json_map<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result<T,()> {
|
||||
async fn parse_json_map<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Option<T> {
|
||||
debug!(response);
|
||||
match serde_json::from_str::<HashMap<&str, T>>(response) {
|
||||
Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")),
|
||||
Ok(mut data) => Some(data.remove("post_view").expect("Element should be present")),
|
||||
Err(e) => {
|
||||
let err_msg = format!("{e} while parsing JSON HashMap");
|
||||
let err_msg = format!("while parsing JSON HashMap: {e}");
|
||||
error!(err_msg);
|
||||
Err(())
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue