diff options
| author | 2025-11-03 21:16:42 +0100 | |
|---|---|---|
| committer | 2025-11-17 14:12:22 +0100 | |
| commit | 29547ce8aba63454e84d277b0597fb2764e8cb23 (patch) | |
| tree | e80d0d74eea123dbc6dd1dcffadff0cb11f82faa /internal/processing | |
| parent | [bugfix] Fix invalid period parameter when generating the TOTP URL (#4536) (diff) | |
| download | gotosocial-29547ce8aba63454e84d277b0597fb2764e8cb23.tar.xz | |
[bugfix] more RSS validation issues (#4517)
Fixes some validation issues relating to author information often expected to be valid email addresses, which our @displayname@username is not. There's still a few more validation issues, but honestly if we're going have better support I think it might be worth dropping gorilla/feeds for our own tagged XML / JSON structs.
Also does a bunch of housekeeping in the typeutils package removing error returns where never used / only ever logged, removing unused contexts etc.
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4517
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal/processing')
| -rw-r--r-- | internal/processing/account/get.go | 2 | ||||
| -rw-r--r-- | internal/processing/account/interactionpolicies.go | 8 | ||||
| -rw-r--r-- | internal/processing/account/rss.go | 85 | ||||
| -rw-r--r-- | internal/processing/account/rss_test.go | 65 | ||||
| -rw-r--r-- | internal/processing/admin/emoji.go | 7 | ||||
| -rw-r--r-- | internal/processing/media/create.go | 10 | ||||
| -rw-r--r-- | internal/processing/media/getfile.go | 2 | ||||
| -rw-r--r-- | internal/processing/media/getmedia.go | 7 | ||||
| -rw-r--r-- | internal/processing/media/unattach.go | 7 | ||||
| -rw-r--r-- | internal/processing/media/update.go | 7 | ||||
| -rw-r--r-- | internal/processing/search/util.go | 48 | ||||
| -rw-r--r-- | internal/processing/tags/follow.go | 5 | ||||
| -rw-r--r-- | internal/processing/tags/followed.go | 10 | ||||
| -rw-r--r-- | internal/processing/tags/followedtags.go | 18 | ||||
| -rw-r--r-- | internal/processing/tags/get.go | 4 | ||||
| -rw-r--r-- | internal/processing/tags/unfollow.go | 5 |
16 files changed, 99 insertions, 191 deletions
diff --git a/internal/processing/account/get.go b/internal/processing/account/get.go index f7bf84961..7f401ed57 100644 --- a/internal/processing/account/get.go +++ b/internal/processing/account/get.go @@ -112,7 +112,7 @@ func (p *Processor) GetWeb(ctx context.Context, username string) (*apimodel.WebA return nil, gtserror.NewErrorInternalError(err) } - webAccount, err := p.converter.AccountToWebAccount(ctx, targetAccount) + webAccount, err := p.converter.AccountToWebAccount(ctx, targetAccount, nil) if err != nil { err := gtserror.Newf("error converting account: %w", err) return nil, gtserror.NewErrorInternalError(err) diff --git a/internal/processing/account/interactionpolicies.go b/internal/processing/account/interactionpolicies.go index 405c92230..d581112a6 100644 --- a/internal/processing/account/interactionpolicies.go +++ b/internal/processing/account/interactionpolicies.go @@ -89,10 +89,10 @@ func (p *Processor) DefaultInteractionPoliciesGet( } return &apimodel.DefaultPolicies{ - Direct: *directAPI, - Private: *privateAPI, - Unlisted: *unlistedAPI, - Public: *publicAPI, + Direct: directAPI, + Private: privateAPI, + Unlisted: unlistedAPI, + Public: publicAPI, }, nil } diff --git a/internal/processing/account/rss.go b/internal/processing/account/rss.go index 205027528..d6f367566 100644 --- a/internal/processing/account/rss.go +++ b/internal/processing/account/rss.go @@ -76,16 +76,44 @@ func (p *Processor) GetRSSFeedForUsername(ctx context.Context, username string, lastPostAt := account.Stats.LastStatusAt return func() (*feeds.Feed, gtserror.WithCode) { - // Assemble author namestring once only. - author := "@" + account.Username + "@" + config.GetAccountDomain() + var image *feeds.Image + + // Assemble author namestring. + author := "@" + account.Username + + "@" + config.GetAccountDomain() + + // Check if account has an avatar media attachment. + if id := account.AvatarMediaAttachmentID; id != "" { + if account.AvatarMediaAttachment == nil { + var err error + + // Populate the account's avatar media attachment from database by its ID. + account.AvatarMediaAttachment, err = p.state.DB.GetAttachmentByID(ctx, id) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + err := gtserror.Newf("db error getting account avatar: %w", err) + return nil, gtserror.NewErrorInternalError(err) + } + } - // Derive image/thumbnail for this account (may be nil if no media). - image, errWithCode := p.rssImageForAccount(ctx, account, author) - if errWithCode != nil { - return nil, errWithCode + // If avatar is found, use as feed image. + if account.AvatarMediaAttachment != nil { + image = &feeds.Image{ + Title: "Avatar for " + author, + Url: account.AvatarMediaAttachment.Thumbnail.URL, + Link: account.URL, + } + } } + // Start creating feed. feed := &feeds.Feed{ + // we specifcally do not set the author, as a lot + // of feed readers rely on the RSS standard of the + // author being an email with optional name. but + // our @username@domain identifiers break this. + // + // attribution is handled in the title/description. + Title: "Posts from " + author, Description: "Posts from " + author, Link: &feeds.Link{Href: account.URL}, @@ -128,6 +156,17 @@ func (p *Processor) GetRSSFeedForUsername(ctx context.Context, username string, return nil, gtserror.NewErrorInternalError(err) } + // Check for no statuses. + if len(statuses) == 0 { + return feed, nil + } + + // Get next / prev paging parameters. + lo := statuses[len(statuses)-1].ID + hi := statuses[0].ID + next := page.Next(lo, hi) + prev := page.Prev(lo, hi) + // Add each status to the rss feed. for _, status := range statuses { item, err := p.converter.StatusToRSSItem(ctx, status) @@ -138,35 +177,11 @@ func (p *Processor) GetRSSFeedForUsername(ctx context.Context, username string, feed.Add(item) } + // TODO: when we have some manner of supporting + // atom:link in RSS (and Atom), set the paging + // parameters for next / prev feed pages here. + _, _ = next, prev + return feed, nil }, lastPostAt, nil } - -func (p *Processor) rssImageForAccount(ctx context.Context, account *gtsmodel.Account, author string) (*feeds.Image, gtserror.WithCode) { - if account.AvatarMediaAttachmentID == "" { - // No image, no problem! - return nil, nil - } - - // Ensure account avatar attachment populated. - if account.AvatarMediaAttachment == nil { - var err error - account.AvatarMediaAttachment, err = p.state.DB.GetAttachmentByID(ctx, account.AvatarMediaAttachmentID) - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - // No attachment found with this ID (race condition?). - return nil, nil - } - - // Real db error. - err = gtserror.Newf("db error fetching avatar media attachment: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } - } - - return &feeds.Image{ - Url: account.AvatarMediaAttachment.Thumbnail.URL, - Title: "Avatar for " + author, - Link: account.URL, - }, nil -} diff --git a/internal/processing/account/rss_test.go b/internal/processing/account/rss_test.go index b053a3795..75aa20891 100644 --- a/internal/processing/account/rss_test.go +++ b/internal/processing/account/rss_test.go @@ -44,7 +44,6 @@ func (suite *GetRSSTestSuite) TestGetAccountRSSAdmin() { <link>http://localhost:8080/@admin/statuses/01F8MHAAY43M6RJ473VQFCVH37</link> <description>@admin@localhost:8080 made a new post: "🐕🐕🐕🐕🐕"</description> <content:encoded><![CDATA[<p>🐕🐕🐕🐕🐕</p>]]></content:encoded> - <author>@admin@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@admin/statuses/01F8MHAAY43M6RJ473VQFCVH37</guid> <pubDate>Wed, 20 Oct 2021 12:36:45 +0000</pubDate> <source>http://localhost:8080/@admin/feed.rss</source> @@ -54,7 +53,6 @@ func (suite *GetRSSTestSuite) TestGetAccountRSSAdmin() { <link>http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R</link> <description>@admin@localhost:8080 posted 1 attachment: "hello world! #welcome ! first post on the instance :rainbow: !"</description> <content:encoded><![CDATA[<p>hello world! <a href="http://localhost:8080/tags/welcome" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>welcome</span></a> ! first post on the instance <img src="http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png" title=":rainbow:" alt=":rainbow:" width="25" height="25" /> !</p>]]></content:encoded> - <author>@admin@localhost:8080</author> <enclosure url="http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpg" length="62529" type="image/jpeg"></enclosure> <guid isPermaLink="true">http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R</guid> <pubDate>Wed, 20 Oct 2021 11:36:45 +0000</pubDate> @@ -78,11 +76,7 @@ func (suite *GetRSSTestSuite) TestGetAccountAtomAdmin() { <id>http://localhost:8080/@admin/statuses/01F8MHAAY43M6RJ473VQFCVH37</id> <content type="html"><p>🐕🐕🐕🐕🐕</p></content> <link href="http://localhost:8080/@admin/statuses/01F8MHAAY43M6RJ473VQFCVH37" rel="alternate"></link> - <link href="" rel="enclosure"></link> <summary type="html">@admin@localhost:8080 made a new post: "🐕🐕🐕🐕🐕"</summary> - <author> - <name>@admin@localhost:8080</name> - </author> </entry> <entry> <title>hello world! #welcome ! first post on the instance :rainbow: !</title> @@ -92,9 +86,6 @@ func (suite *GetRSSTestSuite) TestGetAccountAtomAdmin() { <link href="http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R" rel="alternate"></link> <link href="http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpg" rel="enclosure" type="image/jpeg" length="62529"></link> <summary type="html">@admin@localhost:8080 posted 1 attachment: "hello world! #welcome ! first post on the instance :rainbow: !"</summary> - <author> - <name>@admin@localhost:8080</name> - </author> </entry> </feed>`) } @@ -114,15 +105,7 @@ func (suite *GetRSSTestSuite) TestGetAccountJSONAdmin() { "title": "open to see some \u003cstrong\u003epuppies\u003c/strong\u003e", "content_html": "\u003cp\u003e🐕🐕🐕🐕🐕\u003c/p\u003e", "summary": "@admin@localhost:8080 made a new post: \"🐕🐕🐕🐕🐕\"", - "date_published": "2021-10-20T12:36:45Z", - "author": { - "name": "@admin@localhost:8080" - }, - "authors": [ - { - "name": "@admin@localhost:8080" - } - ] + "date_published": "2021-10-20T12:36:45Z" }, { "id": "http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R", @@ -132,15 +115,7 @@ func (suite *GetRSSTestSuite) TestGetAccountJSONAdmin() { "content_html": "\u003cp\u003ehello world! \u003ca href=\"http://localhost:8080/tags/welcome\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\"\u003e#\u003cspan\u003ewelcome\u003c/span\u003e\u003c/a\u003e ! first post on the instance \u003cimg src=\"http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png\" title=\":rainbow:\" alt=\":rainbow:\" width=\"25\" height=\"25\" /\u003e !\u003c/p\u003e", "summary": "@admin@localhost:8080 posted 1 attachment: \"hello world! #welcome ! first post on the instance :rainbow: !\"", "image": "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpg", - "date_published": "2021-10-20T11:36:45Z", - "author": { - "name": "@admin@localhost:8080" - }, - "authors": [ - { - "name": "@admin@localhost:8080" - } - ] + "date_published": "2021-10-20T11:36:45Z" } ] }`) @@ -165,7 +140,6 @@ func (suite *GetRSSTestSuite) TestGetAccountRSSZork() { <link>http://localhost:8080/@the_mighty_zork/statuses/01JDPZC707CKDN8N4QVWM4Z1NR</link> <description>@the_mighty_zork@localhost:8080 made a new post: "this is the latest revision of the status, with a content-warning"</description> <content:encoded><![CDATA[<p>this is the latest revision of the status, with a content-warning</p>]]></content:encoded> - <author>@the_mighty_zork@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@the_mighty_zork/statuses/01JDPZC707CKDN8N4QVWM4Z1NR</guid> <pubDate>Fri, 01 Nov 2024 09:00:00 +0000</pubDate> <source>http://localhost:8080/@the_mighty_zork/feed.rss</source> @@ -210,7 +184,6 @@ func (suite *GetRSSTestSuite) TestGetAccountRSSZork() { </div> </section> </code></pre><p>There, hope you liked that!</p>]]></content:encoded> - <author>@the_mighty_zork@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@the_mighty_zork/statuses/01HH9KYNQPA416TNJ53NSATP40</guid> <pubDate>Sun, 10 Dec 2023 09:24:00 +0000</pubDate> <source>http://localhost:8080/@the_mighty_zork/feed.rss</source> @@ -220,7 +193,6 @@ func (suite *GetRSSTestSuite) TestGetAccountRSSZork() { <link>http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY</link> <description>@the_mighty_zork@localhost:8080 made a new post: "hello everyone!"</description> <content:encoded><![CDATA[<p>hello everyone!</p>]]></content:encoded> - <author>@the_mighty_zork@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY</guid> <pubDate>Wed, 20 Oct 2021 10:40:37 +0000</pubDate> <source>http://localhost:8080/@the_mighty_zork/feed.rss</source> @@ -248,7 +220,6 @@ func (suite *GetRSSTestSuite) TestGetAccountAtomZork() { <link>http://localhost:8080/@the_mighty_zork/statuses/01JDPZC707CKDN8N4QVWM4Z1NR</link> <description>@the_mighty_zork@localhost:8080 made a new post: "this is the latest revision of the status, with a content-warning"</description> <content:encoded><![CDATA[<p>this is the latest revision of the status, with a content-warning</p>]]></content:encoded> - <author>@the_mighty_zork@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@the_mighty_zork/statuses/01JDPZC707CKDN8N4QVWM4Z1NR</guid> <pubDate>Fri, 01 Nov 2024 09:00:00 +0000</pubDate> <source>http://localhost:8080/@the_mighty_zork/feed.rss</source> @@ -293,7 +264,6 @@ func (suite *GetRSSTestSuite) TestGetAccountAtomZork() { </div> </section> </code></pre><p>There, hope you liked that!</p>]]></content:encoded> - <author>@the_mighty_zork@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@the_mighty_zork/statuses/01HH9KYNQPA416TNJ53NSATP40</guid> <pubDate>Sun, 10 Dec 2023 09:24:00 +0000</pubDate> <source>http://localhost:8080/@the_mighty_zork/feed.rss</source> @@ -303,7 +273,6 @@ func (suite *GetRSSTestSuite) TestGetAccountAtomZork() { <link>http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY</link> <description>@the_mighty_zork@localhost:8080 made a new post: "hello everyone!"</description> <content:encoded><![CDATA[<p>hello everyone!</p>]]></content:encoded> - <author>@the_mighty_zork@localhost:8080</author> <guid isPermaLink="true">http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY</guid> <pubDate>Wed, 20 Oct 2021 10:40:37 +0000</pubDate> <source>http://localhost:8080/@the_mighty_zork/feed.rss</source> @@ -328,15 +297,7 @@ func (suite *GetRSSTestSuite) TestGetAccountJSONZork() { "content_html": "\u003cp\u003ethis is the latest revision of the status, with a content-warning\u003c/p\u003e", "summary": "@the_mighty_zork@localhost:8080 made a new post: \"this is the latest revision of the status, with a content-warning\"", "date_published": "2024-11-01T09:00:00Z", - "date_modified": "2024-11-01T09:02:00Z", - "author": { - "name": "@the_mighty_zork@localhost:8080" - }, - "authors": [ - { - "name": "@the_mighty_zork@localhost:8080" - } - ] + "date_modified": "2024-11-01T09:02:00Z" }, { "id": "http://localhost:8080/@the_mighty_zork/statuses/01HH9KYNQPA416TNJ53NSATP40", @@ -345,15 +306,7 @@ func (suite *GetRSSTestSuite) TestGetAccountJSONZork() { "title": "HTML in post", "content_html": "\u003cp\u003eHere's a bunch of HTML, read it and weep, weep then!\u003c/p\u003e\u003cpre\u003e\u003ccode class=\"language-html\"\u003e\u0026lt;section class=\u0026#34;about-user\u0026#34;\u0026gt;\n \u0026lt;div class=\u0026#34;col-header\u0026#34;\u0026gt;\n \u0026lt;h2\u0026gt;About\u0026lt;/h2\u0026gt;\n \u0026lt;/div\u0026gt; \n \u0026lt;div class=\u0026#34;fields\u0026#34;\u0026gt;\n \u0026lt;h3 class=\u0026#34;sr-only\u0026#34;\u0026gt;Fields\u0026lt;/h3\u0026gt;\n \u0026lt;dl\u0026gt;\n \u0026lt;div class=\u0026#34;field\u0026#34;\u0026gt;\n \u0026lt;dt\u0026gt;should you follow me?\u0026lt;/dt\u0026gt;\n \u0026lt;dd\u0026gt;maybe!\u0026lt;/dd\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;div class=\u0026#34;field\u0026#34;\u0026gt;\n \u0026lt;dt\u0026gt;age\u0026lt;/dt\u0026gt;\n \u0026lt;dd\u0026gt;120\u0026lt;/dd\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;/dl\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;div class=\u0026#34;bio\u0026#34;\u0026gt;\n \u0026lt;h3 class=\u0026#34;sr-only\u0026#34;\u0026gt;Bio\u0026lt;/h3\u0026gt;\n \u0026lt;p\u0026gt;i post about things that concern me\u0026lt;/p\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;div class=\u0026#34;sr-only\u0026#34; role=\u0026#34;group\u0026#34;\u0026gt;\n \u0026lt;h3 class=\u0026#34;sr-only\u0026#34;\u0026gt;Stats\u0026lt;/h3\u0026gt;\n \u0026lt;span\u0026gt;Joined in Jun, 2022.\u0026lt;/span\u0026gt;\n \u0026lt;span\u0026gt;8 posts.\u0026lt;/span\u0026gt;\n \u0026lt;span\u0026gt;Followed by 1.\u0026lt;/span\u0026gt;\n \u0026lt;span\u0026gt;Following 1.\u0026lt;/span\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;div class=\u0026#34;accountstats\u0026#34; aria-hidden=\u0026#34;true\u0026#34;\u0026gt;\n \u0026lt;b\u0026gt;Joined\u0026lt;/b\u0026gt;\u0026lt;time datetime=\u0026#34;2022-06-04T13:12:00.000Z\u0026#34;\u0026gt;Jun, 2022\u0026lt;/time\u0026gt;\n \u0026lt;b\u0026gt;Posts\u0026lt;/b\u0026gt;\u0026lt;span\u0026gt;8\u0026lt;/span\u0026gt;\n \u0026lt;b\u0026gt;Followed by\u0026lt;/b\u0026gt;\u0026lt;span\u0026gt;1\u0026lt;/span\u0026gt;\n \u0026lt;b\u0026gt;Following\u0026lt;/b\u0026gt;\u0026lt;span\u0026gt;1\u0026lt;/span\u0026gt;\n \u0026lt;/div\u0026gt;\n\u0026lt;/section\u0026gt;\n\u003c/code\u003e\u003c/pre\u003e\u003cp\u003eThere, hope you liked that!\u003c/p\u003e", "summary": "@the_mighty_zork@localhost:8080 made a new post: \"Here's a bunch of HTML, read it and weep, weep then!\n\n`+"```"+`html\n\u003csection class=\"about-user\"\u003e\n \u003cdiv class=\"col-header\"\u003e\n \u003ch2\u003eAbout\u003c/h2\u003e\n \u003c/div\u003e \n \u003cdiv class=\"fields\"\u003e\n \u003ch3 class=\"sr-only\"\u003eFields\u003c/h3\u003e\n \u003cdl\u003e\n...", - "date_published": "2023-12-10T09:24:00Z", - "author": { - "name": "@the_mighty_zork@localhost:8080" - }, - "authors": [ - { - "name": "@the_mighty_zork@localhost:8080" - } - ] + "date_published": "2023-12-10T09:24:00Z" }, { "id": "http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY", @@ -362,15 +315,7 @@ func (suite *GetRSSTestSuite) TestGetAccountJSONZork() { "title": "introduction post", "content_html": "\u003cp\u003ehello everyone!\u003c/p\u003e", "summary": "@the_mighty_zork@localhost:8080 made a new post: \"hello everyone!\"", - "date_published": "2021-10-20T10:40:37Z", - "author": { - "name": "@the_mighty_zork@localhost:8080" - }, - "authors": [ - { - "name": "@the_mighty_zork@localhost:8080" - } - ] + "date_published": "2021-10-20T10:40:37Z" } ] }`) diff --git a/internal/processing/admin/emoji.go b/internal/processing/admin/emoji.go index 5c391bf82..8d568b9a8 100644 --- a/internal/processing/admin/emoji.go +++ b/internal/processing/admin/emoji.go @@ -32,6 +32,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/id" "code.superseriousbusiness.org/gotosocial/internal/media" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" "code.superseriousbusiness.org/gotosocial/internal/util" "codeberg.org/gruf/go-iotools" ) @@ -262,11 +263,7 @@ func (p *Processor) EmojiCategoriesGet( apiCategories := make([]*apimodel.EmojiCategory, 0, len(categories)) for _, category := range categories { - apiCategory, err := p.converter.EmojiCategoryToAPIEmojiCategory(ctx, category) - if err != nil { - err := gtserror.Newf("error converting emoji category to api emoji category: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } + apiCategory := typeutils.EmojiCategoryToAPIEmojiCategory(category) apiCategories = append(apiCategories, apiCategory) } diff --git a/internal/processing/media/create.go b/internal/processing/media/create.go index e925297ff..aaccf4bde 100644 --- a/internal/processing/media/create.go +++ b/internal/processing/media/create.go @@ -29,6 +29,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/media" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" "codeberg.org/gruf/go-iotools" ) @@ -89,11 +90,6 @@ func (p *Processor) Create(ctx context.Context, account *gtsmodel.Account, form return nil, errWithCode } - apiAttachment, err := p.converter.AttachmentToAPIAttachment(ctx, attachment) - if err != nil { - err := fmt.Errorf("error parsing media attachment to frontend type: %s", err) - return nil, gtserror.NewErrorInternalError(err) - } - - return &apiAttachment, nil + a := typeutils.AttachmentToAPIAttachment(attachment) + return &a, nil } diff --git a/internal/processing/media/getfile.go b/internal/processing/media/getfile.go index 79ab08291..3b9b92adc 100644 --- a/internal/processing/media/getfile.go +++ b/internal/processing/media/getfile.go @@ -247,7 +247,7 @@ func (p *Processor) getEmojiContent( emoji, err = p.federator.RecacheEmoji( ctx, emoji, - false, + false, // async ) if err != nil { err := gtserror.Newf("error recaching emoji: %w", err) diff --git a/internal/processing/media/getmedia.go b/internal/processing/media/getmedia.go index 5144bbd8e..22e05cab3 100644 --- a/internal/processing/media/getmedia.go +++ b/internal/processing/media/getmedia.go @@ -26,6 +26,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/db" "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" ) func (p *Processor) Get(ctx context.Context, account *gtsmodel.Account, mediaAttachmentID string) (*apimodel.Attachment, gtserror.WithCode) { @@ -42,10 +43,6 @@ func (p *Processor) Get(ctx context.Context, account *gtsmodel.Account, mediaAtt return nil, gtserror.NewErrorNotFound(errors.New("attachment not owned by requesting account")) } - a, err := p.converter.AttachmentToAPIAttachment(ctx, attachment) - if err != nil { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("error converting attachment: %s", err)) - } - + a := typeutils.AttachmentToAPIAttachment(attachment) return &a, nil } diff --git a/internal/processing/media/unattach.go b/internal/processing/media/unattach.go index 8eec907fd..55d793647 100644 --- a/internal/processing/media/unattach.go +++ b/internal/processing/media/unattach.go @@ -26,6 +26,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/db" "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" ) // Unattach unattaches the media attachment with the given ID from any statuses it was attached to, making it available @@ -49,10 +50,6 @@ func (p *Processor) Unattach(ctx context.Context, account *gtsmodel.Account, med return nil, gtserror.NewErrorNotFound(fmt.Errorf("db error updating attachment: %s", err)) } - a, err := p.converter.AttachmentToAPIAttachment(ctx, attachment) - if err != nil { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("error converting attachment: %s", err)) - } - + a := typeutils.AttachmentToAPIAttachment(attachment) return &a, nil } diff --git a/internal/processing/media/update.go b/internal/processing/media/update.go index 3acf238b0..cccc27534 100644 --- a/internal/processing/media/update.go +++ b/internal/processing/media/update.go @@ -29,6 +29,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/text" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" ) // Update updates a media attachment with the given id, using the provided form parameters. @@ -77,11 +78,7 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, media return nil, gtserror.NewErrorInternalError(fmt.Errorf("database error updating media: %s", err)) } - a, err := p.converter.AttachmentToAPIAttachment(ctx, attachment) - if err != nil { - return nil, gtserror.NewErrorNotFound(fmt.Errorf("error converting attachment: %s", err)) - } - + a := typeutils.AttachmentToAPIAttachment(attachment) return &a, nil } diff --git a/internal/processing/search/util.go b/internal/processing/search/util.go index 441f3f946..7d52204c3 100644 --- a/internal/processing/search/util.go +++ b/internal/processing/search/util.go @@ -24,6 +24,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/log" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" ) // return true if given queryType should include accounts. @@ -128,42 +129,22 @@ func (p *Processor) packageStatuses( // packageHashtags is a util function that just // converts the given hashtags into an apimodel // hashtag slice, or errors appropriately. -func (p *Processor) packageHashtags( - ctx context.Context, - requestingAccount *gtsmodel.Account, - tags []*gtsmodel.Tag, - v1 bool, -) ([]any, gtserror.WithCode) { - apiTags := make([]any, 0, len(tags)) - - var rangeF func(*gtsmodel.Tag) +func packageHashtags(tags []*gtsmodel.Tag, v1 bool) []any { + apiTags := make([]any, len(tags)) + if len(apiTags) != len(tags) { + panic(gtserror.New("bound check elimination")) + } if v1 { - // If API version 1, just provide slice of tag names. - rangeF = func(tag *gtsmodel.Tag) { - apiTags = append(apiTags, tag.Name) + for i, tag := range tags { + apiTags[i] = tag.Name } } else { - // If API not version 1, provide slice of full tags. - rangeF = func(tag *gtsmodel.Tag) { - apiTag, err := p.converter.TagToAPITag(ctx, tag, true, nil) - if err != nil { - log.Debugf( - ctx, - "skipping tag %s because it couldn't be converted to its api representation: %s", - tag.Name, err, - ) - return - } - - apiTags = append(apiTags, &apiTag) + for i, tag := range tags { + apiTag := typeutils.TagToAPITag(tag, true, nil) + apiTags[i] = apiTag } } - - for _, tag := range tags { - rangeF(tag) - } - - return apiTags, nil + return apiTags } // packageSearchResult wraps up the given accounts @@ -197,10 +178,7 @@ func (p *Processor) packageSearchResult( return nil, errWithCode } - apiTags, errWithCode := p.packageHashtags(ctx, requestingAccount, tags, v1) - if errWithCode != nil { - return nil, errWithCode - } + apiTags := packageHashtags(tags, v1) return &apimodel.SearchResult{ Accounts: apiAccounts, diff --git a/internal/processing/tags/follow.go b/internal/processing/tags/follow.go index b0ee0a995..879a1d9e8 100644 --- a/internal/processing/tags/follow.go +++ b/internal/processing/tags/follow.go @@ -26,6 +26,8 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/id" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" + "code.superseriousbusiness.org/gotosocial/internal/util" ) // Follow follows the tag with the given name as the given account. @@ -63,5 +65,6 @@ func (p *Processor) Follow( ) } - return p.apiTag(ctx, tag, true) + apiTag := typeutils.TagToAPITag(tag, true, util.Ptr(true)) + return &apiTag, nil } diff --git a/internal/processing/tags/followed.go b/internal/processing/tags/followed.go index eaecc0983..958960623 100644 --- a/internal/processing/tags/followed.go +++ b/internal/processing/tags/followed.go @@ -24,8 +24,8 @@ import ( apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" "code.superseriousbusiness.org/gotosocial/internal/db" "code.superseriousbusiness.org/gotosocial/internal/gtserror" - "code.superseriousbusiness.org/gotosocial/internal/log" "code.superseriousbusiness.org/gotosocial/internal/paging" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" "code.superseriousbusiness.org/gotosocial/internal/util" ) @@ -53,14 +53,10 @@ func (p *Processor) Followed( lo := tags[count-1].ID hi := tags[0].ID - items := make([]interface{}, 0, count) following := util.Ptr(true) + items := make([]interface{}, 0, count) for _, tag := range tags { - apiTag, err := p.converter.TagToAPITag(ctx, tag, true, following) - if err != nil { - log.Errorf(ctx, "error converting tag %s to API representation: %v", tag.ID, err) - continue - } + apiTag := typeutils.TagToAPITag(tag, true, following) items = append(items, apiTag) } diff --git a/internal/processing/tags/followedtags.go b/internal/processing/tags/followedtags.go index 1619e433a..c78e0cc23 100644 --- a/internal/processing/tags/followedtags.go +++ b/internal/processing/tags/followedtags.go @@ -18,11 +18,6 @@ package tags import ( - "context" - - apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" - "code.superseriousbusiness.org/gotosocial/internal/gtserror" - "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/state" "code.superseriousbusiness.org/gotosocial/internal/typeutils" ) @@ -38,16 +33,3 @@ func New(state *state.State, converter *typeutils.Converter) Processor { converter: converter, } } - -// apiTag is a shortcut to return the API version of the given tag, -// or return an appropriate error if conversion fails. -func (p *Processor) apiTag(ctx context.Context, tag *gtsmodel.Tag, following bool) (*apimodel.Tag, gtserror.WithCode) { - apiTag, err := p.converter.TagToAPITag(ctx, tag, true, &following) - if err != nil { - return nil, gtserror.NewErrorInternalError( - gtserror.Newf("error converting tag %s to API representation: %w", tag.Name, err), - ) - } - - return &apiTag, nil -} diff --git a/internal/processing/tags/get.go b/internal/processing/tags/get.go index c7e343150..6c515ee1a 100644 --- a/internal/processing/tags/get.go +++ b/internal/processing/tags/get.go @@ -25,6 +25,7 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/db" "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" ) // Get gets the tag with the given name, including whether it's followed by the given account. @@ -53,5 +54,6 @@ func (p *Processor) Get( ) } - return p.apiTag(ctx, tag, following) + apiTag := typeutils.TagToAPITag(tag, true, &following) + return &apiTag, nil } diff --git a/internal/processing/tags/unfollow.go b/internal/processing/tags/unfollow.go index 8f303466c..3d15d68c2 100644 --- a/internal/processing/tags/unfollow.go +++ b/internal/processing/tags/unfollow.go @@ -25,6 +25,8 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/db" "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" + "code.superseriousbusiness.org/gotosocial/internal/typeutils" + "code.superseriousbusiness.org/gotosocial/internal/util" ) // Unfollow unfollows the tag with the given name as the given account. @@ -54,5 +56,6 @@ func (p *Processor) Unfollow( ) } - return p.apiTag(ctx, tag, false) + apiTag := typeutils.TagToAPITag(tag, true, util.Ptr(false)) + return &apiTag, nil } |
