Add API routes for creating and deleting posts and comments

This commit is contained in:
Bnyro 2023-06-20 10:40:37 +02:00
parent 5046a56df9
commit 7e8b67f4ef
4 changed files with 66 additions and 1 deletions

18
src/api/comment.rs Normal file
View File

@ -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<i32>,
auth: Sensitive<String>,
) -> Result<CommentResponse, reqwest::Error> {
let params = CreateComment {
post_id: PostId(post_id),
content,
parent_id: parent_id.map(CommentId),
auth,
..Default::default()
};
super::post("/comment", &params)
}

View File

@ -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";

29
src/api/moderation.rs Normal file
View File

@ -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<String>,
) -> Result<PostResponse, reqwest::Error> {
let params = RemovePost {
post_id: PostId(post_id),
removed: true,
reason: Some(reason),
auth,
};
super::post("/post/remove", &params)
}
pub fn remove_comment(
comment_id: i32,
reason: String,
auth: Sensitive<String>,
) -> Result<CommentResponse, reqwest::Error> {
let params = RemoveComment {
comment_id: CommentId(comment_id),
removed: true,
reason: Some(reason),
auth,
};
super::post("/comment/remove", &params)
}

View File

@ -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<GetPostResponse, reqwest::Error> {
let params = GetPost {
@ -28,3 +28,19 @@ pub fn get_comments(post_id: PostId) -> std::result::Result<Vec<CommentView>, 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<String>,
) -> Result<PostResponse, reqwest::Error> {
let params = CreatePost {
name,
body: Some(body),
community_id: CommunityId(community_id),
auth,
..Default::default()
};
super::post("/post", &params)
}