Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc. fixes #852

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions api/plc.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ func (s *PLCServer) GetDocument(ctx context.Context, didstr string) (*did.Docume
return &doc, nil
}

func (s *PLCServer) FlushCacheFor(did string) {
return
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant return.

}
func (s *PLCServer) FlushCacheFor(did string) {}

type CreateOp struct {
Type string `json:"type" cborgen:"type"`
Expand Down
8 changes: 2 additions & 6 deletions atproto/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,12 @@ func extractBlobsAtom(atom any) []Blob {
case []any:
for _, el := range v {
down := extractBlobsAtom(el)
for _, d := range down {
out = append(out, d)
}
out = append(out, down...)
}
case map[string]any:
for _, val := range v {
down := extractBlobsAtom(val)
for _, d := range down {
out = append(out, d)
}
out = append(out, down...)
}
default:
}
Expand Down
2 changes: 1 addition & 1 deletion atproto/identity/mock_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func TestMockDirectory(t *testing.T) {

_, err = c.LookupHandle(ctx, syntax.HandleInvalid)
assert.ErrorIs(err, ErrHandleNotFound)
out, err = c.LookupDID(ctx, syntax.DID("did:plc:abc999"))
_, err = c.LookupDID(ctx, syntax.DID("did:plc:abc999"))
assert.ErrorIs(err, ErrDIDNotFound)
}
26 changes: 16 additions & 10 deletions atproto/lexicon/cmd/lextool/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package main
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/bluesky-social/indigo/atproto/data"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/lexicon"
"github.com/bluesky-social/indigo/atproto/syntax"

"github.com/bobg/errors"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A humble suggestion. Please see https://github.com/bobg/errors#readme

"github.com/urfave/cli/v2"
"io"
"log/slog"
"net/http"
)

func runValidateRecord(cctx *cli.Context) error {
Expand All @@ -29,12 +28,12 @@ func runValidateRecord(cctx *cli.Context) error {
cat := lexicon.NewBaseCatalog()
err := cat.LoadDirectory(p)
if err != nil {
return err
return errors.Wrap(err, "in LoadDirectory")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially when a function has multiple error return sites, it's useful to wrap them to know which site did the returning.

}

aturi, err := syntax.ParseATURI(args[1])
if err != nil {
return err
return errors.Wrap(err, "in ParseATURI")
}
if aturi.RecordKey() == "" {
return fmt.Errorf("need a full, not partial, AT-URI: %s", aturi)
Expand All @@ -54,17 +53,24 @@ func runValidateRecord(cctx *cli.Context) error {
pdsURL, ident.DID, aturi.Collection(), aturi.RecordKey())
resp, err := http.Get(url)
if err != nil {
return err
return errors.Wrap(err, "in http.Get")
}
defer resp.Body.Close()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required after any http.Get (and a few other operations).


if resp.StatusCode != http.StatusOK {
return fmt.Errorf("fetch failed")
}

respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
return errors.Wrap(err, "in ReadAll")
}

body, err := data.UnmarshalJSON(respBytes)
if err != nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error was not being checked.

return errors.Wrap(err, "in UnmarshalJSON")
}

record, ok := body["value"].(map[string]any)
if !ok {
return fmt.Errorf("fetched record was not an object")
Expand All @@ -73,7 +79,7 @@ func runValidateRecord(cctx *cli.Context) error {
slog.Info("validating", "did", ident.DID.String(), "collection", aturi.Collection().String(), "rkey", aturi.RecordKey().String())
err = lexicon.ValidateRecord(&cat, record, aturi.Collection().String(), lexicon.LenientMode)
if err != nil {
return err
return errors.Wrap(err, "in ValidateRecord")
}
fmt.Println("success!")
return nil
Expand Down
1 change: 0 additions & 1 deletion atproto/lexicon/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func (s *SchemaDef) SetBase(base string) {
}
s.Inner = v
}
return
}

func (s SchemaDef) MarshalJSON() ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion atproto/lexicon/lexicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func validateArray(cat Catalog, s SchemaArray, arr []any, flags ValidateFlags) e
}

func validateUnion(cat Catalog, s SchemaUnion, d any, flags ValidateFlags) error {
closed := s.Closed != nil && *s.Closed == true
closed := s.Closed != nil && *s.Closed

obj, ok := d.(map[string]any)
if !ok {
Expand Down
1 change: 0 additions & 1 deletion automod/consumer/firehose.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func (fc *FirehoseConsumer) HandleRepoCommit(ctx context.Context, evt *comatprot
action = automod.UpdateOp
default:
logger.Error("impossible event kind", "kind", ek)
break
}
recCID := syntax.CID(op.Cid.String())
op := automod.RecordOp{
Expand Down
6 changes: 3 additions & 3 deletions automod/engine/fetch_account_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (e *Engine) GetAccountMeta(ctx context.Context, ident *identity.Identity) (
var labels []string
var negLabels []string
for _, lbl := range pv.Labels {
if lbl.Neg != nil && *lbl.Neg == true {
if lbl.Neg != nil && *lbl.Neg {
negLabels = append(negLabels, lbl.Val)
} else {
labels = append(labels, lbl.Val)
Expand Down Expand Up @@ -130,10 +130,10 @@ func (e *Engine) GetAccountMeta(ctx context.Context, ident *identity.Identity) (
am.Deactivated = true
}
if rd.Moderation != nil && rd.Moderation.SubjectStatus != nil {
if rd.Moderation.SubjectStatus.Takendown != nil && *rd.Moderation.SubjectStatus.Takendown == true {
if rd.Moderation.SubjectStatus.Takendown != nil && *rd.Moderation.SubjectStatus.Takendown {
am.Takendown = true
}
if rd.Moderation.SubjectStatus.Appealed != nil && *rd.Moderation.SubjectStatus.Appealed == true {
if rd.Moderation.SubjectStatus.Appealed != nil && *rd.Moderation.SubjectStatus.Appealed {
ap.Appealed = true
}
ap.AccountTags = dedupeStrings(rd.Moderation.SubjectStatus.Tags)
Expand Down
2 changes: 1 addition & 1 deletion automod/engine/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (eng *Engine) persistRecordModActions(c *RecordContext) error {
var existingLabels []string
var negLabels []string
for _, lbl := range rv.Labels {
if lbl.Neg != nil && *lbl.Neg == true {
if lbl.Neg != nil && *lbl.Neg {
negLabels = append(negLabels, lbl.Val)
} else {
existingLabels = append(existingLabels, lbl.Val)
Expand Down
2 changes: 1 addition & 1 deletion automod/engine/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func slackBody(header string, acct AccountMeta, newLabels, newFlags []string, ne
msg += fmt.Sprintf("Report `%s`: %s\n", rep.ReasonType, rep.Comment)
}
if newTakedown {
msg += fmt.Sprintf("Takedown!\n")
msg += "Takedown!\n"
}
return msg
}
13 changes: 4 additions & 9 deletions automod/flagstore/flagstore_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ func (s MemFlagStore) Get(ctx context.Context, key string) ([]string, error) {
}

func (s MemFlagStore) Add(ctx context.Context, key string, flags []string) error {
v, ok := s.Data[key]
if !ok {
v = []string{}
}
for _, f := range flags {
v = append(v, f)
}
v, _ := s.Data[key]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If key is not in s.Data, then v will be []string(nil), which is suitable for using directly in the append that follows. All the other stuff is extraneous.

v = append(v, flags...)
v = dedupeStrings(v)
s.Data[key] = v
return nil
Expand All @@ -51,8 +46,8 @@ func (s MemFlagStore) Remove(ctx context.Context, key string, flags []string) er
for _, f := range flags {
delete(m, f)
}
out := []string{}
for f, _ := range m {
var out []string
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is almost always the better way to initialize a slice. This sets it to nil - with no "slice header" - which is exactly equivalent to []string{} (which does have a slice header) in almost all contexts, such as len and append and for range, etc.

for f := range m {
out = append(out, f)
}
s.Data[key] = out
Expand Down
10 changes: 2 additions & 8 deletions automod/helpers/bsky.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
)

func ExtractHashtagsPost(post *appbsky.FeedPost) []string {
var tags []string
for _, tag := range post.Tags {
tags = append(tags, tag)
}
tags := append([]string{}, post.Tags...)
for _, facet := range post.Facets {
for _, feat := range facet.Features {
if feat.RichtextFacet_Tag != nil {
Expand Down Expand Up @@ -205,10 +202,7 @@ func ParentOrRootIsFollower(c *automod.RecordContext, post *appbsky.FeedPost) bo
}

rel = c.GetAccountRelationship(rootDID)
if rel.FollowedBy {
return true
}
return false
return rel.FollowedBy
}

func PostParentOrRootIsDid(post *appbsky.FeedPost, did string) bool {
Expand Down
1 change: 0 additions & 1 deletion automod/keyword/tokenize.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

var (
puncChars = regexp.MustCompile(`[[:punct:]]+`)
nonTokenChars = regexp.MustCompile(`[^\pL\pN\s]+`)
nonTokenCharsSkipCensorChars = regexp.MustCompile(`[^\pL\pN\s#*_-]`)
)
Expand Down
2 changes: 1 addition & 1 deletion automod/rules/harassment.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func HarassmentTrivialPostRule(c *automod.RecordContext, post *appbsky.FeedPost)

if count > 5 {
//c.AddRecordFlag("trivial-harassing-post")
c.ReportAccount(automod.ReportReasonOther, fmt.Sprintf("possible targetted harassment (also labeled; remove label if this isn't harassment!)"))
c.ReportAccount(automod.ReportReasonOther, "possible targetted harassment (also labeled; remove label if this isn't harassment!)")
c.AddAccountLabel("!hide")
c.Notify("slack")
}
Expand Down
3 changes: 1 addition & 2 deletions automod/rules/nostr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rules

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -37,7 +36,7 @@ func NostrSpamPostRule(c *automod.RecordContext, post *appbsky.FeedPost) error {
return nil
}

c.ReportAccount(automod.ReportReasonOther, fmt.Sprintf("likely nostr spam account (also labeled; remove label if this isn't spam!)"))
c.ReportAccount(automod.ReportReasonOther, "likely nostr spam account (also labeled; remove label if this isn't spam!)")
c.AddAccountLabel("!hide")
c.Notify("slack")
return nil
Expand Down
3 changes: 1 addition & 2 deletions automod/rules/promo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rules

import (
"fmt"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -54,7 +53,7 @@ func AggressivePromotionRule(c *automod.RecordContext, post *appbsky.FeedPost) e
uniqueReplies := c.GetCountDistinct("reply-to", did, countstore.PeriodDay)
if uniqueReplies >= 10 {
c.AddAccountFlag("promo-multi-reply")
c.ReportAccount(automod.ReportReasonSpam, fmt.Sprintf("possible aggressive self-promotion"))
c.ReportAccount(automod.ReportReasonSpam, "possible aggressive self-promotion")
c.Notify("slack")
}

Expand Down
4 changes: 2 additions & 2 deletions automod/rules/quick.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func BotLinkProfileRule(c *automod.RecordContext, profile *appbsky.ActorProfile)
}
if strings.Contains(*profile.Description, "🏈🍕🌀") {
c.AddAccountFlag("profile-bot-string")
c.ReportAccount(automod.ReportReasonSpam, fmt.Sprintf("possible bot based on string in profile"))
c.ReportAccount(automod.ReportReasonSpam, "possible bot based on string in profile")
c.Notify("slack")
return nil
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func TrivialSpamPostRule(c *automod.RecordContext, post *appbsky.FeedPost) error
return nil
}

c.ReportAccount(automod.ReportReasonOther, fmt.Sprintf("trivial spam account (also labeled; remove label if this isn't spam!)"))
c.ReportAccount(automod.ReportReasonOther, "trivial spam account (also labeled; remove label if this isn't spam!)")
c.AddAccountLabel("!hide")
c.Notify("slack")
return nil
Expand Down
Loading