Skip to content

Commit

Permalink
[bugfix] Use null for empty api status language (#1268)
Browse files Browse the repository at this point in the history
* [bugfix] Use null for empty api status `language`

* update swagger docs
  • Loading branch information
tsmethurst authored Dec 15, 2022
1 parent bae7398 commit 1d24c1c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
8 changes: 6 additions & 2 deletions docs/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,9 @@ definitions:
type: string
x-go-name: InReplyToID
language:
description: Primary language of this status (ISO 639 Part 1 two-letter language code).
description: |-
Primary language of this status (ISO 639 Part 1 two-letter language code).
Will be null if language is not known.
example: en
type: string
x-go-name: Language
Expand Down Expand Up @@ -1802,7 +1804,9 @@ definitions:
type: string
x-go-name: InReplyToID
language:
description: Primary language of this status (ISO 639 Part 1 two-letter language code).
description: |-
Primary language of this status (ISO 639 Part 1 two-letter language code).
Will be null if language is not known.
example: en
type: string
x-go-name: Language
Expand Down
3 changes: 2 additions & 1 deletion internal/api/model/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type Status struct {
// example: unlisted
Visibility Visibility `json:"visibility"`
// Primary language of this status (ISO 639 Part 1 two-letter language code).
// Will be null if language is not known.
// example: en
Language string `json:"language"`
Language *string `json:"language"`
// ActivityPub URI of the status. Equivalent to the status's activitypub ID.
// example: https://example.org/users/some_user/statuses/01FBVD42CQ3ZEEVMW180SBX03B
URI string `json:"uri"`
Expand Down
7 changes: 6 additions & 1 deletion internal/typeutils/internaltofrontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ func (c *converter) StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, r
interacts = &statusInteractions{}
}

var language *string
if s.Language != "" {
language = &s.Language
}

apiStatus := &model.Status{
ID: s.ID,
CreatedAt: util.FormatISO8601(s.CreatedAt),
Expand All @@ -516,7 +521,7 @@ func (c *converter) StatusToAPIStatus(ctx context.Context, s *gtsmodel.Status, r
Sensitive: *s.Sensitive,
SpoilerText: s.ContentWarning,
Visibility: c.VisToAPIVis(ctx, s.Visibility),
Language: s.Language,
Language: language,
URI: s.URI,
URL: s.URL,
RepliesCount: repliesCount,
Expand Down
14 changes: 14 additions & 0 deletions internal/typeutils/internaltofrontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontend() {
suite.Equal(`{"id":"01F8MH75CBF9JFX4ZAD54N0W0R","created_at":"2021-10-20T11:36:45.000Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"public","language":"en","uri":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R","url":"http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R","replies_count":0,"reblogs_count":0,"favourites_count":1,"favourited":true,"reblogged":false,"muted":false,"bookmarked":true,"pinned":false,"content":"hello world! #welcome ! first post on the instance :rainbow: !","reblog":null,"application":{"name":"superseriousbusiness","website":"https://superserious.business"},"account":{"id":"01F8MH17FWEB39HZJ76B6VXSKF","username":"admin","acct":"admin","display_name":"","locked":false,"bot":false,"created_at":"2022-05-17T13:10:59.000Z","note":"","url":"http://localhost:8080/@admin","avatar":"","avatar_static":"","header":"http://localhost:8080/assets/default_header.png","header_static":"http://localhost:8080/assets/default_header.png","followers_count":1,"following_count":1,"statuses_count":4,"last_status_at":"2021-10-20T10:41:37.000Z","emojis":[],"fields":[],"enable_rss":true,"role":"admin"},"media_attachments":[{"id":"01F8MH6NEM8D7527KZAECTCR76","type":"image","url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpeg","text_url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpeg","preview_url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/small/01F8MH6NEM8D7527KZAECTCR76.jpeg","remote_url":null,"preview_remote_url":null,"meta":{"original":{"width":1200,"height":630,"size":"1200x630","aspect":1.9047619},"small":{"width":256,"height":134,"size":"256x134","aspect":1.9104477},"focus":{"x":0,"y":0}},"description":"Black and white image of some 50's style text saying: Welcome On Board","blurhash":"LNJRdVM{00Rj%Mayt7j[4nWBofRj"}],"mentions":[],"tags":[{"name":"welcome","url":"http://localhost:8080/tags/welcome"}],"emojis":[{"shortcode":"rainbow","url":"http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png","static_url":"http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png","visible_in_picker":true,"category":"reactions"}],"card":null,"poll":null,"text":"hello world! #welcome ! first post on the instance :rainbow: !"}`, string(b))
}

func (suite *InternalToFrontendTestSuite) TestStatusToFrontendUnknownLanguage() {
testStatus := &gtsmodel.Status{}
*testStatus = *suite.testStatuses["admin_account_status_1"]
testStatus.Language = ""
requestingAccount := suite.testAccounts["local_account_1"]
apiStatus, err := suite.typeconverter.StatusToAPIStatus(context.Background(), testStatus, requestingAccount)
suite.NoError(err)

b, err := json.Marshal(apiStatus)
suite.NoError(err)

suite.Equal(`{"id":"01F8MH75CBF9JFX4ZAD54N0W0R","created_at":"2021-10-20T11:36:45.000Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"public","language":null,"uri":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R","url":"http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R","replies_count":0,"reblogs_count":0,"favourites_count":1,"favourited":true,"reblogged":false,"muted":false,"bookmarked":true,"pinned":false,"content":"hello world! #welcome ! first post on the instance :rainbow: !","reblog":null,"application":{"name":"superseriousbusiness","website":"https://superserious.business"},"account":{"id":"01F8MH17FWEB39HZJ76B6VXSKF","username":"admin","acct":"admin","display_name":"","locked":false,"bot":false,"created_at":"2022-05-17T13:10:59.000Z","note":"","url":"http://localhost:8080/@admin","avatar":"","avatar_static":"","header":"http://localhost:8080/assets/default_header.png","header_static":"http://localhost:8080/assets/default_header.png","followers_count":1,"following_count":1,"statuses_count":4,"last_status_at":"2021-10-20T10:41:37.000Z","emojis":[],"fields":[],"enable_rss":true,"role":"admin"},"media_attachments":[{"id":"01F8MH6NEM8D7527KZAECTCR76","type":"image","url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpeg","text_url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpeg","preview_url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/small/01F8MH6NEM8D7527KZAECTCR76.jpeg","remote_url":null,"preview_remote_url":null,"meta":{"original":{"width":1200,"height":630,"size":"1200x630","aspect":1.9047619},"small":{"width":256,"height":134,"size":"256x134","aspect":1.9104477},"focus":{"x":0,"y":0}},"description":"Black and white image of some 50's style text saying: Welcome On Board","blurhash":"LNJRdVM{00Rj%Mayt7j[4nWBofRj"}],"mentions":[],"tags":[{"name":"welcome","url":"http://localhost:8080/tags/welcome"}],"emojis":[{"shortcode":"rainbow","url":"http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png","static_url":"http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png","visible_in_picker":true,"category":"reactions"}],"card":null,"poll":null,"text":"hello world! #welcome ! first post on the instance :rainbow: !"}`, string(b))
}

func (suite *InternalToFrontendTestSuite) TestInstanceToFrontend() {
testInstance := &gtsmodel.Instance{
CreatedAt: testrig.TimeMustParse("2021-10-20T11:36:45Z"),
Expand Down
4 changes: 3 additions & 1 deletion internal/web/opengraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ func (og *ogMeta) withStatus(status *apimodel.Status) *ogMeta {
og.Description = parseDescription(status.Text)
}

og.Locale = status.Language
if status.Language != nil {
og.Locale = *status.Language
}
og.ResourceType = "article"
og.Title = "Post by " + parseTitle(status.Account, og.SiteName)
og.URL = status.URL
Expand Down

0 comments on commit 1d24c1c

Please sign in to comment.