Use object instead of activity for metadata

This commit is contained in:
rinpatch 2019-01-18 09:32:52 +03:00
parent 64143d8040
commit 1b1af4798a
6 changed files with 30 additions and 47 deletions

View file

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata do defmodule Pleroma.Web.Metadata do
alias Phoenix.HTML alias Phoenix.HTML
@ -29,11 +30,11 @@ def to_tag(data) do
end end
end end
def activity_nsfw?(%{data: %{"object" => %{"tag" => tags}}}) do def activity_nsfw?(%{data: %{"sensitive" => sensitive}}) do
if(Pleroma.Config.get([__MODULE__, :unfurl_nsfw], false) == false) do Pleroma.Config.get([__MODULE__, :unfurl_nsfw], false) == false and sensitive
Enum.any?(tags, fn tag -> tag == "nsfw" end) end
else
false def activity_nsfw?(_) do
end false
end end
end end

View file

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.OpenGraph do defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
alias Pleroma.Web.Metadata.Providers.Provider alias Pleroma.Web.Metadata.Providers.Provider
alias Pleroma.Web.Metadata alias Pleroma.Web.Metadata
@ -11,11 +12,11 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
@impl Provider @impl Provider
def build_tags(%{ def build_tags(%{
activity: %{data: %{"object" => %{"id" => object_id}}} = activity, object: object,
user: user user: user
}) do }) do
attachments = build_attachments(activity) attachments = build_attachments(object)
scrubbed_content = scrub_html_and_truncate(activity) scrubbed_content = scrub_html_and_truncate(object)
# Zero width space # Zero width space
content = content =
if scrubbed_content != "" and scrubbed_content != "\u200B" do if scrubbed_content != "" and scrubbed_content != "\u200B" do
@ -36,7 +37,7 @@ def build_tags(%{
property: "og:title", property: "og:title",
content: "#{user.name}" <> content content: "#{user.name}" <> content
], []}, ], []},
{:meta, [property: "og:url", content: object_id], []}, {:meta, [property: "og:url", content: object.data["id"]], []},
{:meta, {:meta,
[ [
property: "og:description", property: "og:description",
@ -44,7 +45,7 @@ def build_tags(%{
], []}, ], []},
{:meta, [property: "og:type", content: "website"], []} {: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", content: attachment_url(User.avatar_url(user))], []},
{:meta, [property: "og:image:width", content: 150], []}, {:meta, [property: "og:image:width", content: 150], []},
@ -74,7 +75,7 @@ def build_tags(%{user: user}) do
end end
end end
defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do defp build_attachments(%{data: %{"attachment" => attachments}}) do
Enum.reduce(attachments, [], fn attachment, acc -> Enum.reduce(attachments, [], fn attachment, acc ->
rendered_tags = rendered_tags =
Enum.reduce(attachment["url"], [], fn url, acc -> Enum.reduce(attachment["url"], [], fn url, acc ->
@ -117,12 +118,12 @@ defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} =
end) end)
end end
defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do defp scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
content content
# html content comes from DB already encoded, decode first and scrub after # html content comes from DB already encoded, decode first and scrub after
|> HtmlEntities.decode() |> HtmlEntities.decode()
|> String.replace(~r/<br\s?\/?>/, " ") |> String.replace(~r/<br\s?\/?>/, " ")
|> HTML.get_cached_stripped_html_for_object(activity, __MODULE__) |> HTML.get_cached_stripped_html_for_object(object, __MODULE__)
|> Formatter.truncate() |> Formatter.truncate()
end end

View file

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.Provider do defmodule Pleroma.Web.Metadata.Providers.Provider do
@callback build_tags(map()) :: list() @callback build_tags(map()) :: list()
end end

View file

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.TwitterCard do defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
alias Pleroma.Web.Metadata.Providers.Provider alias Pleroma.Web.Metadata.Providers.Provider
alias Pleroma.Web.Metadata alias Pleroma.Web.Metadata
@ -8,11 +9,11 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
@behaviour Provider @behaviour Provider
@impl Provider @impl Provider
def build_tags(%{activity: activity}) do def build_tags(%{object: object}) do
if Metadata.activity_nsfw?(activity) or activity.data["object"]["attachment"] == [] do if Metadata.activity_nsfw?(object) or object.data["attachment"] == [] do
build_tags(nil) build_tags(nil)
else else
case find_first_acceptable_media_type(activity) do case find_first_acceptable_media_type(object) do
"image" -> "image" ->
[{:meta, [property: "twitter:card", content: "summary_large_image"], []}] [{:meta, [property: "twitter:card", content: "summary_large_image"], []}]
@ -33,7 +34,7 @@ def build_tags(_) do
[{:meta, [property: "twitter:card", content: "summary"], []}] [{:meta, [property: "twitter:card", content: "summary"], []}]
end 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, fn attachment ->
Enum.find_value(attachment["url"], fn url -> Enum.find_value(attachment["url"], fn url ->
Enum.find(["image", "audio", "video"], fn media_type -> Enum.find(["image", "audio", "video"], fn media_type ->

View file

@ -145,10 +145,11 @@ def notice(conn, %{"id" => id}) do
%User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
case format = get_format(conn) do case format = get_format(conn) do
"html" -> "html" ->
# Only Create actvities have a map at object if activity.data["type"] == "Create" do
if is_map(activity.data["object"]) do %Object{} = object = Object.normalize(activity.data["object"])
Fallback.RedirectController.redirector_with_meta(conn, %{ Fallback.RedirectController.redirector_with_meta(conn, %{
activity: activity, object: object,
user: user user: user
}) })
else else

View file

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest do defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest do
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
@ -47,19 +48,7 @@ test "it renders all supported types of attachments and skips unknown types" do
} }
}) })
note_activity = result = OpenGraph.build_tags(%{object: note, user: user})
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})
assert Enum.all?( assert Enum.all?(
[ [
@ -85,6 +74,7 @@ test "it does not render attachments if post is nsfw" do
"id" => "https://pleroma.gov/objects/whatever", "id" => "https://pleroma.gov/objects/whatever",
"content" => "#cuteposting #nsfw #hambaga", "content" => "#cuteposting #nsfw #hambaga",
"tag" => ["cuteposting", "nsfw", "hambaga"], "tag" => ["cuteposting", "nsfw", "hambaga"],
"sensitive" => true,
"attachment" => [ "attachment" => [
%{ %{
"url" => [ "url" => [
@ -95,19 +85,7 @@ test "it does not render attachments if post is nsfw" do
} }
}) })
note_activity = result = OpenGraph.build_tags(%{object: note, user: user})
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})
assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result