ca26d80ef4db1876b5fdecf34d1c27ec72708521
[akkoma] / lib / pleroma / web / pleroma_api / controllers / scrobble_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.ScrobbleController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
9
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Plugs.OAuthScopesPlug
14
15 plug(Pleroma.Web.ApiSpec.CastAndValidate)
16
17 plug(
18 OAuthScopesPlug,
19 %{scopes: ["read"], fallback: :proceed_unauthenticated} when action == :index
20 )
21
22 plug(OAuthScopesPlug, %{scopes: ["write"]} when action == :create)
23
24 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaScrobbleOperation
25
26 def create(%{assigns: %{user: user}, body_params: params} = conn, _) do
27 with {:ok, activity} <- CommonAPI.listen(user, params) do
28 render(conn, "show.json", activity: activity, for: user)
29 else
30 {:error, message} ->
31 conn
32 |> put_status(:bad_request)
33 |> json(%{"error" => message})
34 end
35 end
36
37 def index(%{assigns: %{user: reading_user}} = conn, %{id: id} = params) do
38 with %User{} = user <- User.get_cached_by_nickname_or_id(id, for: reading_user) do
39 params = Map.put(params, :type, ["Listen"])
40
41 activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params)
42
43 conn
44 |> add_link_headers(activities)
45 |> render("index.json", %{
46 activities: activities,
47 for: reading_user,
48 as: :activity
49 })
50 end
51 end
52 end