aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma')
-rw-r--r--lib/pleroma/html.ex14
-rw-r--r--lib/pleroma/notification.ex6
-rw-r--r--lib/pleroma/plugs/oauth_plug.ex7
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex2
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex24
-rw-r--r--lib/pleroma/web/router.ex2
6 files changed, 50 insertions, 5 deletions
diff --git a/lib/pleroma/html.ex b/lib/pleroma/html.ex
index f5c6e5033..fb602d6b6 100644
--- a/lib/pleroma/html.ex
+++ b/lib/pleroma/html.ex
@@ -58,6 +58,20 @@ defmodule Pleroma.HTML do
"#{signature}#{to_string(scrubber)}"
end)
end
+
+ def extract_first_external_url(object, content) do
+ key = "URL|#{object.id}"
+
+ Cachex.fetch!(:scrubber_cache, key, fn _key ->
+ result =
+ content
+ |> Floki.filter_out("a.mention")
+ |> Floki.attribute("a", "href")
+ |> Enum.at(0)
+
+ {:commit, result}
+ end)
+ end
end
defmodule Pleroma.HTML.Scrubber.TwitterText do
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index e47145601..2364d36da 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -35,7 +35,8 @@ defmodule Pleroma.Notification do
n in Notification,
where: n.user_id == ^user.id,
order_by: [desc: n.id],
- preload: [:activity],
+ join: activity in assoc(n, :activity),
+ preload: [activity: activity],
limit: 20
)
@@ -66,7 +67,8 @@ defmodule Pleroma.Notification do
from(
n in Notification,
where: n.id == ^id,
- preload: [:activity]
+ join: activity in assoc(n, :activity),
+ preload: [activity: activity]
)
notification = Repo.one(query)
diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex
index 437aa95b3..945a1d49f 100644
--- a/lib/pleroma/plugs/oauth_plug.ex
+++ b/lib/pleroma/plugs/oauth_plug.ex
@@ -33,7 +33,12 @@ defmodule Pleroma.Plugs.OAuthPlug do
#
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
defp fetch_user_and_token(token) do
- query = from(q in Token, where: q.token == ^token, preload: [:user])
+ query =
+ from(t in Token,
+ where: t.token == ^token,
+ join: user in assoc(t, :user),
+ preload: [user: user]
+ )
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
{:ok, user, token_record}
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index 6b4682e35..07779fa00 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -64,7 +64,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end
end
- defp check_remote_limit(%{"object" => %{"content" => content}}) do
+ defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
limit = Pleroma.Config.get([:instance, :remote_limit])
String.length(content) <= limit
end
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index f4736fcb5..a60532b55 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
use Pleroma.Web, :controller
alias Pleroma.{Repo, Object, Activity, User, Notification, Stats}
alias Pleroma.Web
+ alias Pleroma.HTML
alias Pleroma.Web.MastodonAPI.{
StatusView,
@@ -1322,6 +1323,29 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
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))
+ end
+
def try_render(conn, target, params)
when is_binary(target) do
res = render(conn, target, params)
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index b83790858..e749aa834 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -258,7 +258,7 @@ defmodule Pleroma.Web.Router do
get("/statuses/:id", MastodonAPIController, :get_status)
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/reblogged_by", MastodonAPIController, :reblogged_by)