use lemmy_api_common::{ comment::{GetComments, GetCommentsResponse}, lemmy_db_schema::{ newtypes::{CommunityId, PostId}, CommentSortType, ListingType, }, lemmy_db_views::structs::CommentView, post::{ CreatePost, CreatePostLike, DeletePost, EditPost, GetPost, GetPostResponse, PostResponse, }, }; use crate::settings; pub fn get_post(id: PostId) -> Result { let params = GetPost { id: Some(id), comment_id: None, auth: settings::get_current_account().jwt, }; super::get("/post", ¶ms) } pub fn get_comments(post_id: PostId) -> Result, reqwest::Error> { let params = GetComments { post_id: Some(post_id), sort: Some(CommentSortType::Hot), type_: Some(ListingType::All), auth: settings::get_current_account().jwt, ..Default::default() }; let mut comments = super::get::("/comment/list", ¶ms)?.comments; // hide removed and deleted comments comments.retain(|c| !c.comment.deleted && !c.comment.removed); Ok(comments) } pub fn default_post() -> GetPostResponse { serde_json::from_str(include_str!("../examples/post.json")).unwrap() } pub fn create_post( name: String, body: String, url: Option, community_id: i32, ) -> Result { let params = CreatePost { name, body: Some(body), url, community_id: CommunityId(community_id), auth: settings::get_current_account().jwt.unwrap(), ..Default::default() }; super::post("/post", ¶ms) } pub fn edit_post( name: String, url: Option, body: String, post_id: i32, ) -> Result { let params = EditPost { name: Some(name), body: Some(body), url, post_id: PostId(post_id), auth: settings::get_current_account().jwt.unwrap(), ..Default::default() }; super::put("/post", ¶ms) } // for score, use 1 to upvote, -1 to vote down and 0 to reset the user's voting pub fn like_post(post_id: PostId, score: i16) -> Result { let params = CreatePostLike { post_id, score, auth: settings::get_current_account().jwt.unwrap(), }; super::post("/post/like", ¶ms) } pub fn delete_post(post_id: PostId) -> Result { let params = DeletePost { post_id, deleted: true, auth: settings::get_current_account().jwt.unwrap(), }; super::post("/post/delete", ¶ms) }