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

Implemented bot editing and deleting

This commit is contained in:
niansa 2021-01-15 10:02:45 +01:00
parent 4a4d0dcbf4
commit 666b6e3405
8 changed files with 146 additions and 41 deletions

View file

@ -6,6 +6,7 @@
#include <memory>
#include <exception>
#include <functional>
#include <random>
#include <fmt/core.h>
#include <drogon/drogon.h>
#include "../config.h"
@ -119,6 +120,18 @@ views::views() {
moderators = MODERATORS;
}
std::string views::htmlBr(const std::string& src) {
std::ostringstream fres;
for (const auto& character : src) {
if (character == '\n') {
fres << "<br>";
} else {
fres << character;
}
}
return fres.str();
}
void views::start(
const HttpRequestPtr&, std::function<void (const HttpResponsePtr &)> &&callback
)
@ -228,18 +241,71 @@ void views::botedit(const HttpRequestPtr& req, std::function<void (const HttpRes
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) {
[this, sessionData, callback, action, req] (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) {
auto final = [callback] () {
callback(HttpResponse::newRedirectionResponse("../detail"));
};
// Get action
if (action == "edit") {
// TODO
switch (req->getMethod()) {
case Get: {
auto data = HttpViewDataPrep(sessionData);
data.insert("bot", bot);
callback(HttpResponse::newHttpViewResponse("botedit.csp", data));
} break;
case Post: {
bool refresh = req->getParameter("refresh") == "on";
db->execSqlAsync("UPDATE bots SET short_description = '"+dbEsc(req->getParameter("short_description"))+"',"
"long_description = '"+dbEsc(req->getParameter("long_description"))+"',"
"support_server = '"+dbEsc(req->getParameter("support_server"))+"',"
"prefix = '"+dbEsc(req->getParameter("prefix"))+"' "
"WHERE app_id = '"+std::to_string(bot.app_id)+"'",
[this, sessionData, refresh, bot, final] (const orm::Result &) {
if (refresh) {
auto app_id = bot.app_id;
getUser(app_id, [this, sessionData, app_id, final] (const Json::Value& botuser) {
if (not botuser.empty()) {
db->execSqlAsync("UPDATE bots SET name = '"+dbEsc(botuser["username"].asString())+"',"
"avatar_url = '"+dbEsc(botuser["avatar_url"].asString())+"',"
"owner = '"+dbEsc(sessionData->discord_fullname())+"' "
"WHERE app_id = '"+std::to_string(app_id)+"'",
[final] (const orm::Result &) {
final();
}, dbErr);
} else {
final();
}
});
} else {
final();
}
}, dbErr);
} break;
default: break;
}
} else if (action == "delete") {
// TODO
switch (req->getMethod()) {
case Get: {
auto data = HttpViewDataPrep(sessionData);
data.insert("botname", HttpViewData::htmlTranslate(bot.name));
callback(HttpResponse::newHttpViewResponse("botdelete.csp", data));
} break;
case Post: {
db->execSqlAsync("DELETE FROM bots WHERE app_id = '"+std::to_string(bot.app_id)+"'",
[callback] (const orm::Result &) {
toStartPage(callback);
}, dbErr);
} break;
default: break;
}
} else if (not sessionData->moderator) {
goto else_part;
} else if (action == "approve") {
@ -275,10 +341,10 @@ void views::botregister_submit(const HttpRequestPtr& req, std::function<void (co
uint64_t app_id;
try {
app_id = std::stoul(req->getParameter("app_id"));
short_description = HttpViewData::htmlTranslate(req->getParameter("short_description"));
long_description = HttpViewData::htmlTranslate(req->getParameter("long_description"));
support_server = HttpViewData::htmlTranslate(req->getParameter("support_server"));
prefix = HttpViewData::htmlTranslate(req->getParameter("prefix"));
short_description = req->getParameter("short_description");
long_description = req->getParameter("long_description");
support_server = req->getParameter("support_server");
prefix = req->getParameter("prefix");
} catch (std::exception& e) {
onError(e.what());
}

View file

@ -35,6 +35,7 @@ class views: public drogon::HttpController<views> {
std::vector<uint64_t> moderators;
public:
views();
static std::string htmlBr(const std::string&);
void start(const HttpRequestPtr&, std::function<void (const HttpResponsePtr &)> &&);
void menu(const HttpRequestPtr&, std::function<void (const HttpResponsePtr &)> &&);
void botlist(const HttpRequestPtr&, std::function<void (const HttpResponsePtr &)> &&);
@ -54,7 +55,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::botedit, "/bots/{1}/edit/{2}", Get, Post, "LoginFilter");
ADD_METHOD_TO(views::discordauth, "/discordauth?code={1}", Get);
ADD_METHOD_TO(views::discorddeauth, "/discorddeauth", Get);
ADD_METHOD_TO(views::menu, "/menu", Get);

View file

@ -2,12 +2,12 @@
#include "config.h"
int main() {
//Set HTTP listener address and port
// Set HTTP listener address and port
drogon::app().addListener(LISTEN_ADDR, LISTEN_PORT).
enableSession(std::chrono::minutes(1200)).
createDbClient(DB_TYPE, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD).
setFileTypes({"css", "png", "ico"});
//Run HTTP framework, the method will block in the internal event loop
// Run HTTP framework, the method will block in the internal event loop
drogon::app().run();
return 0;
}

18
static/dontloose.js Normal file
View file

@ -0,0 +1,18 @@
function dontLoose() {
window.onbeforeunload = function (e) {
e = e || window.event;
let txt = "YOU MIGHT LOOSE DATA WHEN CONTINUING";
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = txt;
}
// For Safari
return txt;
};
}
function loose() {
window.onbeforeunload = null;
}

12
views/botdelete.csp Normal file
View file

@ -0,0 +1,12 @@
<%c++ auto botname = @@.get<std::string>("botname");%>
<%layout global_layout%>
<title>Deleting a bot - DFB</title>
<div class="container text-center">
<p class="title">Are you sure you want to delete {%botname%}?</p>
<form action="delete" method="post">
<input class="special-button" style="background-color:red;" type="submit" value="Yes">
<a class="special-button" href="../detail">No</a>
</form>
</div>

View file

@ -15,17 +15,17 @@
<img class="bot-image" src="{%bot.avatar_url%}"></img>
<div class="bot-text text text-center title">
{%bot.name%}
{%HttpViewData::htmlTranslate(bot.name)%}
</div>
<%c++ if (sessionData and (bot.owner_id == sessionData->discord_id or sessionData->moderator)) {%>
<div class="actionsWrapper actions">
<a class="special-button" href="edit" style="background-color:yellow;color:black;">Edit</a>
<a class="special-button" href="delete" style="background-color:red;color:black;">Delete</a>
<a class="special-button" href="edit/edit" style="background-color:yellow;color:black;">Edit</a>
<a class="special-button" href="edit/delete" style="background-color:red;color:black;">Delete</a>
</div>
<%c++ if (sessionData->moderator and not bot.approved) {%>
<div class="actionsWrapper actions">
<a class="special-button" href="approve" style="background-color:green;color:white;">Approve</a>
<a class="special-button" href="decline" style="background-color:black;color:white;">Decline</a>
<a class="special-button" href="edit/approve" style="background-color:green;color:white;">Approve</a>
<a class="special-button" href="edit/decline" style="background-color:black;color:white;">Decline</a>
</div>
<%c++ }%>
<%c++ }%>
@ -37,24 +37,28 @@
<hr>
<br>
<div class="text long-description">
{%bot.long_description%}
{%HttpViewData::htmlTranslate(views::htmlBr(bot.long_description))%}
</div>
<hr>
<div class="text overview">
<h2>Overview</h2>
<table>
<tr>
<td class="overview-key">Prefix</td>
<td class="overview-value">{%bot.prefix%}</td>
</tr>
<%c++ if (not bot.prefix.empty()) {%>
<tr>
<td class="overview-key">Prefix</td>
<td class="overview-value">{%HttpViewData::htmlTranslate(bot.prefix)%}</td>
</tr>
<%c++ }%>
<tr>
<td class="overview-key">Owner</td>
<td class="overview-value">{%bot.owner%}</td>
<td class="overview-value">{%HttpViewData::htmlTranslate(bot.owner)%}</td>
</tr>
</table>
<br>
<a class="special-button" style="margin:0px;" href="https://discord.gg/{%bot.support_server%}">Support Server</a>
<%c++ if (not bot.support_server.empty()) {%>
<a class="special-button" style="margin:0px;" href="https://discord.gg/{%HttpViewData::htmlTranslate(bot.support_server)%}">Support Server</a>
<%c++ }%>
</div>
</div>

19
views/botedit.csp Normal file
View file

@ -0,0 +1,19 @@
<%inc#include "controllers/views.h" %>
<%c++ auto bot = @@.get<Bot>("bot");%>
<%layout global_layout%>
<title>Editing a bot - DFB</title>
<div class="container">
<form action="edit" method="POST">
<p class="title text-center">Editing {%HttpViewData::htmlTranslate(bot.name)%}</p>
<p>Short description</p><input class="special-input maxwidth" name="short_description" maxlength="80" onclick="dontLoose()" value="{%bot.short_description%}" required>
<p>Long description</p><textarea class="special-input maxwidth" name="long_description" style="height:200px;" onclick="dontLoose()" required>{%HttpViewData::htmlTranslate(bot.long_description)%}</textarea>
<p>Prefix</p><input class="special-input maxwidth" name="prefix" maxlength="6" onclick="dontLoose()" value="{%bot.prefix%}">
<p>Permanent support server invite code</p><input class="special-input maxwidth" name="support_server" maxlength="15" minlength="5" onclick="dontLoose()" value="{%bot.support_server%}">
<p>Refresh data</p><input type="checkbox" name="refresh">
<br><br><br>
<input type="submit" class="special-button" style="margin:0px;" onclick="loose()" value="Submit">
</form>
<script src=/dontloose.js”></script>
</div>

View file

@ -12,26 +12,11 @@
<p>Client ID</p><input tyep="number" class="special-input maxwidth" name="app_id" maxlength="19" minlength="17" pattern="^[0-9]+$" onclick="dontLoose()" required>
<p>Short description</p><input class="special-input maxwidth" name="short_description" maxlength="80" onclick="dontLoose()" required>
<p>Long description</p><textarea class="special-input maxwidth" name="long_description" style="height:200px;" onclick="dontLoose()" required></textarea>
<p>Prefix</p><input class="special-input maxwidth" name="prefix" maxlength="6" onclick="dontLoose()" required>
<p>Permanent support server invite code</p><input class="special-input maxwidth" name="support_server" maxlength="15" minlength="5" onclick="dontLoose()" required>
<p>Prefix</p><input class="special-input maxwidth" name="prefix" maxlength="6" onclick="dontLoose()">
<p>Permanent support server invite code</p><input class="special-input maxwidth" name="support_server" maxlength="15" minlength="5" onclick="dontLoose()">
<p>Owner</p><input class="special-input maxwidth" value="{%sessionData->discord_fullname()%}" autocomplete="off" onclick="dontLoose()" disabled>
<br><br><br>
<input type="submit" class="special-button" style="margin:0px;" onclick="window.onbeforeunload = null;" value="Submit">
<input type="submit" class="special-button" style="margin:0px;" onclick="loose()" value="Submit">
</form>
<script>
function dontLoose() {
window.onbeforeunload = function (e) {
e = e || window.event;
let txt = "YOU MIGHT LOOSE DATA WHEN CONTINUING";
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = txt;
}
// For Safari
return txt;
};
}
</script>
<script src=/dontloose.js”></script>
</div>