diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2023-03-06 17:31:09 -0500 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2023-03-06 17:31:09 -0500 |
commit | 64085fafec86b347c26bb92b758042a0bd4edc75 (patch) | |
tree | 33b7128717e2c63575ef0691c3575d430c3d9a05 /src/notifications | |
parent | 7611ad298af9f46c6ca69a425afed8e4148a47a9 (diff) | |
download | abaddon-portaudio-64085fafec86b347c26bb92b758042a0bd4edc75.tar.gz abaddon-portaudio-64085fafec86b347c26bb92b758042a0bd4edc75.zip |
add basic guild notifications
Diffstat (limited to 'src/notifications')
-rw-r--r-- | src/notifications/notifications.cpp | 93 | ||||
-rw-r--r-- | src/notifications/notifications.hpp | 3 |
2 files changed, 93 insertions, 3 deletions
diff --git a/src/notifications/notifications.cpp b/src/notifications/notifications.cpp index b1c4c2f..bda9f46 100644 --- a/src/notifications/notifications.cpp +++ b/src/notifications/notifications.cpp @@ -4,6 +4,72 @@ Notifications::Notifications() { } +bool CheckGuildMessage(const Message &message) { + auto &discord = Abaddon::Get().GetDiscordClient(); + if (!message.GuildID.has_value()) return false; + + const auto guild = discord.GetGuild(*message.GuildID); + if (!guild.has_value()) return false; + + const auto guild_settings = discord.GetSettingsForGuild(*message.GuildID); + + // if theres no guild settings then just use default message notifications level + // (there will be no channel settings) + if (!guild_settings.has_value()) { + if (guild->DefaultMessageNotifications.has_value()) { + switch (*guild->DefaultMessageNotifications) { + case DefaultNotificationLevel::ALL_MESSAGES: + return true; + case DefaultNotificationLevel::ONLY_MENTIONS: + return message.DoesMentionEveryoneOrUser(discord.GetUserData().ID); + default: + return false; + } + } + return false; + } else if (guild_settings->Muted) { + // if there are guild settings and the guild is muted then dont notify + return false; + } + + // if the channel category is muted then dont notify + const auto channel = discord.GetChannel(message.ChannelID); + std::optional<UserGuildSettingsChannelOverride> category_settings; + if (channel.has_value() && channel->ParentID.has_value()) { + category_settings = guild_settings->GetOverride(*channel->ParentID); + if (category_settings.has_value() && category_settings->Muted) { + return false; + } + } + + const auto channel_settings = guild_settings->GetOverride(message.ChannelID); + + // if there are guild settings but no channel settings then fallback to category or guild + + NotificationLevel level; + if (channel_settings.has_value()) { + level = channel_settings->MessageNotifications; + } else { + if (category_settings.has_value()) { + level = category_settings->MessageNotifications; + } else { + level = guild_settings->MessageNotifications; + } + } + + // there are channel settings, so use them + switch (level) { + case NotificationLevel::ALL_MESSAGES: + return true; + case NotificationLevel::ONLY_MENTIONS: + return message.DoesMentionEveryoneOrUser(discord.GetUserData().ID); + case NotificationLevel::NO_MESSAGES: + return false; + default: + return false; + } +} + void Notifications::CheckMessage(const Message &message) { // ignore if our status is do not disturb if (IsDND()) return; @@ -17,11 +83,13 @@ void Notifications::CheckMessage(const Message &message) { // notify messages in DMs const auto channel = discord.GetChannel(message.ChannelID); if (channel->IsDM()) { - NotifyMessage(message); + NotifyMessageDM(message); + } else if (CheckGuildMessage(message)) { + NotifyMessageGuild(message); } } -void Notifications::NotifyMessage(const Message &message) { +void Notifications::NotifyMessageDM(const Message &message) { Glib::ustring default_action = "app.go-to-channel"; default_action += "::"; default_action += std::to_string(message.ChannelID); @@ -30,6 +98,27 @@ void Notifications::NotifyMessage(const Message &message) { m_notifier.Notify(title, body, default_action); } +void Notifications::NotifyMessageGuild(const Message &message) { + Glib::ustring default_action = "app.go-to-channel"; + default_action += "::"; + default_action += std::to_string(message.ChannelID); + Glib::ustring title = message.Author.Username; + if (const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(message.ChannelID); channel.has_value() && channel->Name.has_value()) { + if (channel->ParentID.has_value()) { + const auto category = Abaddon::Get().GetDiscordClient().GetChannel(*channel->ParentID); + if (category.has_value() && category->Name.has_value()) { + title += " (#" + *channel->Name + ", " + *category->Name + ")"; + } else { + title += " (#" + *channel->Name + ")"; + } + } else { + title += " (#" + *channel->Name + ")"; + } + } + const auto body = message.Content; + m_notifier.Notify(title, body, default_action); +} + bool Notifications::IsDND() const { auto &discord = Abaddon::Get().GetDiscordClient(); const auto status = discord.GetUserStatus(discord.GetUserData().ID); diff --git a/src/notifications/notifications.hpp b/src/notifications/notifications.hpp index fb71349..5498938 100644 --- a/src/notifications/notifications.hpp +++ b/src/notifications/notifications.hpp @@ -10,7 +10,8 @@ public: void CheckMessage(const Message &message); private: - void NotifyMessage(const Message &message); + void NotifyMessageDM(const Message &message); + void NotifyMessageGuild(const Message &message); [[nodiscard]] bool IsDND() const; |