aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/config.exs2
-rw-r--r--lib/mix/tasks/pleroma/sample_config.eex1
-rw-r--r--lib/pleroma/formatter.ex18
-rw-r--r--lib/pleroma/user.ex13
-rw-r--r--lib/pleroma/web/ostatus/metadata.ex74
-rw-r--r--lib/pleroma/web/ostatus/ostatus_controller.ex10
-rw-r--r--lib/pleroma/web/router.ex29
-rw-r--r--priv/static/index.html2
-rw-r--r--test/web/ostatus/ostatus_controller_test.exs19
9 files changed, 159 insertions, 9 deletions
diff --git a/config/config.exs b/config/config.exs
index 1401b0a3d..5477c0c0e 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -171,6 +171,8 @@ config :pleroma, :gopher,
ip: {0, 0, 0, 0},
port: 9999
+config :pleroma, :metadata, opengraph: true
+
config :pleroma, :suggestions,
enabled: false,
third_party_engine:
diff --git a/lib/mix/tasks/pleroma/sample_config.eex b/lib/mix/tasks/pleroma/sample_config.eex
index 0cd572797..8eacefab2 100644
--- a/lib/mix/tasks/pleroma/sample_config.eex
+++ b/lib/mix/tasks/pleroma/sample_config.eex
@@ -12,6 +12,7 @@ config :pleroma, Pleroma.Web.Endpoint,
config :pleroma, :instance,
name: "<%= name %>",
email: "<%= email %>",
+ domain: "<%= domain %>,
limit: 5000,
registrations_open: true,
dedupe_media: false
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex
index 133683794..1d5dc0c99 100644
--- a/lib/pleroma/formatter.ex
+++ b/lib/pleroma/formatter.ex
@@ -163,4 +163,22 @@ defmodule Pleroma.Formatter do
String.replace(result_text, uuid, replacement)
end)
end
+
+ def truncate(text, opts \\ []) do
+ max_length = opts[:max_length] || 200
+ omission = opts[:omission] || "..."
+
+ cond do
+ not String.valid?(text) ->
+ text
+
+ String.length(text) < max_length ->
+ text
+
+ true ->
+ length_with_omission = max_length - String.length(omission)
+
+ "#{String.slice(text, 0, length_with_omission)}#{omission}"
+ end
+ end
end
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 49928bc13..28ff08a39 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -294,6 +294,10 @@ defmodule Pleroma.User do
user.info.locked || false
end
+ def get_by_id(id) do
+ Repo.get_by(User, id: id)
+ end
+
def get_by_ap_id(ap_id) do
Repo.get_by(User, ap_id: ap_id)
end
@@ -320,11 +324,20 @@ defmodule Pleroma.User do
Cachex.fetch!(:user_cache, key, fn _ -> get_by_ap_id(ap_id) end)
end
+ def get_cached_by_id(id) do
+ key = "id:#{id}"
+ Cachex.fetch!(:user_cache, key, fn _ -> get_by_id(id) end)
+ end
+
def get_cached_by_nickname(nickname) do
key = "nickname:#{nickname}"
Cachex.fetch!(:user_cache, key, fn _ -> get_or_fetch_by_nickname(nickname) end)
end
+ def get_cached_by_nickname_or_id(nickname_or_id) do
+ get_cached_by_nickname(nickname_or_id) || get_cached_by_id(nickname_or_id)
+ end
+
def get_by_nickname(nickname) do
Repo.get_by(User, nickname: nickname)
end
diff --git a/lib/pleroma/web/ostatus/metadata.ex b/lib/pleroma/web/ostatus/metadata.ex
new file mode 100644
index 000000000..120a89a8b
--- /dev/null
+++ b/lib/pleroma/web/ostatus/metadata.ex
@@ -0,0 +1,74 @@
+defmodule Pleroma.Web.Metadata do
+ alias Phoenix.HTML
+ alias Pleroma.{Formatter, User}
+ alias Pleroma.Web.MediaProxy
+
+ def build_tags(params) do
+ if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
+ |> Enum.map(&to_tag/1)
+ |> Enum.map(&HTML.safe_to_string/1)
+ |> Enum.join("\n")
+ end
+
+ def meta_enabled?(type) do
+ Pleroma.Config.get([:metadata, type], false)
+ end
+
+ # opengraph for single status
+ defp opengraph_tags(%{activity: activity, user: user}) do
+ with truncated_content = Formatter.truncate(activity.data["object"]["content"]) do
+ [
+ {:meta,
+ [
+ property: "og:title",
+ content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
+ ], []},
+ {:meta, [property: "og:url", content: activity.data["id"]], []},
+ {:meta, [property: "og:description", content: truncated_content], []},
+ {:meta, [property: "og:image", content: user_avatar_url(user)], []},
+ {:meta, [property: "og:image:width", content: 120], []},
+ {:meta, [property: "og:image:height", content: 120], []},
+ {:meta, [property: "twitter:card", content: "summary"], []}
+ ]
+ end
+ end
+
+ # opengraph for user card
+ defp opengraph_tags(%{user: user}) do
+ with truncated_bio = Formatter.truncate(user.bio) do
+ [
+ {:meta,
+ [
+ property: "og:title",
+ content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
+ ], []},
+ {:meta, [property: "og:url", content: User.profile_url(user)], []},
+ {:meta, [property: "og:description", content: truncated_bio], []},
+ {:meta, [property: "og:image", content: user_avatar_url(user)], []},
+ {:meta, [property: "og:image:width", content: 120], []},
+ {:meta, [property: "og:image:height", content: 120], []},
+ {:meta, [property: "twitter:card", content: "summary"], []}
+ ]
+ end
+ 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 user_avatar_url(user) do
+ User.avatar_url(user) |> MediaProxy.url()
+ end
+
+ def pleroma_domain do
+ Pleroma.Web.Endpoint.host()
+ end
+end
diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex
index 9dfcf0f95..5dbee20e1 100644
--- a/lib/pleroma/web/ostatus/ostatus_controller.ex
+++ b/lib/pleroma/web/ostatus/ostatus_controller.ex
@@ -16,7 +16,11 @@ defmodule Pleroma.Web.OStatus.OStatusController do
def feed_redirect(conn, %{"nickname" => nickname}) do
case get_format(conn) do
"html" ->
- Fallback.RedirectController.redirector(conn, nil)
+ with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
+ Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
+ else
+ nil -> {:error, :not_found}
+ end
"activity+json" ->
ActivityPubController.call(conn, :user)
@@ -134,9 +138,7 @@ 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" ->
- conn
- |> put_resp_content_type("text/html")
- |> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html"))
+ Fallback.RedirectController.redirector_with_meta(conn, %{activity: activity, user: user})
_ ->
represent_activity(conn, format, activity, user)
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 6253a28db..751624e80 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -361,7 +361,11 @@ defmodule Pleroma.Web.Router do
end
pipeline :ostatus do
- plug(:accepts, ["xml", "atom", "html", "activity+json"])
+ plug(:accepts, ["html", "xml", "atom", "activity+json"])
+ end
+
+ pipeline :oembed do
+ plug(:accepts, ["json", "xml"])
end
scope "/", Pleroma.Web do
@@ -379,6 +383,12 @@ defmodule Pleroma.Web.Router do
post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
end
+ scope "/", Pleroma.Web do
+ pipe_through(:oembed)
+
+ get("/oembed", OEmbed.OEmbedController, :url)
+ end
+
pipeline :activitypub do
plug(:accepts, ["activity+json"])
plug(Pleroma.Web.Plugs.HTTPSignaturePlug)
@@ -452,11 +462,26 @@ end
defmodule Fallback.RedirectController do
use Pleroma.Web, :controller
+ alias Pleroma.Web.Metadata
def redirector(conn, _params) do
conn
|> put_resp_content_type("text/html")
- |> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html"))
+ |> send_file(200, index_file_path())
+ end
+
+ def redirector_with_meta(conn, params) do
+ {:ok, index_content} = File.read(index_file_path())
+ tags = Metadata.build_tags(params)
+ response = String.replace(index_content, "<!--server-generated-meta-->", tags)
+
+ conn
+ |> put_resp_content_type("text/html")
+ |> send_resp(200, response)
+ end
+
+ def index_file_path do
+ Application.app_dir(:pleroma, "priv/static/index.html")
end
def registration_page(conn, params) do
diff --git a/priv/static/index.html b/priv/static/index.html
index aad8f1697..5958e16b1 100644
--- a/priv/static/index.html
+++ b/priv/static/index.html
@@ -1 +1 @@
-<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Pleroma</title><link rel=icon type=image/png href=/favicon.png><link rel=stylesheet href=/static/font/css/fontello.css><link rel=stylesheet href=/static/font/css/animation.css><link href=/static/css/app.ea00efb3229c8591fcc5249f0241b986.css rel=stylesheet></head><body style="display: none"><div id=app></div><script type=text/javascript src=/static/js/manifest.e076977b8e6c6844fb00.js></script><script type=text/javascript src=/static/js/vendor.cc4190750f6ed4d697bd.js></script><script type=text/javascript src=/static/js/app.67f548ecb9e9fd6b25b0.js></script></body></html> \ No newline at end of file
+<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Pleroma</title><!--server-generated-meta--><link rel=icon type=image/png href=/favicon.png><link rel=stylesheet href=/static/font/css/fontello.css><link rel=stylesheet href=/static/font/css/animation.css><link href=/static/css/app.ea00efb3229c8591fcc5249f0241b986.css rel=stylesheet></head><body style="display: none"><div id=app></div><script type=text/javascript src=/static/js/manifest.e076977b8e6c6844fb00.js></script><script type=text/javascript src=/static/js/vendor.cc4190750f6ed4d697bd.js></script><script type=text/javascript src=/static/js/app.67f548ecb9e9fd6b25b0.js></script></body></html>
diff --git a/test/web/ostatus/ostatus_controller_test.exs b/test/web/ostatus/ostatus_controller_test.exs
index 560305c15..e9e9bdb16 100644
--- a/test/web/ostatus/ostatus_controller_test.exs
+++ b/test/web/ostatus/ostatus_controller_test.exs
@@ -84,6 +84,7 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
conn =
conn
+ |> put_req_header("accept", "application/xml")
|> get(url)
expected =
@@ -110,11 +111,12 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
|> response(404)
end
- test "gets an activity", %{conn: conn} do
+ test "gets an activity in xml format", %{conn: conn} do
note_activity = insert(:note_activity)
[_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
conn
+ |> put_req_header("accept", "application/xml")
|> get("/activities/#{uuid}")
|> response(200)
end
@@ -134,7 +136,20 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
|> response(404)
end
- test "gets a notice", %{conn: conn} do
+ test "renders notice metatags in html format", %{conn: conn} do
+ note_activity = insert(:note_activity)
+ conn = get(conn, "/notice/#{note_activity.id}")
+ body = html_response(conn, 200)
+ twitter_card_summary = "<meta content=\"summary\" property=\"twitter:card\">"
+
+ description_content =
+ "<meta content=\"#{note_activity.data["object"]["content"]}\" property=\"og:description\">"
+
+ assert body =~ twitter_card_summary
+ assert body =~ description_content
+ end
+
+ test "gets a notice in xml format", %{conn: conn} do
note_activity = insert(:note_activity)
conn