Implement disco request for avatars
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
|
"github.com/matterbridge/go-xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pathRegex = regexp.MustCompile("[^a-zA-Z0-9]+")
|
var pathRegex = regexp.MustCompile("[^a-zA-Z0-9]+")
|
||||||
@@ -28,3 +29,23 @@ func (b *Bxmpp) cacheAvatar(msg *config.Message) string {
|
|||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func discoSupportsAvatar(items []*xmpp.DiscoItems) bool {
|
||||||
|
for _, item := range items {
|
||||||
|
if item.Node == xmpp.XMPPNS_AVATAR_PEP_DATA {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bxmpp) handleDisco(items xmpp.DiscoItems) {
|
||||||
|
if discoSupportsAvatar(items) {
|
||||||
|
b.Log.Debugf("%s supports avatars", items.Jid)
|
||||||
|
b.avatarAvailability[items.Jid] = avatarAvailable
|
||||||
|
} else {
|
||||||
|
b.Log.Debugf("%s does not support avatars", items.Jid)
|
||||||
|
b.avatarAvailability[items.Jid] = avatarUnavailable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,6 +15,14 @@ import (
|
|||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type avatarAvailability int
|
||||||
|
|
||||||
|
const (
|
||||||
|
avatarDiscoSent avatarAvailability = iota
|
||||||
|
avatarAvailable
|
||||||
|
avatarUnavailable
|
||||||
|
)
|
||||||
|
|
||||||
type Bxmpp struct {
|
type Bxmpp struct {
|
||||||
*bridge.Config
|
*bridge.Config
|
||||||
|
|
||||||
@@ -24,14 +32,16 @@ type Bxmpp struct {
|
|||||||
connected bool
|
connected bool
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
|
||||||
avatarMap map[string]string
|
avatarMap map[string]string
|
||||||
|
avatarAvailability map[string]avatarAvailability
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *bridge.Config) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Bxmpp{
|
return &Bxmpp{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
xmppMap: make(map[string]string),
|
xmppMap: make(map[string]string),
|
||||||
avatarMap: make(map[string]string),
|
avatarMap: make(map[string]string),
|
||||||
|
avatarAvailability: make(map[string]avatarAvailability),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,10 +247,20 @@ func (b *Bxmpp) handleXMPP() error {
|
|||||||
event = config.EventTopicChange
|
event = config.EventTopicChange
|
||||||
}
|
}
|
||||||
|
|
||||||
avatar := getAvatar(b.avatarMap, v.Remote, b.General)
|
// First check whether the user even supports
|
||||||
if avatar == "" {
|
// avatars
|
||||||
b.Log.Debugf("Requesting avatar data")
|
avatar := ""
|
||||||
b.xc.AvatarRequestData(v.Remote)
|
state, sok := b.avatarAvailability[v.Remote]
|
||||||
|
if !sok {
|
||||||
|
b.Log.Debugf("Sending disco to %s", v.Remote)
|
||||||
|
b.xc.DiscoverEntityItems(v.Remote)
|
||||||
|
b.avatarAvailability[v.Remote] = avatarDiscoSent
|
||||||
|
} else if state == avatarAvailable {
|
||||||
|
avatar = getAvatar(b.avatarMap, v.Remote, b.General)
|
||||||
|
if avatar == "" {
|
||||||
|
b.Log.Debugf("Requesting avatar data")
|
||||||
|
b.xc.AvatarRequestData(v.Remote)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
msgID := v.ID
|
msgID := v.ID
|
||||||
@@ -271,6 +291,8 @@ func (b *Bxmpp) handleXMPP() error {
|
|||||||
}
|
}
|
||||||
case xmpp.AvatarData:
|
case xmpp.AvatarData:
|
||||||
b.handleDownloadAvatar(v)
|
b.handleDownloadAvatar(v)
|
||||||
|
case xmpp.DiscoItems:
|
||||||
|
b.handleDisco(v)
|
||||||
case xmpp.Presence:
|
case xmpp.Presence:
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user