From 9b3a6cdb07383631c9ac11ce5de51306f6c4a464 Mon Sep 17 00:00:00 2001 From: raeno Date: Wed, 12 Dec 2018 22:55:01 +0300 Subject: [PATCH] Extract opengraph/oembed into separate module --- lib/pleroma/web/ostatus/metadata.ex | 66 +++++++++++++++++++ lib/pleroma/web/ostatus/ostatus.ex | 63 +----------------- lib/pleroma/web/ostatus/ostatus_controller.ex | 6 +- 3 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 lib/pleroma/web/ostatus/metadata.ex diff --git a/lib/pleroma/web/ostatus/metadata.ex b/lib/pleroma/web/ostatus/metadata.ex new file mode 100644 index 000000000..dd099e2ad --- /dev/null +++ b/lib/pleroma/web/ostatus/metadata.ex @@ -0,0 +1,66 @@ +defmodule Pleroma.Web.Metadata do + alias Phoenix.HTML + alias Pleroma.{Web, Formatter} + alias Pleroma.{User, Activity} + alias Pleroma.Web.MediaProxy + + def build_tags(activity, user, url) do + Enum.concat([ + if(meta_enabled?(:opengraph), do: opengraph_tags(activity, user), else: []), + if(meta_enabled?(:oembed), do: oembed_links(url), else: []) + ]) + |> Enum.map(&to_tag/1) + |> Enum.map(&HTML.safe_to_string/1) + |> Enum.join("\n") + end + + def meta_enabled?(type) do + config = Pleroma.Config.get(:metadata, []) + Keyword.get(config, type, false) + end + + def to_tag(data) do + with {name, attrs, _content = []} <- data do + HTML.Tag.tag(name, attrs) + else + {name, attrs, content} -> + HTML.Tag.content_tag(name, content, attrs) + + _ -> + raise ArgumentError, message: "make_tag invalid args" + end + end + + defp oembed_links(url) do + Enum.map(["xml", "json"], fn format -> + href = HTML.raw(oembed_path(url, format)) + { :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] } + end) + end + + defp opengraph_tags(activity, user) do + with image = User.avatar_url(user) |> MediaProxy.url(), + truncated_content = Formatter.truncate(activity.data["object"]["content"]), + domain = Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN") do + [ + {:meta, + [ + property: "og:title", + content: "#{user.name} (@#{user.nickname}@#{domain}) post ##{activity.id}" + ], []}, + {:meta, [property: "og:url", content: activity.data["id"]], []}, + {:meta, [property: "og:description", content: truncated_content], + []}, + {:meta, [property: "og:image", content: image], []}, + {:meta, [property: "og:image:width", content: 120], []}, + {:meta, [property: "og:image:height", content: 120], []}, + {:meta, [property: "twitter:card", content: "summary"], []} + ] + end + end + + defp oembed_path(url, format) do + query = URI.encode_query(%{url: url, format: format}) + "#{Web.base_url()}/oembed?#{query}" + end +end \ No newline at end of file diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 73cdd3837..ba44b2e99 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -7,10 +7,9 @@ defmodule Pleroma.Web.OStatus do alias Pleroma.{Repo, User, Web, Object, Activity, Formatter} alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Web.{WebFinger, Websub, MediaProxy} + alias Pleroma.Web.{WebFinger, Websub} alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler} alias Pleroma.Web.ActivityPub.Transmogrifier - alias Phoenix.HTML def is_representable?(%Activity{data: data}) do object = Object.normalize(data["object"]) @@ -27,61 +26,6 @@ def is_representable?(%Activity{data: data}) do end end - def metadata(activity, user, url) do - Enum.concat([ - if(meta_enabled?(:opengraph), do: opengraph_tags(activity, user), else: []), - if(meta_enabled?(:oembed), do: oembed_links(url), else: []) - ]) - |> Enum.map(&to_tag/1) - |> Enum.map(&HTML.safe_to_string/1) - |> Enum.join("\n") - end - - def meta_enabled?(type) do - config = Pleroma.Config.get(:metadata, []) - Keyword.get(config, type, false) - end - - def to_tag(data) do - with {name, attrs, _content = []} <- data do - HTML.Tag.tag(name, attrs) - else - {name, attrs, content} -> - HTML.Tag.content_tag(name, content, attrs) - - _ -> - raise ArgumentError, message: "make_tag invalid args" - end - end - - def oembed_links(url) do - Enum.map(["xml", "json"], fn format -> - href = HTML.raw(oembed_path(url, format)) - { :link, [ type: ["application/#{format}+oembed"], href: href, rel: 'alternate'], [] } - end) - end - - def opengraph_tags(activity, user) do - with image = User.avatar_url(user) |> MediaProxy.url(), - truncated_content = Formatter.truncate(activity.data["object"]["content"]), - domain = Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN") do - [ - {:meta, - [ - property: "og:title", - content: "#{user.name} (@#{user.nickname}@#{domain}) post ##{activity.id}" - ], []}, - {:meta, [property: "og:url", content: activity.data["id"]], []}, - {:meta, [property: "og:description", content: truncated_content], - []}, - {:meta, [property: "og:image", content: image], []}, - {:meta, [property: "og:image:width", content: 120], []}, - {:meta, [property: "og:image:height", content: 120], []}, - {:meta, [property: "twitter:card", content: "summary"], []} - ] - end - end - def feed_path(user) do "#{user.ap_id}/feed.atom" end @@ -98,11 +42,6 @@ def remote_follow_path do "#{Web.base_url()}/ostatus_subscribe?acct={uri}" end - def oembed_path(url, format) do - query = URI.encode_query(%{url: url, format: format}) - "#{Web.base_url()}/oembed?#{query}" - end - def handle_incoming(xml_string) do with doc when doc != :error <- parse_document(xml_string) do entries = :xmerl_xpath.string('//entry', doc) diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index bfc36c0c4..23ca8e088 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -4,7 +4,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do alias Pleroma.{User, Activity, Object} alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter} alias Pleroma.Repo - alias Pleroma.Web.{OStatus, Federator} + alias Pleroma.Web.{OStatus, Federator, Metadata} alias Pleroma.Web.XML alias Pleroma.Web.ActivityPub.ObjectView alias Pleroma.Web.ActivityPub.ActivityPubController @@ -153,8 +153,8 @@ def notice(conn, %{"id" => id}) do defp serve_static_with_meta(conn, activity, user) do {:ok, index_content } = File.read(Application.app_dir(:pleroma, "priv/static/index.html")) - links = OStatus.metadata(activity, user, request_url(conn)) - response = String.replace(index_content, "", links) + tags = Metadata.build_tags(activity, user, request_url(conn)) + response = String.replace(index_content, "", tags) conn |> put_resp_content_type("text/html") |> send_resp(200, response)