Assure all MUC presences are sent to a full JID

This commit is contained in:
Bohdan Horbeshko 2025-07-06 14:43:03 -04:00
parent f5a6cbf199
commit 19d602a99b
3 changed files with 24 additions and 12 deletions

View file

@ -519,6 +519,7 @@ func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) {
mucState, ok := c.mucCache[update.ChatId]
if ok && mucState != nil {
_, toJids := c.getMUCJoinedJIDs(update.ChatId, mucState, false)
for memberID, occupant := range mucState.Occupants {
affiliation, role := c.memberStatusToAffiliationAndRole(occupant.Status, chat)
if affiliation != occupant.Affiliation || role != occupant.Role {
@ -532,6 +533,7 @@ func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) {
gateway.SPMUCJid(gateway.CHATJID(memberID, true)),
gateway.SPMUCAffiliation(affiliation),
gateway.SPMUCRole(role),
gateway.SPToJids(toJids),
)
}
}

View file

@ -567,6 +567,7 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
for mucId, state := range c.mucCache {
occupant, ok := state.Occupants[chatID]
if ok {
_, toJids := c.getMUCJoinedJIDs(mucId, state, false)
newMucArgs := append(
newArgs,
gateway.SPFrom(gateway.MUCNODE(mucId)),
@ -574,6 +575,7 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
gateway.SPMUCAffiliation(occupant.Affiliation),
gateway.SPMUCRole(occupant.Role),
gateway.SPMUCJid(chatJid),
gateway.SPToJids(toJids),
)
err := c.sendPresence(newMucArgs...)
if err != nil {
@ -691,6 +693,8 @@ func (c *Client) updateMUCOccupants(mucState *MUCState, chatID int64, members []
chat, _, _ := c.GetContactByID(chatID, nil)
_, toJids := c.getMUCJoinedJIDs(chatID, mucState, false)
for _, member := range members {
senderId, nickname, affiliation, role := c.TgMemberToMUCOccupant(member, chat)
mucState.Occupants[senderId] = &MUCOccupant{
@ -714,6 +718,7 @@ func (c *Client) updateMUCOccupants(mucState *MUCState, chatID int64, members []
gateway.SPMUCAffiliation(affiliation),
gateway.SPMUCRole(role),
gateway.SPMUCJid(gateway.CHATJID(senderId, true)),
gateway.SPToJids(toJids),
)
}
@ -726,6 +731,7 @@ func (c *Client) updateMUCOccupants(mucState *MUCState, chatID int64, members []
gateway.SPMUCRole(myRole),
gateway.SPMUCJid(myJid),
gateway.SPMUCStatusCodes([]uint16{100, 110, 210}),
gateway.SPToJids(toJids),
)
}
@ -751,6 +757,8 @@ func (c *Client) addMUCOccupant(mucID int64, memberID int64, affiliation, role s
nickname := c.GetMUCNickname(memberID)
_, toJids := c.getMUCJoinedJIDs(mucID, mucState, false)
err := c.sendPresence(
gateway.SPFrom(gateway.MUCNODE(mucID)),
gateway.SPResource(nickname),
@ -758,6 +766,7 @@ func (c *Client) addMUCOccupant(mucID int64, memberID int64, affiliation, role s
gateway.SPMUCAffiliation(affiliation),
gateway.SPMUCRole(role),
gateway.SPMUCJid(gateway.CHATJID(memberID, true)),
gateway.SPToJids(toJids),
)
if err == nil {
@ -827,6 +836,7 @@ func (c *Client) updateMUCsNickname(memberID int64, newNickname string) {
unavailableStatusCodes = append(unavailableStatusCodes, 110)
availableStatusCodes = append(availableStatusCodes, 110)
}
_, toJids := c.getMUCJoinedJIDs(mucId, state, false)
c.sendPresence(
gateway.SPType("unavailable"),
gateway.SPFrom(sMucId),
@ -837,6 +847,7 @@ func (c *Client) updateMUCsNickname(memberID int64, newNickname string) {
gateway.SPMUCNick(newNickname),
gateway.SPMUCStatusCodes(unavailableStatusCodes),
gateway.SPMUCJid(realJid),
gateway.SPToJids(toJids),
)
c.sendPresence(
gateway.SPFrom(sMucId),
@ -846,6 +857,7 @@ func (c *Client) updateMUCsNickname(memberID int64, newNickname string) {
gateway.SPMUCRole(oldOccupant.Role),
gateway.SPMUCStatusCodes(availableStatusCodes),
gateway.SPMUCJid(realJid),
gateway.SPToJids(toJids),
)
}
}
@ -2793,6 +2805,7 @@ func (c *Client) kickMeFromMUC(chatID int64, statusCodes []uint16, destroy bool,
if c.me != nil {
myJid = gateway.CHATJID(c.me.Id, true)
}
_, toJids := c.getMUCJoinedJIDs(chatID, mucState, false)
args := []args.V{
gateway.SPFrom(gateway.MUCNODE(chatID)),
gateway.SPResource(c.GetMUCNickname(0)),
@ -2800,14 +2813,10 @@ func (c *Client) kickMeFromMUC(chatID int64, statusCodes []uint16, destroy bool,
gateway.SPMUCRole("none"),
gateway.SPMUCJid(myJid),
gateway.SPMUCStatusCodes(statusCodes),
gateway.SPToJids(toJids),
}
if destroy {
_, toResources := c.getMUCJoinedJIDs(chatID, mucState, false)
args = append(
args,
gateway.SPMUCDestroy(""),
gateway.SPToResources(toResources),
)
args = append(args, gateway.SPMUCDestroy(""))
} else {
args = append(args, gateway.SPType("unavailable"))
}
@ -2949,12 +2958,15 @@ func CloneChatPermissions(permissions *client.ChatPermissions) *client.ChatPermi
}
func (c *Client) mucOccupantRolePresence(chatID, userID int64, status ChatMemberStatus, nickname string) {
_, toJids := c.getMUCJoinedJIDs(chatID, nil, true)
args := []args.V{
gateway.SPFrom(gateway.MUCNODE(chatID)),
gateway.SPResource(nickname),
gateway.SPImmed(true),
gateway.SPMUCJid(gateway.CHATJID(userID, true)),
gateway.SPToJids(toJids),
}
var statusCodes []uint16
var newAffiliation, newRole string

View file

@ -469,8 +469,8 @@ var SPMUCStatusCodes = args.New()
// SPMUCDestroy is a XEP-0045 room destruction element
var SPMUCDestroy = args.NewString()
// SPToResources achieves to send the presence to certain resources only
var SPToResources = args.New()
// SPToJids achieves to send the presence to certain full jids only
var SPToJids = args.New()
func newPresence(bareJid string, to string, args ...args.V) stanza.Presence {
var presenceFrom string
@ -589,10 +589,8 @@ func SendPresence(component *xmpp.Component, to string, args ...args.V) error {
}).Info("Got presence")
var tos []string
if SPToResources.IsSet(args) {
for _, toResource := range SPToResources.Get(args).([]string) {
tos = append(tos, to + "/" + toResource)
}
if SPToJids.IsSet(args) {
tos = SPToJids.Get(args).([]string)
} else {
tos = []string{to}
}