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
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata do
alias Phoenix.HTML
@ -29,11 +30,11 @@ defmodule Pleroma.Web.Metadata 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

View File

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# 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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
], []},
{: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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph 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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
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/<br\s?\/?>/, " ")
|> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
|> HTML.get_cached_stripped_html_for_object(object, __MODULE__)
|> Formatter.truncate()
end

View File

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

View File

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# 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 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard 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 ->

View File

@ -145,10 +145,11 @@ defmodule Pleroma.Web.OStatus.OStatusController 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

View File

@ -1,6 +1,7 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest do
use Pleroma.DataCase
import Pleroma.Factory
@ -47,19 +48,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest 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 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest do
"id" => "https://pleroma.gov/objects/whatever",
"content" => "#cuteposting #nsfw #hambaga",
"tag" => ["cuteposting", "nsfw", "hambaga"],
"sensitive" => true,
"attachment" => [
%{
"url" => [
@ -95,19 +85,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest 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