-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
policyeval: add initial code for evaluating users in room against ban…
… lists
- Loading branch information
Showing
10 changed files
with
227 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package database | ||
|
||
import ( | ||
"maunium.net/go/mautrix/event" | ||
"maunium.net/go/mautrix/id" | ||
) | ||
|
||
type TakenAction struct { | ||
PolicyList id.RoomID | ||
RuleEntity string | ||
TargetUser id.UserID | ||
Action event.PolicyRecommendation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package database | ||
|
||
import ( | ||
"go.mau.fi/util/dbutil" | ||
) | ||
|
||
type Database struct { | ||
*dbutil.Database | ||
} | ||
|
||
func New(db *dbutil.Database) *Database { | ||
return &Database{ | ||
Database: db, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- v0 -> v1 (compatible with v1+): Latest schema | ||
CREATE TABLE taken_action ( | ||
policy_list TEXT NOT NULL, | ||
rule_entity TEXT NOT NULL, | ||
target_user TEXT NOT NULL, | ||
action TEXT NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package upgrades | ||
|
||
import ( | ||
"embed" | ||
|
||
"go.mau.fi/util/dbutil" | ||
) | ||
|
||
var Table dbutil.UpgradeTable | ||
|
||
//go:embed *.sql | ||
var rawUpgrades embed.FS | ||
|
||
func init() { | ||
Table.RegisterFS(rawUpgrades) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package policyeval | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
"maunium.net/go/mautrix" | ||
"maunium.net/go/mautrix/event" | ||
"maunium.net/go/mautrix/id" | ||
|
||
"go.mau.fi/meowlnir/policylist" | ||
) | ||
|
||
type PolicyEvaluator struct { | ||
Client *mautrix.Client | ||
Store *policylist.Store | ||
|
||
Subscriptions []id.RoomID | ||
ProtectedRooms []id.RoomID | ||
users map[id.UserID][]id.RoomID | ||
usersLock sync.RWMutex | ||
} | ||
|
||
func NewPolicyEvaluator(client *mautrix.Client, store *policylist.Store) *PolicyEvaluator { | ||
return &PolicyEvaluator{ | ||
Client: client, | ||
Store: store, | ||
users: make(map[id.UserID][]id.RoomID), | ||
} | ||
} | ||
|
||
func (pe *PolicyEvaluator) Subscribe(ctx context.Context, roomID id.RoomID) error { | ||
if slices.Contains(pe.Subscriptions, roomID) { | ||
return nil | ||
} | ||
if !pe.Store.Contains(roomID) { | ||
state, err := pe.Client.State(ctx, roomID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get room state: %w", err) | ||
} | ||
pe.Store.Add(roomID, state) | ||
} | ||
pe.Subscriptions = append(pe.Subscriptions, roomID) | ||
return nil | ||
} | ||
|
||
func (pe *PolicyEvaluator) Protect(ctx context.Context, roomID id.RoomID) error { | ||
members, err := pe.Client.Members(ctx, roomID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get room members: %w", err) | ||
} | ||
pe.ProtectedRooms = append(pe.ProtectedRooms, roomID) | ||
start := time.Now() | ||
for _, evt := range members.Chunk { | ||
pe.HandleMember(ctx, evt) | ||
} | ||
zerolog.Ctx(ctx).Debug().Stringer("duration", time.Since(start)).Msg("Processed room members for protection") | ||
return nil | ||
} | ||
|
||
func (pe *PolicyEvaluator) updateUser(userID id.UserID, roomID id.RoomID, add bool) { | ||
pe.usersLock.Lock() | ||
defer pe.usersLock.Unlock() | ||
if add { | ||
if !slices.Contains(pe.users[userID], roomID) { | ||
pe.users[userID] = append(pe.users[userID], roomID) | ||
} | ||
} else if idx := slices.Index(pe.users[userID], roomID); idx >= 0 { | ||
deleted := slices.Delete(pe.users[userID], idx, idx+1) | ||
if len(deleted) == 0 { | ||
delete(pe.users, userID) | ||
} else { | ||
pe.users[userID] = deleted | ||
} | ||
} | ||
} | ||
|
||
func (pe *PolicyEvaluator) HandlePolicyListChange(ctx context.Context, added, removed *policylist.Policy) { | ||
zerolog.Ctx(ctx).Info(). | ||
Any("added", added). | ||
Any("removed", removed). | ||
Msg("Policy list change") | ||
} | ||
|
||
func (pe *PolicyEvaluator) HandleMember(ctx context.Context, evt *event.Event) { | ||
if !slices.Contains(pe.ProtectedRooms, evt.RoomID) { | ||
return | ||
} | ||
switch evt.Content.AsMember().Membership { | ||
case event.MembershipJoin, event.MembershipInvite, event.MembershipKnock: | ||
pe.updateUser(id.UserID(evt.GetStateKey()), evt.RoomID, true) | ||
policy := pe.Store.MatchUser(pe.Subscriptions, id.UserID(evt.GetStateKey())) | ||
if policy != nil { | ||
zerolog.Ctx(ctx).Info(). | ||
Str("user_id", evt.GetStateKey()). | ||
Any("policy", policy). | ||
Msg("Matched user in membership event") | ||
} | ||
case event.MembershipLeave, event.MembershipBan: | ||
pe.updateUser(id.UserID(evt.GetStateKey()), evt.RoomID, false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.