bug fixes

This commit is contained in:
Tyler Murphy 2023-02-14 22:16:29 -05:00
parent f02524b592
commit 192be95f84
5 changed files with 35 additions and 14 deletions

View file

@ -18,6 +18,8 @@
<link rel="stylesheet" href="/css/main.css"> <link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/css/header.css"> <link rel="stylesheet" href="/css/header.css">
<link rel="stylesheet" href="/css/admin.css"> <link rel="stylesheet" href="/css/admin.css">
<script src="/js/admin.js" type="module"></script>
</head> </head>
<body> <body>
<div id="header"> <div id="header">

View file

@ -22,8 +22,13 @@ async function post() {
comments: [] comments: []
} }
data.posts.unshift(post) data.posts.unshift(post)
let html = parsePost(post) let html = parsePost(post, data.users, data.self)
prepend(html, posts_block)
posts_block.insertBefore(
html,
posts_block.firstChild
)
document.getElementById('popup').classList.add('hidden') document.getElementById('popup').classList.add('hidden')
} }

View file

@ -100,10 +100,10 @@ impl Check for PostPageRequest {
} }
async fn page( async fn page(
AuthorizedUser(_user): AuthorizedUser, AuthorizedUser(user): AuthorizedUser,
Json(body): Json<PostPageRequest>, Json(body): Json<PostPageRequest>,
) -> Response { ) -> 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") return ResponseCode::InternalServerError.text("Failed to fetch posts")
}; };
@ -196,10 +196,10 @@ impl Check for UsersPostsRequest {
} }
async fn user( async fn user(
AuthorizedUser(_user): AuthorizedUser, AuthorizedUser(user): AuthorizedUser,
Json(body): Json<UsersPostsRequest>, Json(body): Json<UsersPostsRequest>,
) -> Response { ) -> 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") return ResponseCode::InternalServerError.text("Failed to fetch posts")
}; };

View file

@ -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 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 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 { Ok(Post {
post_id, post_id,
@ -39,7 +38,7 @@ fn post_from_row(row: &Row) -> Result<Post, rusqlite::Error> {
content, content,
date, date,
likes, likes,
liked, liked: false,
comments, comments,
}) })
} }

View file

@ -28,27 +28,42 @@ impl fmt::Debug for Post {
impl Post { impl Post {
#[instrument()] #[instrument()]
pub fn from_post_id(post_id: u64) -> Result<Self> { pub fn from_post_id(self_id: u64, post_id: u64) -> Result<Self> {
let Ok(Some(post)) = database::posts::get_post(post_id) else { let Ok(Some(mut post)) = database::posts::get_post(post_id) else {
return Err(ResponseCode::BadRequest.text("Post does not exist")) 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) Ok(post)
} }
#[instrument()] #[instrument()]
pub fn from_post_page(page: u64) -> Result<Vec<Self>> { pub fn from_post_page(self_id: u64, page: u64) -> Result<Vec<Self>> {
let Ok(posts) = database::posts::get_post_page(page) else { let Ok(mut posts) = database::posts::get_post_page(page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts")) 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) Ok(posts)
} }
#[instrument()] #[instrument()]
pub fn from_user_post_page(user_id: u64, page: u64) -> Result<Vec<Self>> { pub fn from_user_post_page(self_id: u64, user_id: u64, page: u64) -> Result<Vec<Self>> {
let Ok(posts) = database::posts::get_users_post_page(user_id, page) else { let Ok(mut posts) = database::posts::get_users_post_page(user_id, page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts")) 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) Ok(posts)
} }