Add routes to upvote and downvote posts and comments

This commit is contained in:
Bnyro 2023-06-20 15:15:10 +02:00
parent 9376e3a6c7
commit ce9bf6d3ed
7 changed files with 54 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use lemmy_api_common::{comment::{CommentResponse, CreateComment}, lemmy_db_schema::newtypes::{PostId, CommentId}}; use lemmy_api_common::{comment::{CommentResponse, CreateComment, CreateCommentLike}, lemmy_db_schema::newtypes::{PostId, CommentId}};
use crate::util; use crate::util;
@ -17,3 +17,13 @@ pub fn create_comment(
}; };
super::post("/comment", &params) super::post("/comment", &params)
} }
// see posts.rs for possible score parameters
pub fn like_comment(comment_id: CommentId, score: i16) -> Result<CommentResponse, reqwest::Error> {
let params = CreateCommentLike {
comment_id,
score,
auth: util::get_auth_token().unwrap(),
};
super::post("/comment/like", &params)
}

View File

@ -1,5 +1,7 @@
use lemmy_api_common::{community::{ListCommunities, ListCommunitiesResponse}, lemmy_db_schema::{SortType, SearchType}, lemmy_db_views_actor::structs::CommunityView}; use lemmy_api_common::{community::{ListCommunities, ListCommunitiesResponse}, lemmy_db_schema::{SortType, SearchType}, lemmy_db_views_actor::structs::CommunityView};
use crate::util;
use super::search; use super::search;
pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Result<Vec<CommunityView>, reqwest::Error> { pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Result<Vec<CommunityView>, reqwest::Error> {
@ -7,6 +9,7 @@ pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Resul
let params = ListCommunities { let params = ListCommunities {
sort: Some(SortType::TopMonth), sort: Some(SortType::TopMonth),
page: Some(page), page: Some(page),
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };

View File

@ -1,14 +1,29 @@
use lemmy_api_common::community::{GetCommunity, GetCommunityResponse}; use lemmy_api_common::{community::{GetCommunity, GetCommunityResponse, CommunityResponse, FollowCommunity}, lemmy_db_schema::newtypes::CommunityId};
use crate::util;
pub fn get_community(name: String) -> std::result::Result<GetCommunityResponse, reqwest::Error> { pub fn get_community(name: String) -> std::result::Result<GetCommunityResponse, reqwest::Error> {
let params = GetCommunity { let params = GetCommunity {
name: Some(name), name: Some(name),
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };
super::get("/community", &params) super::get("/community", &params)
} }
pub async fn follow_community(
community_id: i32,
follow: bool,
) -> Result<CommunityResponse, reqwest::Error> {
let params = FollowCommunity {
community_id: CommunityId(community_id),
follow,
auth: util::get_auth_token().unwrap(),
};
super::post("/community/follow", &params)
}
pub fn default_community() -> GetCommunityResponse { pub fn default_community() -> GetCommunityResponse {
serde_json::from_str(include_str!("../examples/community.json")).unwrap() serde_json::from_str(include_str!("../examples/community.json")).unwrap()
} }

View File

@ -1,21 +1,23 @@
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}; use lemmy_api_common::{post::{GetPost, GetPostResponse, PostResponse, CreatePost, CreatePostLike}, lemmy_db_schema::{newtypes::{PostId, CommunityId}, CommentSortType, ListingType}, comment::{GetComments, GetCommentsResponse}, lemmy_db_views::structs::CommentView};
use crate::util; use crate::util;
pub fn get_post(id: PostId) -> std::result::Result<GetPostResponse, reqwest::Error> { pub fn get_post(id: PostId) -> Result<GetPostResponse, reqwest::Error> {
let params = GetPost { let params = GetPost {
id: Some(id), id: Some(id),
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };
super::get("/post", &params) super::get("/post", &params)
} }
pub fn get_comments(post_id: PostId) -> std::result::Result<Vec<CommentView>, reqwest::Error> { pub fn get_comments(post_id: PostId) -> Result<Vec<CommentView>, reqwest::Error> {
let params = GetComments { let params = GetComments {
post_id: Some(post_id), post_id: Some(post_id),
sort: Some(CommentSortType::Hot), sort: Some(CommentSortType::Hot),
type_: Some(ListingType::All), type_: Some(ListingType::All),
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };
@ -45,3 +47,13 @@ pub fn create_post(
}; };
super::post("/post", &params) super::post("/post", &params)
} }
// 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<PostResponse, reqwest::Error> {
let params = CreatePostLike {
post_id,
score,
auth: util::get_auth_token().unwrap(),
};
super::post("/post/like", &params)
}

View File

@ -1,9 +1,12 @@
use lemmy_api_common::{post::{GetPostsResponse, GetPosts}, lemmy_db_views::structs::PostView}; use lemmy_api_common::{post::{GetPostsResponse, GetPosts}, lemmy_db_views::structs::PostView};
use crate::util;
pub fn list_posts(page: i64, community_name: Option<String>) -> std::result::Result<Vec<PostView>, reqwest::Error> { pub fn list_posts(page: i64, community_name: Option<String>) -> std::result::Result<Vec<PostView>, reqwest::Error> {
let params = GetPosts { let params = GetPosts {
page: Some(page), page: Some(page),
community_name, community_name,
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };

View File

@ -1,11 +1,14 @@
use lemmy_api_common::{site::{SearchResponse, Search}, lemmy_db_schema::{SortType, SearchType}}; use lemmy_api_common::{site::{SearchResponse, Search}, lemmy_db_schema::{SortType, SearchType}};
use crate::util;
pub fn fetch_search(page: i64, query: String, search_type: Option<SearchType>) -> std::result::Result<SearchResponse, reqwest::Error> { pub fn fetch_search(page: i64, query: String, search_type: Option<SearchType>) -> std::result::Result<SearchResponse, reqwest::Error> {
let params = Search { let params = Search {
q: query, q: query,
sort: Some(SortType::TopMonth), sort: Some(SortType::TopMonth),
page: Some(page), page: Some(page),
type_: search_type, type_: search_type,
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };

View File

@ -1,9 +1,12 @@
use lemmy_api_common::{person::{GetPersonDetailsResponse, GetPersonDetails}}; use lemmy_api_common::{person::{GetPersonDetailsResponse, GetPersonDetails}};
use crate::util;
pub fn get_user(username: String, page: i64) -> std::result::Result<GetPersonDetailsResponse, reqwest::Error> { pub fn get_user(username: String, page: i64) -> std::result::Result<GetPersonDetailsResponse, reqwest::Error> {
let params = GetPersonDetails { let params = GetPersonDetails {
page: Some(page), page: Some(page),
username: Some(username), username: Some(username),
auth: util::get_auth_token(),
..Default::default() ..Default::default()
}; };