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.SPFrom(gateway.MUCNODE(update.ChatId)),
gateway.SPResource(occupant.Nickname), gateway.SPResource(occupant.Nickname),
gateway.SPImmed(true), gateway.SPImmed(true),
gateway.SPMUCJid(gateway.CHATJID(occupant.key, true)), gateway.SPMUCJid(gateway.CHATJID(occupant.id, true)),
gateway.SPMUCAffiliation(affiliation), gateway.SPMUCAffiliation(affiliation),
gateway.SPMUCRole(role), gateway.SPMUCRole(role),
gateway.SPToJids(toJids), gateway.SPToJids(toJids),

View file

@ -23,7 +23,7 @@ type MUCOccupant struct {
Status client.ChatMemberStatus Status client.ChatMemberStatus
prev *MUCOccupant prev *MUCOccupant
next *MUCOccupant next *MUCOccupant
key int64 id int64
} }
func (o *MUCOccupant) cutOut() (prev, next *MUCOccupant) { 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() lru.lock.Lock()
defer lru.lock.Unlock() defer lru.lock.Unlock()
occupant, ok := lru.m[key] occupant, ok := lru.m[id]
return occupant, ok 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 // 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() lru.lock.Lock()
defer lru.lock.Unlock() defer lru.lock.Unlock()
occupant.key = key occupant.id = id
oldOccupant, oldOk := lru.m[key] oldOccupant, oldOk := lru.m[id]
lru.m[key] = occupant lru.m[id] = occupant
if oldOk { if oldOk {
lru.cutOut(oldOccupant) 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 { if len(lru.m) > int(MUCOccupantsLimit) && lru.oldest != nil {
deleted = lru.oldest deleted = lru.oldest
delete(lru.m, lru.oldest.key) delete(lru.m, lru.oldest.id)
lru.cutOut(lru.oldest) lru.cutOut(lru.oldest)
} }
@ -127,12 +127,12 @@ func (lru *MUCOccupantsLRU) Set(key int64, occupant *MUCOccupant) (deleted *MUCO
} }
// Delete occupant by member ID // Delete occupant by member ID
func (lru *MUCOccupantsLRU) Delete(key int64) { func (lru *MUCOccupantsLRU) Delete(id int64) {
lru.lock.Lock() lru.lock.Lock()
defer lru.lock.Unlock() defer lru.lock.Unlock()
oldOccupant, oldOk := lru.m[key] oldOccupant, oldOk := lru.m[id]
delete(lru.m, key) delete(lru.m, id)
if oldOk { if oldOk {
lru.cutOut(oldOccupant) lru.cutOut(oldOccupant)

View file

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