From 43f194c43436186b4ee3a61ca5a7562631983b82 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 16 May 2024 13:44:38 -0400 Subject: [PATCH] Include one scrobble with account --- .../include-scrobbles-with-account.add | 1 + .../web/mastodon_api/views/account_view.ex | 12 ++++++++- .../web/pleroma_api/views/scrobble_view.ex | 26 ++++++++++++------- .../mastodon_api/views/account_view_test.exs | 6 +++-- 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 changelog.d/include-scrobbles-with-account.add diff --git a/changelog.d/include-scrobbles-with-account.add b/changelog.d/include-scrobbles-with-account.add new file mode 100644 index 000000000..1102b362f --- /dev/null +++ b/changelog.d/include-scrobbles-with-account.add @@ -0,0 +1 @@ +Includes scrobble when available in the account field with statuses diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 267c3e3ed..6586dc028 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -9,9 +9,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do alias Pleroma.User alias Pleroma.UserNote alias Pleroma.UserRelationship + alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MediaProxy + alias Pleroma.Web.PleromaAPI.ScrobbleView def render("index.json", %{users: users} = opts) do reading_user = opts[:for] @@ -255,6 +257,13 @@ defp do_render("show.json", %{user: user} = opts) do user.last_status_at && user.last_status_at |> NaiveDateTime.to_date() |> Date.to_iso8601() + scrobbles = + ActivityPub.fetch_user_abstract_activities(user, opts[:for], %{ + type: "Listen", + limit: 1, + id: user.id + }) + %{ id: to_string(user.id), username: username_from_nickname(user.nickname), @@ -303,7 +312,8 @@ defp do_render("show.json", %{user: user} = opts) do skip_thread_containment: user.skip_thread_containment, background_image: image_url(user.background) |> MediaProxy.url(), accepts_chat_messages: user.accepts_chat_messages, - favicon: favicon + favicon: favicon, + scrobbles: safe_render_many(scrobbles, ScrobbleView, "show.json") } } |> maybe_put_role(user, opts[:for]) diff --git a/lib/pleroma/web/pleroma_api/views/scrobble_view.ex b/lib/pleroma/web/pleroma_api/views/scrobble_view.ex index edf0a2390..90dca4288 100644 --- a/lib/pleroma/web/pleroma_api/views/scrobble_view.ex +++ b/lib/pleroma/web/pleroma_api/views/scrobble_view.ex @@ -14,15 +14,27 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleView do alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.MastodonAPI.AccountView - def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do - object = Object.normalize(activity, fetch: false) + def render("show.json", %{scrobble: %Activity{data: %{"type" => "Listen"}} = scrobble} = _opts) do + scrobble_schema_skeleton(scrobble) + end + def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do user = CommonAPI.get_user(activity.data["actor"]) - created_at = Utils.to_masto_date(activity.data["published"]) + + scrobble_schema_skeleton(activity) + |> Map.put(:account, AccountView.render("show.json", %{user: user, for: opts[:for]})) + end + + def render("index.json", opts) do + safe_render_many(opts.activities, __MODULE__, "show.json", opts) + end + + defp scrobble_schema_skeleton(%Activity{data: %{"type" => "Listen"}} = scrobble) do + object = Object.normalize(scrobble, fetch: false) + created_at = Utils.to_masto_date(scrobble.data["published"]) %{ - id: activity.id, - account: AccountView.render("show.json", %{user: user, for: opts[:for]}), + id: scrobble.id, created_at: created_at, title: object.data["title"] |> HTML.strip_tags(), artist: object.data["artist"] |> HTML.strip_tags(), @@ -31,8 +43,4 @@ def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = act length: object.data["length"] } end - - def render("index.json", opts) do - safe_render_many(opts.activities, __MODULE__, "show.json", opts) - end end diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index 8dcdaf447..e474af5ef 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -96,7 +96,8 @@ test "Represent a user account" do hide_follows_count: false, relationship: %{}, skip_thread_containment: false, - accepts_chat_messages: nil + accepts_chat_messages: nil, + scrobbles: [] } } @@ -340,7 +341,8 @@ test "Represent a Service(bot) account" do hide_follows_count: false, relationship: %{}, skip_thread_containment: false, - accepts_chat_messages: nil + accepts_chat_messages: nil, + scrobbles: [] } }