mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 04:07:07 +00:00
Handle MUC exits
This commit is contained in:
parent
4d85e36bbf
commit
29a76e342e
2 changed files with 61 additions and 9 deletions
|
|
@ -539,6 +539,8 @@ func (c *Client) JoinMUC(chatId int64, resource string, limit *MessageLimit) {
|
|||
}
|
||||
c.locks.mucCacheLock.Unlock()
|
||||
|
||||
log.Debugf("Resources in MUC %v: %v", chatId, mucState.Resources)
|
||||
|
||||
c.sendMUCStatuses(chatId)
|
||||
|
||||
messages, err := c.getNLastMessages(chatId, limit)
|
||||
|
|
@ -549,6 +551,23 @@ func (c *Client) JoinMUC(chatId int64, resource string, limit *MessageLimit) {
|
|||
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 {
|
||||
fullName := user.FirstName
|
||||
if user.LastName != "" {
|
||||
|
|
|
|||
|
|
@ -379,7 +379,7 @@ func HandlePresence(s xmpp.Sender, p stanza.Packet) {
|
|||
handleMUCPresence(s, prs, mucExt)
|
||||
return
|
||||
}
|
||||
tryHandleMUCNicknameChange(s, prs)
|
||||
tryHandleMUCPresence(s, prs)
|
||||
}
|
||||
|
||||
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) {
|
||||
if p.Type != "" {
|
||||
return
|
||||
}
|
||||
|
||||
func tryHandleMUCPresence(s xmpp.Sender, p stanza.Presence) {
|
||||
toBare, nickname, ok := gateway.SplitJID(p.To)
|
||||
if !ok || nickname == "" {
|
||||
return
|
||||
|
|
@ -608,16 +604,25 @@ func tryHandleMUCNicknameChange(s xmpp.Sender, p stanza.Presence) {
|
|||
return
|
||||
}
|
||||
|
||||
log.Warn("🗿 Yes")
|
||||
|
||||
component, ok := s.(*xmpp.Component)
|
||||
if !ok {
|
||||
log.Error("Not a component")
|
||||
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
|
||||
nickname, ok = session.GetMyMUCNickname(chatId)
|
||||
nickname, ok := session.GetMyMUCNickname(chatId)
|
||||
if ok {
|
||||
from = from+"/"+nickname
|
||||
}
|
||||
|
|
@ -638,6 +643,34 @@ func tryHandleMUCNicknameChange(s xmpp.Sender, p stanza.Presence) {
|
|||
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) {
|
||||
log.WithFields(log.Fields{
|
||||
"from": iq.From,
|
||||
|
|
|
|||
Loading…
Reference in a new issue