Merge branch 'issue/2069' into 'develop'
[akkoma] / lib / pleroma / web / pleroma_api / views / scrobble_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.ScrobbleView do
6 use Pleroma.Web, :view
7
8 require Pleroma.Constants
9
10 alias Pleroma.Activity
11 alias Pleroma.HTML
12 alias Pleroma.Object
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.MastodonAPI.AccountView
16
17 def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do
18 object = Object.normalize(activity)
19
20 user = CommonAPI.get_user(activity.data["actor"])
21 created_at = Utils.to_masto_date(activity.data["published"])
22
23 %{
24 id: activity.id,
25 account: AccountView.render("show.json", %{user: user, for: opts[:for]}),
26 created_at: created_at,
27 title: object.data["title"] |> HTML.strip_tags(),
28 artist: object.data["artist"] |> HTML.strip_tags(),
29 album: object.data["album"] |> HTML.strip_tags(),
30 length: object.data["length"]
31 }
32 end
33
34 def render("index.json", opts) do
35 safe_render_many(opts.activities, __MODULE__, "show.json", opts)
36 end
37 end