From 7e8b67f4ef5a2ed83f53571efdb275102d5982e6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 20 Jun 2023 10:40:37 +0200 Subject: [PATCH] Add API routes for creating and deleting posts and comments --- src/api/comment.rs | 18 ++++++++++++++++++ src/api/mod.rs | 2 ++ src/api/moderation.rs | 29 +++++++++++++++++++++++++++++ src/api/post.rs | 18 +++++++++++++++++- 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/api/comment.rs create mode 100644 src/api/moderation.rs diff --git a/src/api/comment.rs b/src/api/comment.rs new file mode 100644 index 0000000..17e22f0 --- /dev/null +++ b/src/api/comment.rs @@ -0,0 +1,18 @@ +use lemmy_api_common::{sensitive::Sensitive, comment::{CommentResponse, CreateComment}, lemmy_db_schema::newtypes::{PostId, CommentId}}; + + +pub fn create_comment( + post_id: i32, + content: String, + parent_id: Option, + auth: Sensitive, +) -> Result { + let params = CreateComment { + post_id: PostId(post_id), + content, + parent_id: parent_id.map(CommentId), + auth, + ..Default::default() + }; + super::post("/comment", ¶ms) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index ea3fd82..efac2ea 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -9,6 +9,8 @@ pub mod posts; pub mod search; pub mod user; pub mod auth; +pub mod moderation; +pub mod comment; static API_VERSION: &str = "v3"; diff --git a/src/api/moderation.rs b/src/api/moderation.rs new file mode 100644 index 0000000..ad660fd --- /dev/null +++ b/src/api/moderation.rs @@ -0,0 +1,29 @@ +use lemmy_api_common::{lemmy_db_schema::newtypes::{CommentId, PostId}, comment::{RemoveComment, CommentResponse}, sensitive::Sensitive, post::{PostResponse, RemovePost}}; + +pub fn remove_post( + post_id: i32, + reason: String, + auth: Sensitive, +) -> Result { + let params = RemovePost { + post_id: PostId(post_id), + removed: true, + reason: Some(reason), + auth, + }; + super::post("/post/remove", ¶ms) +} + +pub fn remove_comment( + comment_id: i32, + reason: String, + auth: Sensitive, +) -> Result { + let params = RemoveComment { + comment_id: CommentId(comment_id), + removed: true, + reason: Some(reason), + auth, + }; + super::post("/comment/remove", ¶ms) +} diff --git a/src/api/post.rs b/src/api/post.rs index aa98faa..1cb98f0 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -1,4 +1,4 @@ -use lemmy_api_common::{post::{GetPost, GetPostResponse}, lemmy_db_schema::{newtypes::PostId, CommentSortType, ListingType}, comment::{GetComments, GetCommentsResponse}, lemmy_db_views::structs::CommentView}; +use lemmy_api_common::{post::{GetPost, GetPostResponse, PostResponse, CreatePost}, lemmy_db_schema::{newtypes::{PostId, CommunityId}, CommentSortType, ListingType}, comment::{GetComments, GetCommentsResponse}, lemmy_db_views::structs::CommentView, sensitive::Sensitive}; pub fn get_post(id: PostId) -> std::result::Result { let params = GetPost { @@ -28,3 +28,19 @@ pub fn get_comments(post_id: PostId) -> std::result::Result, re pub fn default_post() -> GetPostResponse { serde_json::from_str(include_str!("../examples/post.json")).unwrap() } + +pub fn create_post( + name: String, + body: String, + community_id: i32, + auth: Sensitive, +) -> Result { + let params = CreatePost { + name, + body: Some(body), + community_id: CommunityId(community_id), + auth, + ..Default::default() + }; + super::post("/post", ¶ms) +}