[bugfix] Outdent placeholder from <aside> when returning unknown media attachments (#2485)

* [bugfix] Outdent placeholder from `<aside>`

* visually separate placeholder note from body

* fix test
This commit is contained in:
tobi 2024-01-09 10:41:32 +01:00 committed by GitHub
parent aad3384c98
commit 7cce1a7cc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 22 deletions

View file

@ -466,7 +466,7 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontendUnknownAttachments
"muted": false, "muted": false,
"bookmarked": false, "bookmarked": false,
"pinned": false, "pinned": false,
"content": "\u003cp\u003ehi \u003cspan class=\"h-card\"\u003e\u003ca href=\"http://localhost:8080/@admin\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\"\u003e@\u003cspan\u003eadmin\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e here's some media for ya\u003c/p\u003e\u003caside\u003e\u003cp\u003eNote from localhost:8080: 2 attachments in this status could not be downloaded. Treat the following external links with care:\u003cul\u003e\u003cli\u003e\u003ca href=\"http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg\" rel=\"nofollow noreferrer noopener\" target=\"_blank\"\u003e01HE7ZGJYTSYMXF927GF9353KR.svg\u003c/a\u003e [SVG line art of a sloth, public domain]\u003c/li\u003e\u003cli\u003e\u003ca href=\"http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3\" rel=\"nofollow noreferrer noopener\" target=\"_blank\"\u003e01HE892Y8ZS68TQCNPX7J888P3.mp3\u003c/a\u003e [Jolly salsa song, public domain.]\u003c/li\u003e\u003c/ul\u003e\u003c/p\u003e\u003c/aside\u003e", "content": "\u003cp\u003ehi \u003cspan class=\"h-card\"\u003e\u003ca href=\"http://localhost:8080/@admin\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\"\u003e@\u003cspan\u003eadmin\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e here's some media for ya\u003c/p\u003e\u003chr\u003e\u003cp\u003e\u003ci lang=\"en\"\u003e Note from localhost:8080: 2 attachments in this status could not be downloaded. Treat the following external links with care:\u003c/i\u003e\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003ca href=\"http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg\" rel=\"nofollow noreferrer noopener\" target=\"_blank\"\u003e01HE7ZGJYTSYMXF927GF9353KR.svg\u003c/a\u003e [SVG line art of a sloth, public domain]\u003c/li\u003e\u003cli\u003e\u003ca href=\"http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3\" rel=\"nofollow noreferrer noopener\" target=\"_blank\"\u003e01HE892Y8ZS68TQCNPX7J888P3.mp3\u003c/a\u003e [Jolly salsa song, public domain.]\u003c/li\u003e\u003c/ul\u003e",
"reblog": null, "reblog": null,
"account": { "account": {
"id": "01FHMQX3GAABWSM0S2VZEC2SWC", "id": "01FHMQX3GAABWSM0S2VZEC2SWC",

View file

@ -93,24 +93,22 @@ func misskeyReportInlineURLs(content string) []*url.URL {
} }
// placeholdUnknownAttachments separates any attachments with type `unknown` // placeholdUnknownAttachments separates any attachments with type `unknown`
// out of the given slice, and returns an `<aside>` tag containing links to // out of the given slice, and returns a piece of text containing links to
// those attachments, as well as the slice of remaining "known" attachments. // those attachments, as well as the slice of remaining "known" attachments.
// If there are no unknown-type attachments in the provided slice, an empty // If there are no unknown-type attachments in the provided slice, an empty
// string and the original slice will be returned. // string and the original slice will be returned.
// //
// If an aside is created, it will be run through the sanitizer before being // Returned text will be run through the sanitizer before being returned, to
// returned, to ensure that malicious links don't cause issues. // ensure that malicious links don't cause issues.
// //
// Example: // Example:
// //
// <aside> // <hr>
// <p>Note from your.instance.com: 2 attachments in this status could not be downloaded. Treat the following external links with care: // <p><i lang="en"> Note from your.instance.com: 2 attachments in this status could not be downloaded. Treat the following external links with care:</i></p>
// <ul> // <ul>
// <li><a href="http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg" rel="nofollow noreferrer noopener" target="_blank">01HE7ZGJYTSYMXF927GF9353KR.svg</a> [SVG line art of a sloth, public domain]</li> // <li><a href="http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg" rel="nofollow noreferrer noopener" target="_blank">01HE7ZGJYTSYMXF927GF9353KR.svg</a> [SVG line art of a sloth, public domain]</li>
// <li><a href="http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3" rel="nofollow noreferrer noopener" target="_blank">01HE892Y8ZS68TQCNPX7J888P3.mp3</a> [Jolly salsa song, public domain.]</li> // <li><a href="http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3" rel="nofollow noreferrer noopener" target="_blank">01HE892Y8ZS68TQCNPX7J888P3.mp3</a> [Jolly salsa song, public domain.]</li>
// </ul> // </ul>
// </p>
// </aside>
func placeholdUnknownAttachments(arr []*apimodel.Attachment) (string, []*apimodel.Attachment) { func placeholdUnknownAttachments(arr []*apimodel.Attachment) (string, []*apimodel.Attachment) {
// Extract unknown-type attachments into a separate // Extract unknown-type attachments into a separate
// slice, deleting them from arr in the process. // slice, deleting them from arr in the process.
@ -146,11 +144,12 @@ func placeholdUnknownAttachments(arr []*apimodel.Attachment) (string, []*apimode
links = "links" links = "links"
} }
var aside strings.Builder var note strings.Builder
aside.WriteString(`<aside>`) note.WriteString(`<hr>`)
aside.WriteString(`<p>`) note.WriteString(`<p><i lang="en">`)
aside.WriteString(`Note from ` + config.GetHost() + `: ` + attachments + ` in this status could not be downloaded. Treat the following external ` + links + ` with care:`) note.WriteString(` Note from ` + config.GetHost() + `: ` + attachments + ` in this status could not be downloaded. Treat the following external ` + links + ` with care:`)
aside.WriteString(`<ul>`) note.WriteString(`</i></p>`)
note.WriteString(`<ul>`)
for _, a := range unknowns { for _, a := range unknowns {
var ( var (
remoteURL = *a.RemoteURL remoteURL = *a.RemoteURL
@ -160,13 +159,11 @@ func placeholdUnknownAttachments(arr []*apimodel.Attachment) (string, []*apimode
if d := a.Description; d != nil && *d != "" { if d := a.Description; d != nil && *d != "" {
entry += ` [` + *d + `]` entry += ` [` + *d + `]`
} }
aside.WriteString(`<li>` + entry + `</li>`) note.WriteString(`<li>` + entry + `</li>`)
} }
aside.WriteString(`</ul>`) note.WriteString(`</ul>`)
aside.WriteString(`</p>`)
aside.WriteString(`</aside>`)
return text.SanitizeToHTML(aside.String()), arr return text.SanitizeToHTML(note.String()), arr
} }
// ContentToContentLanguage tries to // ContentToContentLanguage tries to