From 8b59d4281650ea1bf2aa5f4b1543a926d7fc2f68 Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Sat, 2 Nov 2024 13:09:05 +0100 Subject: [PATCH] Reset score for kicked users. --- misc/infrastructure/powerbot/bot.go | 18 ++++++++++++++++-- misc/infrastructure/powerbot/db.go | 10 +++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/misc/infrastructure/powerbot/bot.go b/misc/infrastructure/powerbot/bot.go index b373c826..88e4400a 100644 --- a/misc/infrastructure/powerbot/bot.go +++ b/misc/infrastructure/powerbot/bot.go @@ -129,6 +129,14 @@ func handleLeave(now time.Time, room id.RoomID, member id.UserID, raw *event.Eve setUserStateAt(room, member, now, Active, NotActive) } +func handleKicked(now time.Time, room id.RoomID, member id.UserID, raw *event.Event) { + log.Printf("[%v] Kicked %v by %v from %v", now, member, raw.Sender, room) + roomUsersMu.Lock() + delete(roomUsers[room], member) + roomUsersMu.Unlock() + setUserStateAt(room, member, now, Active, Kicked) +} + func handlePowerLevels(now time.Time, room id.RoomID, levels *event.PowerLevelsEventContent, raw *event.Event) { // log.Printf("[%v] Power levels for %v are %v", now, room, levels) levelsCopy := *levels // Looks like mautrix always passes the same pointer here. @@ -242,8 +250,14 @@ func Run() (err error) { switch mem.Membership { case event.MembershipJoin: handleJoin(eventTime(evt), evt.RoomID, member, evt) - case event.MembershipLeave, event.MembershipBan: - handleLeave(eventTime(evt), evt.RoomID, member, evt) + case event.MembershipLeave: + if evt.Sender == member { + handleLeave(eventTime(evt), evt.RoomID, member, evt) + } else { + handleKicked(eventTime(evt), evt.RoomID, member, evt) + } + case event.MembershipBan: + handleKicked(eventTime(evt), evt.RoomID, member, evt) default: // Ignore. } }) diff --git a/misc/infrastructure/powerbot/db.go b/misc/infrastructure/powerbot/db.go index d16e11a3..bc37225e 100644 --- a/misc/infrastructure/powerbot/db.go +++ b/misc/infrastructure/powerbot/db.go @@ -11,9 +11,10 @@ import ( type State int const ( - NotActive State = iota - Idle - Active + Kicked State = -1 + NotActive State = 0 + Idle State = 1 + Active State = 2 ) type Score struct { @@ -114,6 +115,9 @@ func advanceScore(score Score, now time.Time) Score { score.Idle += dt case Active: score.Active += dt + case Kicked: + score.Idle = 0 + score.Active = 0 } return score } -- 2.39.2