~mariusor/fedbox-spammy

6cd6cde79b003e4c27807b63cbbbc15ad3353979 — Marius Orcsik 5 months ago 886059a
Improved name generation for objects
2 files changed, 38 insertions(+), 17 deletions(-)

M go.mod
M spammer.go
M go.mod => go.mod +1 -0
@@ 19,6 19,7 @@ require (
	github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 // indirect
	github.com/go-chi/chi/v5 v5.0.12 // indirect
	github.com/golang/protobuf v1.5.4 // indirect
	github.com/grokify/html-strip-tags-go v0.1.0 // indirect
	github.com/mattn/go-colorable v0.1.13 // indirect
	github.com/mattn/go-isatty v0.0.20 // indirect
	github.com/rs/xid v1.5.0 // indirect

M spammer.go => spammer.go +37 -17
@@ 16,6 16,7 @@ import (
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"


@@ 24,6 25,7 @@ import (
	ap "github.com/go-ap/activitypub"
	"github.com/go-ap/client"
	"github.com/go-ap/errors"
	strip "github.com/grokify/html-strip-tags-go"
	"github.com/mariusor/spammy/indieauth"
	"github.com/mariusor/spammy/internal/names"
	"golang.org/x/oauth2"


@@ 218,14 220,13 @@ func RandomObject(parent ap.Item) ap.Item {
		buf := make([]byte, base64.RawStdEncoding.EncodedLen(len(data)))
		base64.RawStdEncoding.Encode(buf, data)
		data = buf
		docTyp, ttyp := filepath.Split(string(mime))
		ob.Name = ap.NaturalLanguageValues{
			{ap.NilLangRef, []byte(fmt.Sprintf("random %s", mime))},
			{ap.NilLangRef, []byte(fmt.Sprintf("Random %s %s", strings.Trim(docTyp, "/"), strings.Trim(ttyp, "/")))},
		}
	} else {
		ob.Name = getNameFromData(data, 5)
		ob.Summary = ap.NaturalLanguageValues{
			{ap.NilLangRef, data[:bytes.Index(data, []byte{'.'})]},
		}
		ob.Name = getNameFromData(data, mime, 5)
		ob.Summary = getSummaryFromData(data, mime)
	}
	ob.Content = ap.NaturalLanguageValues{
		{ap.NilLangRef, data},


@@ 234,20 235,39 @@ func RandomObject(parent ap.Item) ap.Item {
	return ob
}

func getNameFromData(data []byte, wc int) ap.NaturalLanguageValues {
	wordCnt := int(math.Min(float64(bytes.Count(data, []byte(" "))), float64(wc)))
	w := 0
	idx := bytes.IndexFunc(data, func(r rune) bool {
		if r == ' ' && w == wordCnt {
			return true
func getSummaryFromData(data []byte, mimeType ap.MimeType) ap.NaturalLanguageValues {
	if strings.Contains(string(mimeType), "html") {
		data = []byte(strip.StripTags(string(data)))
	}
	return ap.NaturalLanguageValues{
		{ap.NilLangRef, data[:bytes.Index(data, []byte{'.'})]},
	}
}

func getNameFromData(data []byte, mimeType ap.MimeType, wc int) ap.NaturalLanguageValues {
	mime := string(mimeType)
	name := []byte(fmt.Sprintf("Unknown %q item", mime))
	if !strings.Contains(mime, "text") {
		docTyp, typ := filepath.Split(mime)
		name = []byte(fmt.Sprintf("Random %s %s", strings.Trim(typ, "/"), strings.Trim(docTyp, "/")))
	} else {
		if strings.Contains(mime, "html") {
			data = []byte(strip.StripTags(string(data)))
		}
		w++
		return false
	})
	if idx <= 0 {
		return nil
		wordCnt := int(math.Min(float64(bytes.Count(data, []byte(" "))), float64(wc)))
		w := 0
		idx := bytes.IndexFunc(data, func(r rune) bool {
			if r == ' ' && w == wordCnt {
				return true
			}
			w++
			return false
		})
		if idx <= 0 {
			return nil
		}
		name = data[:idx]
	}
	name := data[:idx]
	return ap.NaturalLanguageValues{
		{ap.NilLangRef, name},
	}