1package engine
2
3import (
4 "log/slog"
5 "time"
6
7 appbsky "github.com/bluesky-social/indigo/api/bsky"
8 "github.com/bluesky-social/indigo/atproto/identity"
9 "github.com/bluesky-social/indigo/atproto/syntax"
10 "github.com/bluesky-social/indigo/automod/cachestore"
11 "github.com/bluesky-social/indigo/automod/countstore"
12 "github.com/bluesky-social/indigo/automod/flagstore"
13 "github.com/bluesky-social/indigo/automod/setstore"
14)
15
16var _ PostRuleFunc = simpleRule
17
18func simpleRule(c *RecordContext, post *appbsky.FeedPost) error {
19 for _, tag := range post.Tags {
20 if c.InSet("bad-hashtags", tag) {
21 c.AddRecordLabel("bad-hashtag")
22 break
23 }
24 }
25 for _, facet := range post.Facets {
26 for _, feat := range facet.Features {
27 if feat.RichtextFacet_Tag != nil {
28 tag := feat.RichtextFacet_Tag.Tag
29 if c.InSet("bad-hashtags", tag) {
30 c.AddRecordLabel("bad-hashtag")
31 break
32 }
33 }
34 }
35 }
36 return nil
37}
38
39func EngineTestFixture() Engine {
40 rules := RuleSet{
41 PostRules: []PostRuleFunc{
42 simpleRule,
43 },
44 }
45 cache := cachestore.NewMemCacheStore(10, time.Hour)
46 flags := flagstore.NewMemFlagStore()
47 sets := setstore.NewMemSetStore()
48 sets.Sets["bad-hashtags"] = make(map[string]bool)
49 sets.Sets["bad-hashtags"]["slur"] = true
50 sets.Sets["bad-words"] = make(map[string]bool)
51 sets.Sets["bad-words"]["hardr"] = true
52 sets.Sets["bad-words"]["hardestr"] = true
53 sets.Sets["worst-words"] = make(map[string]bool)
54 sets.Sets["worst-words"]["hardestr"] = true
55 dir := identity.NewMockDirectory()
56 id1 := identity.Identity{
57 DID: syntax.DID("did:plc:abc111"),
58 Handle: syntax.Handle("handle.example.com"),
59 }
60 dir.Insert(id1)
61 eng := Engine{
62 Logger: slog.Default(),
63 Directory: &dir,
64 Counters: countstore.NewMemCountStore(),
65 Sets: sets,
66 Flags: flags,
67 Cache: cache,
68 Rules: rules,
69 }
70 return eng
71}
72
73// Helper to access the private effects field from a context. Intended for use in test code, *not* from rules.
74func ExtractEffects(c *BaseContext) *Effects {
75 return c.effects
76}