From d8bb6c30e9fc450b42f7250aba8838d2f29155ca Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Thu, 17 Oct 2024 14:33:40 +0200 Subject: [PATCH] Add voting icons --- pages/posts.html | 18 +++++++- src/endpoints.c | 92 ++++++++++++++++++++++++----------------- static/global.css | 50 ++++++++++++++++++++-- static/upvote_empty.svg | 1 + static/upvote_full.svg | 1 + 5 files changed, 119 insertions(+), 43 deletions(-) create mode 100644 static/upvote_empty.svg create mode 100644 static/upvote_full.svg diff --git a/pages/posts.html b/pages/posts.html index 33f20d1..a9f13a0 100644 --- a/pages/posts.html +++ b/pages/posts.html @@ -37,10 +37,24 @@
- {{post.upvotes}} + + {{post.upvotes}} + {% if post.upvoted %} + + {% else %} + + {% end %} + - {{post.downvotes}} + + {{post.downvotes}} + {% if post.downvoted %} + + {% else %} + + {% end %} + by {{post.author}} at {{post.created_}} diff --git a/src/endpoints.c b/src/endpoints.c index fbfd0c2..c908ebf 100644 --- a/src/endpoints.c +++ b/src/endpoints.c @@ -107,15 +107,19 @@ void free_endpoints(void) sqlite3_close(db); } -void respond_with_post_details_html(ResponseBuilder *b, int details_post_id) +void respond_with_post_details_html(ResponseBuilder *b, int details_post_id, string login_username) { sqlite3_stmt *stmt = sqlite3_utils_prepare(db, "SELECT P.author, " " STRFTIME('%d/%m/%Y, %H.%M', P.created) AS created_, " " (SELECT COUNT(*) FROM Comments AS C WHERE C.parent = P.id) AS num_comments, " " (SELECT COUNT(*) FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=1) AS upvotes, " - " (SELECT COUNT(*) FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=0) AS downvotes " - "FROM Posts AS P"); + " (SELECT COUNT(*) FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=0) AS downvotes, " + " EXISTS(SELECT * FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=1 AND PV.user=:s) AS upvoted, " + " EXISTS(SELECT * FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=0 AND PV.user=:s) AS downvoted " + "FROM Posts AS P", + login_username, + login_username); if (stmt == NULL) { status_line(b, 500); return; @@ -125,36 +129,46 @@ void respond_with_post_details_html(ResponseBuilder *b, int details_post_id) int num_comments; int upvotes; int downvotes; - if (sqlite3_utils_fetch(stmt, "ssiii", &author, &created, &num_comments, &upvotes, &downvotes)) { + int upvoted; + int downvoted; + if (sqlite3_utils_fetch(stmt, "ssiiiii", &author, &created, &num_comments, &upvotes, &downvotes, &upvoted, &downvoted)) { sqlite3_finalize(stmt); status_line(b, 500); return; } status_line(b, 200); append_content_f(b, - "
\ - \ - \ - \ - \ - \ - \ - \ -
\ - %d \ - \ - %d \ - \ - by %.*s at %.*s \ - \ - %d comments \ -
\ -
", - details_post_id, upvotes, details_post_id, downvotes, - (int) author.size, author.data, - (int) author.size, author.data, - (int) created.size, created.data, - num_comments); + "
\ + \ + \ + \ + \ + \ + \ + \ +
\ + \ + %d\ + \ + \ + \ + \ + %d\ + \ + \ + \ + by %.*s at %.*s\ + \ + %d comments\ +
\ +
", + details_post_id, upvotes, upvoted ? "/static/upvote_full.svg" : "/static/upvote_empty.svg", + details_post_id, downvotes, downvoted ? "/static/upvote_full.svg" : "/static/upvote_empty.svg", + (int) author.size, author.data, + (int) author.size, author.data, + (int) created.size, created.data, + num_comments + ); sqlite3_finalize(stmt); } @@ -175,23 +189,23 @@ static bool voting_endpoints(Request request, ResponseBuilder *b, string login_u // Create the UP row if (sqlite3_utils_exec(db, "INSERT INTO PostVotes(user, post, up) VALUES (:s, :i, :i)", login_username, vote_post_id, up)) { - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } // If we failed, maybe we need to delete the current UP row if (sqlite3_utils_exec(db, "DELETE FROM PostVotes WHERE up=:i AND user=:s AND post=:i", up, login_username, vote_post_id) && sqlite3_changes(db) > 0) { - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } // If we failed, maybe we need to invert the UP row if (sqlite3_utils_exec(db, "UPDATE PostVotes SET up=:i WHERE user=:s AND post=:i", up, login_username, vote_post_id)) { - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } @@ -206,23 +220,23 @@ static bool voting_endpoints(Request request, ResponseBuilder *b, string login_u // Create the UP row if (sqlite3_utils_exec(db, "INSERT INTO PostVotes(user, post, up) VALUES (:s, :i, :i)", login_username, vote_post_id, up)) { - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } // If we failed, maybe we need to delete the current UP row if (sqlite3_utils_exec(db, "DELETE FROM PostVotes WHERE up=:i AND user=:s AND post=:i", up, login_username, vote_post_id) && sqlite3_changes(db) > 0) { - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } // If we failed, maybe we need to invert the UP row if (sqlite3_utils_exec(db, "UPDATE PostVotes SET up=:i WHERE user=:s AND post=:i", up, login_username, vote_post_id)) { - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } - respond_with_post_details_html(b, vote_post_id); + respond_with_post_details_html(b, vote_post_id, login_username); return true; } @@ -394,8 +408,12 @@ void respond(Request request, ResponseBuilder *b) " STRFTIME('%d/%m/%Y, %H.%M', P.created) AS created_, " " (SELECT COUNT(*) FROM Comments AS C WHERE C.parent = P.id) AS num_comments, " " (SELECT COUNT(*) FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=1) AS upvotes, " - " (SELECT COUNT(*) FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=0) AS downvotes " - "FROM Posts AS P"); + " (SELECT COUNT(*) FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=0) AS downvotes, " + " EXISTS(SELECT * FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=1 AND PV.user = :s) AS upvoted, " + " EXISTS(SELECT * FROM PostVotes AS PV WHERE PV.post = P.id AND PV.up=0 AND PV.user = :s) AS downvoted " + "FROM Posts AS P", + login_username, + login_username); if (stmt == NULL) { status_line(b, 500); return; diff --git a/static/global.css b/static/global.css index 711e92f..cc8bdce 100644 --- a/static/global.css +++ b/static/global.css @@ -96,8 +96,9 @@ form input[type=submit] { .post-preview { margin-top: 10px; - border: 1px solid #ccc; + border: 1px solid #404040; border-radius: 3px; + background: white; } .post-preview-title { @@ -105,27 +106,50 @@ form input[type=submit] { } .post-preview-title a { - color: white; + color: #333; font-size: 30px; text-decoration: none; } .post-preview-details { + height: 40px; + background: #eee; border-top: 1px solid #ccc; + border-bottom-left-radius: 3px; + border-bottom-right-radius: 3px; } .post-preview-details table { border-collapse: collapse; + height: 100%; + width: 100%; +} + +.post-preview-details table tr { } .post-preview-details table td { + vertical-align: middle; + display: table-cell; + height: 100%; border-right: 1px solid #ccc; - padding: 10px; +} + +.post-preview-details tr td:nth-child(1) { + width: 40px; +} + +.post-preview-details tr td:nth-child(2) { + width: 40px; +} + +.post-preview-details tr td:last-child { + width: 100px; } .post-preview-details span, .post-preview-details a { - color: #ccc; + color: black; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; } @@ -141,4 +165,22 @@ form input[type=submit] { background: #ccd6fb; border-radius: 3px; margin-top: 10px; +} + +/* === MISC ======================================================== */ + +.rotate90 { + -webkit-transform: rotate(90deg); + -moz-transform: rotate(90deg); + -o-transform: rotate(90deg); + -ms-transform: rotate(90deg); + transform: rotate(90deg); +} + +.rotate180 { + -webkit-transform: rotate(180deg); + -moz-transform: rotate(180deg); + -o-transform: rotate(180deg); + -ms-transform: rotate(180deg); + transform: rotate(180deg); } \ No newline at end of file diff --git a/static/upvote_empty.svg b/static/upvote_empty.svg new file mode 100644 index 0000000..8a79591 --- /dev/null +++ b/static/upvote_empty.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/static/upvote_full.svg b/static/upvote_full.svg new file mode 100644 index 0000000..d4c9baf --- /dev/null +++ b/static/upvote_full.svg @@ -0,0 +1 @@ + \ No newline at end of file