diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 046de5f..0429ac2 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -1,13 +1,14 @@ use std::collections::HashMap; use std::sync::{Arc}; use chrono::{DateTime, Duration, Timelike, Utc}; +use lemmy_api_common::post::CreatePost; use lemmy_db_schema::newtypes::{CommunityId, LanguageId}; use lemmy_db_schema::PostFeatureType; use tokio::sync::{RwLock}; use crate::{jnovel, lemmy, Message, SharedData}; use crate::config::{Config, PostBody, SeriesConfig}; use crate::jnovel::PostInfo; -use crate::lemmy::{CustomCreatePost, Lemmy}; +use crate::lemmy::{Lemmy}; use crate::post_history::SeriesHistory; use tokio::time::sleep; @@ -231,7 +232,7 @@ async fn handle_series( PostBody::Custom(text) => Some(text.clone()), }; - let post_data = CustomCreatePost { + let post_data = CreatePost { name: post_lemmy_info.title, community_id, url: Some(post_lemmy_info.url), diff --git a/src/lemmy/mod.rs b/src/lemmy/mod.rs index 8026195..9617b2f 100644 --- a/src/lemmy/mod.rs +++ b/src/lemmy/mod.rs @@ -1,15 +1,14 @@ use std::collections::HashMap; use std::env; use std::env::VarError; -use lemmy_api_common::community::{ListCommunities}; +use lemmy_api_common::community::{ListCommunities, ListCommunitiesResponse}; +use lemmy_api_common::lemmy_db_views::structs::PostView; use lemmy_api_common::person::{Login, LoginResponse}; -use lemmy_api_common::post::{GetPosts}; +use lemmy_api_common::post::{CreatePost, FeaturePost, GetPosts, GetPostsResponse}; use lemmy_api_common::sensitive::Sensitive; -use lemmy_db_schema::newtypes::{CommunityId, InstanceId, LanguageId, PersonId, PostId}; -use lemmy_db_schema::{ListingType, PostFeatureType, SubscribedType}; +use lemmy_db_schema::newtypes::{CommunityId, PostId}; +use lemmy_db_schema::{ListingType, PostFeatureType}; use reqwest::StatusCode; -use serde_derive::{Deserialize, Serialize}; -use url::Url; use crate::HTTP_CLIENT; pub(crate) struct Credentials { @@ -73,7 +72,7 @@ pub(crate) async fn login(credentials: &Credentials, instance: &str) -> Result 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()) @@ -89,7 +88,7 @@ impl Lemmy { Err(e) => return Err(format!("{}", e)) }; - let json_data = match serde_json::from_str::>(&response) { + 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)) }; @@ -97,7 +96,7 @@ impl Lemmy { Ok(json_data.post.id) } - async fn feature(&self, params: CustomFeaturePost) -> 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()) @@ -113,7 +112,7 @@ impl Lemmy { Err(e) => return Err(format!("{}", e)) }; - let json_data = match serde_json::from_str::>(&response) { + 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)) }; @@ -121,8 +120,8 @@ impl Lemmy { Ok(json_data) } - pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result { - let pin_params = CustomFeaturePost { + pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result { + let pin_params = FeaturePost { post_id, featured: false, feature_type: location, @@ -130,8 +129,8 @@ impl Lemmy { self.feature(pin_params).await } - pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result { - let pin_params = CustomFeaturePost { + pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result { + let pin_params = FeaturePost { post_id, featured: true, feature_type: location, @@ -139,7 +138,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, String> { let list_params = GetPosts { community_id: Some(community), type_: Some(ListingType::Local), @@ -161,7 +160,7 @@ impl Lemmy { Err(e) => return Err(format!("{}", e)) }; - let json_data: CustomGetPostsResponse = match serde_json::from_str(&response) { + let json_data: GetPostsResponse = match serde_json::from_str(&response) { Ok(data) => data, Err(e) => return Err(format!("{}", e)) }; @@ -174,7 +173,7 @@ impl Lemmy { ) } - pub(crate) async fn get_local_pinned(&self) -> Result, String> { + pub(crate) async fn get_local_pinned(&self) -> Result, String> { let list_params = GetPosts { type_: Some(ListingType::Local), ..Default::default() @@ -195,7 +194,7 @@ impl Lemmy { Err(e) => return Err(format!("{}", e)) }; - let json_data: CustomGetPostsResponse = match serde_json::from_str(&response) { + let json_data: GetPostsResponse = match serde_json::from_str(&response) { Ok(data) => data, Err(e) => return Err(format!("{}", e)) }; @@ -229,7 +228,7 @@ impl Lemmy { Err(e) => return Err(format!("{}", e)) }; - let json_data: CustomListCommunitiesResponse = match serde_json::from_str(&response) { + let json_data: ListCommunitiesResponse = match serde_json::from_str(&response) { Ok(data) => data, Err(e) => return Err(format!("{}", e)) }; @@ -243,144 +242,3 @@ impl Lemmy { Ok(communities) } } - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub(crate) struct CustomCreatePost { - pub(crate) name: String, - pub(crate) community_id: CommunityId, - pub(crate) url: Option, - pub(crate) body: Option, - pub(crate) honeypot: Option, - pub(crate) nsfw: Option, - pub(crate) language_id: Option, -} - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub(crate) struct CustomFeaturePost { - pub(crate) post_id: PostId, - pub(crate) featured: bool, - pub(crate) feature_type: PostFeatureType, -} - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub(crate) struct CustomListCommunitiesResponse { - pub(crate) communities: Vec -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomCommunityView { - pub(crate) community: CustomCommunity, - pub(crate) subscribed: SubscribedType, - pub(crate) blocked: bool, - pub(crate) counts: CustomCommunityAggregates -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomCommunity { - pub(crate) actor_id: String, - pub(crate) banner: Option, - pub(crate) deleted: bool, - pub(crate) description: Option, - pub(crate) hidden: bool, - pub(crate) icon: Option, - pub(crate) id: CommunityId, - pub(crate) instance_id: i32, - pub(crate) local: bool, - pub(crate) name: String, - pub(crate) nsfw: bool, - pub(crate) posting_restricted_to_mods: bool, - pub(crate) published: String, - pub(crate) removed: bool, - pub(crate) title: String, - pub(crate) updated: Option, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomCommunityAggregates { - pub(crate) comments: i64, - pub(crate) community_id: i32, - pub(crate) posts: i64, - pub(crate) published: String, - pub(crate) subscribers: i64, - pub(crate) users_active_day: i64, - pub(crate) users_active_half_year: i64, - pub(crate) users_active_month: i64, - pub(crate) users_active_week: i64, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomPostView { - pub(crate) community: CustomCommunity, - pub(crate) counts: CustomPostAggregates, - pub(crate) creator: CustomPerson, - pub(crate) creator_banned_from_community: bool, - pub(crate) creator_blocked: bool, - pub(crate) creator_is_admin: bool, - pub(crate) creator_is_moderator: bool, - pub(crate) my_vote: Option, - pub(crate) post: CustomPost, - pub(crate) read: bool, - pub(crate) saved: bool, - pub(crate) subscribed: SubscribedType, - pub(crate) unread_comments: i64, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomPostAggregates { - pub(crate) comments: i64, - pub(crate) downvotes: i64, - pub(crate) post_id: PostId, - pub(crate) published: String, - pub(crate) score: i64, - pub(crate) upvotes: i64, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomPerson { - pub(crate) actor_id: String, - pub(crate) avatar: Option, - pub(crate) ban_expires: Option, - pub(crate) banned: bool, - pub(crate) banner: Option, - pub(crate) bio: Option, - pub(crate) bot_account: bool, - pub(crate) deleted: bool, - pub(crate) display_name: Option, - pub(crate) id: PersonId, - pub(crate) instance_id : InstanceId, - pub(crate) local: bool, - pub(crate) matrix_user_id: Option, - pub(crate) name: String, - pub(crate) published: String, - pub(crate) updated: Option, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomPost { - pub(crate) id: PostId, - pub(crate) name: String, - pub(crate) url: Option, - pub(crate) body: Option, - pub(crate) creator_id: PersonId, - pub(crate) community_id: CommunityId, - pub(crate) removed: bool, - pub(crate) locked: bool, - pub(crate) published: String, - pub(crate) updated: Option, - pub(crate) deleted: bool, - pub(crate) nsfw: bool, - pub(crate) embed_title: Option, - pub(crate) embed_description: Option, - pub(crate) thumbnail_url: Option, - pub(crate) ap_id: String, - pub(crate) local: bool, - pub(crate) embed_video_url: Option, - pub(crate) language_id: LanguageId, - pub(crate) featured_community: bool, - pub(crate) featured_local: bool, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -pub(crate) struct CustomGetPostsResponse { - pub(crate) posts: Vec, -} diff --git a/src/tui/mod.rs b/src/tui/mod.rs index e429446..8132e8f 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -1,6 +1,6 @@ use std::ops::Deref; use std::sync::{Arc}; -use chrono::{Duration, Local, Utc}; +use chrono::{Duration, Local}; use tokio::sync::{RwLock, RwLockReadGuard}; use tokio::time::sleep; use crate::{SharedData};