From 29e66a21d76caf93edbb2f73f3ba4fc75f25b5a6 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sun, 6 Jul 2025 23:11:54 -0400 Subject: [PATCH] Send announcement messages from a temporary MUC occupant && fix edit/delete message regression for non-MUCs --- telegram/handlers.go | 17 ++++++++++++----- telegram/utils.go | 7 ++++++- xmpp/gateway/gateway.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/telegram/handlers.go b/telegram/handlers.go index f66b1cb..2e2941f 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -276,7 +276,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { if isMUC { _, jids = c.getMUCJoinedJIDs(update.ChatId, nil, true) } else { - c.getCarbonFullJids(true, ignoredResource) + jids = c.getCarbonFullJids(true, ignoredResource) } if len(jids) == 0 { log.Info("The only resource is ignored, aborting") @@ -409,12 +409,19 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { if isGroupchat { fromJid = gateway.MUCJID(update.ChatId) _, jids = c.getMUCJoinedJIDs(update.ChatId, nil, true) + var nickname string + if chat != nil { + nickname = chat.Title + } + for _, jid := range jids { + gateway.SendMUCAnnouncement(jid, fromJid, text, nickname, c.xmpp) + } } else { fromJid = gateway.CHATNODE(update.ChatId) - c.getCarbonFullJids(true, "") - } - for _, jid := range jids { - gateway.SendTextMessage(jid, fromJid, text, c.xmpp, isGroupchat) + jids = c.getCarbonFullJids(true, "") + for _, jid := range jids { + gateway.SendTextMessage(jid, fromJid, text, c.xmpp, isGroupchat) + } } } } diff --git a/telegram/utils.go b/telegram/utils.go index 4504dd0..94c767f 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -2061,7 +2061,12 @@ func (c *Client) returnMessage(returnJid string, chatID int64, text string, code if code != 0 { gateway.SendErrorMessage(returnJid, gateway.MUCJID(chatID), text, code, isGroupchat, c.xmpp) } else { - gateway.SendTextMessage(returnJid, gateway.MUCJID(chatID), text, c.xmpp, isGroupchat) + var nickname string + chat, err := c.GetChatByID(chatID, nil) + if err == nil { + nickname = chat.Title + } + gateway.SendMUCAnnouncement(returnJid, gateway.MUCJID(chatID), text, nickname, c.xmpp) } } else { gateway.SendTextMessage(returnJid, gateway.CHATNODE(chatID), text, c.xmpp, isGroupchat) diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go index 15dcbc0..076635a 100644 --- a/xmpp/gateway/gateway.go +++ b/xmpp/gateway/gateway.go @@ -110,6 +110,40 @@ func SendTextMessage(to, from, body string, component *xmpp.Component, isGroupch sendMessageWrapper(to, from, body, "", "", id, component, nil, nil, 0, "", "", false, isGroupchat, false, false, "", 0, "", "", 0) } +// SendMUCAnnouncement creates and sends a message by a temporary occupant +func SendMUCAnnouncement(to, from, body, nickname string, component *xmpp.Component) { + if nickname == "" { + nickname = "announcement" + } + + fullFrom := from + "/" + nickname + + SendPresence( + component, + to, + SPFullFrom(fullFrom), + SPMUCAffiliation("admin"), + SPMUCRole("moderator"), + SPMUCJid(from), + ) + + var id string + if uuid, err := uuid.NewRandom(); err == nil { + id = uuid.String() + } + sendMessageWrapper(to, fullFrom, body, "", "", id, component, nil, nil, 0, "", "", false, true, false, false, "", 0, "", "", 0) + + SendPresence( + component, + to, + SPType("unavailable"), + SPFullFrom(fullFrom), + SPMUCAffiliation("none"), + SPMUCRole("none"), + SPMUCJid(from), + ) +} + // SendErrorMessage creates and sends an error message stanza func SendErrorMessage(to, from, text string, code int, isGroupchat bool, component *xmpp.Component) { sendMessageWrapper(to, from, "", "", text, "", component, nil, nil, 0, "", "", false, isGroupchat, false, false, "", code, "", "", 0)