From 1b1af4798a74c4ab357140ef2c5928dd9ebd3221 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Fri, 18 Jan 2019 09:32:52 +0300 Subject: [PATCH] Use object instead of activity for metadata --- lib/pleroma/web/metadata.ex | 13 ++++---- lib/pleroma/web/metadata/opengraph.ex | 17 ++++++----- lib/pleroma/web/metadata/provider.ex | 1 + lib/pleroma/web/metadata/twitter_card.ex | 9 +++--- lib/pleroma/web/ostatus/ostatus_controller.ex | 7 +++-- test/web/metadata/opengraph_test.exs | 30 +++---------------- 6 files changed, 30 insertions(+), 47 deletions(-) diff --git a/lib/pleroma/web/metadata.ex b/lib/pleroma/web/metadata.ex index be3c384ae..8761260f2 100644 --- a/lib/pleroma/web/metadata.ex +++ b/lib/pleroma/web/metadata.ex @@ -1,6 +1,7 @@ # Pleroma: A lightweight social networking server # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Web.Metadata do alias Phoenix.HTML @@ -29,11 +30,11 @@ def to_tag(data) do end end - def activity_nsfw?(%{data: %{"object" => %{"tag" => tags}}}) do - if(Pleroma.Config.get([__MODULE__, :unfurl_nsfw], false) == false) do - Enum.any?(tags, fn tag -> tag == "nsfw" end) - else - false - end + def activity_nsfw?(%{data: %{"sensitive" => sensitive}}) do + Pleroma.Config.get([__MODULE__, :unfurl_nsfw], false) == false and sensitive + end + + def activity_nsfw?(_) do + false end end diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex index cbd0b7d1b..43303859c 100644 --- a/lib/pleroma/web/metadata/opengraph.ex +++ b/lib/pleroma/web/metadata/opengraph.ex @@ -1,6 +1,7 @@ # Pleroma: A lightweight social networking server # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Web.Metadata.Providers.OpenGraph do alias Pleroma.Web.Metadata.Providers.Provider alias Pleroma.Web.Metadata @@ -11,11 +12,11 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do @impl Provider def build_tags(%{ - activity: %{data: %{"object" => %{"id" => object_id}}} = activity, + object: object, user: user }) do - attachments = build_attachments(activity) - scrubbed_content = scrub_html_and_truncate(activity) + attachments = build_attachments(object) + scrubbed_content = scrub_html_and_truncate(object) # Zero width space content = if scrubbed_content != "" and scrubbed_content != "\u200B" do @@ -36,7 +37,7 @@ def build_tags(%{ property: "og:title", content: "#{user.name}" <> content ], []}, - {:meta, [property: "og:url", content: object_id], []}, + {:meta, [property: "og:url", content: object.data["id"]], []}, {:meta, [ property: "og:description", @@ -44,7 +45,7 @@ def build_tags(%{ ], []}, {:meta, [property: "og:type", content: "website"], []} ] ++ - if attachments == [] or Metadata.activity_nsfw?(activity) do + if attachments == [] or Metadata.activity_nsfw?(object) do [ {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, {:meta, [property: "og:image:width", content: 150], []}, @@ -74,7 +75,7 @@ def build_tags(%{user: user}) do end end - defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do + defp build_attachments(%{data: %{"attachment" => attachments}}) do Enum.reduce(attachments, [], fn attachment, acc -> rendered_tags = Enum.reduce(attachment["url"], [], fn url, acc -> @@ -117,12 +118,12 @@ defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = end) end - defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do + defp scrub_html_and_truncate(%{data: %{"content" => content}} = object) do content # html content comes from DB already encoded, decode first and scrub after |> HtmlEntities.decode() |> String.replace(~r//, " ") - |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__) + |> HTML.get_cached_stripped_html_for_object(object, __MODULE__) |> Formatter.truncate() end diff --git a/lib/pleroma/web/metadata/provider.ex b/lib/pleroma/web/metadata/provider.ex index a39008bcc..197fb2a77 100644 --- a/lib/pleroma/web/metadata/provider.ex +++ b/lib/pleroma/web/metadata/provider.ex @@ -1,6 +1,7 @@ # Pleroma: A lightweight social networking server # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Web.Metadata.Providers.Provider do @callback build_tags(map()) :: list() end diff --git a/lib/pleroma/web/metadata/twitter_card.ex b/lib/pleroma/web/metadata/twitter_card.ex index 9a1245e59..32b979357 100644 --- a/lib/pleroma/web/metadata/twitter_card.ex +++ b/lib/pleroma/web/metadata/twitter_card.ex @@ -1,6 +1,7 @@ # Pleroma: A lightweight social networking server # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Web.Metadata.Providers.TwitterCard do alias Pleroma.Web.Metadata.Providers.Provider alias Pleroma.Web.Metadata @@ -8,11 +9,11 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do @behaviour Provider @impl Provider - def build_tags(%{activity: activity}) do - if Metadata.activity_nsfw?(activity) or activity.data["object"]["attachment"] == [] do + def build_tags(%{object: object}) do + if Metadata.activity_nsfw?(object) or object.data["attachment"] == [] do build_tags(nil) else - case find_first_acceptable_media_type(activity) do + case find_first_acceptable_media_type(object) do "image" -> [{:meta, [property: "twitter:card", content: "summary_large_image"], []}] @@ -33,7 +34,7 @@ def build_tags(_) do [{:meta, [property: "twitter:card", content: "summary"], []}] end - def find_first_acceptable_media_type(%{data: %{"object" => %{"attachment" => attachment}}}) do + def find_first_acceptable_media_type(%{data: %{"attachment" => attachment}}) do Enum.find_value(attachment, fn attachment -> Enum.find_value(attachment["url"], fn url -> Enum.find(["image", "audio", "video"], fn media_type -> diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index f7ba57389..4844b84ad 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -145,10 +145,11 @@ def notice(conn, %{"id" => id}) do %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do case format = get_format(conn) do "html" -> - # Only Create actvities have a map at object - if is_map(activity.data["object"]) do + if activity.data["type"] == "Create" do + %Object{} = object = Object.normalize(activity.data["object"]) + Fallback.RedirectController.redirector_with_meta(conn, %{ - activity: activity, + object: object, user: user }) else diff --git a/test/web/metadata/opengraph_test.exs b/test/web/metadata/opengraph_test.exs index 1c4fadc46..c3502bad3 100644 --- a/test/web/metadata/opengraph_test.exs +++ b/test/web/metadata/opengraph_test.exs @@ -1,6 +1,7 @@ # Pleroma: A lightweight social networking server # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest do use Pleroma.DataCase import Pleroma.Factory @@ -47,19 +48,7 @@ test "it renders all supported types of attachments and skips unknown types" do } }) - note_activity = - insert(:note_activity, %{ - data: %{ - "actor" => note.data["actor"], - "to" => note.data["to"], - "object" => note.data, - "context" => note.data["context"] - }, - actor: note.data["actor"], - recipients: note.data["to"] - }) - - result = OpenGraph.build_tags(%{activity: note_activity, user: user}) + result = OpenGraph.build_tags(%{object: note, user: user}) assert Enum.all?( [ @@ -85,6 +74,7 @@ test "it does not render attachments if post is nsfw" do "id" => "https://pleroma.gov/objects/whatever", "content" => "#cuteposting #nsfw #hambaga", "tag" => ["cuteposting", "nsfw", "hambaga"], + "sensitive" => true, "attachment" => [ %{ "url" => [ @@ -95,19 +85,7 @@ test "it does not render attachments if post is nsfw" do } }) - note_activity = - insert(:note_activity, %{ - data: %{ - "actor" => note.data["actor"], - "to" => note.data["to"], - "object" => note.data, - "context" => note.data["context"] - }, - actor: note.data["actor"], - recipients: note.data["to"] - }) - - result = OpenGraph.build_tags(%{activity: note_activity, user: user}) + result = OpenGraph.build_tags(%{object: note, user: user}) assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result