From ba9d6b467a1f03447789844048d913738c843569 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Fri, 10 Nov 2023 19:29:26 +0100 Subject: [feature] Media attachment placeholders (#2331) * [feature] Use placeholders for unknown media types * fix read of underreported small files * switch to reduce nesting * simplify cleanup --- internal/typeutils/util.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'internal/typeutils/util.go') diff --git a/internal/typeutils/util.go b/internal/typeutils/util.go index a99d9e7ae..a19588221 100644 --- a/internal/typeutils/util.go +++ b/internal/typeutils/util.go @@ -22,10 +22,17 @@ import ( "errors" "fmt" "net/url" + "path" + "slices" + "strconv" + "strings" "github.com/superseriousbusiness/gotosocial/internal/ap" + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/regexes" + "github.com/superseriousbusiness/gotosocial/internal/text" ) type statusInteractions struct { @@ -100,3 +107,80 @@ func getURI(withID ap.WithJSONLDId) (*url.URL, string, error) { id := idProp.Get() return id, id.String(), nil } + +// placeholdUnknownAttachments separates any attachments with type `unknown` +// out of the given slice, and returns an `
+// +func placeholdUnknownAttachments(arr []apimodel.Attachment) (string, []apimodel.Attachment) { + // Extract unknown-type attachments into a separate + // slice, deleting them from arr in the process. + var unknowns []apimodel.Attachment + arr = slices.DeleteFunc(arr, func(elem apimodel.Attachment) bool { + unknown := elem.Type == "unknown" + if unknown { + // Set aside unknown-type attachment. + unknowns = append(unknowns, elem) + } + + return unknown + }) + + unknownsLen := len(unknowns) + if unknownsLen == 0 { + // No unknown attachments, + // nothing to do. + return "", arr + } + + // Plural / singular. + var ( + attachments string + links string + ) + + if unknownsLen == 1 { + attachments = "1 attachment" + links = "link" + } else { + attachments = strconv.Itoa(unknownsLen) + " attachments" + links = "links" + } + + var aside strings.Builder + aside.WriteString(``) + aside.WriteString(`Note from ` + config.GetHost() + `: ` + attachments + ` in this status could not be downloaded. Treat the following external ` + links + ` with care:`) + aside.WriteString(`