From 3a18e9dc80ab549c9e95fa9bb3a63071283c3958 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Thu, 31 Jul 2025 10:07:23 -0400 Subject: [PATCH] Occupants LRU refactoring: key -> id --- telegram/handlers.go | 2 +- telegram/muc.go | 22 +++++++++++----------- telegram/utils.go | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/telegram/handlers.go b/telegram/handlers.go index b495423..1185f2f 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -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), diff --git a/telegram/muc.go b/telegram/muc.go index 9021de5..4f718d9 100644 --- a/telegram/muc.go +++ b/telegram/muc.go @@ -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) diff --git a/telegram/utils.go b/telegram/utils.go index 596c0d0..08f8d7e 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -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