From b29282e4d367f79f42794db42cd567d6efc1214b Mon Sep 17 00:00:00 2001 From: niansa Date: Sun, 10 Jan 2021 18:21:27 +0100 Subject: [PATCH] Per-bot-user voting --- controllers/views.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/controllers/views.cc b/controllers/views.cc index 01b824f..b39c152 100644 --- a/controllers/views.cc +++ b/controllers/views.cc @@ -5,11 +5,12 @@ #include "../config.h" #include "views.h" -static unordered_map last_votes; +static unordered_map last_votes; #define isAuthed() getOptional("discord_authed").has_value() #define authenticate(cb) cb(HttpResponse::newRedirectionResponse(OAUTH_URL)); return #define toStart(cb) cb(HttpResponse::newRedirectionResponse("/")); return -#define cantVote(uid) auto _cantvoteres = last_votes.find(uid); auto now = trantor::Date::date(); if (_cantvoteres != last_votes.end() and now < _cantvoteres->second.after(43200)) +#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)) @@ -88,7 +89,7 @@ void views::botdetail( uint64_t bot_id) { db->execSqlAsync("SELECT * FROM bots WHERE app_id = '"+std::to_string(bot_id)+"'", - [req, callback] (const orm::Result &rows) { + [req, callback, bot_id] (const orm::Result &rows) { if (rows.empty()) { // Bot not found callback(HttpResponse::newNotFoundResponse()); @@ -98,7 +99,7 @@ void views::botdetail( HttpViewData data; data.insert("bot_id", bot.app_id); data.insert("bot", bot); - {cantVote(req->session()->get("discord_user_id")) { + {cantVote(voteID(req->session()->get("discord_user_id"), bot_id)) { data.insert("canVote", false); } else { data.insert("canVote", true); @@ -118,18 +119,19 @@ void views::botvote(const HttpRequestPtr& req, std::functionget("discord_user_id"); - {cantVote(user_id) { + auto vote_id = voteID(user_id, bot_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)+"'", - [user_id, callback] (const orm::Result &rows) { + [vote_id, callback] (const orm::Result &rows) { if (rows.affectedRows() == 0) { // Bot not found callback(HttpResponse::newNotFoundResponse()); } else { - last_votes[user_id] = trantor::Date::date(); + last_votes[vote_id] = trantor::Date::date(); // Redirect back callback(HttpResponse::newRedirectionResponse("detail")); }