Include scrobbles with account

This commit is contained in:
NEETzsche 2023-11-10 11:49:31 -07:00
parent a51f3937ee
commit afcb38569a
3 changed files with 29 additions and 10 deletions

View file

@ -0,0 +1 @@
Includes five most scrobbles, when available, in the #account field with statuses

View file

@ -9,8 +9,10 @@ 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.PleromaAPI.ScrobbleView
alias Pleroma.Web.MediaProxy
def render("index.json", %{users: users} = opts) do
@ -249,6 +251,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
nil
end
scrobbles =
ActivityPub.fetch_user_abstract_activities(user, opts[:for], %{
type: "Listen",
limit: 5,
id: user.id
})
%{
id: to_string(user.id),
username: username_from_nickname(user.nickname),
@ -297,7 +306,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView 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: render_many(scrobbles, ScrobbleView, "show.json")
}
}
|> 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.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(),
@ -30,8 +42,4 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleView do
length: object.data["length"]
}
end
def render("index.json", opts) do
safe_render_many(opts.activities, __MODULE__, "show.json", opts)
end
end