Skip to content

Commit

Permalink
automod orga (#494)
Browse files Browse the repository at this point in the history
Several more package extractions.

These are all the easy ones -- flagstore, cachestore, setstore,
directory -- all get yanked out in the same way as countstore already
was.

These didn't cause any major changes to package graph overall, it just
makes the main automod package smaller (yay). Near future targets are to
try to tug engines and events a bit further apart, and that might be
more complicated, so I thought I'd put up the easy-to-review diffs
first.
  • Loading branch information
bnewbold authored Dec 22, 2023
2 parents 5d2ac2e + 07c4fd6 commit f886793
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 44 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
11 changes: 11 additions & 0 deletions automod/cachestore/cachestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cachestore

import (
"context"
)

type CacheStore interface {
Get(ctx context.Context, name, key string) (string, error)
Set(ctx context.Context, name, key string, val string) error
Purge(ctx context.Context, name, key string) error
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package automod
package cachestore

import (
"context"
Expand All @@ -7,12 +7,6 @@ import (
"github.com/hashicorp/golang-lru/v2/expirable"
)

type CacheStore interface {
Get(ctx context.Context, name, key string) (string, error)
Set(ctx context.Context, name, key string, val string) error
Purge(ctx context.Context, name, key string) error
}

type MemCacheStore struct {
Data *expirable.LRU[string, string]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package automod
package cachestore

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

import (
"context"
Expand Down
9 changes: 6 additions & 3 deletions automod/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (

"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/automod/cachestore"
"github.com/bluesky-social/indigo/automod/countstore"
"github.com/bluesky-social/indigo/automod/flagstore"
"github.com/bluesky-social/indigo/automod/setstore"
"github.com/bluesky-social/indigo/xrpc"
)

Expand All @@ -20,9 +23,9 @@ type Engine struct {
Directory identity.Directory
Rules RuleSet
Counters countstore.CountStore
Sets SetStore
Cache CacheStore
Flags FlagStore
Sets setstore.SetStore
Cache cachestore.CacheStore
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
2 changes: 1 addition & 1 deletion automod/setstore.go → automod/setstore/setstore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package automod
package setstore

import (
"context"
Expand Down
9 changes: 6 additions & 3 deletions automod/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ 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/cachestore"
"github.com/bluesky-social/indigo/automod/countstore"
"github.com/bluesky-social/indigo/automod/flagstore"
"github.com/bluesky-social/indigo/automod/setstore"
)

func simpleRule(evt *RecordEvent, post *appbsky.FeedPost) error {
Expand Down Expand Up @@ -41,9 +44,9 @@ func EngineTestFixture() Engine {
simpleRule,
},
}
cache := NewMemCacheStore(10, time.Hour)
flags := NewMemFlagStore()
sets := NewMemSetStore()
cache := cachestore.NewMemCacheStore(10, time.Hour)
flags := flagstore.NewMemFlagStore()
sets := setstore.NewMemSetStore()
sets.Sets["bad-hashtags"] = make(map[string]bool)
sets.Sets["bad-hashtags"]["slur"] = true
dir := identity.NewMockDirectory()
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
4 changes: 2 additions & 2 deletions cmd/hepa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/automod"
"github.com/bluesky-social/indigo/automod/directory"

"github.com/carlmjohnson/versioninfo"
_ "github.com/joho/godotenv/autoload"
Expand Down Expand Up @@ -116,7 +116,7 @@ func configDirectory(cctx *cli.Context) (identity.Directory, error) {
}
var dir identity.Directory
if cctx.String("redis-url") != "" {
rdir, err := automod.NewRedisDirectory(&baseDir, cctx.String("redis-url"), time.Hour*24, time.Minute*2)
rdir, err := directory.NewRedisDirectory(&baseDir, cctx.String("redis-url"), time.Hour*24, time.Minute*2)
if err != nil {
return nil, err
}
Expand Down
17 changes: 10 additions & 7 deletions cmd/hepa/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/automod"
"github.com/bluesky-social/indigo/automod/cachestore"
"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/automod/setstore"
"github.com/bluesky-social/indigo/util"
"github.com/bluesky-social/indigo/xrpc"

Expand Down Expand Up @@ -78,7 +81,7 @@ func NewServer(dir identity.Directory, config Config) (*Server, error) {
xrpcc.Auth.Handle = auth.Handle
}

sets := automod.NewMemSetStore()
sets := setstore.NewMemSetStore()
if config.SetsFileJSON != "" {
if err := sets.LoadFromFileJSON(config.SetsFileJSON); err != nil {
return nil, fmt.Errorf("initializing in-process setstore: %v", err)
Expand All @@ -88,8 +91,8 @@ func NewServer(dir identity.Directory, config Config) (*Server, error) {
}

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

csh, err := automod.NewRedisCacheStore(config.RedisURL, 30*time.Minute)
csh, err := cachestore.NewRedisCacheStore(config.RedisURL, 30*time.Minute)
if err != nil {
return nil, fmt.Errorf("initializing redis cachestore: %v", err)
}
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()
cache = cachestore.NewMemCacheStore(5_000, 30*time.Minute)
flags = flagstore.NewMemFlagStore()
}

engine := automod.Engine{
Expand Down

0 comments on commit f886793

Please sign in to comment.