Skip to content

Commit

Permalink
automod: quick extraction of flagstore package.
Browse files Browse the repository at this point in the history
Did not rename, so some names are stuttery (flagstore.FlagStore).
This can be fixed in a future pass if desired; today's goal is just
more packages and getting the filesystem tree approximately right.
  • Loading branch information
warpfork committed Dec 22, 2023
1 parent 5d2ac2e commit be984df
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 24 deletions.
5 changes: 3 additions & 2 deletions automod/account_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
appbsky "github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/automod/util"
)

type ProfileSummary struct {
Expand Down Expand Up @@ -95,8 +96,8 @@ func (e *Engine) GetAccountMeta(ctx context.Context, ident *identity.Identity) (
Description: pv.Description,
DisplayName: pv.DisplayName,
},
AccountLabels: dedupeStrings(labels),
AccountNegatedLabels: dedupeStrings(negLabels),
AccountLabels: util.DedupeStrings(labels),
AccountNegatedLabels: util.DedupeStrings(negLabels),
AccountFlags: flags,
}
if pv.PostsCount != nil {
Expand Down
3 changes: 2 additions & 1 deletion automod/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/automod/countstore"
"github.com/bluesky-social/indigo/automod/flagstore"
"github.com/bluesky-social/indigo/xrpc"
)

Expand All @@ -22,7 +23,7 @@ type Engine struct {
Counters countstore.CountStore
Sets SetStore
Cache CacheStore
Flags FlagStore
Flags flagstore.FlagStore
RelayClient *xrpc.Client
BskyClient *xrpc.Client
// used to persist moderation actions in mod service (optional)
Expand Down
9 changes: 5 additions & 4 deletions automod/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
appbsky "github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/automod/countstore"
"github.com/bluesky-social/indigo/automod/util"
"github.com/bluesky-social/indigo/xrpc"
)

Expand Down Expand Up @@ -164,7 +165,7 @@ func slackBody(header string, acct AccountMeta, newLabels, newFlags []string, ne

func dedupeLabelActions(labels, existing, existingNegated []string) []string {
newLabels := []string{}
for _, val := range dedupeStrings(labels) {
for _, val := range util.DedupeStrings(labels) {
exists := false
for _, e := range existingNegated {
if val == e {
Expand All @@ -187,7 +188,7 @@ func dedupeLabelActions(labels, existing, existingNegated []string) []string {

func dedupeFlagActions(flags, existing []string) []string {
newFlags := []string{}
for _, val := range dedupeStrings(flags) {
for _, val := range util.DedupeStrings(flags) {
exists := false
for _, e := range existing {
if val == e {
Expand Down Expand Up @@ -483,8 +484,8 @@ func (e *RecordEvent) ReportRecord(reason, comment string) {
func (e *RecordEvent) PersistRecordActions(ctx context.Context) error {

// NOTE: record-level actions are *not* currently de-duplicated (aka, the same record could be labeled multiple times, or re-reported, etc)
newLabels := dedupeStrings(e.RecordLabels)
newFlags := dedupeStrings(e.RecordFlags)
newLabels := util.DedupeStrings(e.RecordLabels)
newFlags := util.DedupeStrings(e.RecordFlags)
newReports := circuitBreakReports(&e.RepoEvent, e.RecordReports)
newTakedown := circuitBreakTakedown(&e.RepoEvent, e.RecordTakedown)
atURI := fmt.Sprintf("at://%s/%s/%s", e.Account.Identity.DID, e.Collection, e.RecordKey)
Expand Down
11 changes: 11 additions & 0 deletions automod/flagstore/flagstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package flagstore

import (
"context"
)

type FlagStore interface {
Get(ctx context.Context, key string) ([]string, error)
Add(ctx context.Context, key string, flags []string) error
Remove(ctx context.Context, key string, flags []string) error
}
12 changes: 4 additions & 8 deletions automod/flagstore.go → automod/flagstore/flagstore_mem.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package automod
package flagstore

import (
"context"
)

type FlagStore interface {
Get(ctx context.Context, key string) ([]string, error)
Add(ctx context.Context, key string, flags []string) error
Remove(ctx context.Context, key string, flags []string) error
}
"github.com/bluesky-social/indigo/automod/util"
)

type MemFlagStore struct {
Data map[string][]string
Expand Down Expand Up @@ -36,7 +32,7 @@ func (s MemFlagStore) Add(ctx context.Context, key string, flags []string) error
for _, f := range flags {
v = append(v, f)
}
v = dedupeStrings(v)
v = util.DedupeStrings(v)
s.Data[key] = v
return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package automod
package flagstore

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package automod
package flagstore

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package automod
package flagstore

import (
"context"
Expand Down
3 changes: 2 additions & 1 deletion automod/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/automod/countstore"
"github.com/bluesky-social/indigo/automod/flagstore"
)

func simpleRule(evt *RecordEvent, post *appbsky.FeedPost) error {
Expand Down Expand Up @@ -42,7 +43,7 @@ func EngineTestFixture() Engine {
},
}
cache := NewMemCacheStore(10, time.Hour)
flags := NewMemFlagStore()
flags := flagstore.NewMemFlagStore()
sets := NewMemSetStore()
sets.Sets["bad-hashtags"] = make(map[string]bool)
sets.Sets["bad-hashtags"]["slur"] = true
Expand Down
4 changes: 2 additions & 2 deletions automod/util.go → automod/util/strings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package automod
package util

func dedupeStrings(in []string) []string {
func DedupeStrings(in []string) []string {
var out []string
seen := make(map[string]bool)
for _, v := range in {
Expand Down
7 changes: 4 additions & 3 deletions cmd/hepa/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/automod"
"github.com/bluesky-social/indigo/automod/countstore"
"github.com/bluesky-social/indigo/automod/flagstore"
"github.com/bluesky-social/indigo/automod/rules"
"github.com/bluesky-social/indigo/util"
"github.com/bluesky-social/indigo/xrpc"
Expand Down Expand Up @@ -89,7 +90,7 @@ func NewServer(dir identity.Directory, config Config) (*Server, error) {

var counters countstore.CountStore
var cache automod.CacheStore
var flags automod.FlagStore
var flags flagstore.FlagStore
var rdb *redis.Client
if config.RedisURL != "" {
// generic client, for cursor state
Expand All @@ -116,15 +117,15 @@ func NewServer(dir identity.Directory, config Config) (*Server, error) {
}
cache = csh

flg, err := automod.NewRedisFlagStore(config.RedisURL)
flg, err := flagstore.NewRedisFlagStore(config.RedisURL)
if err != nil {
return nil, fmt.Errorf("initializing redis flagstore: %v", err)
}
flags = flg
} else {
counters = countstore.NewMemCountStore()
cache = automod.NewMemCacheStore(5_000, 30*time.Minute)
flags = automod.NewMemFlagStore()
flags = flagstore.NewMemFlagStore()
}

engine := automod.Engine{
Expand Down

0 comments on commit be984df

Please sign in to comment.