summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/api_spec/operations/pleroma_scrobble_operation.ex
blob: b6273bfcfd003eba857884d2d6c917219ae777d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ApiSpec.PleromaScrobbleOperation do
  alias OpenApiSpex.Operation
  alias OpenApiSpex.Reference
  alias OpenApiSpex.Schema
  alias Pleroma.Web.ApiSpec.Schemas.Account
  alias Pleroma.Web.ApiSpec.Schemas.VisibilityScope

  import Pleroma.Web.ApiSpec.Helpers

  def open_api_operation(action) do
    operation = String.to_existing_atom("#{action}_operation")
    apply(__MODULE__, operation, [])
  end

  def create_operation do
    %Operation{
      tags: ["Scrobbles"],
      summary: "Creates a new Listen activity for an account",
      security: [%{"oAuth" => ["write"]}],
      operationId: "PleromaAPI.ScrobbleController.create",
      requestBody: request_body("Parameters", create_request(), requried: true),
      responses: %{
        200 => Operation.response("Scrobble", "application/json", scrobble())
      }
    }
  end

  def index_operation do
    %Operation{
      tags: ["Scrobbles"],
      summary: "Requests a list of current and recent Listen activities for an account",
      operationId: "PleromaAPI.ScrobbleController.index",
      parameters: [
        %Reference{"$ref": "#/components/parameters/accountIdOrNickname"} | pagination_params()
      ],
      security: [%{"oAuth" => ["read"]}],
      responses: %{
        200 =>
          Operation.response("Array of Scrobble", "application/json", %Schema{
            type: :array,
            items: scrobble()
          })
      }
    }
  end

  defp create_request do
    %Schema{
      type: :object,
      required: [:title],
      properties: %{
        title: %Schema{type: :string, description: "The title of the media playing"},
        album: %Schema{type: :string, description: "The album of the media playing"},
        artist: %Schema{type: :string, description: "The artist of the media playing"},
        length: %Schema{type: :integer, description: "The length of the media playing"},
        visibility: %Schema{
          allOf: [VisibilityScope],
          default: "public",
          description: "Scrobble visibility"
        }
      },
      example: %{
        "title" => "Some Title",
        "artist" => "Some Artist",
        "album" => "Some Album",
        "length" => 180_000
      }
    }
  end

  defp scrobble do
    %Schema{
      type: :object,
      properties: %{
        id: %Schema{type: :string},
        account: Account,
        title: %Schema{type: :string, description: "The title of the media playing"},
        album: %Schema{type: :string, description: "The album of the media playing"},
        artist: %Schema{type: :string, description: "The artist of the media playing"},
        length: %Schema{
          type: :integer,
          description: "The length of the media playing",
          nullable: true
        },
        created_at: %Schema{type: :string, format: :"date-time"}
      },
      example: %{
        "id" => "1234",
        "account" => Account.schema().example,
        "title" => "Some Title",
        "artist" => "Some Artist",
        "album" => "Some Album",
        "length" => 180_000,
        "created_at" => "2019-09-28T12:40:45.000Z"
      }
    }
  end
end