From d3ec09bb380bb990bea6edc5dae6bbda7f2322c5 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Thu, 13 Dec 2018 15:13:02 +0300 Subject: [PATCH 1/2] fix tags --- lib/pleroma/web/mastodon_api/views/status_view.ex | 21 ++++++++++++++++++++- test/web/mastodon_api/status_view_test.exs | 17 ++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index c3c735d5d..f2a47f594 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -140,7 +140,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do visibility: get_visibility(object), media_attachments: attachments |> Enum.take(4), mentions: mentions, - tags: tags, + tags: build_tags(tags), application: %{ name: "Web", website: nil @@ -235,6 +235,25 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do def render_content(object), do: object["content"] || "" @doc """ + Builds a dictionary tags. + + ## Examples + + iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"]) + [{"name": "fediverse", "url": "/tag/fediverse"}, + {"name": "nextcloud", "url": "/tag/nextcloud"}] + + """ + @spec build_tags(list(String.t())) :: list(map()) + def build_tags(object_tags) when is_list(object_tags) do + Enum.reduce(object_tags, [], fn tag, tags -> + tags ++ [%{name: tag, url: "/tag/#{tag}"}] + end) + end + + def build_tags(_), do: [] + + @doc """ Builds list emojis. Arguments: `nil` or list tuple of name and url. diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index d10d59d6c..4f9805c78 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -62,7 +62,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do visibility: "public", media_attachments: [], mentions: [], - tags: note.data["object"]["tag"], + tags: [ + %{ + name: "#{note.data["object"]["tag"]}", + url: "/tag/#{note.data["object"]["tag"]}" + } + ], application: %{ name: "Web", website: nil @@ -151,4 +156,14 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do assert represented[:reblog][:id] == to_string(activity.id) assert represented[:emojis] == [] end + + describe "build_tags/1" do + test "it returns a a dictionary tags" do + assert StatusView.build_tags(["fediverse", "mastodon", "nextcloud"]) == [ + %{name: "fediverse", url: "/tag/fediverse"}, + %{name: "mastodon", url: "/tag/mastodon"}, + %{name: "nextcloud", url: "/tag/nextcloud"} + ] + end + end end From ea72ac549b2ac52623462d6862154fb6f800c01c Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Fri, 14 Dec 2018 22:56:37 +0300 Subject: [PATCH 2/2] fix case when tags is invalid --- lib/pleroma/web/mastodon_api/views/status_view.ex | 4 +++- test/web/mastodon_api/status_view_test.exs | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index f2a47f594..46c559e3a 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -244,8 +244,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do {"name": "nextcloud", "url": "/tag/nextcloud"}] """ - @spec build_tags(list(String.t())) :: list(map()) + @spec build_tags(list(any())) :: list(map()) def build_tags(object_tags) when is_list(object_tags) do + object_tags = for tag when is_binary(tag) <- object_tags, do: tag + Enum.reduce(object_tags, [], fn tag, tags -> tags ++ [%{name: tag, url: "/tag/#{tag}"}] end) diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index 4f9805c78..b7ac92760 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -159,7 +159,18 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do describe "build_tags/1" do test "it returns a a dictionary tags" do - assert StatusView.build_tags(["fediverse", "mastodon", "nextcloud"]) == [ + object_tags = [ + "fediverse", + "mastodon", + "nextcloud", + %{ + "href" => "https://kawen.space/users/lain", + "name" => "@lain@kawen.space", + "type" => "Mention" + } + ] + + assert StatusView.build_tags(object_tags) == [ %{name: "fediverse", url: "/tag/fediverse"}, %{name: "mastodon", url: "/tag/mastodon"}, %{name: "nextcloud", url: "/tag/nextcloud"}