Handle MUC exits

This commit is contained in:
Bohdan Horbeshko 2025-05-23 13:56:12 -04:00
parent 4d85e36bbf
commit 29a76e342e
2 changed files with 61 additions and 9 deletions

View file

@ -539,6 +539,8 @@ func (c *Client) JoinMUC(chatId int64, resource string, limit *MessageLimit) {
} }
c.locks.mucCacheLock.Unlock() c.locks.mucCacheLock.Unlock()
log.Debugf("Resources in MUC %v: %v", chatId, mucState.Resources)
c.sendMUCStatuses(chatId) c.sendMUCStatuses(chatId)
messages, err := c.getNLastMessages(chatId, limit) messages, err := c.getNLastMessages(chatId, limit)
@ -549,6 +551,23 @@ func (c *Client) JoinMUC(chatId int64, resource string, limit *MessageLimit) {
c.sendMUCSubject(chatId, resource) c.sendMUCSubject(chatId, resource)
} }
// LeaveMUC removes MUC date from the cache
func (c *Client) LeaveMUC(chatId int64, resource string) {
c.locks.mucCacheLock.Lock()
defer c.locks.mucCacheLock.Unlock()
mucState, ok := c.mucCache[chatId]
if !ok || mucState == nil {
return
}
delete(mucState.Resources, resource)
log.Debugf("Resources in MUC %v: %v", chatId, mucState.Resources)
if len(mucState.Resources) == 0 {
delete(c.mucCache, chatId)
}
}
func (c *Client) getFullName(user *client.User) string { func (c *Client) getFullName(user *client.User) string {
fullName := user.FirstName fullName := user.FirstName
if user.LastName != "" { if user.LastName != "" {

View file

@ -379,7 +379,7 @@ func HandlePresence(s xmpp.Sender, p stanza.Packet) {
handleMUCPresence(s, prs, mucExt) handleMUCPresence(s, prs, mucExt)
return return
} }
tryHandleMUCNicknameChange(s, prs) tryHandleMUCPresence(s, prs)
} }
func handleSubscription(s xmpp.Sender, p stanza.Presence) { func handleSubscription(s xmpp.Sender, p stanza.Presence) {
@ -567,11 +567,7 @@ func handleMUCPresence(s xmpp.Sender, p stanza.Presence, mucExt stanza.MucPresen
} }
} }
func tryHandleMUCNicknameChange(s xmpp.Sender, p stanza.Presence) { func tryHandleMUCPresence(s xmpp.Sender, p stanza.Presence) {
if p.Type != "" {
return
}
toBare, nickname, ok := gateway.SplitJID(p.To) toBare, nickname, ok := gateway.SplitJID(p.To)
if !ok || nickname == "" { if !ok || nickname == "" {
return return
@ -608,16 +604,25 @@ func tryHandleMUCNicknameChange(s xmpp.Sender, p stanza.Presence) {
return return
} }
log.Warn("🗿 Yes")
component, ok := s.(*xmpp.Component) component, ok := s.(*xmpp.Component)
if !ok { if !ok {
log.Error("Not a component") log.Error("Not a component")
return return
} }
switch p.Type {
case "":
handleMUCNicknameChange(component, p, session, chatId, toBare)
case stanza.PresenceTypeUnavailable:
handleMUCUnavailable(component, p, session, chatId, fromResource)
}
}
func handleMUCNicknameChange(component *xmpp.Component, p stanza.Presence, session *telegram.Client, chatId int64, toBare string) {
log.Warn("🗿 Yes")
from := toBare from := toBare
nickname, ok = session.GetMyMUCNickname(chatId) nickname, ok := session.GetMyMUCNickname(chatId)
if ok { if ok {
from = from+"/"+nickname from = from+"/"+nickname
} }
@ -638,6 +643,34 @@ func tryHandleMUCNicknameChange(s xmpp.Sender, p stanza.Presence) {
gateway.ResumableSend(component, reply) gateway.ResumableSend(component, reply)
} }
func handleMUCUnavailable(component *xmpp.Component, p stanza.Presence, session *telegram.Client, chatId int64, resource string) {
log.Warn("No, it's a MUC exit")
session.LeaveMUC(chatId, resource)
reply := &stanza.Presence{
Attrs: stanza.Attrs{
From: p.To,
To: p.From,
Id: p.Id,
Type: stanza.PresenceTypeUnavailable,
},
Extensions: []stanza.PresExtension{
extensions.PresenceXMucUserExtension{
Item: extensions.PresenceXMucUserItem{
Affiliation: "member",
Jid: p.From,
Role: "none",
},
Statuses: []extensions.PresenceXMucUserStatus{
extensions.PresenceXMucUserStatus{Code: 110},
},
},
},
}
gateway.ResumableSend(component, reply)
}
func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) { func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"from": iq.From, "from": iq.From,