Skip to content

Commit 275b58a

Browse files
committed
policyeval: add support for creator power
1 parent 13e92e8 commit 275b58a

File tree

7 files changed

+46
-24
lines changed

7 files changed

+46
-24
lines changed

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module go.mau.fi/meowlnir
22

33
go 1.23.0
44

5-
toolchain go1.24.4
5+
toolchain go1.24.5
66

77
require (
88
github.com/lib/pq v1.10.9
@@ -12,7 +12,7 @@ require (
1212
go.mau.fi/zeroconfig v0.1.3
1313
gopkg.in/yaml.v3 v3.0.1
1414
maunium.net/go/mauflag v1.0.0
15-
maunium.net/go/mautrix v0.24.2-0.20250701202943-71b994b3fd47
15+
maunium.net/go/mautrix v0.24.3-0.20250718205928-4866da52005c
1616
)
1717

1818
require (
@@ -37,11 +37,11 @@ require (
3737
github.com/tidwall/pretty v1.2.1 // indirect
3838
github.com/tidwall/sjson v1.2.5 // indirect
3939
github.com/yuin/goldmark v1.7.12 // indirect
40-
golang.org/x/crypto v0.39.0 // indirect
41-
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 // indirect
42-
golang.org/x/net v0.41.0 // indirect
43-
golang.org/x/sys v0.33.0 // indirect
44-
golang.org/x/text v0.26.0 // indirect
40+
golang.org/x/crypto v0.40.0 // indirect
41+
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc // indirect
42+
golang.org/x/net v0.42.0 // indirect
43+
golang.org/x/sys v0.34.0 // indirect
44+
golang.org/x/text v0.27.0 // indirect
4545
google.golang.org/protobuf v1.36.6 // indirect
4646
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
4747
)

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ go.mau.fi/util v0.8.8 h1:OnuEEc/sIJFhnq4kFggiImUpcmnmL/xpvQMRu5Fiy5c=
7272
go.mau.fi/util v0.8.8/go.mod h1:Y/kS3loxTEhy8Vill513EtPXr+CRDdae+Xj2BXXMy/c=
7373
go.mau.fi/zeroconfig v0.1.3 h1:As9wYDKmktjmNZW5i1vn8zvJlmGKHeVxHVIBMXsm4kM=
7474
go.mau.fi/zeroconfig v0.1.3/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
75-
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
76-
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
77-
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 h1:bsqhLWFR6G6xiQcb+JoGqdKdRU6WzPWmK8E0jxTjzo4=
78-
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
79-
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
80-
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
75+
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
76+
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
77+
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc h1:TS73t7x3KarrNd5qAipmspBDS1rkMcgVG/fS1aRb4Rc=
78+
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc/go.mod h1:A+z0yzpGtvnG90cToK5n2tu8UJVP2XUATh+r+sfOOOc=
79+
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
80+
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
8181
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8282
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8383
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
84-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
85-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
86-
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
87-
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
84+
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
85+
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
86+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
87+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
8888
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
8989
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
9090
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -96,5 +96,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9696
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9797
maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
9898
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
99-
maunium.net/go/mautrix v0.24.2-0.20250701202943-71b994b3fd47 h1:l7O373huomz05z57V/12x3dERxa7P6dG/dRQmKzuGrg=
100-
maunium.net/go/mautrix v0.24.2-0.20250701202943-71b994b3fd47/go.mod h1:Xy6o+pXmbqmgWsUWh15EQ1eozjC+k/VT/7kloByv9PI=
99+
maunium.net/go/mautrix v0.24.3-0.20250718205928-4866da52005c h1:Sl+wOfsBaC34D9vNaQVY0O5n6SDFJzGAY1xv6TaJC/M=
100+
maunium.net/go/mautrix v0.24.3-0.20250718205928-4866da52005c/go.mod h1:1ut900w++eE9by9yqCR2dQdMqwsHwZG5L+1bKB1EvSA=

policyeval/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ var cmdPowerLevel = &CommandHandler{
152152
}
153153
for _, room := range rooms {
154154
var pls event.PowerLevelsEventContent
155+
// No need to fetch the create event here, this is a manual update that is allowed to fail if the user holds it wrong
155156
err = ce.Meta.Bot.Client.StateEvent(ce.Ctx, room, event.StatePowerLevels, "", &pls)
156157
if err != nil {
157158
ce.Reply("Failed to get power levels in %s: %v", format.SafeMarkdownCode(room), err)

policyeval/eventhandle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (pe *PolicyEvaluator) HandleConfigChange(ctx context.Context, evt *event.Ev
1919
var errorMsg, successMsg string
2020
switch evt.Type {
2121
case event.StatePowerLevels:
22-
errorMsg = pe.handlePowerLevels(evt)
22+
errorMsg = pe.handlePowerLevels(ctx, evt)
2323
case config.StateWatchedLists:
2424
successMsgs, errorMsgs := pe.handleWatchedLists(ctx, evt, false)
2525
successMsg = strings.Join(successMsgs, "\n")

policyeval/execute.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ func (pe *PolicyEvaluator) redactEventsInRoom(ctx context.Context, userID id.Use
378378
}
379379

380380
func (pe *PolicyEvaluator) redactRecentMessages(ctx context.Context, roomID id.RoomID, sender id.UserID, maxAge time.Duration, redactState bool, reason string) (int, error) {
381-
var pls event.PowerLevelsEventContent
382-
err := pe.Bot.StateEvent(ctx, roomID, event.StatePowerLevels, "", &pls)
381+
pls, err := pe.Bot.Intent.PowerLevels(ctx, roomID)
383382
if err != nil {
384383
return 0, fmt.Errorf("failed to get power levels: %w", err)
385384
}

policyeval/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (pe *PolicyEvaluator) tryLoad(ctx context.Context) error {
183183
var errors []string
184184
if evt, ok := state[event.StatePowerLevels][""]; !ok {
185185
return fmt.Errorf("no power level event found in management room")
186-
} else if errMsg := pe.handlePowerLevels(evt); errMsg != "" {
186+
} else if errMsg := pe.handlePowerLevels(ctx, evt); errMsg != "" {
187187
errors = append(errors, errMsg)
188188
}
189189
if evt, ok := state[config.StateWatchedLists][""]; !ok {
@@ -225,13 +225,25 @@ func (pe *PolicyEvaluator) tryLoad(ctx context.Context) error {
225225
return nil
226226
}
227227

228-
func (pe *PolicyEvaluator) handlePowerLevels(evt *event.Event) string {
228+
func (pe *PolicyEvaluator) handlePowerLevels(ctx context.Context, evt *event.Event) string {
229229
content, ok := evt.Content.Parsed.(*event.PowerLevelsEventContent)
230230
if !ok {
231231
return "* Failed to parse power level event"
232232
}
233+
err := pe.Bot.Intent.FillPowerLevelCreateEvent(ctx, evt.RoomID, content)
234+
if err != nil {
235+
zerolog.Ctx(ctx).Err(err).
236+
Stringer("room_id", evt.RoomID).
237+
Msg("Failed to get create event for power levels in management room power level handler")
238+
}
233239
adminLevel := content.GetEventLevel(config.StateWatchedLists)
234240
admins := exsync.NewSet[id.UserID]()
241+
if content.CreateEvent != nil && content.CreateEvent.Content.AsCreate().SupportsCreatorPower() {
242+
admins.Add(content.CreateEvent.Sender)
243+
for _, creator := range content.CreateEvent.Content.AsCreate().AdditionalCreators {
244+
admins.Add(creator)
245+
}
246+
}
235247
for user, level := range content.Users {
236248
if level >= adminLevel {
237249
admins.Add(user)

policyeval/protectedrooms.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ func (pe *PolicyEvaluator) HandleProtectedRoomMeta(ctx context.Context, evt *eve
6363

6464
func (pe *PolicyEvaluator) handleProtectedRoomPowerLevels(ctx context.Context, evt *event.Event) {
6565
powerLevels := evt.Content.AsPowerLevels()
66+
err := pe.Bot.Intent.FillPowerLevelCreateEvent(ctx, evt.RoomID, powerLevels)
67+
if err != nil {
68+
zerolog.Ctx(ctx).Err(err).
69+
Stringer("room_id", evt.RoomID).
70+
Msg("Failed to get create event for power levels in protected room power level handler")
71+
}
6672
ownLevel := powerLevels.GetUserLevel(pe.Bot.UserID)
6773
minLevel := max(powerLevels.Ban(), powerLevels.Redact())
6874
pe.protectedRoomsLock.RLock()
@@ -141,6 +147,10 @@ func (pe *PolicyEvaluator) tryProtectingRoom(ctx context.Context, joinedRooms *m
141147
if err != nil {
142148
return nil, fmt.Sprintf("* Failed to get power levels for [%s](%s): %v", roomID, roomID.URI().MatrixToURL(), err)
143149
}
150+
powerLevels.CreateEvent, err = pe.Bot.FullStateEvent(ctx, roomID, event.StateCreate, "")
151+
if err != nil {
152+
return nil, fmt.Sprintf("* Failed to get creation content for [%s](%s): %v", roomID, roomID.URI().MatrixToURL(), err)
153+
}
144154
ownLevel := powerLevels.GetUserLevel(pe.Bot.UserID)
145155
minLevel := max(powerLevels.Ban(), powerLevels.Redact())
146156
if ownLevel < minLevel && !pe.DryRun {

0 commit comments

Comments
 (0)