From 6e9afab45c61947f10fe5d6cf5a9ac2c400627ac Mon Sep 17 00:00:00 2001 From: niansa Date: Sun, 8 Jan 2023 22:12:01 +0100 Subject: [PATCH] Fixed a couple of issues --- main.cpp | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/main.cpp b/main.cpp index 8bbdaa9..fd69e2e 100644 --- a/main.cpp +++ b/main.cpp @@ -21,7 +21,7 @@ class Cache { std::unordered_map 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::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") {