diff options
Diffstat (limited to 'bot/src/schemas')
| -rw-r--r-- | bot/src/schemas/channel.cpp | 60 | ||||
| -rw-r--r-- | bot/src/schemas/channel.hpp | 7 | ||||
| -rw-r--r-- | bot/src/schemas/user.cpp | 42 | ||||
| -rw-r--r-- | bot/src/schemas/user.hpp | 5 |
4 files changed, 114 insertions, 0 deletions
diff --git a/bot/src/schemas/channel.cpp b/bot/src/schemas/channel.cpp index ae4ea53..540b7c0 100644 --- a/bot/src/schemas/channel.cpp +++ b/bot/src/schemas/channel.cpp @@ -1,5 +1,7 @@ #include "channel.hpp" +#include <optional> + namespace bot::schemas { std::optional<ChannelFeature> string_to_channel_feature( const std::string &value) { @@ -11,4 +13,62 @@ namespace bot::schemas { return std::nullopt; } } + + std::optional<std::string> channelfeature_to_string( + const ChannelFeature &value) { + switch (value) { + case MARKOV_RESPONSES: + return "markov_responses"; + case RANDOM_MARKOV_RESPONSES: + return "random_markov_responses"; + default: + std::nullopt; + } + } + + sol::table Channel::as_lua_table(std::shared_ptr<sol::state> luaState) const { + sol::table o = luaState->create_table(); + + o["id"] = this->id; + o["alias_id"] = this->alias_id; + o["alias_name"] = this->alias_name; + + o["joined_at"] = + static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>( + this->joined_at.time_since_epoch()) + .count()); + if (this->opted_out_at.has_value()) { + o["opted_out_at"] = + static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>( + this->opted_out_at->time_since_epoch()) + .count()); + } else { + o["opted_out_at"] = sol::lua_nil; + } + + return o; + } + + sol::table ChannelPreferences::as_lua_table( + std::shared_ptr<sol::state> luaState) const { + sol::table o = luaState->create_table(); + + o["id"] = this->channel_id; // TODO: remove it later too. + o["channel_id"] = this->channel_id; + o["prefix"] = this->prefix; + o["language"] = this->locale; + + sol::table f = luaState->create_table(); + + for (const ChannelFeature &feature : this->features) { + std::optional<std::string> ff = channelfeature_to_string(feature); + if (ff.has_value()) { + f.add(ff.value()); + } + } + + o["features"] = f; + + return o; + } }
\ No newline at end of file diff --git a/bot/src/schemas/channel.hpp b/bot/src/schemas/channel.hpp index 08ed8ad..d2f13eb 100644 --- a/bot/src/schemas/channel.hpp +++ b/bot/src/schemas/channel.hpp @@ -3,6 +3,7 @@ #include <chrono> #include <optional> #include <pqxx/pqxx> +#include <sol/sol.hpp> #include <string> #include <vector> @@ -40,6 +41,8 @@ namespace bot::schemas { return this->opted_out_at; } + sol::table as_lua_table(std::shared_ptr<sol::state> luaState) const; + private: int id, alias_id; std::string alias_name; @@ -50,6 +53,8 @@ namespace bot::schemas { enum ChannelFeature { MARKOV_RESPONSES, RANDOM_MARKOV_RESPONSES }; std::optional<ChannelFeature> string_to_channel_feature( const std::string &value); + std::optional<std::string> channelfeature_to_string( + const ChannelFeature &value); class ChannelPreferences { public: @@ -90,6 +95,8 @@ namespace bot::schemas { return this->features; } + sol::table as_lua_table(std::shared_ptr<sol::state> luaState) const; + private: int channel_id; std::string prefix, locale; diff --git a/bot/src/schemas/user.cpp b/bot/src/schemas/user.cpp new file mode 100644 index 0000000..bbf5711 --- /dev/null +++ b/bot/src/schemas/user.cpp @@ -0,0 +1,42 @@ +#include "schemas/user.hpp" + +#include <chrono> +#include <sol/types.hpp> + +namespace bot::schemas { + sol::table User::as_lua_table(std::shared_ptr<sol::state> luaState) const { + sol::table o = luaState->create_table(); + + o["id"] = this->id; + o["alias_id"] = this->alias_id; + o["alias_name"] = this->alias_name; + + o["joined_at"] = + static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>( + this->joined_at.time_since_epoch()) + .count()); + if (this->opted_out_at.has_value()) { + o["opted_out_at"] = + static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>( + this->opted_out_at->time_since_epoch()) + .count()); + } else { + o["opted_out_at"] = sol::lua_nil; + } + + return o; + } + + sol::table UserRights::as_lua_table( + std::shared_ptr<sol::state> luaState) const { + sol::table o = luaState->create_table(); + + o["id"] = this->id; + o["user_id"] = this->user_id; + o["channel_id"] = this->channel_id; + o["level"] = this->level; + o["is_fixed"] = false; // TODO: remove it later + + return o; + } +}
\ No newline at end of file diff --git a/bot/src/schemas/user.hpp b/bot/src/schemas/user.hpp index 0bd1368..e9d7e0f 100644 --- a/bot/src/schemas/user.hpp +++ b/bot/src/schemas/user.hpp @@ -3,6 +3,7 @@ #include <chrono> #include <optional> #include <pqxx/pqxx> +#include <sol/sol.hpp> #include <string> #include "../utils/chrono.hpp" @@ -40,6 +41,8 @@ namespace bot::schemas { return this->opted_out_at; } + sol::table as_lua_table(std::shared_ptr<sol::state> luaState) const; + private: int id, alias_id; std::string alias_name; @@ -66,6 +69,8 @@ namespace bot::schemas { const PermissionLevel &get_level() const { return this->level; } void set_level(PermissionLevel level) { this->level = level; } + sol::table as_lua_table(std::shared_ptr<sol::state> luaState) const; + private: int id, user_id, channel_id; PermissionLevel level; |
