From 0cd33fa0a2da72747659ba21d068dc9fd37413aa Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 18 Jan 2023 03:52:51 +0100 Subject: [PATCH] Properly handle mutes (closes #6) --- tgcli/Util.cs | 17 ++++++++++++++++- tgcli/tgcli.cs | 6 +++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tgcli/Util.cs b/tgcli/Util.cs index 54b05dd..79ab9a7 100644 --- a/tgcli/Util.cs +++ b/tgcli/Util.cs @@ -101,6 +101,21 @@ namespace tgcli { return history; } + + public static bool IsMuted(Chat c) { + if (c.NotificationSettings.MuteFor == 0 && !c.NotificationSettings.UseDefaultMuteFor) + return false; + + NotificationSettingsScope scope = c.Type switch { + ChatType.ChatTypeBasicGroup => new NotificationSettingsScope.NotificationSettingsScopeGroupChats(), + ChatType.ChatTypeSupergroup => new NotificationSettingsScope.NotificationSettingsScopeGroupChats(), + ChatType.ChatTypePrivate => new NotificationSettingsScope.NotificationSettingsScopePrivateChats(), + ChatType.ChatTypeSecret => new NotificationSettingsScope.NotificationSettingsScopePrivateChats(), + _ => throw new ArgumentOutOfRangeException() + }; + + return client.GetScopeNotificationSettingsAsync(scope).Result.MuteFor != 0; + } public static List GetUnreadChats(bool all = false) { var output = new List(); @@ -109,7 +124,7 @@ namespace tgcli { output.AddRange(all ? response.ChatIds.Select(GetChat).Where(c => c.UnreadCount > 0 || c.IsMarkedAsUnread).ToList() : response.ChatIds.Select(GetChat) - .Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread) && c.NotificationSettings.MuteFor == 0) + .Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread) && !IsMuted(c)) .ToList()); return output; diff --git a/tgcli/tgcli.cs b/tgcli/tgcli.cs index 2703fdc..0472832 100644 --- a/tgcli/tgcli.cs +++ b/tgcli/tgcli.cs @@ -553,7 +553,7 @@ namespace tgcli { public static void AddMessageToQueue(Message msg) { //handle muted - if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId) + if (IsMuted(GetChat(msg.ChatId)) && currentChatId != msg.ChatId) return; //we aren't interested in backlog @@ -576,7 +576,7 @@ namespace tgcli { public static void AddMessageToQueue(Update.UpdateMessageContent msg) { //handle muted - if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId || GetMessage(msg.ChatId, msg.MessageId).EditDate == 0) + if (IsMuted(GetChat(msg.ChatId)) && currentChatId != msg.ChatId || GetMessage(msg.ChatId, msg.MessageId).EditDate == 0) return; var formattedMessage = FormatMessage(msg); @@ -592,7 +592,7 @@ namespace tgcli { public static void AddMessageToQueue(Update.UpdateMessageUnreadReactions msg) { //handle muted - if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId) + if (IsMuted(GetChat(msg.ChatId)) && currentChatId != msg.ChatId) return; if (!msg.UnreadReactions.Any(p => p.Type is ReactionType.ReactionTypeEmoji))