Skip to content

Commit

Permalink
ignore unread state from undisplayed channel types
Browse files Browse the repository at this point in the history
  • Loading branch information
ouwou committed Jun 5, 2024
1 parent 0186507 commit 25ecbce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/discord/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ void DiscordClient::GetArchivedPrivateThreads(Snowflake channel_id, const sigc::
}

std::vector<Snowflake> DiscordClient::GetChildChannelIDs(Snowflake parent_id) const {
return m_store.GetChannelIDsWithParentID(parent_id);
std::vector<Snowflake> ids;
for (auto [id, type] : m_store.GetChannelIDsWithParentID(parent_id)) ids.push_back(id);
return ids;
}

std::optional<WebhookMessageData> DiscordClient::GetWebhookMessageData(Snowflake message_id) const {
Expand Down Expand Up @@ -1371,7 +1373,8 @@ int DiscordClient::GetUnreadStateForChannel(Snowflake id) const noexcept {

int DiscordClient::GetUnreadChannelsCountForCategory(Snowflake id) const noexcept {
int result = 0;
for (Snowflake channel_id : m_store.GetChannelIDsWithParentID(id)) {
for (auto [channel_id, channel_type] : m_store.GetChannelIDsWithParentID(id)) {
if (!ShouldChannelTypeCountInUnread(channel_type)) continue;
if (IsChannelMuted(channel_id)) continue;
const auto iter = m_unread.find(channel_id);
if (iter == m_unread.end()) continue;
Expand All @@ -1393,6 +1396,9 @@ bool DiscordClient::GetUnreadStateForGuild(Snowflake id, int &total_mentions) co
if (const auto iter = m_channel_muted_parent.find(channel_id); iter != m_channel_muted_parent.end())
continue;

const auto channel = GetChannel(channel_id);
if (channel.has_value() && !ShouldChannelTypeCountInUnread(channel->Type)) continue;

if (!has_any_unread && channel_unread > -1 && !IsChannelMuted(channel_id))
has_any_unread = true;
}
Expand Down Expand Up @@ -2753,6 +2759,14 @@ bool DiscordClient::CheckCode(const http::response_type &r, int expected) {
return true;
}

bool DiscordClient::ShouldChannelTypeCountInUnread(ChannelType type) {
return type != ChannelType::GUILD_VOICE &&
type != ChannelType::GUILD_FORUM &&
type != ChannelType::GUILD_MEDIA &&
type != ChannelType::GUILD_STORE &&
type != ChannelType::GUILD_DIRECTORY;
}

void DiscordClient::StoreMessageData(Message &msg) {
const auto chan = m_store.GetChannel(msg.ChannelID);
if (chan.has_value() && chan->GuildID.has_value())
Expand Down
2 changes: 2 additions & 0 deletions src/discord/discord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ class DiscordClient {

void StoreMessageData(Message &msg);

static bool ShouldChannelTypeCountInUnread(ChannelType type);

void HandleReadyReadState(const ReadyEventData &data);
void HandleReadyGuildSettings(const ReadyEventData &data);

Expand Down
12 changes: 6 additions & 6 deletions src/discord/store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,16 +638,16 @@ std::vector<ChannelData> Store::GetActiveThreads(Snowflake channel_id) const {
return ret;
}

std::vector<Snowflake> Store::GetChannelIDsWithParentID(Snowflake channel_id) const {
std::vector<std::pair<Snowflake, ChannelType>> Store::GetChannelIDsWithParentID(Snowflake channel_id) const {
auto &s = m_stmt_get_chan_ids_parent;

s->Bind(1, channel_id);

std::vector<Snowflake> ret;
std::vector<std::pair<Snowflake, ChannelType>> ret;
while (s->FetchOne()) {
Snowflake x;
s->Get(0, x);
ret.push_back(x);
auto &p = ret.emplace_back();
s->Get(0, p.first);
s->Get(1, p.second);
}

s->Reset();
Expand Down Expand Up @@ -2315,7 +2315,7 @@ bool Store::CreateStatements() {
}

m_stmt_get_chan_ids_parent = std::make_unique<Statement>(m_db, R"(
SELECT id FROM channels WHERE parent_id = ?
SELECT id, type FROM channels WHERE parent_id = ?
)");
if (!m_stmt_get_chan_ids_parent->OK()) {
fprintf(stderr, "failed to prepare get channel ids for parent statement: %s\n", m_db.ErrStr());
Expand Down
2 changes: 1 addition & 1 deletion src/discord/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Store {
std::vector<Message> GetMessagesBefore(Snowflake channel_id, Snowflake message_id, size_t limit) const;
std::vector<Message> GetPinnedMessages(Snowflake channel_id) const;
std::vector<ChannelData> GetActiveThreads(Snowflake channel_id) const; // public
std::vector<Snowflake> GetChannelIDsWithParentID(Snowflake channel_id) const;
std::vector<std::pair<Snowflake, ChannelType>> 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

Expand Down

0 comments on commit 25ecbce

Please sign in to comment.