Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federate custom emoji (outbound) #791

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/typeutils/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ type TypeConverter interface {
FollowToAS(ctx context.Context, f *gtsmodel.Follow, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) (vocab.ActivityStreamsFollow, error)
// MentionToAS converts a gts model mention into an activity streams Mention, suitable for federation
MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab.ActivityStreamsMention, error)
// EmojiToAS converts a gts emoji into a mastodon ns Emoji, suitable for federation
EmojiToAS(ctx context.Context, e *gtsmodel.Emoji) (vocab.TootEmoji, error)
// AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation
AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error)
// FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation.
Expand Down
90 changes: 75 additions & 15 deletions internal/typeutils/internaltoas.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,13 @@ func (c *converter) StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.A
}

// tag -- emojis
// TODO
for _, emoji := range s.Emojis {
asMention, err := c.EmojiToAS(ctx, emoji)
if err != nil {
return nil, fmt.Errorf("StatusToAS: error converting emoji to AS emoji: %s", err)
}
tagProp.AppendTootEmoji(asMention)
}

// tag -- hashtags
// TODO
Expand Down Expand Up @@ -632,6 +638,58 @@ func (c *converter) MentionToAS(ctx context.Context, m *gtsmodel.Mention) (vocab
return mention, nil
}

/*
we're making something like this:
{
"id": "https://example.com/emoji/123",
"type": "Emoji",
"name": ":kappa:",
"icon": {
"type": "Image",
"mediaType": "image/png",
"url": "https://example.com/files/kappa.png"
}
}
*/
func (c *converter) EmojiToAS(ctx context.Context, e *gtsmodel.Emoji) (vocab.TootEmoji, error) {
// create the emoji
emoji := streams.NewTootEmoji()

// set the ID property to the blocks's URI
idProp := streams.NewJSONLDIdProperty()
idIRI, err := url.Parse(e.URI)
if err != nil {
return nil, fmt.Errorf("EmojiToAS: error parsing uri %s: %s", e.URI, err)
}
idProp.Set(idIRI)
emoji.SetJSONLDId(idProp)

nameProp := streams.NewActivityStreamsNameProperty()
nameString := fmt.Sprintf(":%s:", e.Shortcode)
nameProp.AppendXMLSchemaString(nameString)
emoji.SetActivityStreamsName(nameProp)

iconProperty := streams.NewActivityStreamsIconProperty()
iconImage := streams.NewActivityStreamsImage()

mediaType := streams.NewActivityStreamsMediaTypeProperty()
mediaType.Set(e.ImageContentType)
iconImage.SetActivityStreamsMediaType(mediaType)

emojiURLProperty := streams.NewActivityStreamsUrlProperty()
emojiURL, err := url.Parse(e.ImageURL)
if err != nil {
return nil, fmt.Errorf("EmojiToAS: error parsing url %s: %s", e.ImageURL, err)
}
emojiURLProperty.AppendIRI(emojiURL)
iconImage.SetActivityStreamsUrl(emojiURLProperty)

iconProperty.AppendActivityStreamsImage(iconImage)
emoji.SetActivityStreamsIcon(iconProperty)

return emoji, nil
}

func (c *converter) AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error) {
// type -- Document
doc := streams.NewActivityStreamsDocument()
Expand Down Expand Up @@ -667,15 +725,15 @@ func (c *converter) AttachmentToAS(ctx context.Context, a *gtsmodel.MediaAttachm
}

/*
We want to end up with something like this:

{
"@context": "https://www.w3.org/ns/activitystreams",
"actor": "https://ondergrond.org/users/dumpsterqueer",
"id": "https://ondergrond.org/users/dumpsterqueer#likes/44584",
"object": "https://testingtesting123.xyz/users/gotosocial_test_account/statuses/771aea80-a33d-4d6d-8dfd-57d4d2bfcbd4",
"type": "Like"
}
We want to end up with something like this:

{
"@context": "https://www.w3.org/ns/activitystreams",
"actor": "https://ondergrond.org/users/dumpsterqueer",
"id": "https://ondergrond.org/users/dumpsterqueer#likes/44584",
"object": "https://testingtesting123.xyz/users/gotosocial_test_account/statuses/771aea80-a33d-4d6d-8dfd-57d4d2bfcbd4",
"type": "Like"
}
*/
func (c *converter) FaveToAS(ctx context.Context, f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) {
// check if targetStatus is already pinned to this fave, and fetch it if not
Expand Down Expand Up @@ -825,7 +883,7 @@ func (c *converter) BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel.
}

/*
we want to end up with something like this:
we want to end up with something like this:

{
"@context": "https://www.w3.org/ns/activitystreams",
Expand Down Expand Up @@ -895,7 +953,7 @@ func (c *converter) BlockToAS(ctx context.Context, b *gtsmodel.Block) (vocab.Act
}

/*
the goal is to end up with something like this:
the goal is to end up with something like this:

{
"@context": "https://www.w3.org/ns/activitystreams",
Expand Down Expand Up @@ -960,7 +1018,8 @@ func (c *converter) StatusToASRepliesCollection(ctx context.Context, status *gts
}

/*
the goal is to end up with something like this:
the goal is to end up with something like this:

{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://example.org/users/whatever/statuses/01FCNEXAGAKPEX1J7VJRPJP490/replies?only_other_accounts=true&page=true",
Expand Down Expand Up @@ -1030,7 +1089,8 @@ func (c *converter) StatusURIsToASRepliesPage(ctx context.Context, status *gtsmo
}

/*
the goal is to end up with something like this:
the goal is to end up with something like this:

{
"id": "https://example.org/users/whatever/outbox?page=true",
"type": "OrderedCollectionPage",
Expand Down Expand Up @@ -1134,7 +1194,7 @@ func (c *converter) StatusesToASOutboxPage(ctx context.Context, outboxID string,
}

/*
we want something that looks like this:
we want something that looks like this:

{
"@context": "https://www.w3.org/ns/activitystreams",
Expand Down
19 changes: 19 additions & 0 deletions internal/typeutils/internaltoas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ func (suite *InternalToASTestSuite) TestStatusToAS() {
suite.Equal(`{"@context":"https://www.w3.org/ns/activitystreams","attachment":[],"attributedTo":"http://localhost:8080/users/the_mighty_zork","cc":"http://localhost:8080/users/the_mighty_zork/followers","content":"hello everyone!","id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY","published":"2021-10-20T12:40:37+02:00","replies":{"first":{"id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?page=true","next":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies?only_other_accounts=false\u0026page=true","partOf":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies","type":"CollectionPage"},"id":"http://localhost:8080/users/the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY/replies","type":"Collection"},"sensitive":true,"summary":"introduction post","tag":[],"to":"https://www.w3.org/ns/activitystreams#Public","type":"Note","url":"http://localhost:8080/@the_mighty_zork/statuses/01F8MHAMCHF6Y650WCRSCP4WMY"}`, string(bytes))
}


func (suite *InternalToASTestSuite) TestStatusWithTagsToAS() {
ctx := context.Background()
//get the entire status with all tags
testStatus, err := suite.db.GetStatusByID(ctx, suite.testStatuses["admin_account_status_1"].ID)
suite.NoError(err)

asStatus, err := suite.typeconverter.StatusToAS(ctx, testStatus)
suite.NoError(err)

ser, err := streams.Serialize(asStatus)
suite.NoError(err)

bytes, err := json.Marshal(ser)
suite.NoError(err)

suite.Equal(`{"@context":["https://www.w3.org/ns/activitystreams","http://joinmastodon.org/ns"],"attachment":{"blurhash":"LNJRdVM{00Rj%Mayt7j[4nWBofRj","mediaType":"image/jpeg","name":"Black and white image of some 50's style text saying: Welcome On Board","type":"Document","url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpeg"},"attributedTo":"http://localhost:8080/users/admin","cc":"http://localhost:8080/users/admin/followers","content":"hello world! #welcome ! first post on the instance :rainbow: !","id":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R","published":"2021-10-20T11:36:45Z","replies":{"first":{"id":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R/replies?page=true","next":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R/replies?only_other_accounts=false\u0026page=true","partOf":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R/replies","type":"CollectionPage"},"id":"http://localhost:8080/users/admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R/replies","type":"Collection"},"sensitive":false,"summary":"","tag":{"icon":{"mediaType":"image/png","type":"Image","url":"http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png"},"id":"http://localhost:8080/emoji/01F8MH9H8E4VG3KDYJR9EGPXCQ","name":":rainbow:","type":"Emoji"},"to":"https://www.w3.org/ns/activitystreams#Public","type":"Note","url":"http://localhost:8080/@admin/statuses/01F8MH75CBF9JFX4ZAD54N0W0R"}`, string(bytes))
}

func (suite *InternalToASTestSuite) TestStatusToASWithMentions() {
testStatusID := suite.testStatuses["admin_account_status_3"].ID
ctx := context.Background()
Expand Down