From a74e2bcb7d3262073d05aa89140b1d202b7f179d Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sun, 5 May 2024 13:16:38 -0400 Subject: Mute/unmute whole chats with no arguments --- telegram/commands.go | 100 +++++++++++++++++++++++++++------------------------ telegram/handlers.go | 11 ++++++ 2 files changed, 64 insertions(+), 47 deletions(-) (limited to 'telegram') diff --git a/telegram/commands.go b/telegram/commands.go index 1ce316b..4730a3f 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -85,8 +85,8 @@ var chatCommands = map[string]command{ "invite": command{"id or @username", "add user to current chat"}, "link": command{"", "get invite link for current chat"}, "kick": command{"id or @username", "remove user to current chat"}, - "mute": command{"id or @username [hours]", "mute user in current chat"}, - "unmute": command{"id or @username", "unrestrict user from current chat"}, + "mute": command{"[id or @username] [hours]", "mute the whole chat or a user in current chat"}, + "unmute": command{"[id or @username]", "unmute the whole chat or a user in the current chat"}, "ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"}, "unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"}, "promote": command{"id or @username [title]", "promote user to admin in current chat"}, @@ -771,59 +771,65 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if err != nil { return err.Error(), true } - // mute @username [n hours] + // mute [@username [n hours]] case "mute": - if len(args) < 1 { - return notEnoughArguments, true - } + if len(args) > 0 { + contact, _, err := c.GetContactByUsername(args[0]) + if err != nil { + return err.Error(), true + } - contact, _, err := c.GetContactByUsername(args[0]) - if err != nil { - return err.Error(), true - } + var hours int64 + if len(args) > 1 { + hours, err = strconv.ParseInt(args[1], 10, 32) + if err != nil { + return "Invalid number of hours", true + } + } - var hours int64 - if len(args) > 1 { - hours, err = strconv.ParseInt(args[1], 10, 32) + _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: chatID, + MemberId: &client.MessageSenderUser{UserId: contact.Id}, + Status: &client.ChatMemberStatusRestricted{ + IsMember: true, + RestrictedUntilDate: c.formatBantime(hours), + Permissions: &permissionsReadonly, + }, + }) if err != nil { - return "Invalid number of hours", true + return err.Error(), true } + } else { + if !c.Session.IgnoreChat(chatID) { + return "Chat is already ignored", true + } + gateway.DirtySessions = true } - - _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - ChatId: chatID, - MemberId: &client.MessageSenderUser{UserId: contact.Id}, - Status: &client.ChatMemberStatusRestricted{ - IsMember: true, - RestrictedUntilDate: c.formatBantime(hours), - Permissions: &permissionsReadonly, - }, - }) - if err != nil { - return err.Error(), true - } - // unmute @username + // unmute [@username] case "unmute": - if len(args) < 1 { - return notEnoughArguments, true - } - - contact, _, err := c.GetContactByUsername(args[0]) - if err != nil { - return err.Error(), true - } + if len(args) > 0 { + contact, _, err := c.GetContactByUsername(args[0]) + if err != nil { + return err.Error(), true + } - _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - ChatId: chatID, - MemberId: &client.MessageSenderUser{UserId: contact.Id}, - Status: &client.ChatMemberStatusRestricted{ - IsMember: true, - RestrictedUntilDate: 0, - Permissions: &permissionsMember, - }, - }) - if err != nil { - return err.Error(), true + _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: chatID, + MemberId: &client.MessageSenderUser{UserId: contact.Id}, + Status: &client.ChatMemberStatusRestricted{ + IsMember: true, + RestrictedUntilDate: 0, + Permissions: &permissionsMember, + }, + }) + if err != nil { + return err.Error(), true + } + } else { + if !c.Session.UnignoreChat(chatID) { + return "Chat wasn't ignored", true + } + gateway.DirtySessions = true } // ban @username from current chat [for N hours] case "ban": diff --git a/telegram/handlers.go b/telegram/handlers.go index 6266292..1ccd622 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -235,6 +235,9 @@ func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) { // message received func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { chatId := update.Message.ChatId + if c.Session.IsChatIgnored(chatId) { + return + } // guarantee sequential message delivering per chat lock := c.getChatMessageLock(chatId) @@ -261,6 +264,10 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { // message content updated func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { + if c.Session.IsChatIgnored(update.ChatId) { + return + } + markupFunction := c.getFormatter() defer c.updateLastMessageHash(update.ChatId, update.MessageId, update.NewContent) @@ -353,6 +360,10 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { // message(s) deleted func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { if update.IsPermanent { + if c.Session.IsChatIgnored(update.ChatId) { + return + } + var deleteChar string if c.Session.AsciiArrows { deleteChar = "X " -- cgit v1.2.3