From 4a4d0dcbf4bae85a307e58af31eb6aea6d351510 Mon Sep 17 00:00:00 2001 From: niansa Date: Thu, 14 Jan 2021 08:46:44 +0100 Subject: [PATCH] Improved main style --- config.h | 2 ++ controllers/views.cc | 45 ++++++++++++++++++++++++++++++++++-------- controllers/views.h | 4 ++++ static/global.css | 47 ++++++++++++++++++++++++++++++++++++++++++-- views/botdetail.csp | 9 ++++----- views/botlist.csp | 28 ++++++++++++-------------- 6 files changed, 105 insertions(+), 30 deletions(-) diff --git a/config.h b/config.h index c1a6145..f193f79 100644 --- a/config.h +++ b/config.h @@ -14,4 +14,6 @@ #define REDIRECT_URI "http://localhost:8082/discordauth" #define BOT_TOKEN "Nzk3NTY1NTkyODM1NDU3MDI0.X_oU1w.fCVL8j58pphoEaU7e3q4yH7cFa4" +#define MODERATORS {609486822715818000} + #define COMMUNITY "https://discord.gg/ZMj5ytaQRZ" diff --git a/controllers/views.cc b/controllers/views.cc index f84e3d5..b06776b 100644 --- a/controllers/views.cc +++ b/controllers/views.cc @@ -11,7 +11,6 @@ #include "../config.h" #include "views.h" -static unordered_map last_votes; #define authenticate(cb) cb(HttpResponse::newRedirectionResponse(OAUTH_URL)); return #define toStartPage(cb) cb(HttpResponse::newRedirectionResponse("/")); return #define voteID(uid, bid) std::to_string(uid)+'-'+std::to_string(bid) @@ -117,6 +116,7 @@ void LoginFilter::doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, Filt views::views() { db = drogon::app().getDbClient(); + moderators = MODERATORS; } void views::start( @@ -175,7 +175,7 @@ void views::botdetail( uint64_t bot_id) { db->execSqlAsync("SELECT * FROM bots WHERE app_id = '"+std::to_string(bot_id)+"'", - [req, callback, bot_id] (const orm::Result &rows) { + [this, req, callback, bot_id] (const orm::Result &rows) { if (rows.empty()) { // Bot not found callback(HttpResponse::newNotFoundResponse()); @@ -184,10 +184,7 @@ void views::botdetail( auto sessionData = getSessionData(req->session()); auto bot = deserializeBot(rows[0]); auto data = HttpViewDataPrep(sessionData); - data.insert("modView", false); - data.insert("bot_id", bot.app_id); data.insert("bot", bot); - data.insert("owner", sessionData and sessionData->discord_id == bot.owner_id); if (not sessionData) { // Vote button is not grezed out if user isn"t logged in data.insert("canVote", bot.approved); @@ -213,7 +210,7 @@ void views::botvote(const HttpRequestPtr& req, std::functionexecSqlAsync("UPDATE bots SET votes = votes + 1 WHERE app_id = '"+std::to_string(bot_id)+"'", - [vote_id, callback] (const orm::Result &rows) { + [this, vote_id, callback] (const orm::Result &rows) { if (rows.affectedRows() == 0) { // Bot not found callback(HttpResponse::newNotFoundResponse()); @@ -227,6 +224,37 @@ void views::botvote(const HttpRequestPtr& req, std::function &&callback, + uint64_t bot_id, const std::string& action) { + auto sessionData = getSessionData(req->session()); + db->execSqlAsync("SELECT * FROM bots WHERE app_id = '"+std::to_string(bot_id)+"'", + [sessionData, callback, action] (const orm::Result &rows) { + if (rows.empty()) { + callback(HttpResponse::newNotFoundResponse()); + } else { + auto bot = deserializeBot(rows[0]); + // Check if user is botowner or moderator + if (sessionData->discord_id == bot.owner_id or sessionData->moderator) { + // Get action + if (action == "edit") { + // TODO + } else if (action == "delete") { + // TODO + } else if (not sessionData->moderator) { + goto else_part; + } else if (action == "approve") { + // TODO + } else if (action == "decline") { + // TODO + } else { + else_part: + callback(HttpResponse::newNotFoundResponse()); + } + } + } + }, dbErr); +} + void views::botregister_view(const HttpRequestPtr& req, std::function &&callback, const std::string& error) { // Display page @@ -309,7 +337,7 @@ void views::discordauth( req->setParameter("scope", "identify"); req->setParameter("code", code); } - discordapi->sendRequest(req, [discordapi, client_req, callback] (ReqResult, const HttpResponsePtr &response) { + discordapi->sendRequest(req, [this, discordapi, client_req, callback] (ReqResult, const HttpResponsePtr &response) { // Check for success if (response->getStatusCode() == HttpStatusCode::k200OK) { // Auth success @@ -324,13 +352,14 @@ void views::discordauth( req->setPath("/api/v8/users/@me"); req->setMethod(HttpMethod::Get); req->addHeader("Authorization", "Bearer "+sessionData->discord_access_token); - discordapi->sendRequest(req, [client_req, callback, session, sessionData] (ReqResult, const HttpResponsePtr &response) { + discordapi->sendRequest(req, [this, client_req, callback, session, sessionData] (ReqResult, const HttpResponsePtr &response) { if (response->getStatusCode() == HttpStatusCode::k200OK) { // Getting user data success auto &userdata = *response->getJsonObject().get(); sessionData->discord_id = std::stoul(userdata["id"].asString()); sessionData->discord_username = userdata["username"].asString(); sessionData->discord_discriminator = userdata["discriminator"].asString(); + sessionData->moderator = std::find(moderators.begin(), moderators.end(), sessionData->discord_id) != moderators.end(); setSessionData(session, sessionData); // Show success page auto data = HttpViewDataPrep(sessionData); diff --git a/controllers/views.h b/controllers/views.h index b98ca66..f676d2b 100644 --- a/controllers/views.h +++ b/controllers/views.h @@ -31,6 +31,8 @@ public: using namespace drogon; class views: public drogon::HttpController { orm::DbClientPtr db; + std::unordered_map last_votes; + std::vector moderators; public: views(); void start(const HttpRequestPtr&, std::function &&); @@ -38,6 +40,7 @@ public: void botlist(const HttpRequestPtr&, std::function &&); void botdetail(const HttpRequestPtr&, std::function &&, uint64_t); void botvote(const HttpRequestPtr&, std::function &&, uint64_t); + void botedit(const HttpRequestPtr&, std::function &&, uint64_t, const std::string&); void botregister_view(const HttpRequestPtr&, std::function &&, const std::string&); void botregister_submit(const HttpRequestPtr&, std::function &&); void discordauth(const HttpRequestPtr&, std::function &&, const std::string&); @@ -51,6 +54,7 @@ public: ADD_METHOD_TO(views::botregister_submit, "/bots/register", Post, "LoginFilter"); ADD_METHOD_TO(views::botdetail, "/bots/{1}/detail", Get); ADD_METHOD_TO(views::botvote, "/bots/{1}/vote", Get, "LoginFilter"); + ADD_METHOD_TO(views::botedit, "/bots/{1}/edit/{2}", Get, "LoginFilter"); ADD_METHOD_TO(views::discordauth, "/discordauth?code={1}", Get); ADD_METHOD_TO(views::discorddeauth, "/discorddeauth", Get); ADD_METHOD_TO(views::menu, "/menu", Get); diff --git a/static/global.css b/static/global.css index 728eccb..f243d26 100644 --- a/static/global.css +++ b/static/global.css @@ -75,10 +75,53 @@ body { text-align: center; } +.votesbox { + background-color:#39004d; + color:#FFFFFF; + padding:4px; + margin:10px; + border-radius:4px; + height:min-content; +} +.votestext { + line-height:0px; + font-size:10px; +} + .content-desktop {display: block;} .content-mobile {display: none;} @media screen and (max-width: 1000px) { -.content-desktop {display: none;} -.content-mobile {display: block;} + .content-desktop {display: none;} + .content-mobile {display: block;} +} + + +.tile { + max-width: 20vw; + min-width: 20vw; +} +.tilespacer { + display:none; +} + +@media screen and (max-width: 1580px) { + .tile { + max-width: 15vw; + min-width: 15vw; + } +} + +@media screen and (max-width: 900px) { + .tile { + border:unset; + background-color:transparent !important; + max-width: 100%; + min-width: 100%; + padding:0; + margin:0; + } + .tilespacer { + display:block; + } } diff --git a/views/botdetail.csp b/views/botdetail.csp index cfa7cac..85660f9 100644 --- a/views/botdetail.csp +++ b/views/botdetail.csp @@ -1,9 +1,8 @@ <%inc#include "controllers/views.h" %> -<%c++ auto bot_id = @@.get("bot_id");%> <%c++ auto bot = @@.get("bot");%> <%c++ auto canVote = @@.get("canVote");%> <%c++ auto owner = @@.get("owner");%> -<%c++ auto modView = @@.get("modView");%> +<%c++ auto sessionData = @@.get("sessionData");%> <%layout global_layout%> @@ -18,12 +17,12 @@
{%bot.name%}
- <%c++ if (owner or modView) {%> + <%c++ if (sessionData and (bot.owner_id == sessionData->discord_id or sessionData->moderator)) {%> - <%c++ if (modView) {%> + <%c++ if (sessionData->moderator and not bot.approved) {%>
Approve Decline @@ -31,7 +30,7 @@ <%c++ }%> <%c++ }%>
diff --git a/views/botlist.csp b/views/botlist.csp index 9e4e31d..46110d8 100644 --- a/views/botlist.csp +++ b/views/botlist.csp @@ -20,21 +20,19 @@

<%c++ for (const auto& [bot_id, bot] : @@.get>("bots")) {%> - - - - - - -
- - -

{%bot.name%}

-

{%bot.short_description%}

- <%c++ if (not bot.approved) {%> - Not yet approved - <%c++ }%> -
+
+ +
+
+

Votes: {%bot.votes%}

+
+
+

{%bot.name%}

+

{%bot.short_description%}

+ <%c++ if (not bot.approved) {%> + Not yet approved + <%c++ }%>
+
<%c++ }%>