-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor rule helpers in to sub-module (#783)
- Loading branch information
Showing
19 changed files
with
275 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package helpers | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/bluesky-social/indigo/automod" | ||
) | ||
|
||
// no accounts exist before this time | ||
var atprotoAccountEpoch = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) | ||
|
||
// returns true if account creation timestamp is plausible: not-nil, not in distant past, not in the future | ||
func plausibleAccountCreation(when *time.Time) bool { | ||
if when == nil { | ||
return false | ||
} | ||
// this is mostly to check for misconfigurations or null values (eg, UNIX epoch zero means "unknown" not actually 1970) | ||
if !when.After(atprotoAccountEpoch) { | ||
return false | ||
} | ||
// a timestamp in the future would also indicate some misconfiguration | ||
if when.After(time.Now().Add(time.Hour)) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// checks if account was created recently, based on either public or private account metadata. if metadata isn't available at all, or seems bogus, returns 'false' | ||
func AccountIsYoungerThan(c *automod.AccountContext, age time.Duration) bool { | ||
// TODO: consider swapping priority order here (and below) | ||
if c.Account.CreatedAt != nil && plausibleAccountCreation(c.Account.CreatedAt) { | ||
return time.Since(*c.Account.CreatedAt) < age | ||
} | ||
if c.Account.Private != nil && plausibleAccountCreation(c.Account.Private.IndexedAt) { | ||
return time.Since(*c.Account.Private.IndexedAt) < age | ||
} | ||
return false | ||
} | ||
|
||
// checks if account was *not* created recently, based on either public or private account metadata. if metadata isn't available at all, or seems bogus, returns 'false' | ||
func AccountIsOlderThan(c *automod.AccountContext, age time.Duration) bool { | ||
if c.Account.CreatedAt != nil && plausibleAccountCreation(c.Account.CreatedAt) { | ||
return time.Since(*c.Account.CreatedAt) >= age | ||
} | ||
if c.Account.Private != nil && plausibleAccountCreation(c.Account.Private.IndexedAt) { | ||
return time.Since(*c.Account.Private.IndexedAt) >= age | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/bluesky-social/indigo/atproto/identity" | ||
"github.com/bluesky-social/indigo/atproto/syntax" | ||
"github.com/bluesky-social/indigo/automod" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAccountIsYoungerThan(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
am := automod.AccountMeta{ | ||
Identity: &identity.Identity{ | ||
DID: syntax.DID("did:plc:abc111"), | ||
Handle: syntax.Handle("handle.example.com"), | ||
}, | ||
Profile: automod.ProfileSummary{}, | ||
Private: nil, | ||
} | ||
now := time.Now() | ||
ac := automod.AccountContext{ | ||
Account: am, | ||
} | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
ac.Account.CreatedAt = &now | ||
assert.True(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
yesterday := time.Now().Add(-1 * time.Hour * 24) | ||
ac.Account.CreatedAt = &yesterday | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.True(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
old := time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC) | ||
ac.Account.CreatedAt = &old | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour*24*365*100)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour*24*365*100)) | ||
|
||
future := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC) | ||
ac.Account.CreatedAt = &future | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
ac.Account.CreatedAt = nil | ||
ac.Account.Private = &automod.AccountPrivate{ | ||
Email: "[email protected]", | ||
IndexedAt: &yesterday, | ||
} | ||
assert.True(AccountIsYoungerThan(&ac, 48*time.Hour)) | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.True(AccountIsOlderThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, 48*time.Hour)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 1 addition & 111 deletions
112
automod/rules/helpers_test.go → automod/helpers/bsky_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,13 @@ | ||
package rules | ||
package helpers | ||
|
||
import ( | ||
comatproto "github.com/bluesky-social/indigo/api/atproto" | ||
appbsky "github.com/bluesky-social/indigo/api/bsky" | ||
"testing" | ||
"time" | ||
|
||
"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/keyword" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTokenizeText(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
fixtures := []struct { | ||
s string | ||
out []string | ||
}{ | ||
{ | ||
s: "1 'Two' three!", | ||
out: []string{"1", "two", "three"}, | ||
}, | ||
{ | ||
s: " foo1;bar2,baz3...", | ||
out: []string{"foo1", "bar2", "baz3"}, | ||
}, | ||
{ | ||
s: "https://example.com/index.html", | ||
out: []string{"https", "example", "com", "index", "html"}, | ||
}, | ||
} | ||
|
||
for _, fix := range fixtures { | ||
assert.Equal(fix.out, keyword.TokenizeText(fix.s)) | ||
} | ||
} | ||
|
||
func TestExtractURL(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
fixtures := []struct { | ||
s string | ||
out []string | ||
}{ | ||
{ | ||
s: "this is a description with example.com mentioned in the middle", | ||
out: []string{"example.com"}, | ||
}, | ||
{ | ||
s: "this is another example with https://en.wikipedia.org/index.html: and archive.org, and https://eff.org/... and bsky.app.", | ||
out: []string{"https://en.wikipedia.org/index.html", "archive.org", "https://eff.org/", "bsky.app"}, | ||
}, | ||
} | ||
|
||
for _, fix := range fixtures { | ||
assert.Equal(fix.out, ExtractTextURLs(fix.s)) | ||
} | ||
} | ||
|
||
func TestHashOfString(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
// hashing function should be consistent over time | ||
assert.Equal("4e6f69c0e3d10992", HashOfString("dummy-value")) | ||
} | ||
|
||
func TestAccountIsYoungerThan(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
am := automod.AccountMeta{ | ||
Identity: &identity.Identity{ | ||
DID: syntax.DID("did:plc:abc111"), | ||
Handle: syntax.Handle("handle.example.com"), | ||
}, | ||
Profile: automod.ProfileSummary{}, | ||
Private: nil, | ||
} | ||
now := time.Now() | ||
ac := automod.AccountContext{ | ||
Account: am, | ||
} | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
ac.Account.CreatedAt = &now | ||
assert.True(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
yesterday := time.Now().Add(-1 * time.Hour * 24) | ||
ac.Account.CreatedAt = &yesterday | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.True(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
old := time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC) | ||
ac.Account.CreatedAt = &old | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour*24*365*100)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour*24*365*100)) | ||
|
||
future := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC) | ||
ac.Account.CreatedAt = &future | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, time.Hour)) | ||
|
||
ac.Account.CreatedAt = nil | ||
ac.Account.Private = &automod.AccountPrivate{ | ||
Email: "[email protected]", | ||
IndexedAt: &yesterday, | ||
} | ||
assert.True(AccountIsYoungerThan(&ac, 48*time.Hour)) | ||
assert.False(AccountIsYoungerThan(&ac, time.Hour)) | ||
assert.True(AccountIsOlderThan(&ac, time.Hour)) | ||
assert.False(AccountIsOlderThan(&ac, 48*time.Hour)) | ||
} | ||
|
||
func TestParentOrRootIsDid(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
|
Oops, something went wrong.