Skip to content

Commit

Permalink
update testmodels with unknown media type to have unset file details,…
Browse files Browse the repository at this point in the history
… update attachment focus handling converting to frontend, update tests
  • Loading branch information
NyaaaWhatsUpDoc committed Jul 17, 2024
1 parent 8a9de8b commit b5eaf90
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
32 changes: 19 additions & 13 deletions internal/typeutils/internaltofrontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,9 @@ func (c *Converter) AttachmentToAPIAttachment(ctx context.Context, media *gtsmod
api.Type = media.Type.String()
api.ID = media.ID

// Only add file details if stored.
// Only add file details if
// we have stored locally.
if media.File.Path != "" {
api.URL = util.Ptr(media.URL)
api.TextURL = util.Ptr(media.URL)
api.Meta = new(apimodel.MediaMeta)
api.Meta.Original = apimodel.MediaDimensions{
Width: media.FileMeta.Original.Width,
Expand All @@ -583,24 +582,31 @@ func (c *Converter) AttachmentToAPIAttachment(ctx context.Context, media *gtsmod
Bitrate: int(util.PtrOrZero(media.FileMeta.Original.Bitrate)),
}

// Only add thumb details if stored.
// Copy over local file URL.
api.URL = util.Ptr(media.URL)
api.TextURL = util.Ptr(media.URL)

// Set file focus details.
// (this doesn't make much sense if media
// has no image, but the API doesn't yet
// distinguish between zero values vs. none).
api.Meta.Focus = new(apimodel.MediaFocus)
api.Meta.Focus.X = media.FileMeta.Focus.X
api.Meta.Focus.Y = media.FileMeta.Focus.Y

// Only add thumbnail details if
// we have thumbnail stored locally.
if media.Thumbnail.Path != "" {
api.PreviewURL = util.Ptr(media.Thumbnail.URL)
api.Meta.Small = apimodel.MediaDimensions{
Width: media.FileMeta.Small.Width,
Height: media.FileMeta.Small.Height,
Aspect: media.FileMeta.Small.Aspect,
Size: toAPISize(media.FileMeta.Small.Width, media.FileMeta.Small.Height),
}
}
}

// Only add focus details if set.
if media.FileMeta.Focus.X != 0 ||
media.FileMeta.Focus.Y != 0 {
api.Meta.Focus = new(apimodel.MediaFocus)
api.Meta.Focus.X = media.FileMeta.Focus.X
api.Meta.Focus.Y = media.FileMeta.Focus.Y
// Copy over local thumbnail file URL.
api.PreviewURL = util.Ptr(media.Thumbnail.URL)
}
}

// Set remaining API attachment fields.
Expand Down
26 changes: 16 additions & 10 deletions internal/typeutils/internaltofrontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontendUnknownAttachments
"muted": false,
"bookmarked": 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\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",
"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\u003chr\u003e\u003cp\u003e\u003ci lang=\"en\"\u003eℹ️ Note from localhost:8080: 2 attachments in this status were not 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,
"account": {
"id": "01FHMQX3GAABWSM0S2VZEC2SWC",
Expand Down Expand Up @@ -990,30 +990,30 @@ func (suite *InternalToFrontendTestSuite) TestStatusToWebStatus() {
{
"id": "01HE7ZFX9GKA5ZZVD4FACABSS9",
"type": "unknown",
"url": "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7ZFX9GKA5ZZVD4FACABSS9.svg",
"text_url": "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7ZFX9GKA5ZZVD4FACABSS9.svg",
"preview_url": "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/small/01HE7ZFX9GKA5ZZVD4FACABSS9.jpg",
"url": null,
"text_url": null,
"preview_url": null,
"remote_url": "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg",
"preview_remote_url": null,
"meta": null,
"description": "SVG line art of a sloth, public domain",
"blurhash": "L26*j+~qE1RP?wxut7ofRlM{R*of",
"Sensitive": true,
"MIMEType": "image/svg"
"MIMEType": ""
},
{
"id": "01HE88YG74PVAB81PX2XA9F3FG",
"type": "unknown",
"url": "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3",
"text_url": "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3",
"preview_url": "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/small/01HE88YG74PVAB81PX2XA9F3FG.jpg",
"url": null,
"text_url": null,
"preview_url": null,
"remote_url": "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3",
"preview_remote_url": null,
"meta": null,
"description": "Jolly salsa song, public domain.",
"blurhash": null,
"Sensitive": true,
"MIMEType": "audio/mpeg"
"MIMEType": ""
}
],
"LanguageTag": "en",
Expand Down Expand Up @@ -1163,13 +1163,19 @@ func (suite *InternalToFrontendTestSuite) TestVideoAttachmentToFrontend() {
"height": 404,
"frame_rate": "30/1",
"duration": 15.033334,
"bitrate": 1206522
"bitrate": 1206522,
"size": "720x404",
"aspect": 1.7821782
},
"small": {
"width": 720,
"height": 404,
"size": "720x404",
"aspect": 1.7821782
},
"focus": {
"x": 0,
"y": 0
}
},
"description": "A cow adorably licking another cow!",
Expand Down
38 changes: 10 additions & 28 deletions testrig/testmodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,20 +1188,11 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
Description: "SVG line art of a sloth, public domain",
Blurhash: "L26*j+~qE1RP?wxut7ofRlM{R*of",
Processing: 2,
File: gtsmodel.File{
Path: "01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7ZFX9GKA5ZZVD4FACABSS9.svg",
ContentType: "image/svg",
FileSize: 147819,
},
Thumbnail: gtsmodel.Thumbnail{
Path: "01FHMQX3GAABWSM0S2VZEC2SWC/attachment/small/01HE7ZFX9GKA5ZZVD4FACABSS9.jpg",
ContentType: "image/jpeg",
FileSize: 0,
URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/small/01HE7ZFX9GKA5ZZVD4FACABSS9.jpg",
},
Avatar: util.Ptr(false),
Header: util.Ptr(false),
Cached: util.Ptr(false),
File: gtsmodel.File{},
Thumbnail: gtsmodel.Thumbnail{RemoteURL: ""},
Avatar: util.Ptr(false),
Header: util.Ptr(false),
Cached: util.Ptr(false),
},
"remote_account_2_status_1_attachment_3": {
ID: "01HE88YG74PVAB81PX2XA9F3FG",
Expand All @@ -1216,20 +1207,11 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
Description: "Jolly salsa song, public domain.",
Blurhash: "",
Processing: 2,
File: gtsmodel.File{
Path: "01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3",
ContentType: "audio/mpeg",
FileSize: 147819,
},
Thumbnail: gtsmodel.Thumbnail{
Path: "01FHMQX3GAABWSM0S2VZEC2SWC/attachment/small/01HE88YG74PVAB81PX2XA9F3FG.jpg",
ContentType: "image/jpeg",
FileSize: 0,
URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/small/01HE88YG74PVAB81PX2XA9F3FG.jpg",
},
Avatar: util.Ptr(false),
Header: util.Ptr(false),
Cached: util.Ptr(false),
File: gtsmodel.File{},
Thumbnail: gtsmodel.Thumbnail{RemoteURL: ""},
Avatar: util.Ptr(false),
Header: util.Ptr(false),
Cached: util.Ptr(false),
},
}
}
Expand Down

0 comments on commit b5eaf90

Please sign in to comment.