From 9690957ce2b44d2628b0497ab9d03a53ec4a822c Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Sat, 30 Sep 2023 13:06:03 +0200 Subject: [PATCH] Allow naming rooms in the json config. The name isn't used for anything yet, but makes it easier to update. --- misc/infrastructure/powerbot/bot.go | 21 +++++++++++++-------- misc/infrastructure/powerbot/powerlevels.go | 6 +++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/misc/infrastructure/powerbot/bot.go b/misc/infrastructure/powerbot/bot.go index 6ea17bc1..451cc030 100644 --- a/misc/infrastructure/powerbot/bot.go +++ b/misc/infrastructure/powerbot/bot.go @@ -18,13 +18,18 @@ const ( syncForceFrequency = int(7 * 24 * time.Hour / syncInterval) ) +type Room struct { + ID id.RoomID + Name string +} + type Config struct { - Homeserver string `json:"homeserver"` - UserID id.UserID `json:"user_id"` - Password string `json:"password,omitempty"` - DeviceID id.DeviceID `json:"device_id,omitempty"` - AccessToken string `json:"access_token,omitempty"` - Rooms [][]id.RoomID `json:"rooms"` + Homeserver string `json:"homeserver"` + UserID id.UserID `json:"user_id"` + Password string `json:"password,omitempty"` + DeviceID id.DeviceID `json:"device_id,omitempty"` + AccessToken string `json:"access_token,omitempty"` + Rooms [][]Room `json:"rooms"` } func (c *Config) Load() error { @@ -178,7 +183,7 @@ func Run() (err error) { } for _, group := range config.Rooms { for _, room := range group { - roomUsers[room] = map[id.UserID]struct{}{} + roomUsers[room.ID] = map[id.UserID]struct{}{} } } client, err := Login(config) @@ -254,7 +259,7 @@ func Run() (err error) { } for _, group := range config.Rooms { for _, room := range group { - syncPowerLevels(client, room, group, scoreData, counter%syncForceFrequency == 0) + syncPowerLevels(client, room.ID, group, scoreData, counter%syncForceFrequency == 0) } } roomUsersMu.RUnlock() diff --git a/misc/infrastructure/powerbot/powerlevels.go b/misc/infrastructure/powerbot/powerlevels.go index eb11f790..c07855e8 100644 --- a/misc/infrastructure/powerbot/powerlevels.go +++ b/misc/infrastructure/powerbot/powerlevels.go @@ -77,7 +77,7 @@ func allPowerLevels(roomLevels *event.PowerLevelsEventContent) []int { return ret } -func syncPowerLevels(client *mautrix.Client, room id.RoomID, roomGroup []id.RoomID, scores map[id.RoomID]map[id.UserID]*Score, force bool) { +func syncPowerLevels(client *mautrix.Client, room id.RoomID, roomGroup []Room, scores map[id.RoomID]map[id.UserID]*Score, force bool) { roomLevels := roomPowerLevels[room] if roomLevels == nil { log.Printf("trying to ensure power levels for room %v, but did not get power level map yet", room) @@ -131,10 +131,10 @@ func syncPowerLevels(client *mautrix.Client, room id.RoomID, roomGroup []id.Room prevLevel := roomLevels.Users[user] level, raw := computePowerLevel(roomLevels.UsersDefault, *score) for _, otherRoom := range roomGroup { - if otherRoom == room { + if otherRoom.ID == room { continue } - otherScore := scores[otherRoom][user] + otherScore := scores[otherRoom.ID][user] if otherScore == nil { continue } -- 2.39.2