rich media: add helpers module, use instead of MastodonAPI module

This commit is contained in:
William Pitcock 2019-01-28 06:04:54 +00:00
parent 24a103a1fe
commit 8e42251e06
6 changed files with 41 additions and 29 deletions

View file

@ -1,20 +1 @@
# 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

View file

@ -7,8 +7,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
alias Pleroma.Web
alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MastodonAPI.{
StatusView,
AccountView,
@ -1347,9 +1345,19 @@ def suggestions(%{assigns: %{user: user}} = conn, _) do
end
def status_card(conn, %{"id" => status_id}) do
data = StatusView.render("card.json", MastodonAPI.get_status_card(status_id))
with %Activity{} = activity <- Repo.get(Activity, status_id),
true <- ActivityPub.is_public?(activity) do
data =
StatusView.render(
"card.json",
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
)
json(conn, data)
json(conn, data)
else
_e ->
%{}
end
end
def try_render(conn, target, params)

View file

@ -11,7 +11,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
alias Pleroma.User
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MediaProxy
alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.StatusView
@ -141,7 +140,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
__MODULE__
)
card = render("card.json", MastodonAPI.get_status_card(activity.id))
card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
%{
id: to_string(activity.id),

View file

@ -0,0 +1,18 @@
# Pleroma: A lightweight social networking server
# Copyright _ 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Helpers do
alias Pleroma.{Repo, Activity, Object, HTML}
alias Pleroma.Web.RichMedia.Parser
def fetch_data_for_activity(%Activity{} = activity) do
with %Object{} = object <- Object.normalize(activity.data["object"]),
page_url <- HTML.extract_first_external_url(object, object.data["content"]),
{:ok, rich_media} <- Parser.parse(page_url) do
%{page_url: page_url, rich_media: rich_media}
else
_ -> %{}
end
end
end

View file

@ -12,7 +12,6 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Formatter
alias Pleroma.HTML
alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MastodonAPI.StatusView
defp user_by_ap_id(user_list, ap_id) do
@ -188,7 +187,11 @@ def to_map(
summary = HTML.strip_tags(object["summary"])
card = StatusView.render("card.json", MastodonAPI.get_status_card(activity.id))
card =
StatusView.render(
"card.json",
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
)
%{
"id" => activity.id,

View file

@ -10,7 +10,6 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
alias Pleroma.Web.TwitterAPI.ActivityView
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Activity
alias Pleroma.HTML
@ -276,7 +275,11 @@ def render(
summary = HTML.strip_tags(summary)
card = StatusView.render("card.json", MastodonAPI.get_status_card(activity.id))
card =
StatusView.render(
"card.json",
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
)
%{
"id" => activity.id,