diff --git a/telegram/handlers.go b/telegram/handlers.go index 8173ecd..abc1f5d 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -214,7 +214,6 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { } else { log.Infof("Couldn't retrieve XMPP message ids for %v, an echo may happen", update.Message.Id) } - log.Warnf("xmppId: %v, ignoredResource: %v", xmppId, ignoredResource) // guarantee sequential message delivering per chat lock := c.getChatMessageLock(chatId) @@ -233,6 +232,24 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { // message content updated func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { markupFunction := formatter.EntityToXEP0393 + + 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 { textContent := update.NewContent.(*client.MessageText) var editChar string @@ -246,7 +263,9 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { textContent.Text.Entities, markupFunction, )) - gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false) + for _, jid := range jids { + gateway.SendMessage(jid, strconv.FormatInt(update.ChatId, 10), text, "e"+strconv.FormatInt(update.MessageId, 10), c.xmpp, nil, false) + } } } diff --git a/telegram/utils.go b/telegram/utils.go index 9664857..9486349 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -891,7 +891,6 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File { // ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message, ignoredResource string) { - var jids []string var isPM bool var err error if gateway.MessageOutgoingPermission && c.Session.Carbons { @@ -900,21 +899,13 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message, i log.Errorf("Could not determine if chat is PM: %v", err) } } + isOutgoing := message.IsOutgoing isCarbon := isPM && isOutgoing - log.Warnf("isOutgoing: %v", isOutgoing) - if isOutgoing { - for resource := range c.resourcesRange() { - if ignoredResource == "" || resource != ignoredResource { - jids = append(jids, c.jid+"/"+resource) - } - } - if len(jids) == 0 { - log.Info("The only resource is ignored, aborting") - return - } - } else { - jids = []string{c.jid} + jids := c.getCarbonFullJids(isOutgoing, ignoredResource) + if len(jids) == 0 { + log.Info("The only resource is ignored, aborting") + return } var text, oob, auxText string @@ -1379,3 +1370,17 @@ func (c *Client) popFromOutbox(xmppId string) string { } return resource } + +func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []string { + var jids []string + if isOutgoing { + for resource := range c.resourcesRange() { + if ignoredResource == "" || resource != ignoredResource { + jids = append(jids, c.jid+"/"+resource) + } + } + } else { + jids = []string{c.jid} + } + return jids +} diff --git a/xmpp/handlers.go b/xmpp/handlers.go index e6671bc..c5ec029 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -177,13 +177,14 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { if err != nil { log.Errorf("Failed to replace id %v with %v %v", replace.Id, msg.Id, tgMessageId) } */ + session.AddToOutbox(replace.Id, resource) } else { err = gateway.IdsDB.Set(session.Session.Login, bare, toID, tgMessageId, msg.Id) if err != nil { log.Errorf("Failed to save ids %v/%v %v", toID, tgMessageId, msg.Id) } + session.AddToOutbox(msg.Id, resource) } - session.AddToOutbox(msg.Id, resource) } else { /* // if a message failed to edit on Telegram side, match new XMPP ID with old Telegram ID anyway