From 6ab0aba50a1b41c027c0c23cc6342719ba439e06 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 18 Feb 2018 13:51:03 +0100 Subject: [PATCH] Transmogrify outgoing hashtags. --- lib/pleroma/web/activity_pub/transmogrifier.ex | 9 +++++++++ test/web/activity_pub/transmogrifier_test.exs | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index af6b5befc..3781b212a 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -100,6 +100,7 @@ def get_obj_helper(id) do """ def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do object = object + |> add_hashtags |> add_mention_tags |> add_attributed_to |> prepare_attachments @@ -118,6 +119,14 @@ def prepare_outgoing(%{"type" => type} = data) when type in ["Follow", "Accept", {:ok, data} end + def add_hashtags(object) do + tags = (object["tag"] || []) + |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end + + object + |> Map.put("tag", tags) + end + def add_mention_tags(object) do mentions = object["to"] |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end) diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index a39f4c139..e2db615eb 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -100,18 +100,24 @@ test "it turns mentions into tags" do user = insert(:user) other_user = insert(:user) - {:ok, activity} = CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya?"}) + {:ok, activity} = CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"}) {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) object = modified["object"] - expected_tag = %{ + expected_mention = %{ "href" => other_user.ap_id, "name" => "@#{other_user.nickname}", "type" => "Mention" } + expected_tag = %{ + "href" => Pleroma.Web.Endpoint.url <> "/tags/2hu", + "type" => "Hashtag", + "name" => "#2hu" + } assert Enum.member?(object["tag"], expected_tag) + assert Enum.member?(object["tag"], expected_mention) end test "it adds the json-ld context" do