From 8e42251e064b4de6f1d767f28c79d64ca74dc245 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 28 Jan 2019 06:04:54 +0000 Subject: [PATCH] rich media: add helpers module, use instead of MastodonAPI module --- lib/pleroma/web/mastodon_api/mastodon_api.ex | 19 ------------------- .../mastodon_api/mastodon_api_controller.ex | 16 ++++++++++++---- .../web/mastodon_api/views/status_view.ex | 3 +-- lib/pleroma/web/rich_media/helpers.ex | 18 ++++++++++++++++++ .../representers/activity_representer.ex | 7 +++++-- .../web/twitter_api/views/activity_view.ex | 7 +++++-- 6 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 lib/pleroma/web/rich_media/helpers.ex diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index 9d1fb22ea..8b1378917 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -1,20 +1 @@ -# Pleroma: A lightweight social networking server -# Copyright _ 2017-2019 Pleroma Authors -# 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 diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index b5231a326..65b612026 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index ccc954985..68df1623e 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -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 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do __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), diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex new file mode 100644 index 000000000..e90e35180 --- /dev/null +++ b/lib/pleroma/web/rich_media/helpers.ex @@ -0,0 +1,18 @@ +# Pleroma: A lightweight social networking server +# Copyright _ 2017-2019 Pleroma Authors +# 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 diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 364aa7af3..c4025cbd7 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -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 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do 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, diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 77e9af348..d0d1221c3 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -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 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do 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,