Skip to content

Commit

Permalink
automod: more persisting and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bnewbold committed Nov 14, 2023
1 parent 32ec9f6 commit 26c784a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions automod/countstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
type CountStore interface {
GetCount(ctx context.Context, key, period string) (int, error)
Increment(ctx context.Context, key string) error
// TODO: batch increment method
}

// TODO: this implementation isn't race-safe (yet)!
Expand Down
41 changes: 32 additions & 9 deletions automod/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ func (e *Engine) ProcessIdentityEvent(ctx context.Context, t string, did syntax.
Account: AccountMeta{Identity: ident},
},
}
// TODO: call rules
// TODO: handle errors
_ = evt
if err := e.Rules.CallIdentityRules(&evt); err != nil {
return err
}
if evt.Err != nil {
return evt.Err
}
evt.CanonicalLogLine()
if err := evt.PersistAccountActions(ctx); err != nil {
return err
}
if err := evt.PersistCounters(ctx); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -81,19 +88,35 @@ func (e *Engine) ProcessRecord(ctx context.Context, did syntax.DID, path, recCID
}
evt := e.NewPostEvent(ident, path, recCID, post)
e.Logger.Info("processing post", "did", ident.DID, "path", path)
_ = evt
// TODO: call rules
if err := e.Rules.CallPostRules(&evt); err != nil {
return err
}
if evt.Err != nil {
return evt.Err
}
evt.CanonicalLogLine()
if err := evt.PersistAccountActions(ctx); err != nil {
return err
}
if err := evt.PersistCounters(ctx); err != nil {
return err
}
default:
evt := e.NewRecordEvent(ident, path, recCID, rec)
e.Logger.Info("processing record", "did", ident.DID, "path", path)
_ = evt
// TODO: call rules
if err := e.Rules.CallRecordRules(&evt); err != nil {
return err
}
if evt.Err != nil {
return evt.Err
}
evt.CanonicalLogLine()
if err := evt.PersistAccountActions(ctx); err != nil {
return err
}
if err := evt.PersistCounters(ctx); err != nil {
return err
}
}

return nil
Expand All @@ -105,7 +128,7 @@ func (e *Engine) NewPostEvent(ident *identity.Identity, path, recCID string, pos
RecordEvent{
Event{
Engine: e,
Logger: e.Logger,
Logger: e.Logger.With("did", ident.DID, "collection", parts[0], "rkey", parts[1]),
Account: AccountMeta{Identity: ident},
},
parts[0],
Expand All @@ -125,7 +148,7 @@ func (e *Engine) NewRecordEvent(ident *identity.Identity, path, recCID string, r
return RecordEvent{
Event{
Engine: e,
Logger: e.Logger,
Logger: e.Logger.With("did", ident.DID, "collection", parts[0], "rkey", parts[1]),
Account: AccountMeta{Identity: ident},
},
parts[0],
Expand Down
2 changes: 1 addition & 1 deletion automod/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ func TestEngineBasics(t *testing.T) {
Text: "some post blah",
Tags: []string{"one", "slur"},
}
assert.NoError(engine.ProcessRecord(ctx, id1.DID, path, cid1, &p2))
assert.Error(engine.ProcessRecord(ctx, id1.DID, path, cid1, &p2))
}
36 changes: 34 additions & 2 deletions automod/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (e *Event) PersistAccountActions(ctx context.Context) error {
if len(e.AccountLabels) > 0 {
_, err := comatproto.AdminTakeModerationAction(ctx, xrpcc, &comatproto.AdminTakeModerationAction_Input{
Action: "com.atproto.admin.defs#createLabels",
CreateLabelVals: e.AccountLabels,
CreateLabelVals: dedupeStrings(e.AccountLabels),
Reason: "automod",
CreatedBy: xrpcc.Auth.Did,
Subject: &comatproto.AdminTakeModerationAction_Input_Subject{
Expand Down Expand Up @@ -144,6 +144,25 @@ func (e *Event) PersistAccountActions(ctx context.Context) error {
return nil
}

func (e *Event) PersistCounters(ctx context.Context) error {
for _, k := range dedupeStrings(e.CounterIncrements) {
err := e.Engine.Counters.Increment(ctx, k)
if err != nil {
return err
}
}
return nil
}

func (e *Event) CanonicalLogLine() {
e.Logger.Info("canonical-event-line",
"accountLabels", e.AccountLabels,
"accountFlags", e.AccountFlags,
"accountTakedown", e.AccountTakedown,
"accountReports", len(e.AccountReports),
)
}

type IdentityEvent struct {
Event
}
Expand Down Expand Up @@ -189,7 +208,7 @@ func (e *RecordEvent) PersistRecordActions(ctx context.Context) error {
if len(e.RecordLabels) > 0 {
_, err := comatproto.AdminTakeModerationAction(ctx, xrpcc, &comatproto.AdminTakeModerationAction_Input{
Action: "com.atproto.admin.defs#createLabels",
CreateLabelVals: e.RecordLabels,
CreateLabelVals: dedupeStrings(e.RecordLabels),
Reason: "automod",
CreatedBy: xrpcc.Auth.Did,
Subject: &comatproto.AdminTakeModerationAction_Input_Subject{
Expand Down Expand Up @@ -229,6 +248,19 @@ func (e *RecordEvent) PersistRecordActions(ctx context.Context) error {
return nil
}

func (e *RecordEvent) CanonicalLogLine() {
e.Logger.Info("canonical-event-line",
"accountLabels", e.AccountLabels,
"accountFlags", e.AccountFlags,
"accountTakedown", e.AccountTakedown,
"accountReports", len(e.AccountReports),
"recordLabels", e.RecordLabels,
"recordFlags", e.RecordFlags,
"recordTakedown", e.RecordTakedown,
"recordReports", len(e.RecordReports),
)
}

type PostEvent struct {
RecordEvent

Expand Down
13 changes: 13 additions & 0 deletions automod/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package automod

func dedupeStrings(in []string) []string {
var out []string
seen := make(map[string]bool)
for _, v := range in {
if !seen[v] {
out = append(out, v)
seen[v] = true
}
}
return out
}

0 comments on commit 26c784a

Please sign in to comment.