summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/api/posts.rs8
-rw-r--r--src/database/posts.rs3
-rw-r--r--src/types/post.rs27
3 files changed, 26 insertions, 12 deletions
diff --git a/src/api/posts.rs b/src/api/posts.rs
index ee590ec..57b2ca8 100644
--- a/src/api/posts.rs
+++ b/src/api/posts.rs
@@ -100,10 +100,10 @@ impl Check for PostPageRequest {
}
async fn page(
- AuthorizedUser(_user): AuthorizedUser,
+ AuthorizedUser(user): AuthorizedUser,
Json(body): Json<PostPageRequest>,
) -> Response {
- let Ok(posts) = Post::from_post_page(body.page) else {
+ let Ok(posts) = Post::from_post_page(user.user_id, body.page) else {
return ResponseCode::InternalServerError.text("Failed to fetch posts")
};
@@ -196,10 +196,10 @@ impl Check for UsersPostsRequest {
}
async fn user(
- AuthorizedUser(_user): AuthorizedUser,
+ AuthorizedUser(user): AuthorizedUser,
Json(body): Json<UsersPostsRequest>,
) -> Response {
- let Ok(posts) = Post::from_user_post_page(body.user_id, body.page) else {
+ let Ok(posts) = Post::from_user_post_page(user.user_id, body.user_id, body.page) else {
return ResponseCode::InternalServerError.text("Failed to fetch posts")
};
diff --git a/src/database/posts.rs b/src/database/posts.rs
index 7da3bf0..c33e7e7 100644
--- a/src/database/posts.rs
+++ b/src/database/posts.rs
@@ -31,7 +31,6 @@ fn post_from_row(row: &Row) -> Result<Post, rusqlite::Error> {
let comments = comments::get_comments_page(0, post_id).unwrap_or_else(|_| Vec::new());
let likes = likes::get_like_count(post_id).unwrap_or(None).unwrap_or(0);
- let liked = likes::get_liked(user_id, post_id).unwrap_or(false);
Ok(Post {
post_id,
@@ -39,7 +38,7 @@ fn post_from_row(row: &Row) -> Result<Post, rusqlite::Error> {
content,
date,
likes,
- liked,
+ liked: false,
comments,
})
}
diff --git a/src/types/post.rs b/src/types/post.rs
index a067512..09f2f50 100644
--- a/src/types/post.rs
+++ b/src/types/post.rs
@@ -28,27 +28,42 @@ impl fmt::Debug for Post {
impl Post {
#[instrument()]
- pub fn from_post_id(post_id: u64) -> Result<Self> {
- let Ok(Some(post)) = database::posts::get_post(post_id) else {
+ pub fn from_post_id(self_id: u64, post_id: u64) -> Result<Self> {
+ let Ok(Some(mut post)) = database::posts::get_post(post_id) else {
return Err(ResponseCode::BadRequest.text("Post does not exist"))
};
+ let liked = database::likes::get_liked(self_id, post.post_id).unwrap_or(false);
+ post.liked = liked;
+
Ok(post)
}
#[instrument()]
- pub fn from_post_page(page: u64) -> Result<Vec<Self>> {
- let Ok(posts) = database::posts::get_post_page(page) else {
+ pub fn from_post_page(self_id: u64, page: u64) -> Result<Vec<Self>> {
+ let Ok(mut posts) = database::posts::get_post_page(page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
};
+
+ for post in &mut posts {
+ let liked = database::likes::get_liked(self_id, post.post_id).unwrap_or(false);
+ post.liked = liked;
+ }
+
Ok(posts)
}
#[instrument()]
- pub fn from_user_post_page(user_id: u64, page: u64) -> Result<Vec<Self>> {
- let Ok(posts) = database::posts::get_users_post_page(user_id, page) else {
+ pub fn from_user_post_page(self_id: u64, user_id: u64, page: u64) -> Result<Vec<Self>> {
+ let Ok(mut posts) = database::posts::get_users_post_page(user_id, page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
};
+
+ for post in &mut posts {
+ let liked = database::likes::get_liked(self_id, post.post_id).unwrap_or(false);
+ post.liked = liked;
+ }
+
Ok(posts)
}