little refactoring

This commit is contained in:
Syping 2024-02-04 07:10:02 +01:00
parent 92b480c8eb
commit b10593440d
14 changed files with 116 additions and 106 deletions

View file

@ -26,8 +26,8 @@ set(DTRANSLATEBOT_HEADERS
src/settings.h
src/slashcommands.h
src/submit_queue.h
src/translate_core.h
src/translate_libretranslate.h
src/translator_core.h
src/translator_libretranslate.h
src/webhook_push.h
)
set(DTRANSLATEBOT_SOURCES
@ -36,8 +36,8 @@ set(DTRANSLATEBOT_SOURCES
src/settings.cpp
src/slashcommands.cpp
src/submit_queue.cpp
src/translate_core.cpp
src/translate_libretranslate.cpp
src/translator_core.cpp
src/translator_libretranslate.cpp
src/webhook_push.cpp
)

View file

@ -32,7 +32,7 @@ int main(int argc, char* argv[]) {
}
bot::settings::settings settings;
if (!settings.parse(argv[1]))
if (!settings.parse_file(argv[1]))
return 1;
if (settings.get_translator()->get_languages().empty()) {

View file

@ -19,29 +19,30 @@
#include <thread>
#include "message_queue.h"
#include "settings.h"
using namespace bot;
using namespace std::chrono_literals;
void bot::message_queue::add(const bot::message &message)
void message_queue::add(const message &message)
{
m_mutex.lock();
m_queue.push(message);
m_mutex.unlock();
}
void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queue *submit_queue)
void message_queue::run(bot::settings::settings *settings, submit_queue *submit_queue)
{
m_running = true;
while (m_running) {
m_mutex.lock();
if (!m_queue.empty()) {
const bot::message message = m_queue.front();
const message message = m_queue.front();
m_queue.pop();
m_mutex.unlock();
std::unique_ptr<bot::translate::translator> translator = settings->get_translator();
std::unique_ptr<bot::translator::translator> translator = settings->get_translator();
for (auto target = message.targets.begin(); target != message.targets.end(); target++) {
bot::translated_message tr_message;
translated_message tr_message;
tr_message.author = message.author;
tr_message.avatar = message.avatar;
tr_message.message = translator->translate(message.message, message.source, target->target);
@ -58,7 +59,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
}
}
void bot::message_queue::terminate()
void message_queue::terminate()
{
m_running = false;
}

View file

@ -37,14 +37,14 @@ namespace bot {
class message_queue {
public:
void add(const bot::message &message);
void run(bot::settings::settings *settings, bot::submit_queue *submit_queue);
void add(const message &message);
void run(bot::settings::settings *settings, submit_queue *submit_queue);
void terminate();
private:
bool m_running;
std::mutex m_mutex;
std::queue<bot::message> m_queue;
std::queue<message> m_queue;
};
}

View file

@ -22,9 +22,10 @@
#include <fstream>
#include <iostream>
#include "settings.h"
#include "translate_libretranslate.h"
#include "translator_libretranslate.h"
using namespace bot::settings;
void bot::settings::settings::add_channel(const bot::settings::channel &channel, dpp::snowflake guild_id)
void settings::add_channel(const channel &channel, dpp::snowflake guild_id)
{
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
@ -34,13 +35,13 @@ void bot::settings::settings::add_channel(const bot::settings::channel &channel,
}
// We will create the guild structure when it is not in memory
bot::settings::guild guild;
guild guild;
guild.id = guild_id;
guild.channel.push_back(std::move(channel));
m_guilds.push_back(std::move(guild));
}
bool bot::settings::settings::add_target(const bot::settings::target &target, dpp::snowflake guild_id, dpp::snowflake channel_id)
bool settings::add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id)
{
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
@ -56,17 +57,17 @@ bool bot::settings::settings::add_target(const bot::settings::target &target, dp
return false;
}
void bot::settings::settings::add_translatebot_webhook(dpp::snowflake webhook_id)
void settings::add_translatebot_webhook(dpp::snowflake webhook_id)
{
m_webhookIds.push_back(webhook_id);
}
uint16_t bot::settings::settings::get_avatar_size()
uint16_t settings::get_avatar_size()
{
return m_avatarSize;
}
const bot::settings::channel* bot::settings::settings::get_channel(const bot::settings::guild *guild, dpp::snowflake channel_id)
const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id)
{
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id)
@ -75,7 +76,7 @@ const bot::settings::channel* bot::settings::settings::get_channel(const bot::se
return nullptr;
}
const bot::settings::channel* bot::settings::settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
{
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
@ -89,7 +90,7 @@ const bot::settings::channel* bot::settings::settings::get_channel(dpp::snowflak
return nullptr;
}
const bot::settings::guild* bot::settings::settings::get_guild(dpp::snowflake guild_id)
const guild* settings::get_guild(dpp::snowflake guild_id)
{
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id)
@ -98,35 +99,35 @@ const bot::settings::guild* bot::settings::settings::get_guild(dpp::snowflake gu
return nullptr;
}
const std::vector<std::string> bot::settings::settings::get_preferred_languages()
const std::vector<std::string> settings::get_preferred_languages()
{
return m_preflangs;
}
const std::filesystem::path bot::settings::settings::get_storage_path()
const std::filesystem::path settings::get_storage_path()
{
return m_storagepath;
}
const bot::settings::translate* bot::settings::settings::get_translate()
const translator* settings::get_translate()
{
return &m_translate;
return &m_translator;
}
std::unique_ptr<bot::translate::translator> bot::settings::settings::get_translator()
std::unique_ptr<bot::translator::translator> settings::get_translator()
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::unique_ptr<bot::translate::libretranslate> libretranslate(
new bot::translate::libretranslate(m_translate.hostname, m_translate.port, m_translate.url, m_translate.tls, m_translate.apiKey));
std::unique_ptr<bot::translator::libretranslate> libretranslate(
new bot::translator::libretranslate(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey));
return libretranslate;
}
const std::string bot::settings::settings::get_token()
const std::string settings::get_token()
{
return m_token;
}
bool bot::settings::settings::is_translatebot(dpp::snowflake webhook_id)
bool settings::is_translatebot(dpp::snowflake webhook_id)
{
for (auto id = m_webhookIds.begin(); id != m_webhookIds.end(); id++) {
if (*id == webhook_id)
@ -135,24 +136,15 @@ bool bot::settings::settings::is_translatebot(dpp::snowflake webhook_id)
return false;
}
void bot::settings::settings::lock()
void settings::lock()
{
m_mutex.lock();
}
bool bot::settings::settings::parse(const std::string &filename)
bool settings::parse(const std::string &data)
{
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
std::cerr << "Failed to open JSON configuration file located at " << filename << std::endl;
return false;
}
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
try {
const dpp::json json = dpp::json::parse(sdata);
const dpp::json json = dpp::json::parse(data);
if (!json.is_object()) {
std::cerr << "JSON configuration file is corrupt" << std::endl;
return false;
@ -193,33 +185,33 @@ bool bot::settings::settings::parse(const std::string &filename)
std::cerr << "\"hostname\" can not be found in Translate settings" << std::endl;
return false;
}
m_translate.hostname = json_translate_hostname.value();
m_translator.hostname = json_translate_hostname.value();
auto json_translate_port = json_translate.value().find("port");
if (json_translate_port == json_translate.value().end()) {
std::cerr << "\"port\" can not be found in Translate settings" << std::endl;
return false;
}
m_translate.port = json_translate_port.value();
m_translator.port = json_translate_port.value();
auto json_translate_url = json_translate.value().find("url");
if (json_translate_url == json_translate.value().end()) {
std::cerr << "\"url\" can not be found in Translate settings" << std::endl;
return false;
}
m_translate.url = json_translate_url.value();
m_translator.url = json_translate_url.value();
auto json_translate_tls = json_translate.value().find("tls");
if (json_translate_tls != json_translate.value().end())
m_translate.tls = json_translate_tls.value();
m_translator.tls = json_translate_tls.value();
else
m_translate.tls = false;
m_translator.tls = false;
auto json_translate_apiKey = json_translate.value().find("apiKey");
if (json_translate_apiKey != json_translate.value().end())
m_translate.apiKey = json_translate_apiKey.value();
m_translator.apiKey = json_translate_apiKey.value();
else
m_translate.apiKey.clear();
m_translator.apiKey.clear();
auto json_avatarSize = json.find("avatar_size");
if (json_avatarSize != json.end()) {
@ -240,7 +232,7 @@ bool bot::settings::settings::parse(const std::string &filename)
if (json_guilds != json.end()) {
for (auto json_guild = json_guilds.value().begin(); json_guild != json_guilds.value().end(); json_guild++) {
if (json_guild.value().is_object()) {
bot::settings::guild guild;
guild guild;
auto json_guild_id = json_guild.value().find("id");
if (json_guild_id != json_guild.value().end()) {
@ -256,7 +248,7 @@ bool bot::settings::settings::parse(const std::string &filename)
for (auto json_channel = json_guild.value().begin(); json_channel != json_guild.value().end(); json_channel++) {
if (json_channel.value().is_object()) {
bot::settings::channel channel;
channel channel;
auto json_channel_id = json_channel.value().find("id");
if (json_channel_id != json_channel.value().end()) {
@ -277,7 +269,7 @@ bool bot::settings::settings::parse(const std::string &filename)
auto json_channel_target = json_channel.value().find("target");
if (json_channel_target != json_channel.value().end()) {
if (json_channel_target.value().is_string()) {
bot::settings::target target;
target target;
target.target = json_channel_target.value();
target.webhook = dpp::webhook(json_channel->at("webhook"));
m_webhookIds.push_back(target.webhook.id);
@ -285,7 +277,7 @@ bool bot::settings::settings::parse(const std::string &filename)
}
else if (json_channel_target.value().is_object()) {
for (auto json_target = json_channel_target.value().begin(); json_target != json_channel_target.value().end(); json_target++) {
bot::settings::target target;
target target;
target.target = json_target.key();
target.webhook = dpp::webhook(json_target.value());
m_webhookIds.push_back(target.webhook.id);
@ -327,7 +319,21 @@ bool bot::settings::settings::parse(const std::string &filename)
return false;
}
void bot::settings::settings::unlock()
bool settings::parse_file(const std::string &filename)
{
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
std::cerr << "Failed to open JSON configuration file located at " << filename << std::endl;
return false;
}
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
return parse(std::move(sdata));
}
void settings::unlock()
{
m_mutex.unlock();
}

View file

@ -25,7 +25,7 @@
#include <mutex>
#include <string>
#include <vector>
#include "translate_core.h"
#include "translator_core.h"
namespace bot {
namespace settings {
@ -42,7 +42,7 @@ namespace bot {
dpp::snowflake id;
std::vector<bot::settings::channel> channel;
};
struct translate {
struct translator {
std::string hostname;
uint16_t port;
std::string url;
@ -52,30 +52,31 @@ namespace bot {
class settings {
public:
void add_channel(const bot::settings::channel &channel, dpp::snowflake guild_id);
bool add_target(const bot::settings::target &target, dpp::snowflake guild_id, dpp::snowflake channel_id);
void add_channel(const channel &channel, dpp::snowflake guild_id);
bool add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id);
void add_translatebot_webhook(dpp::snowflake webhook_id);
uint16_t get_avatar_size();
const bot::settings::channel* get_channel(const bot::settings::guild *guild, dpp::snowflake channel_id);
const bot::settings::channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
const bot::settings::guild* get_guild(dpp::snowflake guild_id);
const channel* get_channel(const guild *guild, dpp::snowflake channel_id);
const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
const guild* get_guild(dpp::snowflake guild_id);
const std::vector<std::string> get_preferred_languages();
const std::filesystem::path get_storage_path();
const bot::settings::translate* get_translate();
std::unique_ptr<bot::translate::translator> get_translator();
const translator* get_translate();
std::unique_ptr<bot::translator::translator> get_translator();
const std::string get_token();
bool is_translatebot(dpp::snowflake webhook_id);
void lock();
bool parse(const std::string &filename);
bool parse(const std::string &data);
bool parse_file(const std::string &filename);
void unlock();
private:
uint16_t m_avatarSize;
std::recursive_mutex m_mutex;
std::vector<bot::settings::guild> m_guilds;
std::vector<guild> m_guilds;
std::vector<std::string> m_preflangs;
std::filesystem::path m_storagepath;
bot::settings::translate m_translate;
translator m_translator;
std::string m_token;
std::vector<dpp::snowflake> m_webhookIds;
};

View file

@ -34,11 +34,11 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
v_target = dpp::webhook(std::get<std::string>(event.get_parameter("webhook")));
}
const std::vector<bot::translate::language> languages = settings->get_translator()->get_languages();
const std::vector<bot::translator::language> languages = settings->get_translator()->get_languages();
std::ostringstream language_codes;
bool source_valid = false, target_valid = false;
for (const bot::translate::language &language : languages) {
for (const bot::translator::language &language : languages) {
if (language.code == source)
source_valid = true;
if (language.code == target)
@ -122,7 +122,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::settings *settings)
{
settings->lock();
const std::vector<bot::translate::language> languages = settings->get_translator()->get_languages();
const std::vector<bot::translator::language> languages = settings->get_translator()->get_languages();
const std::vector<std::string> preferred_languages = settings->get_preferred_languages();
settings->unlock();
@ -156,7 +156,7 @@ void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::set
dpp::command_option webhook_pref_subcommand(dpp::co_sub_command, "webhook", "Translate current channel to a webhook (Preferred languages)");
dpp::command_option source_pref_option(dpp::co_string, "source", "Source language", true);
dpp::command_option target_pref_option(dpp::co_string, "target", "Target language", true);
for (const bot::translate::language &language : languages) {
for (const bot::translator::language &language : languages) {
if (std::find(preferred_languages.begin(), preferred_languages.end(), language.code) != preferred_languages.end()) {
source_pref_option.add_choice(dpp::command_option_choice(language.name, language.code));
target_pref_option.add_choice(dpp::command_option_choice(language.name, language.code));

View file

@ -23,11 +23,10 @@
#include "settings.h"
namespace bot {
class slashcommands {
public:
static void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
static void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
};
namespace slashcommands {
extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
extern void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
}
}
#endif // SLASHCOMMANDS_H

View file

@ -19,22 +19,23 @@
#include <thread>
#include "submit_queue.h"
#include "webhook_push.h"
using namespace bot;
using namespace std::chrono_literals;
void bot::submit_queue::add(const bot::translated_message &message)
void submit_queue::add(const translated_message &message)
{
m_mutex.lock();
m_queue.push(message);
m_mutex.unlock();
}
void bot::submit_queue::run(dpp::cluster *bot)
void submit_queue::run(dpp::cluster *bot)
{
m_running = true;
while (m_running) {
m_mutex.lock();
if (!m_queue.empty()) {
const bot::translated_message message = m_queue.front();
const translated_message message = m_queue.front();
m_queue.pop();
m_mutex.unlock();
@ -49,7 +50,7 @@ void bot::submit_queue::run(dpp::cluster *bot)
}
}
void bot::submit_queue::terminate()
void submit_queue::terminate()
{
m_running = false;
}

View file

@ -34,14 +34,14 @@ namespace bot {
class submit_queue {
public:
void add(const bot::translated_message &message);
void add(const translated_message &message);
void run(dpp::cluster *bot);
void terminate();
private:
bool m_running;
std::mutex m_mutex;
std::queue<bot::translated_message> m_queue;
std::queue<translated_message> m_queue;
};
}

View file

@ -17,23 +17,24 @@
*****************************************************************************/
#include <iostream>
#include "translate_core.h"
#include "translator_core.h"
using namespace bot::translator;
bot::translate::translator::translator()
translator::translator()
{
}
bot::translate::translator::~translator()
translator::~translator()
{
}
const std::vector<bot::translate::language> bot::translate::translator::get_languages()
const std::vector<language> translator::get_languages()
{
std::cerr << "WARNING: translator::get_languages() have being called." << std::endl;
return {};
}
const std::string bot::translate::translator::translate(const std::string &text, const std::string &source, const std::string &target)
const std::string translator::translate(const std::string &text, const std::string &source, const std::string &target)
{
std::cerr << "WARNING: translator:translate(const std::string&, const std::string&, const std::string&) have being called." << std::endl;
return {};

View file

@ -16,14 +16,14 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef TRANSLATE_CORE_H
#define TRANSLATE_CORE_H
#ifndef TRANSLATOR_CORE_H
#define TRANSLATOR_CORE_H
#include <string>
#include <vector>
namespace bot {
namespace translate {
namespace translator {
struct language {
std::string code;
std::string name;
@ -33,10 +33,10 @@ namespace bot {
public:
explicit translator();
virtual ~translator();
virtual const std::vector<bot::translate::language> get_languages();
virtual const std::vector<language> get_languages();
virtual const std::string translate(const std::string &text, const std::string &source, const std::string &target);
};
}
}
#endif // TRANSLATE_CORE_H
#endif // TRANSLATOR_CORE_H

View file

@ -18,21 +18,22 @@
#include <dpp/json.h>
#include <dpp/httpsclient.h>
#include "translate_libretranslate.h"
#include "translator_libretranslate.h"
using namespace bot::translator;
using namespace std::string_literals;
bot::translate::libretranslate::libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey) :
libretranslate::libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey) :
m_hostname(hostname), m_port(port), m_url(url), m_tls(tls), m_apiKey(apiKey)
{
}
bot::translate::libretranslate::~libretranslate()
libretranslate::~libretranslate()
{
}
const std::vector<bot::translate::language> bot::translate::libretranslate::get_languages()
const std::vector<language> libretranslate::get_languages()
{
std::vector<bot::translate::language> languages;
std::vector<language> languages;
try {
dpp::https_client http_request(m_hostname, m_port, m_url + "languages", "GET", {}, {}, !m_tls);
@ -41,7 +42,7 @@ const std::vector<bot::translate::language> bot::translate::libretranslate::get_
if (response.is_array()) {
for (const auto &json_language : response) {
if (json_language.is_object()) {
bot::translate::language language;
language language;
auto json_lang_code = json_language.find("code");
if (json_lang_code != json_language.end())
@ -68,7 +69,7 @@ const std::vector<bot::translate::language> bot::translate::libretranslate::get_
return languages;
}
const std::string bot::translate::libretranslate::translate(const std::string &text, const std::string &source, const std::string &target)
const std::string libretranslate::translate(const std::string &text, const std::string &source, const std::string &target)
{
const dpp::http_headers http_headers = {
{"Content-Type"s, "application/json"s}

View file

@ -16,20 +16,20 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef TRANSLATE_LIBRETRANSLATE_H
#define TRANSLATE_LIBRETRANSLATE_H
#ifndef TRANSLATOR_LIBRETRANSLATE_H
#define TRANSLATOR_LIBRETRANSLATE_H
#include <cstdint>
#include <string>
#include "translate_core.h"
#include "translator_core.h"
namespace bot {
namespace translate {
namespace translator {
class libretranslate : public translator {
public:
explicit libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey = {});
~libretranslate() override;
const std::vector<bot::translate::language> get_languages() override;
const std::vector<language> get_languages() override;
const std::string translate(const std::string &text, const std::string &source, const std::string &target) override;
private:
@ -42,4 +42,4 @@ namespace bot {
}
}
#endif // TRANSLATE_LIBRETRANSLATE_H
#endif // TRANSLATOR_LIBRETRANSLATE_H