1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.ScrobbleController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Plugs.OAuthScopesPlug
15 plug(Pleroma.Web.ApiSpec.CastAndValidate)
19 %{scopes: ["read"], fallback: :proceed_unauthenticated} when action == :index
22 plug(OAuthScopesPlug, %{scopes: ["write"]} when action == :create)
24 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaScrobbleOperation
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)
32 |> put_status(:bad_request)
33 |> json(%{"error" => message})
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"])
41 activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params)
44 |> add_link_headers(activities)
45 |> render("index.json", %{
46 activities: activities,