@@ 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},
}