this repo has no description
0
fork

Configure Feed

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

add `PostParentOrRootIsDid` and `PostMentionsDid` to automod helpers (#778)

couple of helpers for a case i keep running into.

authored by

bnewbold and committed by
GitHub
150e0519 06bacb46

+191
+57
automod/rules/helpers.go
··· 283 283 } 284 284 return false 285 285 } 286 + 287 + func PostParentOrRootIsDid(post *appbsky.FeedPost, did string) bool { 288 + if post.Reply == nil { 289 + return false 290 + } 291 + 292 + rootUri, err := syntax.ParseATURI(post.Reply.Root.Uri) 293 + if err != nil || !rootUri.Authority().IsDID() { 294 + return false 295 + } 296 + 297 + parentUri, err := syntax.ParseATURI(post.Reply.Parent.Uri) 298 + if err != nil || !parentUri.Authority().IsDID() { 299 + return false 300 + } 301 + 302 + return rootUri.Authority().String() == did || parentUri.Authority().String() == did 303 + } 304 + 305 + func PostParentOrRootIsAnyDid(post *appbsky.FeedPost, dids []string) bool { 306 + if post.Reply == nil { 307 + return false 308 + } 309 + 310 + for _, did := range dids { 311 + if PostParentOrRootIsDid(post, did) { 312 + return true 313 + } 314 + } 315 + 316 + return false 317 + } 318 + 319 + func PostMentionsDid(post *appbsky.FeedPost, did string) bool { 320 + facets, err := ExtractFacets(post) 321 + if err != nil { 322 + return false 323 + } 324 + 325 + for _, facet := range facets { 326 + if facet.DID != nil && *facet.DID == did { 327 + return true 328 + } 329 + } 330 + 331 + return false 332 + } 333 + 334 + func PostMentionsAnyDid(post *appbsky.FeedPost, dids []string) bool { 335 + for _, did := range dids { 336 + if PostMentionsDid(post, did) { 337 + return true 338 + } 339 + } 340 + 341 + return false 342 + }
+134
automod/rules/helpers_test.go
··· 1 1 package rules 2 2 3 3 import ( 4 + comatproto "github.com/bluesky-social/indigo/api/atproto" 5 + appbsky "github.com/bluesky-social/indigo/api/bsky" 4 6 "testing" 5 7 "time" 6 8 ··· 115 117 assert.True(AccountIsOlderThan(&ac, time.Hour)) 116 118 assert.False(AccountIsOlderThan(&ac, 48*time.Hour)) 117 119 } 120 + 121 + func TestParentOrRootIsDid(t *testing.T) { 122 + assert := assert.New(t) 123 + 124 + post1 := &appbsky.FeedPost{ 125 + Text: "some random post that i dreamt up last night, idk", 126 + Reply: &appbsky.FeedPost_ReplyRef{ 127 + Root: &comatproto.RepoStrongRef{ 128 + Uri: "at://did:plc:abc123/app.bsky.feed.post/rkey123", 129 + }, 130 + Parent: &comatproto.RepoStrongRef{ 131 + Uri: "at://did:plc:abc123/app.bsky.feed.post/rkey123", 132 + }, 133 + }, 134 + } 135 + 136 + post2 := &appbsky.FeedPost{ 137 + Text: "some random post that i dreamt up last night, idk", 138 + Reply: &appbsky.FeedPost_ReplyRef{ 139 + Root: &comatproto.RepoStrongRef{ 140 + Uri: "at://did:plc:321abc/app.bsky.feed.post/rkey123", 141 + }, 142 + Parent: &comatproto.RepoStrongRef{ 143 + Uri: "at://did:plc:abc123/app.bsky.feed.post/rkey123", 144 + }, 145 + }, 146 + } 147 + 148 + post3 := &appbsky.FeedPost{ 149 + Text: "some random post that i dreamt up last night, idk", 150 + Reply: &appbsky.FeedPost_ReplyRef{ 151 + Root: &comatproto.RepoStrongRef{ 152 + Uri: "at://did:plc:abc123/app.bsky.feed.post/rkey123", 153 + }, 154 + Parent: &comatproto.RepoStrongRef{ 155 + Uri: "at://did:plc:321abc/app.bsky.feed.post/rkey123", 156 + }, 157 + }, 158 + } 159 + 160 + post4 := &appbsky.FeedPost{ 161 + Text: "some random post that i dreamt up last night, idk", 162 + Reply: &appbsky.FeedPost_ReplyRef{ 163 + Root: &comatproto.RepoStrongRef{ 164 + Uri: "at://did:plc:321abc/app.bsky.feed.post/rkey123", 165 + }, 166 + Parent: &comatproto.RepoStrongRef{ 167 + Uri: "at://did:plc:321abc/app.bsky.feed.post/rkey123", 168 + }, 169 + }, 170 + } 171 + 172 + assert.True(PostParentOrRootIsDid(post1, "did:plc:abc123")) 173 + assert.False(PostParentOrRootIsDid(post1, "did:plc:321abc")) 174 + 175 + assert.True(PostParentOrRootIsDid(post2, "did:plc:abc123")) 176 + assert.True(PostParentOrRootIsDid(post2, "did:plc:321abc")) 177 + 178 + assert.True(PostParentOrRootIsDid(post3, "did:plc:abc123")) 179 + assert.True(PostParentOrRootIsDid(post3, "did:plc:321abc")) 180 + 181 + assert.False(PostParentOrRootIsDid(post4, "did:plc:abc123")) 182 + assert.True(PostParentOrRootIsDid(post4, "did:plc:321abc")) 183 + 184 + didList1 := []string{ 185 + "did:plc:cba321", 186 + "did:web:bsky.app", 187 + "did:plc:abc123", 188 + } 189 + 190 + didList2 := []string{ 191 + "did:plc:321cba", 192 + "did:web:bsky.app", 193 + "did:plc:123abc", 194 + } 195 + 196 + assert.True(PostParentOrRootIsAnyDid(post1, didList1)) 197 + assert.False(PostParentOrRootIsAnyDid(post1, didList2)) 198 + } 199 + 200 + func TestPostMentionsDid(t *testing.T) { 201 + assert := assert.New(t) 202 + 203 + post := &appbsky.FeedPost{ 204 + Text: "@hailey.at what is upppp also hello to @darthbluesky.bsky.social", 205 + Facets: []*appbsky.RichtextFacet{ 206 + { 207 + Features: []*appbsky.RichtextFacet_Features_Elem{ 208 + { 209 + RichtextFacet_Mention: &appbsky.RichtextFacet_Mention{ 210 + Did: "did:plc:abc123", 211 + }, 212 + }, 213 + }, 214 + Index: &appbsky.RichtextFacet_ByteSlice{ 215 + ByteStart: 0, 216 + ByteEnd: 9, 217 + }, 218 + }, 219 + { 220 + Features: []*appbsky.RichtextFacet_Features_Elem{ 221 + { 222 + RichtextFacet_Mention: &appbsky.RichtextFacet_Mention{ 223 + Did: "did:plc:abc456", 224 + }, 225 + }, 226 + }, 227 + Index: &appbsky.RichtextFacet_ByteSlice{ 228 + ByteStart: 39, 229 + ByteEnd: 63, 230 + }, 231 + }, 232 + }, 233 + } 234 + assert.True(PostMentionsDid(post, "did:plc:abc123")) 235 + assert.False(PostMentionsDid(post, "did:plc:cba321")) 236 + 237 + didList1 := []string{ 238 + "did:plc:cba321", 239 + "did:web:bsky.app", 240 + "did:plc:abc456", 241 + } 242 + 243 + didList2 := []string{ 244 + "did:plc:321cba", 245 + "did:web:bsky.app", 246 + "did:plc:123abc", 247 + } 248 + 249 + assert.True(PostMentionsAnyDid(post, didList1)) 250 + assert.False(PostMentionsAnyDid(post, didList2)) 251 + }