WIP. Implement oembed route and handle both json/xml for "Note" type activity

This commit is contained in:
raeno 2018-12-10 23:08:02 +04:00
parent 21afdf6d99
commit 8902942128
9 changed files with 142 additions and 5 deletions

View File

@ -163,4 +163,20 @@ 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

View File

@ -0,0 +1,25 @@
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

@ -0,0 +1,23 @@
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,11 +1,31 @@
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" => uri} ) do
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(%{ status: "success"} )
|> 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

@ -0,0 +1,21 @@
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

@ -31,7 +31,7 @@ defmodule Pleroma.Web.OStatus do
def oembed_links(url) do
Enum.map(["xml", "json"], fn format ->
href = oembed_path(url, format)
"<link rel=\"alternate\" type=\"application/#{format}+oembed\" href=\"#{href}\""
"<link rel=\"alternate\" type=\"application/#{format}+oembed\" href=\"#{href}\">"
end)
|> Enum.join("\r\n")
end

View File

@ -9,7 +9,6 @@ defmodule Pleroma.Web.OStatus.OStatusController do
alias Pleroma.Web.ActivityPub.ObjectView
alias Pleroma.Web.ActivityPub.ActivityPubController
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Router.Helpers, as: Routes
plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
action_fallback(:errors)

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
<version>1.0</version>
<type>link</type>
<author_name>raeno</author_name>
<author_url>http://iamcal.com/</author_url>
<cache_age>86400</cache_age>
<provider_name>iamcal.com</provider_name>
<provider_url>http://iamcal.com/</provider_url>
</oembed>

View File

@ -0,0 +1,23 @@
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