1
0
Fork 0
mirror of https://gitlab.com/niansa/dpplogger.git synced 2025-03-06 20:48:29 +01:00

Fixed a couple of issues

This commit is contained in:
niansa 2023-01-08 22:12:01 +01:00
parent 9d1de26109
commit 6e9afab45c

View file

@ -21,7 +21,7 @@ class Cache {
std::unordered_map<Discord::Snowflake, Json::Value> cache;
public:
struct StoreRes {
struct StoreResult {
const Json::Value& data;
enum {
unchanged = 0b00,
@ -39,7 +39,7 @@ public:
logger.inst_ptr = this;
}
StoreRes store(Discord::Snowflake id, const Json::Value& object) {
StoreResult store(Discord::Snowflake id, const Json::Value& object) {
logger.log(Loglevel::verbose, "Stored "+id.str()+" in cache");
auto cache_hit = cache.find(id);
if (cache_hit != cache.end()) {
@ -49,10 +49,10 @@ public:
}
return {cache_hit->second, changed};
} else {
return {cache.emplace(id, object).first->second, StoreRes::created};
return {cache.emplace(id, object).first->second, StoreResult::created};
}
}
StoreRes store(Discord::Snowflake id, Json::Value&& object) {
StoreResult store(Discord::Snowflake id, Json::Value&& object) {
logger.log(Loglevel::verbose, "Moved "+id.str()+" into cache");
auto cache_hit = cache.find(id);
if (cache_hit != cache.end()) {
@ -65,11 +65,11 @@ public:
return {cache.emplace(id, std::move(object)).first->second, true};
}
}
StoreRes store(const Json::Value& object) {
StoreResult store(const Json::Value& object) {
auto id = object["id"].asString();
return store(id, object);
}
StoreRes store(Json::Value&& object) {
StoreResult store(Json::Value&& object) {
auto id = object["id"].asString();
return store(id, std::move(object));
}
@ -236,7 +236,7 @@ protected:
co_await asyncSleep(rng.getUInt(6000, 15000));
}
}
Cache::StoreRes processGuild(Json::Value&& data) {
Cache::StoreResult processGuild(Json::Value&& data) {
const auto& id = data["id"];
// Insert into database
@ -296,7 +296,7 @@ protected:
// Insert and cache user
auto changed = cache.store(user_data).changed;
if (changed) {
insertUserUpdate(user_data, (changed&Cache::StoreRes::created)?true:false);
insertUserUpdate(user_data, (changed&Cache::StoreResult::created)?true:false);
}
}
@ -308,7 +308,7 @@ protected:
insertMemberUpdate(data, user_id, guild_id, !cache.has(data["id"]));
}
}
Cache::StoreRes cacheStoreMember(const Json::Value& data, Discord::Snowflake user_id, Discord::Snowflake guild_id) {
Cache::StoreResult cacheStoreMember(const Json::Value& data, Discord::Snowflake user_id, Discord::Snowflake guild_id) {
return cache.store(guild_id.uint()|user_id.uint(), data);
}
const Json::Value& fetchFetchMember(Discord::Snowflake user_id, Discord::Snowflake guild_id) {
@ -340,16 +340,22 @@ protected:
*/
void processMessage(Json::Value&& data) {
const auto& guild_id = data["guild_id"];
const auto& user = data["author"];
const auto& user_data = data["author"];
// Process member
auto& member_data = data["member"];
if (member_data.isObject() && guild_id.isString()) {
updateMember(std::move(member_data), guild_id, user["id"]);
updateMember(std::move(member_data), guild_id, user_data["id"]);
}
// Insert and cache user
const auto& [cached_user_data, changed] = cache.store(std::move(user_data));
if (changed) {
insertUserUpdate(user_data, (changed&Cache::StoreResult::created)?true:false);
}
// Insert message into database
insertMessageContent(data, true);
insertMessageUpdate(data, true);
}
void insertMessageContent(const Json::Value& data, bool is_initial) {
const auto& embeds = data["embeds"];
@ -361,14 +367,11 @@ protected:
void insertMessageUpdate(const Json::Value& data, bool is_initial) {
bool has_content = !data["content"].asString().empty() || data["embeds"].isArray();
if (is_initial) {
const auto& author = data["author"];
int type = data["type"].asInt();
db << "INSERT INTO messages (id, type, channel_id, author_id, replied_to_id, creation_timestamp)"
" VALUES (?, ?, ?, ?, ?, ? );"
<< data["id"].asString() << type << data["channel_id"].asString() << author["id"].asString()
<< data["id"].asString() << type << data["channel_id"].asString() << data["author"]["id"].asString()
<< (type==19?data["referenced_message"]["id"].asString():std::optional<std::string>()) << std::to_string(time(nullptr));
cache.store(author);
if (has_content) insertMessageContent(data, true);
} else {
db << "UPDATE messages "
"SET is_edited = 1, update_timestamp = ? "
@ -418,7 +421,7 @@ protected:
insertGuildDelete(data);
}
else if (intent == "MESSAGE_CREATE") [[likely]] {
insertMessageUpdate(data, true);
processMessage(std::move(data));
} else if (intent == "MESSAGE_UPDATE") [[likely]] {
insertMessageUpdate(data, false);
} else if (intent == "MESSAGE_DELETE") {