Remove oembed for now, will submit it in another MR. Fix warnings

This commit is contained in:
raeno 2018-12-13 22:16:54 +01:00
parent 018516d3f3
commit b5de7c4c4d
11 changed files with 22 additions and 164 deletions

View file

@ -171,9 +171,7 @@
ip: {0, 0, 0, 0}, ip: {0, 0, 0, 0},
port: 9999 port: 9999
config :pleroma, :metadata, config :pleroma, :metadata, opengraph: true
oembed: false,
opengraph: true
config :pleroma, :suggestions, config :pleroma, :suggestions,
enabled: false, enabled: false,

View file

@ -165,14 +165,16 @@ def finalize({subs, text}) do
end end
def truncate(text, opts \\ []) do def truncate(text, opts \\ []) do
max_length = opts[:max_length] || 200 max_length = opts[:max_length] || 200
omission = opts[:omission] || "..." omission = opts[:omission] || "..."
cond do cond do
not String.valid?(text) -> not String.valid?(text) ->
text text
String.length(text) < max_length -> String.length(text) < max_length ->
text text
true -> true ->
length_with_omission = max_length - String.length(omission) length_with_omission = max_length - String.length(omission)

View file

@ -1,25 +0,0 @@
defmodule Pleroma.Web.OEmbed.ActivityRepresenter do
alias Pleroma.{Activity, User, Object}
alias Pleroma.Web.OStatus.UserRepresenter
def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
h = fn str -> [to_charlist(str)] end
content = if activity.data["object"]["content"] do
[{:content, [], h.(activity.data["object"]["content"])}]
else
[]
end
[
{:version, ["1.0"]},
{:type, ["link"]},
] ++ content
end
def wrap_with_entry(simple_form) do
[ { :oembed, [], simple_form } ]
end
end

View file

@ -1,23 +0,0 @@
defmodule Pleroma.Web.OEmbed do
alias Pleroma.{Repo, Object, Activity, User}
alias Pleroma.Formatter
def recognize_path(url) do
details = Regex.named_captures(~r/.+\/(?<route>.+)\/(?<id>\w+).*$/, url)
case details do
%{ "route" => "notice", "id" => id } ->
%{type: :activity, entity: Repo.get(Activity, id) }
%{ "route" => "users", "id" => nickname } ->
%{type: :user, entity: User.get_by_nickname(nickname) }
_ ->
{ :error, "no matching route"}
end
end
def truncated_content(activity) do
content = activity.data['object']['content']
IO.puts(content)
Formatter.truncate(content)
end
end

View file

@ -1,31 +0,0 @@
defmodule Pleroma.Web.OEmbed.OEmbedController do
use Pleroma.Web, :controller
alias Pleroma.Web.OEmbed
alias Pleroma.Web.OEmbed.{NoteView, ActivityRepresenter}
alias Pleroma.Web.MediaProxy
alias Pleroma.Repo
alias Pleroma.User
def url(conn, %{ "url" => url} ) do
case format = get_format(conn) do
_ ->
result = OEmbed.recognize_path(url)
render_oembed(conn, format, result)
end
end
def render_oembed(conn, format \\ "json", result)
def render_oembed(conn, "json", result) do
conn
|> put_resp_content_type("application/json")
|> json(NoteView.render("note.json", result))
end
def render_oembed(conn, "xml", result) do
conn
|> put_resp_content_type("application/xml")
|> NoteView.render("note.json", result)
end
end

View file

@ -1,21 +0,0 @@
defmodule Pleroma.Web.OEmbed.NoteView do
use Pleroma.Web, :view
alias Pleroma.{User, Activity}
alias Pleroma.Web.OEmbed
def render("note.json", %{type: type, entity: activity }) do
oembed_data(activity)
end
def oembed_data(activity) do
with %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]),
image = User.avatar_url(user) |> MediaProxy.url() do
%{
version: "1.0",
type: "link",
title: OEmbed.truncated_content(activity),
provider_url: "https://pleroma.site",
thumbnail_url: image,
}
end
end

View file

@ -1,14 +1,10 @@
defmodule Pleroma.Web.Metadata do defmodule Pleroma.Web.Metadata do
alias Phoenix.HTML alias Phoenix.HTML
alias Pleroma.{Web, Formatter} alias Pleroma.{Formatter, User}
alias Pleroma.{User, Activity}
alias Pleroma.Web.MediaProxy alias Pleroma.Web.MediaProxy
def build_tags(request_url, params) do def build_tags(params) do
Enum.concat([ if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: []),
if(meta_enabled?(:oembed), do: oembed_links(request_url), else: [])
])
|> Enum.map(&to_tag/1) |> Enum.map(&to_tag/1)
|> Enum.map(&HTML.safe_to_string/1) |> Enum.map(&HTML.safe_to_string/1)
|> Enum.join("\n") |> Enum.join("\n")
@ -24,13 +20,12 @@ defp opengraph_tags(%{activity: activity, user: user}) do
with truncated_content = Formatter.truncate(activity.data["object"]["content"]) do with truncated_content = Formatter.truncate(activity.data["object"]["content"]) do
[ [
{:meta, {:meta,
[ [
property: "og:title", property: "og:title",
content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}" content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
], []}, ], []},
{:meta, [property: "og:url", content: activity.data["id"]], []}, {:meta, [property: "og:url", content: activity.data["id"]], []},
{:meta, [property: "og:description", content: truncated_content], {:meta, [property: "og:description", content: truncated_content], []},
[]},
{:meta, [property: "og:image", content: user_avatar_url(user)], []}, {:meta, [property: "og:image", content: user_avatar_url(user)], []},
{:meta, [property: "og:image:width", content: 120], []}, {:meta, [property: "og:image:width", content: 120], []},
{:meta, [property: "og:image:height", content: 120], []}, {:meta, [property: "og:image:height", content: 120], []},
@ -44,10 +39,10 @@ defp opengraph_tags(%{user: user}) do
with truncated_bio = Formatter.truncate(user.bio) do with truncated_bio = Formatter.truncate(user.bio) do
[ [
{:meta, {:meta,
[ [
property: "og:title", property: "og:title",
content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile" content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
], []}, ], []},
{:meta, [property: "og:url", content: User.profile_url(user)], []}, {:meta, [property: "og:url", content: User.profile_url(user)], []},
{:meta, [property: "og:description", content: truncated_bio], []}, {:meta, [property: "og:description", content: truncated_bio], []},
{:meta, [property: "og:image", content: user_avatar_url(user)], []}, {:meta, [property: "og:image", content: user_avatar_url(user)], []},
@ -58,13 +53,6 @@ defp opengraph_tags(%{user: user}) do
end end
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
def to_tag(data) do def to_tag(data) do
with {name, attrs, _content = []} <- data do with {name, attrs, _content = []} <- data do
HTML.Tag.tag(name, attrs) HTML.Tag.tag(name, attrs)
@ -77,11 +65,6 @@ def to_tag(data) do
end end
end end
defp oembed_path(url, format) do
query = URI.encode_query(%{url: url, format: format})
"#{Web.base_url()}/oembed?#{query}"
end
defp user_avatar_url(user) do defp user_avatar_url(user) do
User.avatar_url(user) |> MediaProxy.url() User.avatar_url(user) |> MediaProxy.url()
end end

View file

@ -5,7 +5,7 @@ defmodule Pleroma.Web.OStatus do
import Pleroma.Web.XML import Pleroma.Web.XML
require Logger require Logger
alias Pleroma.{Repo, User, Web, Object, Activity, Formatter} alias Pleroma.{Repo, User, Web, Object, Activity}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.{WebFinger, Websub} alias Pleroma.Web.{WebFinger, Websub}
alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler} alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler}

View file

@ -14,9 +14,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
action_fallback(:errors) action_fallback(:errors)
def feed_redirect(conn, %{"nickname" => nickname}) do def feed_redirect(conn, %{"nickname" => nickname}) do
format = get_format(conn) case get_format(conn) do
IO.puts(format)
case format do
"html" -> "html" ->
with %User{} = user <- User.get_cached_by_nickname(nickname) do with %User{} = user <- User.get_cached_by_nickname(nickname) do
Fallback.RedirectController.redirector_with_meta(conn, %{user: user}) Fallback.RedirectController.redirector_with_meta(conn, %{user: user})

View file

@ -453,8 +453,8 @@ def redirector(conn, _params) do
end end
def redirector_with_meta(conn, params) do def redirector_with_meta(conn, params) do
{:ok, index_content } = File.read(Application.app_dir(:pleroma, "priv/static/index.html")) {:ok, index_content} = File.read(Application.app_dir(:pleroma, "priv/static/index.html"))
tags = Metadata.build_tags(request_url(conn), params) tags = Metadata.build_tags(params)
response = String.replace(index_content, "<!--server-generated-meta-->", tags) response = String.replace(index_content, "<!--server-generated-meta-->", tags)
conn conn

View file

@ -1,23 +0,0 @@
defmodule Pleroma.Web.OEmbedTest do
use Pleroma.DataCase
alias Pleroma.Web.OEmbed
alias Pleroma.Web.XML
alias Pleroma.{Object, Repo, User, Activity}
import Pleroma.Factory
import ExUnit.CaptureLog
setup_all do
:ok
end
test 'recognizes notices in given url' do
url = "https://pleroma.site/notice/5"
assert { :activity, _ } = OEmbed.recognize_path(url)
end
test 'recognizes user card in given url' do
url = "https://pleroma.site/users/raeno"
assert { :user, _ } = OEmbed.recognize_path(url)
end
end