Occupants LRU refactoring: key -> id

This commit is contained in:
Bohdan Horbeshko 2025-07-31 10:07:23 -04:00
parent 0e8d34aafa
commit 3a18e9dc80
3 changed files with 15 additions and 15 deletions

View file

@ -551,7 +551,7 @@ func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) {
gateway.SPFrom(gateway.MUCNODE(update.ChatId)),
gateway.SPResource(occupant.Nickname),
gateway.SPImmed(true),
gateway.SPMUCJid(gateway.CHATJID(occupant.key, true)),
gateway.SPMUCJid(gateway.CHATJID(occupant.id, true)),
gateway.SPMUCAffiliation(affiliation),
gateway.SPMUCRole(role),
gateway.SPToJids(toJids),

View file

@ -23,7 +23,7 @@ type MUCOccupant struct {
Status client.ChatMemberStatus
prev *MUCOccupant
next *MUCOccupant
key int64
id int64
}
func (o *MUCOccupant) cutOut() (prev, next *MUCOccupant) {
@ -63,11 +63,11 @@ func NewMUCOccupantsLRU() *MUCOccupantsLRU {
}
}
func (lru *MUCOccupantsLRU) Get(key int64) (*MUCOccupant, bool) {
func (lru *MUCOccupantsLRU) Get(id int64) (*MUCOccupant, bool) {
lru.lock.Lock()
defer lru.lock.Unlock()
occupant, ok := lru.m[key]
occupant, ok := lru.m[id]
return occupant, ok
}
@ -91,14 +91,14 @@ func (lru *MUCOccupantsLRU) insertNewest(occupant *MUCOccupant) {
}
// Set adds or replaces an occupant and possibly returns an occupant removed instead because of overflow
func (lru *MUCOccupantsLRU) Set(key int64, occupant *MUCOccupant) (deleted *MUCOccupant) {
func (lru *MUCOccupantsLRU) Set(id int64, occupant *MUCOccupant) (deleted *MUCOccupant) {
lru.lock.Lock()
defer lru.lock.Unlock()
occupant.key = key
occupant.id = id
oldOccupant, oldOk := lru.m[key]
lru.m[key] = occupant
oldOccupant, oldOk := lru.m[id]
lru.m[id] = occupant
if oldOk {
lru.cutOut(oldOccupant)
@ -119,7 +119,7 @@ func (lru *MUCOccupantsLRU) Set(key int64, occupant *MUCOccupant) (deleted *MUCO
if len(lru.m) > int(MUCOccupantsLimit) && lru.oldest != nil {
deleted = lru.oldest
delete(lru.m, lru.oldest.key)
delete(lru.m, lru.oldest.id)
lru.cutOut(lru.oldest)
}
@ -127,12 +127,12 @@ func (lru *MUCOccupantsLRU) Set(key int64, occupant *MUCOccupant) (deleted *MUCO
}
// Delete occupant by member ID
func (lru *MUCOccupantsLRU) Delete(key int64) {
func (lru *MUCOccupantsLRU) Delete(id int64) {
lru.lock.Lock()
defer lru.lock.Unlock()
oldOccupant, oldOk := lru.m[key]
delete(lru.m, key)
oldOccupant, oldOk := lru.m[id]
delete(lru.m, id)
if oldOk {
lru.cutOut(oldOccupant)

View file

@ -996,7 +996,7 @@ func (c *Client) GetMUCMemberIdByNickname(chatID int64, nickname string) int64 {
for occupant := range mucState.Occupants.Range() {
if occupant.Nickname == nickname {
return occupant.key
return occupant.id
}
}
@ -2934,8 +2934,8 @@ func (c *Client) kickStaleOccupant(chatID int64, deleted *MUCOccupant, mucState
}
// u mad? put me back!
if c.me != nil && c.me.Id == deleted.key {
deleted = mucState.Occupants.Set(deleted.key, deleted)
if c.me != nil && c.me.Id == deleted.id {
deleted = mucState.Occupants.Set(deleted.id, deleted)
if deleted == nil {
// WTF but okay
return