2019-01-16 14:15:13 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2019-01-15 20:00:22 +00:00
|
|
|
defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
|
2019-01-15 20:25:28 +00:00
|
|
|
alias Pleroma.Web.Metadata.Providers.Provider
|
2019-01-17 08:00:02 +00:00
|
|
|
alias Pleroma.Web.Metadata
|
2019-01-15 20:25:28 +00:00
|
|
|
|
|
|
|
@behaviour Provider
|
|
|
|
|
|
|
|
@impl Provider
|
2019-01-15 20:00:22 +00:00
|
|
|
def build_tags(%{activity: activity}) do
|
2019-01-17 08:00:02 +00:00
|
|
|
if Metadata.activity_nsfw?(activity) or activity.data["object"]["attachment"] == [] do
|
2019-01-15 20:00:22 +00:00
|
|
|
build_tags(nil)
|
|
|
|
else
|
|
|
|
case find_first_acceptable_media_type(activity) do
|
|
|
|
"image" ->
|
|
|
|
[{:meta, [property: "twitter:card", content: "summary_large_image"], []}]
|
|
|
|
|
|
|
|
"audio" ->
|
|
|
|
[{:meta, [property: "twitter:card", content: "player"], []}]
|
|
|
|
|
|
|
|
"video" ->
|
|
|
|
[{:meta, [property: "twitter:card", content: "player"], []}]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
build_tags(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 20:25:28 +00:00
|
|
|
@impl Provider
|
2019-01-15 20:00:22 +00:00
|
|
|
def build_tags(_) do
|
|
|
|
[{:meta, [property: "twitter:card", content: "summary"], []}]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_first_acceptable_media_type(%{data: %{"object" => %{"attachment" => attachment}}}) do
|
|
|
|
Enum.find_value(attachment, fn attachment ->
|
|
|
|
Enum.find_value(attachment["url"], fn url ->
|
|
|
|
Enum.find(["image", "audio", "video"], fn media_type ->
|
|
|
|
String.starts_with?(url["mediaType"], media_type)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|