From 34c3e3cd3d195367d5ca74a04cfe2c364ce9b39e Mon Sep 17 00:00:00 2001 From: niansa Date: Mon, 11 Jan 2021 20:53:05 +0100 Subject: [PATCH] Added a few buttons and slt --- bots.sql | 6 ++- controllers/views.cc | 95 ++++++++++++++++++++++++++------------------ views/botdetail.csp | 24 +++++++---- 3 files changed, 77 insertions(+), 48 deletions(-) diff --git a/bots.sql b/bots.sql index cb13120..bc8481f 100644 --- a/bots.sql +++ b/bots.sql @@ -35,7 +35,8 @@ CREATE TABLE public.bots ( owner_id text, app_id text, votes integer, - approved boolean + approved boolean, + CONSTRAINT can_vote_check CHECK ((((votes > 0) AND approved) OR (votes < 1))) ); @@ -47,7 +48,8 @@ ALTER TABLE public.bots OWNER TO nils; COPY public.bots (name, short_description, long_description, avatar_url, owner, support_server, prefix, owner_id, app_id, votes, approved) FROM stdin; DFB This Bot is for the Server DFB This bot was created for our Discordlist for Bots Server https://cdn.discordapp.com/avatars/795612465130897420/c3bd0733f876a664b4b79ec03866f131.png Julius#1755 42vDtZxZSt dfb? 703944517048598568 795612465130897420 3744 t -Tuxiflux A fun but simple bot with globalchat Tuxiflux is a funny, useful and intuitive bot for server moderation and play with in-bot money. https://cdn.discordapp.com/embed/avatars/2.png Tuxifan#4660 6smrmKkjP7 t# 609486822715818000 788310535799308288 7 t +Tuxiflux A fun but simple bot with globalchat Tuxiflux is a funny, useful and intuitive bot for server moderation and play with in-bot money. https://cdn.discordapp.com/embed/avatars/2.png Tuxifan#4660 6smrmKkjP7 t# 609486822715818000 788310535799308288 9 t +MEE6 szreszxer eszxertgszx https://cdn.discordapp.com/avatars/159985870458322944/b50adff099924dd5e6b72d13f77eb9d7.png Tuxifan#4660 erdsxzt redsxz 609486822715818000 159985870458322944 0 f \. diff --git a/controllers/views.cc b/controllers/views.cc index 9a3d4ed..56b3f45 100644 --- a/controllers/views.cc +++ b/controllers/views.cc @@ -15,7 +15,7 @@ static unordered_map last_votes; #define authenticate(cb) cb(HttpResponse::newRedirectionResponse(OAUTH_URL)); return #define toStart(cb) cb(HttpResponse::newRedirectionResponse("/")); return #define voteID(uid, bid) std::to_string(uid)+'-'+std::to_string(bid) -#define cantVote(vid) auto _cantvoteres = last_votes.find(vid); auto now = trantor::Date::date(); if (_cantvoteres != last_votes.end() and now < _cantvoteres->second.after(43200)) +#define cantVote(vid, ...) {auto _cantvoteres = last_votes.find(vid); auto now = trantor::Date::date(); if (_cantvoteres != last_votes.end() and now < _cantvoteres->second.after(43200)) __VA_ARGS__} @@ -47,6 +47,19 @@ std::string dbEsc(const std::string& src) { } return fres.str(); } +std::string htmlEsc(const std::string& src) { + std::ostringstream fres; + for (const auto &character : src) { + switch (character) { + case '<': fres << "<"; break; + case '>': fres << ">"; break; + case '&': fres << "&"; break; + case '"': fres << """; break; + default: fres << character; + } + } + return fres.str(); +} auto errPage(const std::exception& e) { HttpViewData data; @@ -146,15 +159,18 @@ void views::botdetail( callback(HttpResponse::newNotFoundResponse()); } else { // Bot found + auto session = req->session(); auto bot = deserializeBot(rows[0]); HttpViewData data; + data.insert("modView", false); data.insert("bot_id", bot.app_id); data.insert("bot", bot); - {cantVote(voteID(req->session()->get("discord_user_id"), bot_id)) { + data.insert("owner", session->isAuthed() and session->get("discord_user_id") == bot.owner_id); + cantVote(voteID(req->session()->get("discord_user_id"), bot_id), { data.insert("canVote", false); } else { - data.insert("canVote", true); - }} + data.insert("canVote", bot.approved); + }) callback(HttpResponse::newHttpViewResponse("botdetail.csp", data)); } @@ -167,10 +183,10 @@ void views::botvote(const HttpRequestPtr& req, std::functionget("discord_user_id"); auto vote_id = voteID(user_id, bot_id); - {cantVote(vote_id) { + cantVote(vote_id, { callback(HttpResponse::newRedirectionResponse("detail")); return; - }} + }) // Register vote db->execSqlAsync("UPDATE bots SET votes = votes + 1 WHERE app_id = '"+std::to_string(bot_id)+"'", [vote_id, callback] (const orm::Result &rows) { @@ -182,7 +198,9 @@ void views::botvote(const HttpRequestPtr& req, std::function &&callback, @@ -198,44 +216,45 @@ void views::botregister_view(const HttpRequestPtr& req, std::function &&callback ) { auto session = req->session(); - // Get and check parameters + // Set error handler auto onError = [callback] (const std::string& e) { callback(HttpResponse::newRedirectionResponse("register?error="+e)); }; + // Get and check parameters + string short_description, long_description, support_server, prefix; + uint64_t app_id; try { - auto app_id = std::stoul(req->getParameter("app_id")); - auto short_description = req->getParameter("short_description"); - auto long_description = req->getParameter("long_description"); - auto support_server = req->getParameter("support_server"); - auto prefix = req->getParameter("prefix"); - // Check if bot already exists - db->execSqlAsync("select 1 from bots where app_id ='"+std::to_string(app_id)+"'", - [=] (const orm::Result &r) { - if (not r.empty()) { - onError("Bot%20has%20already%20been%20registered"); - return; - } - // Get bots avatar - getUser(app_id, [=] (const Json::Value& botuser) { - // Check result - if (botuser.empty() or not botuser["bot"].asBool()) { - onError("Invalid%20client%20ID"); - return; - } - // Perform database operation - db->execSqlAsync(fmt::format("INSERT INTO bots (name, short_description, long_description, avatar_url, owner, support_server, prefix, owner_id, app_id, votes, approved) " - "VALUES('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', 0, 'f')", - dbEsc(botuser["username"].asString()), dbEsc(short_description), dbEsc(long_description), dbEsc(botuser["avatar_url"].asString()), session->get("discord_user_fullname"), dbEsc(support_server), dbEsc(prefix), session->get("discord_user_id"), app_id), - [app_id, callback] (const orm::Result &) { - callback(HttpResponse::newRedirectionResponse(std::to_string(app_id)+"/detail")); - }, [onError] (const orm::DrogonDbException &e) { - onError(e.base().what()); - }); - }); - }, dbErr); + app_id = std::stoul(req->getParameter("app_id")); + short_description = htmlEsc(req->getParameter("short_description")); + long_description = htmlEsc(req->getParameter("long_description")); + support_server = htmlEsc(req->getParameter("support_server")); + prefix = htmlEsc(req->getParameter("prefix")); } catch (std::exception& e) { onError(e.what()); } + // Check if bot already exists + db->execSqlAsync("select 1 from bots where app_id ='"+std::to_string(app_id)+"'", + [=] (const orm::Result &r) { + if (not r.empty()) { + onError("Bot%20has%20already%20been%20registered"); + return; + } + // Get bots avatar + getUser(app_id, [=] (const Json::Value& botuser) { + // Check result + if (botuser.empty() or not botuser["bot"].asBool()) { + onError("Invalid%20client%20ID"); + return; + } + // Perform database operation + db->execSqlAsync(fmt::format("INSERT INTO bots (name, short_description, long_description, avatar_url, owner, support_server, prefix, owner_id, app_id, votes, approved) " + "VALUES('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', 0, 'f')", + dbEsc(botuser["username"].asString()), dbEsc(short_description), dbEsc(long_description), dbEsc(botuser["avatar_url"].asString()), session->get("discord_user_fullname"), dbEsc(support_server), dbEsc(prefix), session->get("discord_user_id"), app_id), + [app_id, callback] (const orm::Result &) { + callback(HttpResponse::newRedirectionResponse(std::to_string(app_id)+"/detail")); + }, dbErr); + }); + }, dbErr); } void views::discorddeauth( diff --git a/views/botdetail.csp b/views/botdetail.csp index 72d11a3..fc0d3a0 100644 --- a/views/botdetail.csp +++ b/views/botdetail.csp @@ -2,6 +2,8 @@ <%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");%> @@ -23,11 +25,21 @@
{%bot.name%}
-
-
- Invite - Vote + <%c++ if (owner or modView) {%> +
+ Edit + Delete
+ <%c++ if (modView) {%> + + <%c++ }%> + <%c++ }%> +

@@ -48,10 +60,6 @@ Owner {%bot.owner%} - - Votes - {%bot.votes%} -
Support Server