this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

:sparkles: Parse mention posts

+57 -12
+53 -12
cmd/automod/rules.go
··· 16 16 var JABRONI_THRESHOLD = 2 17 17 18 18 func GoodbotBadbotRule(c *automod.RecordContext, post *appbsky.FeedPost) error { 19 - if post.Reply == nil || IsSelfThread(c, post) { 19 + botType := GetBotResponseType(post.Text) 20 + 21 + if botType == -1 { 20 22 return nil 21 23 } 22 24 23 - botType := GetBotResponseType(post.Text) 25 + authorDid := c.Account.Identity.DID.String() 24 26 25 - if botType == -1 { 27 + if post.Reply == nil || IsSelfThread(c, post) { 28 + mentionedDids := mentionedDids(post) 29 + for _, botDid := range mentionedDids { 30 + handleBotSignal(c, botDid, authorDid, botType) 31 + } 26 32 return nil 27 33 } 28 - 29 34 parentURI, err := syntax.ParseATURI(post.Reply.Parent.Uri) 30 35 if err != nil { 31 36 return nil 32 37 } 33 38 34 39 botDid := parentURI.Authority().String() 35 - authorDid := c.Account.Identity.DID.String() 40 + handleBotSignal(c, botDid, authorDid, botType) 41 + 42 + return nil 43 + } 36 44 45 + func handleBotSignal(c *automod.RecordContext, botDid string, authorDid string, botType int) { 37 46 if botType == 1 { 38 47 c.IncrementDistinct("goodbot", botDid, authorDid) 39 48 c.IncrementDistinct("bladerunner", authorDid, botDid) ··· 51 60 // c.Notify("slack") 52 61 } 53 62 54 - return nil 63 + return 55 64 } 56 65 57 66 c.IncrementDistinct("badbot", botDid, authorDid) ··· 70 79 c.Logger.Error("jabroni") 71 80 // c.Notify("slack") 72 81 } 82 + } 73 83 74 - return nil 84 + func mentionedDids(post *appbsky.FeedPost) []string { 85 + mentionedDids := []string{} 86 + for _, facet := range post.Facets { 87 + for _, feature := range facet.Features { 88 + mention := feature.RichtextFacet_Mention 89 + if mention == nil { 90 + continue 91 + } 92 + mentionedDids = append(mentionedDids, mention.Did) 93 + } 94 + } 95 + 96 + return mentionedDids 75 97 } 76 98 77 - // @TODO: this isn't a clean check and doing some duplicate regex checks that can be avoided 99 + // @TODO: this is a dumb check that only matches text exactly, could be improved 78 100 func GetBotResponseType(s string) int { 79 101 // Normalize the string by converting to lowercase and trimming spaces 80 - s = strings.TrimSpace(strings.ToLower(s)) 102 + tokens := TokenizeText(strings.TrimSpace(strings.ToLower(s))) 103 + hasGoodBot := false 104 + hasBadBot := false 81 105 82 - if s == "good bot" { 106 + for i, token := range tokens { 107 + if token != "good" && token != "bad" && token != "bot" && !strings.HasPrefix(token, "@") { 108 + return -1 109 + } 110 + 111 + if (token == "good" || token == "bad") && i+1 < len(tokens) && tokens[i+1] == "bot" { 112 + if token == "good" { 113 + hasGoodBot = true 114 + } else { 115 + hasBadBot = true 116 + } 117 + } 118 + } 119 + 120 + if hasGoodBot { 83 121 return 1 84 122 } 85 123 86 - if s == "bad bot" { 124 + if hasBadBot { 87 125 return 0 88 126 } 89 127 90 - // If neither pattern matches 91 128 return -1 92 129 } 93 130 ··· 111 148 } 112 149 return false 113 150 } 151 + 152 + func TokenizeText(text string) []string { 153 + return strings.Fields(text) 154 + }
+4
cmd/automod/rules_test.go
··· 12 12 assert.Equal(GetBotResponseType("bad bot"), 0) 13 13 assert.Equal(GetBotResponseType("good bot"), 1) 14 14 assert.Equal(GetBotResponseType("good bot beahvior is punished"), -1) 15 + assert.Equal(GetBotResponseType("testing good bot one"), -1) 16 + assert.Equal(GetBotResponseType("testing good bot"), -1) 17 + assert.Equal(GetBotResponseType("@test.bsky.social good bot"), 1) 18 + assert.Equal(GetBotResponseType("bad bot @one.bsky.social"), 0) 15 19 }