diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-09-17 06:16:09 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-09-17 06:16:09 +0300 |
commit | 9dbd487dae9b5a74981873722be685d3706ab772 (patch) | |
tree | f5d5ab8fc1718a58cd5630aa6365f624a6fdbe98 /telegram/handlers.go | |
parent | 7eaf28ad7c4d2bdf5aa6313503d751de90a6811c (diff) | |
parent | 282a6fc21b9626ab1bdc9c5a78162d90b7d28aa2 (diff) |
Merge branch 'master' into muc
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r-- | telegram/handlers.go | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go index 126d858..0d1cda9 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -203,11 +203,11 @@ func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) { // message received func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { - go func() { - chatId := update.Message.ChatId + chatId := update.Message.ChatId - // guarantee sequential message delivering per chat - lock := c.getChatMessageLock(chatId) + // guarantee sequential message delivering per chat + lock := c.getChatMessageLock(chatId) + go func() { lock.Lock() defer lock.Unlock() @@ -223,13 +223,35 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { }).Warn("New message from chat") c.ProcessIncomingMessage(chatId, update.Message) + + c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content) }() } // message content updated func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { - markupFunction := formatter.EntityToXEP0393 - if update.NewContent.MessageContentType() == client.TypeMessageText { + markupFunction := c.getFormatter() + + defer c.updateLastMessageHash(update.ChatId, update.MessageId, update.NewContent) + + c.SendMessageLock.Lock() + c.SendMessageLock.Unlock() + xmppId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, update.ChatId, update.MessageId) + var ignoredResource string + if err == nil { + ignoredResource = c.popFromOutbox(xmppId) + } else { + log.Infof("Couldn't retrieve XMPP message ids for %v, an echo may happen", update.MessageId) + } + log.Infof("ignoredResource: %v", ignoredResource) + + jids := c.getCarbonFullJids(true, ignoredResource) + if len(jids) == 0 { + log.Info("The only resource is ignored, aborting") + return + } + + if update.NewContent.MessageContentType() == client.TypeMessageText && c.hasLastMessageHashChanged(update.ChatId, update.MessageId, update.NewContent) { textContent := update.NewContent.(*client.MessageText) var editChar string if c.Session.AsciiArrows { @@ -242,7 +264,9 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { textContent.Text.Entities, markupFunction, )) - gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) + for _, jid := range jids { + gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false) + } } } @@ -256,7 +280,7 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { deleteChar = "✗ " } text := deleteChar + strings.Join(int64SliceToStringSlice(update.MessageIds), ",") - gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) + gateway.SendTextMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) } } @@ -272,6 +296,11 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat // clean uploaded files func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucceeded) { + log.Debugf("replace message %v with %v", update.OldMessageId, update.Message.Id) + if err := gateway.IdsDB.ReplaceTgId(c.Session.Login, c.jid, update.Message.ChatId, update.OldMessageId, update.Message.Id); err != nil { + log.Errorf("failed to replace %v with %v: %v", update.OldMessageId, update.Message.Id, err.Error()) + } + file, _ := c.contentToFile(update.Message.Content) if file != nil && file.Local != nil { c.cleanTempFile(file.Local.Path) @@ -289,8 +318,13 @@ func (c *Client) updateChatTitle(update *client.UpdateChatTitle) { gateway.SetNickname(c.jid, strconv.FormatInt(update.ChatId, 10), update.Title, c.xmpp) // set also the status (for group chats only) - _, user, _ := c.GetContactByID(update.ChatId, nil) + chat, user, _ := c.GetContactByID(update.ChatId, nil) if user == nil { c.ProcessStatusUpdate(update.ChatId, update.Title, "chat", gateway.SPImmed(true)) } + + // update chat title in the cache + if chat != nil { + chat.Title = update.Title + } } |