diff options
| author | ilotterytea <iltsu@alright.party> | 2025-07-21 16:34:23 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-07-21 16:34:23 +0500 |
| commit | 8b86e9a8f071a1af5ce1531a9ebd2b2140e73ff7 (patch) | |
| tree | 0e7bb1124987feed7318601693d84d8bec151f50 | |
| parent | 123d99d5db3ab2f3d26e70b9f7e90292f8099825 (diff) | |
feat: a function for getting events
| -rw-r--r-- | bot/src/schemas/event.hpp | 13 | ||||
| -rw-r--r-- | bot/src/utils/events.cpp | 56 | ||||
| -rw-r--r-- | bot/src/utils/events.hpp | 15 |
3 files changed, 84 insertions, 0 deletions
diff --git a/bot/src/schemas/event.hpp b/bot/src/schemas/event.hpp new file mode 100644 index 0000000..a91aef3 --- /dev/null +++ b/bot/src/schemas/event.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <string> +#include <vector> + +namespace bot::schemas { + struct Event { + int id, alias_id; + std::string message, channel_alias_name; + bool is_massping; + std::vector<std::string> subs; + }; +}
\ No newline at end of file diff --git a/bot/src/utils/events.cpp b/bot/src/utils/events.cpp new file mode 100644 index 0000000..a6e0834 --- /dev/null +++ b/bot/src/utils/events.cpp @@ -0,0 +1,56 @@ +#include "utils/events.hpp" + +#include <memory> +#include <string> +#include <vector> + +namespace bot::utils { + std::vector<schemas::Event> get_events(std::unique_ptr<db::BaseDatabase> conn, + api::twitch::HelixClient &api_client, + int moderator_id, int type, + const std::string &name) { + std::vector<schemas::Event> events; + + db::DatabaseRows rows = conn->exec( + "SELECT e.id, e.message, is_massping, c.alias_name AS channel_aname, " + "c.alias_id AS channel_aid FROM " + "events e " + "INNER JOIN channels c ON c.id = e.channel_id " + "WHERE e.event_type = $1 AND e.name = $2", + {std::to_string(type), name}); + + for (const db::DatabaseRow &row : rows) { + schemas::Event event; + + event.id = std::stoi(row.at("id")); + event.alias_id = std::stoi(row.at("channel_aid")); + event.is_massping = std::stoi(row.at("is_massping")); + event.message = row.at("message"); + event.channel_alias_name = row.at("channel_aname"); + + if (event.is_massping) { + auto chatters = api_client.get_chatters(event.alias_id, moderator_id); + + std::for_each( + chatters.begin(), chatters.end(), + [&event](const auto &x) { event.subs.push_back(x.login); }); + } else { + db::DatabaseRows subs = conn->exec( + "SELECT u.alias_name FROM users u " + "INNER JOIN events e ON e.id = $1 " + "INNER JOIN event_subscriptions es ON es.event_id = e.id " + "WHERE u.id = es.user_id", + {std::to_string(event.id)}); + + std::for_each(subs.begin(), subs.end(), + [&event](const db::DatabaseRow &x) { + event.subs.push_back(x.at("alias_name")); + }); + } + + events.push_back(event); + } + + return events; + } +}
\ No newline at end of file diff --git a/bot/src/utils/events.hpp b/bot/src/utils/events.hpp new file mode 100644 index 0000000..e806c94 --- /dev/null +++ b/bot/src/utils/events.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include <string> +#include <vector> + +#include "api/twitch/helix_client.hpp" +#include "database.hpp" +#include "schemas/event.hpp" + +namespace bot::utils { + std::vector<schemas::Event> get_events(std::unique_ptr<db::BaseDatabase> conn, + api::twitch::HelixClient &api_client, + int moderator_id, int type, + const std::string &name); +};
\ No newline at end of file |
