1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ApiSpec.AccountOperation do
6 alias OpenApiSpex.Operation
7 alias OpenApiSpex.Reference
8 alias OpenApiSpex.Schema
9 alias Pleroma.Web.ApiSpec.Schemas.Account
10 alias Pleroma.Web.ApiSpec.Schemas.AccountRelationship
11 alias Pleroma.Web.ApiSpec.Schemas.ActorType
12 alias Pleroma.Web.ApiSpec.Schemas.ApiError
13 alias Pleroma.Web.ApiSpec.Schemas.BooleanLike
14 alias Pleroma.Web.ApiSpec.Schemas.List
15 alias Pleroma.Web.ApiSpec.Schemas.Status
16 alias Pleroma.Web.ApiSpec.Schemas.VisibilityScope
18 import Pleroma.Web.ApiSpec.Helpers
20 @spec open_api_operation(atom) :: Operation.t()
21 def open_api_operation(action) do
22 operation = String.to_existing_atom("#{action}_operation")
23 apply(__MODULE__, operation, [])
26 @spec create_operation() :: Operation.t()
27 def create_operation do
30 summary: "Register an account",
32 "Creates a user and account records. Returns an account access token for the app that initiated the request. The app should save this token for later, and should wait for the user to confirm their account by clicking a link in their email inbox.",
33 operationId: "AccountController.create",
34 requestBody: request_body("Parameters", create_request(), required: true),
36 200 => Operation.response("Account", "application/json", create_response()),
37 400 => Operation.response("Error", "application/json", ApiError),
38 403 => Operation.response("Error", "application/json", ApiError),
39 429 => Operation.response("Error", "application/json", ApiError)
44 def verify_credentials_operation do
47 description: "Test to make sure that the user token works.",
48 summary: "Verify account credentials",
49 operationId: "AccountController.verify_credentials",
50 security: [%{"oAuth" => ["read:accounts"]}],
52 200 => Operation.response("Account", "application/json", Account)
57 def update_credentials_operation do
60 summary: "Update account credentials",
61 description: "Update the user's display and preferences.",
62 operationId: "AccountController.update_credentials",
63 security: [%{"oAuth" => ["write:accounts"]}],
64 requestBody: request_body("Parameters", update_credentials_request(), required: true),
66 200 => Operation.response("Account", "application/json", Account),
67 403 => Operation.response("Error", "application/json", ApiError)
72 def relationships_operation do
75 summary: "Check relationships to other accounts",
76 operationId: "AccountController.relationships",
77 description: "Find out whether a given account is followed, blocked, muted, etc.",
78 security: [%{"oAuth" => ["read:follows"]}],
84 oneOf: [%Schema{type: :array, items: %Schema{type: :string}}, %Schema{type: :string}]
91 200 => Operation.response("Account", "application/json", array_of_relationships())
100 operationId: "AccountController.show",
101 description: "View information about a profile.",
102 parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
104 200 => Operation.response("Account", "application/json", Account),
105 401 => Operation.response("Error", "application/json", ApiError),
106 404 => Operation.response("Error", "application/json", ApiError)
111 def statuses_operation do
115 operationId: "AccountController.statuses",
117 "Statuses posted to the given account. Public (for public statuses only), or user token + `read:statuses` (for private statuses the user is authorized to see)",
120 %Reference{"$ref": "#/components/parameters/accountIdOrNickname"},
121 Operation.parameter(:pinned, :query, BooleanLike, "Include only pinned statuses"),
122 Operation.parameter(:tagged, :query, :string, "With tag"),
127 "Include only statuses with media attached"
133 "Include statuses from muted acccounts."
135 Operation.parameter(:exclude_reblogs, :query, BooleanLike, "Exclude reblogs"),
136 Operation.parameter(:exclude_replies, :query, BooleanLike, "Exclude replies"),
138 :exclude_visibilities,
140 %Schema{type: :array, items: VisibilityScope},
141 "Exclude visibilities"
143 ] ++ pagination_params(),
145 200 => Operation.response("Statuses", "application/json", array_of_statuses()),
146 401 => Operation.response("Error", "application/json", ApiError),
147 404 => Operation.response("Error", "application/json", ApiError)
152 def followers_operation do
155 summary: "Followers",
156 operationId: "AccountController.followers",
157 security: [%{"oAuth" => ["read:accounts"]}],
159 "Accounts which follow the given account, if network is not hidden by the account owner.",
161 %Reference{"$ref": "#/components/parameters/accountIdOrNickname"},
162 Operation.parameter(:id, :query, :string, "ID of the resource owner"),
163 with_relationships_param() | pagination_params()
166 200 => Operation.response("Accounts", "application/json", array_of_accounts())
171 def following_operation do
174 summary: "Following",
175 operationId: "AccountController.following",
176 security: [%{"oAuth" => ["read:accounts"]}],
178 "Accounts which the given account is following, if network is not hidden by the account owner.",
180 %Reference{"$ref": "#/components/parameters/accountIdOrNickname"},
181 Operation.parameter(:id, :query, :string, "ID of the resource owner"),
182 with_relationships_param() | pagination_params()
184 responses: %{200 => Operation.response("Accounts", "application/json", array_of_accounts())}
188 def lists_operation do
191 summary: "Lists containing this account",
192 operationId: "AccountController.lists",
193 security: [%{"oAuth" => ["read:lists"]}],
194 description: "User lists that you have added this account to.",
195 parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
196 responses: %{200 => Operation.response("Lists", "application/json", array_of_lists())}
200 def follow_operation do
204 operationId: "AccountController.follow",
205 security: [%{"oAuth" => ["follow", "write:follows"]}],
206 description: "Follow the given account",
208 %Reference{"$ref": "#/components/parameters/accountIdOrNickname"}
218 description: "Receive this account's reblogs in home timeline? Defaults to true.",
226 200 => Operation.response("Relationship", "application/json", AccountRelationship),
227 400 => Operation.response("Error", "application/json", ApiError),
228 404 => Operation.response("Error", "application/json", ApiError)
233 def unfollow_operation do
237 operationId: "AccountController.unfollow",
238 security: [%{"oAuth" => ["follow", "write:follows"]}],
239 description: "Unfollow the given account",
240 parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
242 200 => Operation.response("Relationship", "application/json", AccountRelationship),
243 400 => Operation.response("Error", "application/json", ApiError),
244 404 => Operation.response("Error", "application/json", ApiError)
249 def mute_operation do
253 operationId: "AccountController.mute",
254 security: [%{"oAuth" => ["follow", "write:mutes"]}],
255 requestBody: request_body("Parameters", mute_request()),
257 "Mute the given account. Clients should filter statuses and notifications from this account, if received (e.g. due to a boost in the Home timeline).",
259 %Reference{"$ref": "#/components/parameters/accountIdOrNickname"},
263 %Schema{allOf: [BooleanLike], default: true},
264 "Mute notifications in addition to statuses? Defaults to `true`."
269 %Schema{type: :integer, default: 0},
270 "Expire the mute in `expires_in` seconds. Default 0 for infinity"
274 200 => Operation.response("Relationship", "application/json", AccountRelationship)
279 def unmute_operation do
283 operationId: "AccountController.unmute",
284 security: [%{"oAuth" => ["follow", "write:mutes"]}],
285 description: "Unmute the given account.",
286 parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
288 200 => Operation.response("Relationship", "application/json", AccountRelationship)
293 def block_operation do
297 operationId: "AccountController.block",
298 security: [%{"oAuth" => ["follow", "write:blocks"]}],
300 "Block the given account. Clients should filter statuses from this account if received (e.g. due to a boost in the Home timeline)",
301 parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
303 200 => Operation.response("Relationship", "application/json", AccountRelationship)
308 def unblock_operation do
312 operationId: "AccountController.unblock",
313 security: [%{"oAuth" => ["follow", "write:blocks"]}],
314 description: "Unblock the given account.",
315 parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
317 200 => Operation.response("Relationship", "application/json", AccountRelationship)
322 def follow_by_uri_operation do
325 summary: "Follow by URI",
326 operationId: "AccountController.follows",
327 security: [%{"oAuth" => ["follow", "write:follows"]}],
328 requestBody: request_body("Parameters", follow_by_uri_request(), required: true),
330 200 => Operation.response("Account", "application/json", AccountRelationship),
331 400 => Operation.response("Error", "application/json", ApiError),
332 404 => Operation.response("Error", "application/json", ApiError)
337 def mutes_operation do
340 summary: "Muted accounts",
341 operationId: "AccountController.mutes",
342 description: "Accounts the user has muted.",
343 security: [%{"oAuth" => ["follow", "read:mutes"]}],
345 200 => Operation.response("Accounts", "application/json", array_of_accounts())
350 def blocks_operation do
353 summary: "Blocked users",
354 operationId: "AccountController.blocks",
355 description: "View your blocks. See also accounts/:id/{block,unblock}",
356 security: [%{"oAuth" => ["read:blocks"]}],
358 200 => Operation.response("Accounts", "application/json", array_of_accounts())
363 def endorsements_operation do
366 summary: "Endorsements",
367 operationId: "AccountController.endorsements",
368 description: "Not implemented",
369 security: [%{"oAuth" => ["read:accounts"]}],
371 200 => empty_array_response()
376 def identity_proofs_operation do
379 summary: "Identity proofs",
380 operationId: "AccountController.identity_proofs",
381 description: "Not implemented",
383 200 => empty_array_response()
388 defp create_request do
390 title: "AccountCreateRequest",
391 description: "POST body for creating an account",
393 required: [:username, :password, :agreement],
399 "Text that will be reviewed by moderators if registrations require manual approval"
401 username: %Schema{type: :string, description: "The desired username for the account"},
406 "The email address to be used for login. Required when `account_activation_required` is enabled.",
411 description: "The password to be used for login",
415 allOf: [BooleanLike],
417 "Whether the user agrees to the local rules, terms, and policies. These should be presented to the user in order to allow them to consent before setting this parameter to TRUE."
422 description: "The language of the confirmation email that will be sent"
424 # Pleroma-specific properties:
425 fullname: %Schema{type: :string, nullable: true, description: "Full name"},
426 bio: %Schema{type: :string, description: "Bio", nullable: true, default: ""},
427 captcha_solution: %Schema{
430 description: "Provider-specific captcha solution"
432 captcha_token: %Schema{
435 description: "Provider-specific captcha token"
437 captcha_answer_data: %Schema{
440 description: "Provider-specific captcha data"
445 description: "Invite token required when the registrations aren't public"
449 "username" => "cofe",
450 "email" => "cofe@example.com",
451 "password" => "secret",
452 "agreement" => "true",
458 # Note: this is a token response (if login succeeds!), but there's no oauth operation file yet.
459 defp create_response do
461 title: "AccountCreateResponse",
462 description: "Response schema for an account",
465 # The response when auto-login on create succeeds (token is issued):
466 token_type: %Schema{type: :string},
467 access_token: %Schema{type: :string},
468 refresh_token: %Schema{type: :string},
469 scope: %Schema{type: :string},
470 created_at: %Schema{type: :integer, format: :"date-time"},
471 me: %Schema{type: :string},
472 expires_in: %Schema{type: :integer},
474 # The response when registration succeeds but auto-login fails (no token):
475 identifier: %Schema{type: :string},
476 message: %Schema{type: :string}
479 # Note: example of successful registration with failed login response:
481 # "identifier" => "missing_confirmed_email",
482 # "message" => "You have been registered. Please check your email for further instructions."
485 "token_type" => "Bearer",
486 "access_token" => "i9hAVVzGld86Pl5JtLtizKoXVvtTlSCJvwaugCxvZzk",
487 "refresh_token" => "i9hAVVzGld86Pl5JtLtizKoXVvtTlSCJvwaugCxvZzz",
488 "created_at" => 1_585_918_714,
490 "scope" => "read write follow push",
491 "me" => "https://gensokyo.2hu/users/raymoo"
496 defp update_credentials_request do
498 title: "AccountUpdateCredentialsRequest",
499 description: "POST body for creating an account",
503 allOf: [BooleanLike],
505 description: "Whether the account has a bot flag."
507 display_name: %Schema{
510 description: "The display name to use for the profile."
512 note: %Schema{type: :string, description: "The account bio."},
516 description: "Avatar image encoded using multipart/form-data",
522 description: "Header image encoded using multipart/form-data",
526 allOf: [BooleanLike],
528 description: "Whether manual approval of follow requests is required."
530 accepts_chat_messages: %Schema{
531 allOf: [BooleanLike],
533 description: "Whether the user accepts receiving chat messages."
535 fields_attributes: %Schema{
538 %Schema{type: :array, items: attribute_field()},
539 %Schema{type: :object, additionalProperties: %Schema{type: attribute_field()}}
542 # NOTE: `source` field is not supported
547 # privacy: %Schema{type: :string},
548 # sensitive: %Schema{type: :boolean},
549 # language: %Schema{type: :string}
553 # Pleroma-specific fields
554 no_rich_text: %Schema{
555 allOf: [BooleanLike],
557 description: "html tags are stripped from all statuses requested from the API"
559 hide_followers: %Schema{
560 allOf: [BooleanLike],
562 description: "user's followers will be hidden"
564 hide_follows: %Schema{
565 allOf: [BooleanLike],
567 description: "user's follows will be hidden"
569 hide_followers_count: %Schema{
570 allOf: [BooleanLike],
572 description: "user's follower count will be hidden"
574 hide_follows_count: %Schema{
575 allOf: [BooleanLike],
577 description: "user's follow count will be hidden"
579 hide_favorites: %Schema{
580 allOf: [BooleanLike],
582 description: "user's favorites timeline will be hidden"
585 allOf: [BooleanLike],
587 description: "user's role (e.g admin, moderator) will be exposed to anyone in the
590 default_scope: VisibilityScope,
591 pleroma_settings_store: %Schema{
594 description: "Opaque user settings to be saved on the backend."
596 skip_thread_containment: %Schema{
597 allOf: [BooleanLike],
599 description: "Skip filtering out broken threads"
601 allow_following_move: %Schema{
602 allOf: [BooleanLike],
604 description: "Allows automatically follow moved following accounts"
606 pleroma_background_image: %Schema{
609 description: "Sets the background image of the user.",
612 discoverable: %Schema{
613 allOf: [BooleanLike],
616 "Discovery of this account in search results and other services is allowed."
618 actor_type: ActorType
622 display_name: "cofe",
624 fields_attributes: [%{name: "foo", value: "bar"}],
626 hide_followers: true,
628 hide_followers_count: false,
629 hide_follows_count: false,
630 hide_favorites: false,
632 default_scope: "private",
633 pleroma_settings_store: %{"pleroma-fe" => %{"key" => "val"}},
634 skip_thread_containment: false,
635 allow_following_move: false,
642 def array_of_accounts do
644 title: "ArrayOfAccounts",
647 example: [Account.schema().example]
651 defp array_of_relationships do
653 title: "ArrayOfRelationships",
654 description: "Response schema for account relationships",
656 items: AccountRelationship,
661 "showing_reblogs" => true,
662 "followed_by" => true,
664 "blocked_by" => true,
666 "muting_notifications" => false,
667 "requested" => false,
668 "domain_blocking" => false,
669 "subscribing" => false,
675 "showing_reblogs" => true,
676 "followed_by" => true,
678 "blocked_by" => true,
680 "muting_notifications" => false,
682 "domain_blocking" => false,
683 "subscribing" => false,
689 "showing_reblogs" => true,
690 "followed_by" => true,
692 "blocked_by" => false,
694 "muting_notifications" => false,
695 "requested" => false,
696 "domain_blocking" => true,
697 "subscribing" => true,
704 defp follow_by_uri_request do
706 title: "AccountFollowsRequest",
707 description: "POST body for muting an account",
710 uri: %Schema{type: :string, nullable: true, format: :uri}
718 title: "AccountMuteRequest",
719 description: "POST body for muting an account",
722 notifications: %Schema{
723 allOf: [BooleanLike],
725 description: "Mute notifications in addition to statuses? Defaults to true.",
731 description: "Expire the mute in `expires_in` seconds. Default 0 for infinity",
736 "notifications" => true,
737 "expires_in" => 86_400
742 defp array_of_lists do
744 title: "ArrayOfLists",
745 description: "Response schema for lists",
749 %{"id" => "123", "title" => "my list"},
750 %{"id" => "1337", "title" => "anotehr list"}
755 defp array_of_statuses do
757 title: "ArrayOfStatuses",
763 defp attribute_field do
765 title: "AccountAttributeField",
766 description: "Request schema for account custom fields",
769 name: %Schema{type: :string},
770 value: %Schema{type: :string}
772 required: [:name, :value],
775 "value" => "https://pleroma.com"