Skip to content

Commit

Permalink
policyeval: actually evaluate rules on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 2, 2024
1 parent e111c11 commit 44331ac
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 320 deletions.
24 changes: 0 additions & 24 deletions cmd/meowlnir/eventhandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -94,29 +93,6 @@ func (m *Meowlnir) HandleCommand(ctx context.Context, evt *event.Event) {
for _, arg := range args {
m.Client.JoinRoomByID(ctx, id.RoomID(arg))
}
case "!load":
for _, arg := range args {
start := time.Now()
err := room.Subscribe(ctx, id.RoomID(arg))
dur := time.Since(start)
if err != nil {
m.Client.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Failed to load ban list: %v", err))
} else {
m.Client.SendNotice(ctx, evt.RoomID, "Ban list loaded in "+dur.String())
}
}
runtime.GC()
case "!protect":
for _, arg := range args {
start := time.Now()
err := room.Protect(ctx, id.RoomID(arg))
dur := time.Since(start)
if err != nil {
m.Client.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Failed to protect %s: %v", arg, err))
} else {
m.Client.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Protected %s in %s", arg, dur))
}
}
case "!match":
start := time.Now()
match := m.PolicyStore.MatchUser(nil, id.UserID(args[0]))
Expand Down
5 changes: 3 additions & 2 deletions config/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ var (
)

type WatchedPolicyList struct {
Name string `json:"name"`
RoomID id.RoomID `json:"room_id"`
Name string `json:"name"`
}

type WatchedListsEventContent struct {
Lists map[id.RoomID]WatchedPolicyList `json:"lists"`
Lists []WatchedPolicyList `json:"lists"`
}

type ProtectedRoomsEventContent struct {
Expand Down
45 changes: 45 additions & 0 deletions policyeval/evaluate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package policyeval

import (
"context"
"maps"
"slices"

"github.com/rs/zerolog"
"maunium.net/go/mautrix/id"

"go.mau.fi/meowlnir/policylist"
)

func (pe *PolicyEvaluator) EvaluateAll(ctx context.Context) {
pe.usersLock.RLock()
users := slices.Collect(maps.Keys(pe.users))
pe.usersLock.RUnlock()
pe.EvaluateAllMembers(ctx, users)
}

func (pe *PolicyEvaluator) EvaluateAllMembers(ctx context.Context, members []id.UserID) {
for _, member := range members {
pe.EvaluateNewMember(ctx, member)
}
}

func (pe *PolicyEvaluator) EvaluateNewMember(ctx context.Context, userID id.UserID) {
match := pe.Store.MatchUser(pe.GetWatchedLists(), userID)
if match == nil {
return
}
zerolog.Ctx(ctx).Info().
Stringer("user_id", userID).
Any("recommendation", match.Recommendations()).
Any("matches", match).
Msg("Matched user in membership event")
}

func (pe *PolicyEvaluator) EvaluateRemovedRule(ctx context.Context, policy *policylist.Policy) {
// TODO
}

func (pe *PolicyEvaluator) EvaluateAddedRule(ctx context.Context, policy *policylist.Policy) {
// TODO
}
273 changes: 0 additions & 273 deletions policyeval/evaluator.go

This file was deleted.

Loading

0 comments on commit 44331ac

Please sign in to comment.