xmpp: Add support for replies

This commit is contained in:
uwaru 2024-11-04 04:52:16 +01:00
parent c4157a4d5b
commit b74b884793
Signed by: uwaru
GPG Key ID: 2BB83941980B3FAC

View File

@ -14,6 +14,7 @@ import (
"github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/bridge/helper"
lru "github.com/hashicorp/golang-lru"
"github.com/jpillora/backoff" "github.com/jpillora/backoff"
"github.com/matterbridge/go-xmpp" "github.com/matterbridge/go-xmpp"
"github.com/rs/xid" "github.com/rs/xid"
@ -28,13 +29,20 @@ type Bxmpp struct {
connected bool connected bool
sync.RWMutex sync.RWMutex
StanzaIDs *lru.Cache
OriginIDs *lru.Cache
avatarAvailability map[string]bool avatarAvailability map[string]bool
avatarMap map[string]string avatarMap map[string]string
} }
func New(cfg *bridge.Config) bridge.Bridger { func New(cfg *bridge.Config) bridge.Bridger {
stanzaIDs, _ := lru.New(5000)
originIDs, _ := lru.New(5000)
return &Bxmpp{ return &Bxmpp{
Config: cfg, Config: cfg,
StanzaIDs: stanzaIDs,
OriginIDs: originIDs,
xmppMap: make(map[string]string), xmppMap: make(map[string]string),
avatarAvailability: make(map[string]bool), avatarAvailability: make(map[string]bool),
avatarMap: make(map[string]string), avatarMap: make(map[string]string),
@ -124,12 +132,20 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
return "", nil return "", nil
} }
if msg.ParentNotFound() {
msg.ParentID = ""
}
// Post normal message. // Post normal message.
var msgReplaceID string var msgReplaceID string
msgID := xid.New().String() msgID := xid.New().String()
if msg.ID != "" { if msg.ID != "" {
msgReplaceID = msg.ID msgReplaceID = msg.ID
} }
var replyID string
if res, ok := b.StanzaIDs.Get(msg.ParentID); ok {
replyID, _ = res.(string)
}
b.Log.Debugf("=> Sending message %#v", msg) b.Log.Debugf("=> Sending message %#v", msg)
if _, err := b.xc.Send(xmpp.Chat{ if _, err := b.xc.Send(xmpp.Chat{
Type: "groupchat", Type: "groupchat",
@ -137,6 +153,7 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
Text: msg.Username + msg.Text, Text: msg.Username + msg.Text,
ID: msgID, ID: msgID,
ReplaceID: msgReplaceID, ReplaceID: msgReplaceID,
ReplyID: replyID,
}); err != nil { }); err != nil {
return "", err return "", err
} }
@ -297,6 +314,11 @@ func (b *Bxmpp) handleXMPP() error {
if v.Type == "groupchat" { if v.Type == "groupchat" {
b.Log.Debugf("== Receiving %#v", v) b.Log.Debugf("== Receiving %#v", v)
if v.ID != "" && v.StanzaID != "" {
b.StanzaIDs.Add(v.ID, v.StanzaID)
b.OriginIDs.Add(v.StanzaID, v.ID)
}
// Skip invalid messages. // Skip invalid messages.
if b.skipMessage(v) { if b.skipMessage(v) {
continue continue
@ -321,6 +343,12 @@ func (b *Bxmpp) handleXMPP() error {
if v.ReplaceID != "" { if v.ReplaceID != "" {
msgID = v.ReplaceID msgID = v.ReplaceID
} }
var parentID string
if res, ok := b.OriginIDs.Get(v.ReplyID); ok {
parentID, _ = res.(string)
}
rmsg := config.Message{ rmsg := config.Message{
Username: b.parseNick(v.Remote), Username: b.parseNick(v.Remote),
Text: v.Text, Text: v.Text,
@ -328,6 +356,7 @@ func (b *Bxmpp) handleXMPP() error {
Account: b.Account, Account: b.Account,
Avatar: avatar, Avatar: avatar,
UserID: v.Remote, UserID: v.Remote,
ParentID: parentID,
ID: msgID, ID: msgID,
Event: event, Event: event,
} }