Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/270
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Mar 30, 2024
1 parent d17bf36 commit 1f7091e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 12 deletions.
30 changes: 22 additions & 8 deletions lib/federator/transformer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -495,16 +495,30 @@ defmodule ActivityPub.Federator.Transformer do

def fix_tag(object), do: object

# content map usually only has one language so this will do for now.
def fix_content_map(%{"contentMap" => content_map} = object) when is_map(content_map) do
content_groups = Map.to_list(content_map)

if Enum.empty?(content_groups) do
object
if Enum.count(content_map) == 1 do
# content usually has the same data as single language so this should do for now
Map.put_new_lazy(
object,
"content",
fn -> List.first(Map.values(content_map)) end
)
else
{_, content} = Enum.at(content_groups, 0)

Map.put(object, "content", content)
Map.put(
object,
"content",
Enum.map(content_map, fn {locale, content} ->
lang =
with {:ok, lang_localized} <- Cldr.LocaleDisplay.display_name(locale, locale: locale) do
String.capitalize(lang_localized)
else
_ -> String.upcase(locale)
end

"<div lang='#{locale}'><em data-role='lang'>#{lang}</em>:\n#{content}</div>"
end)
|> Enum.join("\n")
)
end
end

Expand Down
13 changes: 12 additions & 1 deletion lib/fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ defmodule ActivityPub.Fixtures do
Application.put_env(:tesla, :adapter, previous_adapter)
end

def insert_file(file) do
previous_adapter = mock_prepare()

file(file)
|> maybe_insert()

Application.put_env(:tesla, :adapter, previous_adapter)
end

defp mock_prepare do
previous_adapter = Application.get_env(:tesla, :adapter)

Expand Down Expand Up @@ -58,7 +67,7 @@ defmodule ActivityPub.Fixtures do
error(e, "a fixture could not be read")
end

defp maybe_insert(%{body: data}) when is_binary(data) do
defp maybe_insert(data) when is_binary(data) do
data
|> Jason.decode()
~> ActivityPub.Federator.Transformer.handle_incoming()
Expand All @@ -70,6 +79,8 @@ defmodule ActivityPub.Fixtures do
error(e, "a fixture could not be inserted")
end

defp maybe_insert(%{body: data}), do: maybe_insert(data)

defp maybe_insert(skip) do
debug(skip, "skipping")
end
Expand Down
17 changes: 15 additions & 2 deletions test/activity_pub/data/transformer/note_handling_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,27 @@ defmodule ActivityPub.Federator.Transformer.NoteHandlingTest do
assert "moo" == Enum.at(object.data["tag"], 2)
end

test "it works for incoming notices with contentMap" do
test "it works for incoming notices with contentMap with multiple languages" do
data = file("fixtures/mastodon/mastodon-post-activity-contentmaps.json") |> Jason.decode!()

{:ok, %Activity{data: data, local: false}} = Transformer.handle_incoming(data)
object = Object.normalize(data["object"], fetch: false)

assert object.data["content"] =~
"<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span> testing</p>"

assert object.data["content"] =~
"<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span> probando</p>"
end

test "it works for incoming notices with contentMap with single language" do
data = file("fixtures/mastodon/mastodon-post-activity-contentmap.json") |> Jason.decode!()

{:ok, %Activity{data: data, local: false}} = Transformer.handle_incoming(data)
object = Object.normalize(data["object"], fetch: false)

assert object.data["content"] ==
"<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span></p>"
"<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span> testing</p>"
end

test "it works for incoming notices with to/cc not being an array (kroeg)" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"https://testing.local/users/karen"
],
"contentMap": {
"en": "<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span></p>"
"en": "<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span> testing</p>"
},
"conversation": "tag:mastodon.local,2018-02-12:objectId=20:objectType=Conversation",
"id": "https://mastodon.local/users/admin/statuses/99512778738411822",
Expand Down
68 changes: 68 additions & 0 deletions test/fixtures/mastodon/mastodon-post-activity-contentmaps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"Emoji": "toot:Emoji",
"Hashtag": "as:Hashtag",
"atomUri": "ostatus:atomUri",
"conversation": "ostatus:conversation",
"inReplyToAtomUri": "ostatus:inReplyToAtomUri",
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"movedTo": "as:movedTo",
"ostatus": "http://ostatus.org#",
"sensitive": "as:sensitive",
"toot": "http://joinmastodon.org/ns#"
}
],
"actor": "https://mastodon.local/users/admin",
"cc": [
"https://mastodon.local/users/admin/followers",
"https://testing.local/users/karen"
],
"id": "https://mastodon.local/users/admin/statuses/99512778738411822/activity",
"nickname": "karen",
"object": {
"atomUri": "https://mastodon.local/users/admin/statuses/99512778738411822",
"attachment": [],
"attributedTo": "https://mastodon.local/users/admin",
"cc": [
"https://mastodon.local/users/admin/followers",
"https://testing.local/users/karen"
],
"contentMap": {
"en": "<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span> testing</p>",
"es": "<p><span class=\"h-card\"><a href=\"https://testing.local/users/karen\" class=\"u-url mention\">@<span>karen</span></a></span> probando</p>"
},
"conversation": "tag:mastodon.local,2018-02-12:objectId=20:objectType=Conversation",
"id": "https://mastodon.local/users/admin/statuses/99512778738411822",
"inReplyTo": null,
"inReplyToAtomUri": null,
"published": "2018-02-12T14:08:20Z",
"sensitive": true,
"summary": "cw",
"tag": [
{
"href": "https://testing.local/users/karen",
"name": "@karen@testing.local",
"type": "Mention"
}
],
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"type": "Note",
"url": "https://mastodon.local/@admin/99512778738411822"
},
"published": "2018-02-12T14:08:20Z",
"signature": {
"created": "2018-02-12T14:08:20Z",
"creator": "https://mastodon.local/users/admin#main-key",
"signatureValue": "rnNfcopkc6+Ju73P806popcfwrK9wGYHaJVG1/ZvrlEbWVDzaHjkXqj9Q3/xju5l8CSn9tvSgCCtPFqZsFQwn/pFIFUcw7ZWB2xi4bDm3NZ3S4XQ8JRaaX7og5hFxAhWkGhJhAkfxVnOg2hG+w2d/7d7vRVSC1vo5ip4erUaA/PkWusZvPIpxnRWoXaxJsFmVx0gJgjpJkYDyjaXUlp+jmaoseeZ4EPQUWqHLKJ59PRG0mg8j2xAjYH9nQaN14qMRmTGPxY8gfv/CUFcatA+8VJU9KEsJkDAwLVvglydNTLGrxpAJU78a2eaht0foV43XUIZGe3DKiJPgE+UOKGCJw==",
"type": "RsaSignature2017"
},
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"type": "Create"
}

0 comments on commit 1f7091e

Please sign in to comment.