+# Pleroma: A lightweight social networking server
+# Copyright _ 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+defmodule Pleroma.Web.MastodonAPI do
+ alias Pleroma.{Repo, Activity, Object, HTML}
+ alias Pleroma.Web.ActivityPub.ActivityPub
+
+ def get_status_card(status_id) do
+ with %Activity{} = activity <- Repo.get(Activity, status_id),
+ true <- ActivityPub.is_public?(activity),
+ %Object{} = object <- Object.normalize(activity.data["object"]),
+ page_url <- HTML.extract_first_external_url(object, object.data["content"]),
+ {:ok, rich_media} <- Pleroma.Web.RichMedia.Parser.parse(page_url) do
+ %{page_url: page_url, rich_media: rich_media}
+ else
+ _ -> %{}
+ end
+ end
+end
use Pleroma.Web, :controller
alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
alias Pleroma.Web
- alias Pleroma.HTML
+ alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MastodonAPI.{
StatusView,
AccountView,
end
end
- def get_status_card(status_id) do
- with %Activity{} = activity <- Repo.get(Activity, status_id),
- true <- ActivityPub.is_public?(activity),
- %Object{} = object <- Object.normalize(activity.data["object"]),
- page_url <- HTML.extract_first_external_url(object, object.data["content"]),
- {:ok, rich_media} <- Pleroma.Web.RichMedia.Parser.parse(page_url) do
- page_url = rich_media[:url] || page_url
- site_name = rich_media[:site_name] || URI.parse(page_url).host
-
- rich_media
- |> Map.take([:image, :title, :description])
- |> Map.put(:type, "link")
- |> Map.put(:provider_name, site_name)
- |> Map.put(:url, page_url)
- else
- _ -> %{}
- end
- end
-
def status_card(conn, %{"id" => status_id}) do
- json(conn, get_status_card(status_id))
+ data = StatusView.render("card.json", MastodonAPI.get_status_card(status_id))
+
+ json(conn, data)
end
def try_render(conn, target, params)
nil
end
+ def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
+ page_url = rich_media[:url] || page_url
+ page_url_data = URI.parse(page_url)
+ site_name = rich_media[:site_name] || page_url_data.host
+
+ %{
+ type: "link",
+ provider_name: site_name,
+ provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
+ url: page_url,
+ image: rich_media[:image],
+ title: rich_media[:title],
+ description: rich_media[:description],
+ pleroma: %{
+ opengraph: rich_media
+ }
+ }
+ end
+
+ def render("card.json", _) do
+ nil
+ end
+
def render("attachment.json", %{attachment: attachment}) do
[attachment_url | _] = attachment["url"]
media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
assert response == %{
"image" => "http://ia.media-imdb.com/images/rock.jpg",
"provider_name" => "www.imdb.com",
+ "provider_url" => "http://www.imdb.com",
"title" => "The Rock",
"type" => "link",
- "url" => "http://www.imdb.com/title/tt0117500/"
+ "url" => "http://www.imdb.com/title/tt0117500/",
+ "description" => nil,
+ "pleroma" => %{
+ "opengraph" => %{
+ "image" => "http://ia.media-imdb.com/images/rock.jpg",
+ "title" => "The Rock",
+ "type" => "video.movie",
+ "url" => "http://www.imdb.com/title/tt0117500/"
+ }
+ }
}
end
end