forked from OpenGamers/abaddon
optimize sql for getting unknown member ids
This commit is contained in:
@@ -328,16 +328,14 @@ void Abaddon::ShowGuildVerificationGateDialog(Snowflake guild_id) {
|
|||||||
void Abaddon::CheckMessagesForMembers(const ChannelData &chan, const std::vector<Message> &msgs) {
|
void Abaddon::CheckMessagesForMembers(const ChannelData &chan, const std::vector<Message> &msgs) {
|
||||||
if (!chan.GuildID.has_value()) return;
|
if (!chan.GuildID.has_value()) return;
|
||||||
|
|
||||||
// TODO sql query
|
std::vector<Snowflake> unknown;
|
||||||
std::set<Snowflake> fetch;
|
std::transform(msgs.begin(), msgs.end(),
|
||||||
std::set<Snowflake> ids;
|
std::back_inserter(unknown),
|
||||||
for (const auto &msg : msgs)
|
[](const Message &msg) -> Snowflake {
|
||||||
ids.insert(msg.Author.ID);
|
return msg.Author.ID;
|
||||||
for (const auto id : ids) {
|
});
|
||||||
const auto member = m_discord.GetMember(id, *chan.GuildID);
|
|
||||||
if (!member.has_value())
|
const auto fetch = m_discord.FilterUnknownMembersFrom(*chan.GuildID, unknown.begin(), unknown.end());
|
||||||
fetch.insert(id);
|
|
||||||
}
|
|
||||||
m_discord.RequestMembers(*chan.GuildID, fetch.begin(), fetch.end());
|
m_discord.RequestMembers(*chan.GuildID, fetch.begin(), fetch.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,6 +84,17 @@ public:
|
|||||||
void GetArchivedPrivateThreads(Snowflake channel_id, sigc::slot<void(DiscordError, const ArchivedThreadsResponseData &)> callback);
|
void GetArchivedPrivateThreads(Snowflake channel_id, sigc::slot<void(DiscordError, const ArchivedThreadsResponseData &)> callback);
|
||||||
std::vector<Snowflake> GetChildChannelIDs(Snowflake parent_id) const;
|
std::vector<Snowflake> GetChildChannelIDs(Snowflake parent_id) const;
|
||||||
|
|
||||||
|
// get ids of given list of members for who we do not have the member data
|
||||||
|
template<typename Iter>
|
||||||
|
std::unordered_set<Snowflake> FilterUnknownMembersFrom(Snowflake guild_id, Iter begin, Iter end) {
|
||||||
|
std::unordered_set<Snowflake> ret;
|
||||||
|
const auto known = m_store.GetMembersInGuild(guild_id);
|
||||||
|
for (auto iter = begin; iter != end; iter++)
|
||||||
|
if (known.find(*iter) == known.end())
|
||||||
|
ret.insert(*iter);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
bool IsThreadJoined(Snowflake thread_id) const;
|
bool IsThreadJoined(Snowflake thread_id) const;
|
||||||
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
|
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
|
||||||
|
|
||||||
|
|||||||
@@ -576,6 +576,23 @@ std::vector<Snowflake> Store::GetChannelIDsWithParentID(Snowflake channel_id) co
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unordered_set<Snowflake> Store::GetMembersInGuild(Snowflake guild_id) const {
|
||||||
|
auto &s = m_stmt_get_guild_member_ids;
|
||||||
|
|
||||||
|
s->Bind(1, guild_id);
|
||||||
|
|
||||||
|
std::unordered_set<Snowflake> ret;
|
||||||
|
while (s->FetchOne()) {
|
||||||
|
Snowflake x;
|
||||||
|
s->Get(0, x);
|
||||||
|
ret.insert(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
s->Reset();
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
void Store::AddReaction(const MessageReactionAddObject &data, bool byself) {
|
void Store::AddReaction(const MessageReactionAddObject &data, bool byself) {
|
||||||
auto &s = m_stmt_add_reaction;
|
auto &s = m_stmt_add_reaction;
|
||||||
|
|
||||||
@@ -2134,6 +2151,14 @@ bool Store::CreateStatements() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_stmt_get_guild_member_ids = std::make_unique<Statement>(m_db, R"(
|
||||||
|
SELECT user_id FROM members WHERE guild_id = ?
|
||||||
|
)");
|
||||||
|
if (!m_stmt_get_guild_member_ids->OK()) {
|
||||||
|
fprintf(stderr, "failed to prepare get guild member ids statement: %s\n", m_db.ErrStr());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ public:
|
|||||||
std::vector<Message> GetPinnedMessages(Snowflake channel_id) const;
|
std::vector<Message> GetPinnedMessages(Snowflake channel_id) const;
|
||||||
std::vector<ChannelData> GetActiveThreads(Snowflake channel_id) const; // public
|
std::vector<ChannelData> GetActiveThreads(Snowflake channel_id) const; // public
|
||||||
std::vector<Snowflake> GetChannelIDsWithParentID(Snowflake channel_id) const;
|
std::vector<Snowflake> GetChannelIDsWithParentID(Snowflake channel_id) const;
|
||||||
|
std::unordered_set<Snowflake> GetMembersInGuild(Snowflake guild_id) const;
|
||||||
|
// ^ not the same as GetUsersInGuild since users in a guild may include users who do not have retrieved member data
|
||||||
|
|
||||||
void AddReaction(const MessageReactionAddObject &data, bool byself);
|
void AddReaction(const MessageReactionAddObject &data, bool byself);
|
||||||
void RemoveReaction(const MessageReactionRemoveObject &data, bool byself);
|
void RemoveReaction(const MessageReactionRemoveObject &data, bool byself);
|
||||||
@@ -302,5 +304,6 @@ private:
|
|||||||
STMT(sub_reaction);
|
STMT(sub_reaction);
|
||||||
STMT(get_reactions);
|
STMT(get_reactions);
|
||||||
STMT(get_chan_ids_parent);
|
STMT(get_chan_ids_parent);
|
||||||
|
STMT(get_guild_member_ids);
|
||||||
#undef STMT
|
#undef STMT
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user