Add `account_activation_required` to /api/v1/instance
[akkoma] / lib / pleroma / web / pleroma_api / controllers / scrobble_controller.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.ScrobbleController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
9
10 alias Pleroma.Plugs.OAuthScopesPlug
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
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 =
40 params
41 |> Map.new(fn {key, value} -> {to_string(key), value} end)
42 |> Map.put("type", ["Listen"])
43
44 activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params)
45
46 conn
47 |> add_link_headers(activities)
48 |> render("index.json", %{
49 activities: activities,
50 for: reading_user,
51 as: :activity
52 })
53 end
54 end
55 end