Include one scrobble with account

This commit is contained in:
Mark Felder 2024-05-16 13:44:38 -04:00 committed by Moon Man
parent 1750164b2b
commit 43f194c434
4 changed files with 33 additions and 12 deletions

View File

@ -0,0 +1 @@
Includes scrobble when available in the account field with statuses

View File

@ -9,9 +9,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
alias Pleroma.User alias Pleroma.User
alias Pleroma.UserNote alias Pleroma.UserNote
alias Pleroma.UserRelationship alias Pleroma.UserRelationship
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MediaProxy alias Pleroma.Web.MediaProxy
alias Pleroma.Web.PleromaAPI.ScrobbleView
def render("index.json", %{users: users} = opts) do def render("index.json", %{users: users} = opts) do
reading_user = opts[:for] 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 &&
user.last_status_at |> NaiveDateTime.to_date() |> Date.to_iso8601() 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), id: to_string(user.id),
username: username_from_nickname(user.nickname), 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, skip_thread_containment: user.skip_thread_containment,
background_image: image_url(user.background) |> MediaProxy.url(), background_image: image_url(user.background) |> MediaProxy.url(),
accepts_chat_messages: user.accepts_chat_messages, 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]) |> maybe_put_role(user, opts[:for])

View File

@ -14,15 +14,27 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleView do
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do def render("show.json", %{scrobble: %Activity{data: %{"type" => "Listen"}} = scrobble} = _opts) do
object = Object.normalize(activity, fetch: false) scrobble_schema_skeleton(scrobble)
end
def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do
user = CommonAPI.get_user(activity.data["actor"]) 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, id: scrobble.id,
account: AccountView.render("show.json", %{user: user, for: opts[:for]}),
created_at: created_at, created_at: created_at,
title: object.data["title"] |> HTML.strip_tags(), title: object.data["title"] |> HTML.strip_tags(),
artist: object.data["artist"] |> 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"] length: object.data["length"]
} }
end end
def render("index.json", opts) do
safe_render_many(opts.activities, __MODULE__, "show.json", opts)
end
end end

View File

@ -96,7 +96,8 @@ test "Represent a user account" do
hide_follows_count: false, hide_follows_count: false,
relationship: %{}, relationship: %{},
skip_thread_containment: false, 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, hide_follows_count: false,
relationship: %{}, relationship: %{},
skip_thread_containment: false, skip_thread_containment: false,
accepts_chat_messages: nil accepts_chat_messages: nil,
scrobbles: []
} }
} }