1
0
Fork 0
mirror of https://gitlab.com/niansa/discordlistforbots.git synced 2025-03-06 20:49:22 +01:00

Per-bot-user voting

This commit is contained in:
niansa 2021-01-10 18:21:27 +01:00
parent 980c9ffb95
commit b29282e4d3

View file

@ -5,11 +5,12 @@
#include "../config.h"
#include "views.h"
static unordered_map <uint64_t, trantor::Date> last_votes;
static unordered_map <std::string, trantor::Date> last_votes;
#define isAuthed() getOptional<bool>("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<uint64_t>("discord_user_id")) {
{cantVote(voteID(req->session()->get<uint64_t>("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::function<void (const HttpRes
}
// Check if user is able to vote again
auto user_id = session->get<uint64_t>("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"));
}