Web.MastodonAPI.MastodonAPIController: Add Rich-Media support
This commit is contained in:
parent
d9f3af477d
commit
3f64379b13
3 changed files with 40 additions and 1 deletions
|
@ -1322,6 +1322,29 @@ def suggestions(%{assigns: %{user: user}} = conn, _) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp status_first_external_url(content) do
|
||||||
|
content
|
||||||
|
|> Floki.filter_out("a.mention")
|
||||||
|
|> Floki.attribute("a", "href")
|
||||||
|
|> Enum.at(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def status_card(conn, %{"id" => status_id}) do
|
||||||
|
with %Activity{} = activity <- Repo.get(Activity, status_id),
|
||||||
|
true <- ActivityPub.is_public?(activity),
|
||||||
|
page_url <- status_first_external_url(activity.data["object"]["content"]),
|
||||||
|
{:ok, rich_media} <- Pleroma.Web.RichMedia.Parser.parse(page_url) do
|
||||||
|
card =
|
||||||
|
rich_media
|
||||||
|
|> Map.take([:image, :title, :url, :description])
|
||||||
|
|> Map.put(:type, "link")
|
||||||
|
|
||||||
|
json(conn, card)
|
||||||
|
else
|
||||||
|
_ -> json(conn, %{})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def try_render(conn, target, params)
|
def try_render(conn, target, params)
|
||||||
when is_binary(target) do
|
when is_binary(target) do
|
||||||
res = render(conn, target, params)
|
res = render(conn, target, params)
|
||||||
|
|
|
@ -258,7 +258,7 @@ defmodule Pleroma.Web.Router do
|
||||||
|
|
||||||
get("/statuses/:id", MastodonAPIController, :get_status)
|
get("/statuses/:id", MastodonAPIController, :get_status)
|
||||||
get("/statuses/:id/context", MastodonAPIController, :get_context)
|
get("/statuses/:id/context", MastodonAPIController, :get_context)
|
||||||
get("/statuses/:id/card", MastodonAPIController, :empty_object)
|
get("/statuses/:id/card", MastodonAPIController, :status_card)
|
||||||
get("/statuses/:id/favourited_by", MastodonAPIController, :favourited_by)
|
get("/statuses/:id/favourited_by", MastodonAPIController, :favourited_by)
|
||||||
get("/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by)
|
get("/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by)
|
||||||
|
|
||||||
|
|
|
@ -1623,5 +1623,21 @@ test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
|
||||||
|> post("/api/v1/statuses/#{activity_two.id}/pin")
|
|> post("/api/v1/statuses/#{activity_two.id}/pin")
|
||||||
|> json_response(400)
|
|> json_response(400)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "Status rich-media Card", %{conn: conn, user: user} do
|
||||||
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "http://example.com/ogp"})
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/statuses/#{activity.id}/card")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert response == %{
|
||||||
|
"image" => "http://ia.media-imdb.com/images/rock.jpg",
|
||||||
|
"title" => "The Rock",
|
||||||
|
"type" => "link",
|
||||||
|
"url" => "http://www.imdb.com/title/tt0117500/"
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue