2019-09-30 06:10:54 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 06:10:54 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.AccountController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2019-09-30 01:28:12 -06:00
|
|
|
import Pleroma.Web.ControllerHelper,
|
2020-04-01 10:49:09 -06:00
|
|
|
only: [
|
|
|
|
add_link_headers: 2,
|
|
|
|
assign_account_by_id: 2,
|
2020-05-13 09:56:45 -06:00
|
|
|
embed_relationships?: 1,
|
2020-05-09 09:05:44 -06:00
|
|
|
json_response: 3
|
2020-04-01 10:49:09 -06:00
|
|
|
]
|
2019-09-30 06:10:54 -06:00
|
|
|
|
2020-06-05 08:48:02 -06:00
|
|
|
alias Pleroma.Maps
|
2019-09-30 06:10:54 -06:00
|
|
|
alias Pleroma.User
|
2021-11-21 08:53:30 -07:00
|
|
|
alias Pleroma.UserNote
|
2019-09-30 06:10:54 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2020-06-22 05:59:45 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.Builder
|
2020-06-22 06:02:29 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.Pipeline
|
2019-09-30 03:08:29 -06:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-09-30 06:10:54 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.ListView
|
2019-09-30 03:08:29 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.MastodonAPI
|
2020-04-06 01:20:44 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.MastodonAPIController
|
2019-09-30 03:08:29 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2020-07-31 05:13:38 -06:00
|
|
|
alias Pleroma.Web.OAuth.OAuthController
|
2020-06-24 04:07:47 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
|
|
|
alias Pleroma.Web.Plugs.RateLimiter
|
2019-09-30 03:08:29 -06:00
|
|
|
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
2021-06-07 15:45:33 -06:00
|
|
|
alias Pleroma.Web.Utils.Params
|
2019-09-30 06:10:54 -06:00
|
|
|
|
2020-05-04 10:16:18 -06:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2020-04-27 10:46:52 -06:00
|
|
|
|
2022-12-19 13:32:16 -07:00
|
|
|
plug(:skip_auth when action in [:create])
|
2020-04-21 07:29:19 -06:00
|
|
|
|
2021-06-08 18:14:12 -06:00
|
|
|
plug(:skip_public_check when action in [:show, :statuses])
|
2020-04-06 01:20:44 -06:00
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
|
2022-12-19 13:32:16 -07:00
|
|
|
when action in [:show, :followers, :following, :lookup]
|
2020-04-21 07:29:19 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{fallback: :proceed_unauthenticated, scopes: ["read:statuses"]}
|
|
|
|
when action == :statuses
|
2019-10-02 11:42:40 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:accounts"]}
|
2020-04-24 07:52:38 -06:00
|
|
|
when action in [:verify_credentials, :endorsements, :identity_proofs]
|
2019-10-02 11:42:40 -06:00
|
|
|
)
|
|
|
|
|
2021-11-21 08:53:30 -07:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:accounts"]}
|
|
|
|
when action in [:update_credentials, :note]
|
|
|
|
)
|
2019-10-02 11:42:40 -06:00
|
|
|
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :lists)
|
|
|
|
|
2019-10-06 08:12:17 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["follow", "read:blocks"]} when action == :blocks
|
|
|
|
)
|
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["follow", "write:blocks"]} when action in [:block, :unblock]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:follows"]} when action == :relationships)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2022-10-19 04:01:14 -06:00
|
|
|
%{scopes: ["follow", "write:follows"]}
|
|
|
|
when action in [:follow_by_uri, :follow, :unfollow, :remove_from_followers]
|
2019-10-02 11:42:40 -06:00
|
|
|
)
|
|
|
|
|
2019-10-06 08:12:17 -06:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["follow", "read:mutes"]} when action == :mutes)
|
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action in [:mute, :unmute])
|
|
|
|
|
2022-10-19 04:01:14 -06:00
|
|
|
@relationship_actions [:follow, :unfollow, :remove_from_followers]
|
|
|
|
@needs_account ~W(followers following lists follow unfollow mute unmute block unblock note remove_from_followers)a
|
2019-09-30 06:10:54 -06:00
|
|
|
|
2020-03-25 00:04:00 -06:00
|
|
|
plug(
|
|
|
|
RateLimiter,
|
2020-05-26 05:45:54 -06:00
|
|
|
[name: :relation_id_action, params: [:id, :uri]] when action in @relationship_actions
|
2020-03-25 00:04:00 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
plug(RateLimiter, [name: :relations_actions] when action in @relationship_actions)
|
2019-11-11 05:13:06 -07:00
|
|
|
plug(RateLimiter, [name: :app_account_creation] when action == :create)
|
2019-09-30 03:08:29 -06:00
|
|
|
plug(:assign_account_by_id when action in @needs_account)
|
2019-09-30 06:10:54 -06:00
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2020-04-03 12:45:08 -06:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AccountOperation
|
|
|
|
|
2019-09-30 03:08:29 -06:00
|
|
|
@doc "POST /api/v1/accounts"
|
2020-04-03 12:45:08 -06:00
|
|
|
def create(%{assigns: %{app: app}, body_params: params} = conn, _params) do
|
2020-02-26 09:13:53 -07:00
|
|
|
with :ok <- validate_email_param(params),
|
2020-04-29 10:48:08 -06:00
|
|
|
:ok <- TwitterAPI.validate_captcha(app, params),
|
2020-07-29 03:47:03 -06:00
|
|
|
{:ok, user} <- TwitterAPI.register_user(params),
|
2020-07-31 05:13:38 -06:00
|
|
|
{_, {:ok, token}} <-
|
|
|
|
{:login, OAuthController.login(user, app, app.scopes)} do
|
2020-11-25 11:47:23 -07:00
|
|
|
OAuthController.after_token_exchange(conn, %{user: user, token: token})
|
2019-09-30 03:08:29 -06:00
|
|
|
else
|
2020-07-31 05:13:38 -06:00
|
|
|
{:login, {:account_status, :confirmation_pending}} ->
|
|
|
|
json_response(conn, :ok, %{
|
|
|
|
message: "You have been registered. Please check your email for further instructions.",
|
|
|
|
identifier: "missing_confirmed_email"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:login, {:account_status, :approval_pending}} ->
|
|
|
|
json_response(conn, :ok, %{
|
|
|
|
message:
|
|
|
|
"You have been registered. You'll be able to log in once your account is approved.",
|
|
|
|
identifier: "awaiting_approval"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:login, _} ->
|
|
|
|
json_response(conn, :ok, %{
|
|
|
|
message:
|
|
|
|
"You have been registered. Some post-registration steps may be pending. " <>
|
|
|
|
"Please log in manually.",
|
|
|
|
identifier: "manual_login_required"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
json_response(conn, :bad_request, %{error: error})
|
2019-09-30 03:08:29 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(%{assigns: %{app: _app}} = conn, _) do
|
|
|
|
render_error(conn, :bad_request, "Missing parameters")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, _) do
|
|
|
|
render_error(conn, :forbidden, "Invalid credentials")
|
|
|
|
end
|
|
|
|
|
2020-04-29 10:48:08 -06:00
|
|
|
defp validate_email_param(%{email: email}) when not is_nil(email), do: :ok
|
2020-02-26 09:13:53 -07:00
|
|
|
|
|
|
|
defp validate_email_param(_) do
|
|
|
|
case Pleroma.Config.get([:instance, :account_activation_required]) do
|
2020-04-29 10:48:08 -06:00
|
|
|
true -> {:error, dgettext("errors", "Missing parameter: %{name}", name: "email")}
|
2020-02-26 09:13:53 -07:00
|
|
|
_ -> :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 03:08:29 -06:00
|
|
|
@doc "GET /api/v1/accounts/verify_credentials"
|
|
|
|
def verify_credentials(%{assigns: %{user: user}} = conn, _) do
|
|
|
|
render(conn, "show.json",
|
|
|
|
user: user,
|
|
|
|
for: user,
|
2022-07-21 04:29:28 -06:00
|
|
|
with_pleroma_settings: true
|
2019-09-30 03:08:29 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-30 02:47:01 -06:00
|
|
|
@doc "PATCH /api/v1/accounts/update_credentials"
|
2020-06-01 05:03:22 -06:00
|
|
|
def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _params) do
|
2020-04-07 04:53:12 -06:00
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Enum.filter(fn {_, value} -> not is_nil(value) end)
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
2020-07-07 08:20:50 -06:00
|
|
|
# We use an empty string as a special value to reset
|
|
|
|
# avatars, banners, backgrounds
|
|
|
|
user_image_value = fn
|
|
|
|
"" -> {:ok, nil}
|
|
|
|
value -> {:ok, value}
|
|
|
|
end
|
|
|
|
|
2022-11-28 06:34:54 -07:00
|
|
|
status_ttl_days_value = fn
|
|
|
|
-1 -> {:ok, nil}
|
|
|
|
value -> {:ok, value}
|
|
|
|
end
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
user_params =
|
2019-09-30 02:47:01 -06:00
|
|
|
[
|
|
|
|
:no_rich_text,
|
|
|
|
:hide_followers_count,
|
|
|
|
:hide_follows_count,
|
|
|
|
:hide_followers,
|
|
|
|
:hide_follows,
|
|
|
|
:hide_favorites,
|
|
|
|
:show_role,
|
|
|
|
:skip_thread_containment,
|
2019-11-12 04:36:50 -07:00
|
|
|
:allow_following_move,
|
2023-05-23 06:16:20 -06:00
|
|
|
:also_known_as
|
2019-09-30 02:47:01 -06:00
|
|
|
]
|
|
|
|
|> Enum.reduce(%{}, fn key, acc ->
|
2021-05-22 10:41:55 -06:00
|
|
|
Maps.put_if_present(acc, key, params[key], &{:ok, Params.truthy_param?(&1)})
|
2019-09-30 02:47:01 -06:00
|
|
|
end)
|
2020-06-05 08:48:02 -06:00
|
|
|
|> Maps.put_if_present(:name, params[:display_name])
|
|
|
|
|> Maps.put_if_present(:bio, params[:note])
|
2020-03-23 15:52:25 -06:00
|
|
|
|> Maps.put_if_present(:raw_bio, params[:note])
|
2020-07-07 08:20:50 -06:00
|
|
|
|> Maps.put_if_present(:avatar, params[:avatar], user_image_value)
|
|
|
|
|> Maps.put_if_present(:banner, params[:header], user_image_value)
|
|
|
|
|> Maps.put_if_present(:background, params[:pleroma_background_image], user_image_value)
|
2020-06-05 08:48:02 -06:00
|
|
|
|> Maps.put_if_present(
|
2020-01-31 11:07:46 -07:00
|
|
|
:raw_fields,
|
2020-06-05 08:48:02 -06:00
|
|
|
params[:fields_attributes],
|
2020-01-31 11:07:46 -07:00
|
|
|
&{:ok, normalize_fields_attributes(&1)}
|
|
|
|
)
|
2020-06-05 08:48:02 -06:00
|
|
|
|> Maps.put_if_present(:pleroma_settings_store, params[:pleroma_settings_store])
|
|
|
|
|> Maps.put_if_present(:default_scope, params[:default_scope])
|
|
|
|
|> Maps.put_if_present(:default_scope, params["source"]["privacy"])
|
2020-06-19 13:18:07 -06:00
|
|
|
|> Maps.put_if_present(:actor_type, params[:bot], fn bot ->
|
|
|
|
if bot, do: {:ok, "Service"}, else: {:ok, "Person"}
|
|
|
|
end)
|
2020-06-05 08:48:02 -06:00
|
|
|
|> Maps.put_if_present(:actor_type, params[:actor_type])
|
2020-08-07 15:48:03 -06:00
|
|
|
|> Maps.put_if_present(:also_known_as, params[:also_known_as])
|
2020-11-19 09:30:02 -07:00
|
|
|
# Note: param name is indeed :locked (not an error)
|
2020-10-14 10:03:17 -06:00
|
|
|
|> Maps.put_if_present(:is_locked, params[:locked])
|
2020-11-19 09:30:02 -07:00
|
|
|
# Note: param name is indeed :discoverable (not an error)
|
2020-10-14 09:44:18 -06:00
|
|
|
|> Maps.put_if_present(:is_discoverable, params[:discoverable])
|
2022-03-01 23:41:13 -07:00
|
|
|
|> Maps.put_if_present(:language, Pleroma.Web.Gettext.normalize_locale(params[:language]))
|
2022-11-28 06:34:54 -07:00
|
|
|
|> Maps.put_if_present(:status_ttl_days, params[:status_ttl_days], status_ttl_days_value)
|
2023-05-23 03:29:08 -06:00
|
|
|
|> Maps.put_if_present(:accepts_direct_messages_from, params[:accepts_direct_messages_from])
|
2019-10-16 12:59:21 -06:00
|
|
|
|
2020-06-22 05:59:45 -06:00
|
|
|
# What happens here:
|
|
|
|
#
|
|
|
|
# We want to update the user through the pipeline, but the ActivityPub
|
|
|
|
# update information is not quite enough for this, because this also
|
|
|
|
# contains local settings that don't federate and don't even appear
|
2020-06-22 06:02:29 -06:00
|
|
|
# in the Update activity.
|
|
|
|
#
|
2020-06-22 05:59:45 -06:00
|
|
|
# So we first build the normal local changeset, then apply it to the
|
|
|
|
# user data, but don't persist it. With this, we generate the object
|
|
|
|
# data for our update activity. We feed this and the changeset as meta
|
|
|
|
# inforation into the pipeline, where they will be properly updated and
|
|
|
|
# federated.
|
|
|
|
with changeset <- User.update_changeset(user, user_params),
|
|
|
|
{:ok, unpersisted_user} <- Ecto.Changeset.apply_action(changeset, :update),
|
|
|
|
updated_object <-
|
2020-08-10 02:33:05 -06:00
|
|
|
Pleroma.Web.ActivityPub.UserView.render("user.json", user: unpersisted_user)
|
2020-06-22 05:59:45 -06:00
|
|
|
|> Map.delete("@context"),
|
|
|
|
{:ok, update_data, []} <- Builder.update(user, updated_object),
|
|
|
|
{:ok, _update, _} <-
|
|
|
|
Pipeline.common_pipeline(update_data,
|
|
|
|
local: true,
|
|
|
|
user_update_changeset: changeset
|
|
|
|
) do
|
|
|
|
render(conn, "show.json",
|
|
|
|
user: unpersisted_user,
|
|
|
|
for: unpersisted_user,
|
|
|
|
with_pleroma_settings: true
|
|
|
|
)
|
2019-09-30 02:47:01 -06:00
|
|
|
else
|
2022-12-11 14:54:47 -07:00
|
|
|
{:error, %Ecto.Changeset{errors: [avatar: {"file is too large", _}]}} ->
|
|
|
|
render_error(conn, :request_entity_too_large, "File is too large")
|
|
|
|
|
2022-12-11 15:15:08 -07:00
|
|
|
{:error, %Ecto.Changeset{errors: [banner: {"file is too large", _}]}} ->
|
|
|
|
render_error(conn, :request_entity_too_large, "File is too large")
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{errors: [background: {"file is too large", _}]}} ->
|
|
|
|
render_error(conn, :request_entity_too_large, "File is too large")
|
|
|
|
|
2022-12-11 14:54:47 -07:00
|
|
|
_e ->
|
|
|
|
render_error(conn, :forbidden, "Invalid request")
|
2019-09-30 02:47:01 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 10:39:22 -06:00
|
|
|
defp normalize_fields_attributes(fields) do
|
|
|
|
if Enum.all?(fields, &is_tuple/1) do
|
|
|
|
Enum.map(fields, fn {_, v} -> v end)
|
|
|
|
else
|
2020-04-07 04:53:12 -06:00
|
|
|
Enum.map(fields, fn
|
2020-04-27 12:55:05 -06:00
|
|
|
%{} = field -> %{"name" => field.name, "value" => field.value}
|
|
|
|
field -> field
|
2020-04-07 04:53:12 -06:00
|
|
|
end)
|
2019-09-30 10:39:22 -06:00
|
|
|
end
|
2019-09-30 02:47:01 -06:00
|
|
|
end
|
|
|
|
|
2019-09-30 03:08:29 -06:00
|
|
|
@doc "GET /api/v1/accounts/relationships"
|
2020-04-07 08:29:05 -06:00
|
|
|
def relationships(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-30 03:08:29 -06:00
|
|
|
targets = User.get_all_by_ids(List.wrap(id))
|
|
|
|
|
|
|
|
render(conn, "relationships.json", user: user, targets: targets)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Instead of returning a 400 when no "id" params is present, Mastodon returns an empty array.
|
|
|
|
def relationships(%{assigns: %{user: _user}} = conn, _), do: json(conn, [])
|
|
|
|
|
2019-09-30 06:10:54 -06:00
|
|
|
@doc "GET /api/v1/accounts/:id"
|
2021-01-28 22:41:21 -07:00
|
|
|
def show(%{assigns: %{user: for_user}} = conn, %{id: nickname_or_id} = params) do
|
2019-09-30 06:10:54 -06:00
|
|
|
with %User{} = user <- User.get_cached_by_nickname_or_id(nickname_or_id, for: for_user),
|
2020-05-15 11:29:09 -06:00
|
|
|
:visible <- User.visible_for(user, for_user) do
|
2021-01-28 22:41:21 -07:00
|
|
|
render(conn, "show.json",
|
|
|
|
user: user,
|
|
|
|
for: for_user,
|
|
|
|
embed_relationships: embed_relationships?(params)
|
|
|
|
)
|
2019-09-30 06:10:54 -06:00
|
|
|
else
|
2020-05-07 04:44:38 -06:00
|
|
|
error -> user_visibility_error(conn, error)
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/accounts/:id/statuses"
|
|
|
|
def statuses(%{assigns: %{user: reading_user}} = conn, params) do
|
2020-04-08 12:33:25 -06:00
|
|
|
with %User{} = user <- User.get_cached_by_nickname_or_id(params.id, for: reading_user),
|
2020-05-15 11:29:09 -06:00
|
|
|
:visible <- User.visible_for(user, reading_user) do
|
2019-12-06 06:25:13 -07:00
|
|
|
params =
|
|
|
|
params
|
2020-04-08 12:33:25 -06:00
|
|
|
|> Map.delete(:tagged)
|
2020-06-04 11:33:16 -06:00
|
|
|
|> Map.put(:tag, params[:tagged])
|
2019-12-06 06:25:13 -07:00
|
|
|
|
2019-09-30 06:10:54 -06:00
|
|
|
activities = ActivityPub.fetch_user_activities(user, reading_user, params)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
|
|
|
|> put_view(StatusView)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: reading_user,
|
2020-11-16 11:23:25 -07:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2020-03-20 04:04:37 -06:00
|
|
|
else
|
2020-05-07 04:44:38 -06:00
|
|
|
error -> user_visibility_error(conn, error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp user_visibility_error(conn, error) do
|
|
|
|
case error do
|
|
|
|
:restrict_unauthenticated ->
|
|
|
|
render_error(conn, :unauthorized, "This API requires an authenticated user")
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
render_error(conn, :not_found, "Can't find user")
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/accounts/:id/followers"
|
|
|
|
def followers(%{assigns: %{user: for_user, account: user}} = conn, params) do
|
2020-04-08 13:16:20 -06:00
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Enum.map(fn {key, value} -> {to_string(key), value} end)
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
2019-09-30 06:10:54 -06:00
|
|
|
followers =
|
|
|
|
cond do
|
|
|
|
for_user && user.id == for_user.id -> MastodonAPI.get_followers(user, params)
|
2019-10-16 12:59:21 -06:00
|
|
|
user.hide_followers -> []
|
2019-09-30 06:10:54 -06:00
|
|
|
true -> MastodonAPI.get_followers(user, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(followers)
|
2020-05-12 10:14:35 -06:00
|
|
|
# https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838#note_59223
|
|
|
|
|> render("index.json",
|
|
|
|
for: for_user,
|
|
|
|
users: followers,
|
|
|
|
as: :user,
|
2020-05-13 09:56:45 -06:00
|
|
|
embed_relationships: embed_relationships?(params)
|
2020-05-12 10:14:35 -06:00
|
|
|
)
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/accounts/:id/following"
|
|
|
|
def following(%{assigns: %{user: for_user, account: user}} = conn, params) do
|
2020-04-08 13:38:07 -06:00
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Enum.map(fn {key, value} -> {to_string(key), value} end)
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
2019-09-30 06:10:54 -06:00
|
|
|
followers =
|
|
|
|
cond do
|
|
|
|
for_user && user.id == for_user.id -> MastodonAPI.get_friends(user, params)
|
2019-10-16 12:59:21 -06:00
|
|
|
user.hide_follows -> []
|
2019-09-30 06:10:54 -06:00
|
|
|
true -> MastodonAPI.get_friends(user, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(followers)
|
2020-05-12 10:14:35 -06:00
|
|
|
# https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838#note_59223
|
|
|
|
|> render("index.json",
|
|
|
|
for: for_user,
|
|
|
|
users: followers,
|
|
|
|
as: :user,
|
2020-05-13 09:56:45 -06:00
|
|
|
embed_relationships: embed_relationships?(params)
|
2020-05-12 10:14:35 -06:00
|
|
|
)
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/accounts/:id/lists"
|
|
|
|
def lists(%{assigns: %{user: user, account: account}} = conn, _params) do
|
|
|
|
lists = Pleroma.List.get_lists_account_belongs(user, account)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(ListView)
|
|
|
|
|> render("index.json", lists: lists)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/accounts/:id/follow"
|
|
|
|
def follow(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
|
2020-04-22 07:04:26 -06:00
|
|
|
{:error, "Can not follow yourself"}
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
|
2020-07-08 03:52:29 -06:00
|
|
|
def follow(%{body_params: params, assigns: %{user: follower, account: followed}} = conn, _) do
|
2020-04-09 05:25:24 -06:00
|
|
|
with {:ok, follower} <- MastodonAPI.follow(follower, followed, params) do
|
2019-09-30 06:10:54 -06:00
|
|
|
render(conn, "relationship.json", user: follower, target: followed)
|
|
|
|
else
|
2019-09-30 01:28:12 -06:00
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 01:28:12 -06:00
|
|
|
@doc "POST /api/v1/accounts/:id/unfollow"
|
2019-09-30 06:10:54 -06:00
|
|
|
def unfollow(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
|
2020-04-22 07:04:26 -06:00
|
|
|
{:error, "Can not unfollow yourself"}
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow(%{assigns: %{user: follower, account: followed}} = conn, _params) do
|
|
|
|
with {:ok, follower} <- CommonAPI.unfollow(follower, followed) do
|
|
|
|
render(conn, "relationship.json", user: follower, target: followed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/accounts/:id/mute"
|
2020-04-09 08:28:14 -06:00
|
|
|
def mute(%{assigns: %{user: muter, account: muted}, body_params: params} = conn, _params) do
|
2020-09-08 04:26:44 -06:00
|
|
|
with {:ok, _user_relationships} <- User.mute(muter, muted, params) do
|
2019-09-30 06:10:54 -06:00
|
|
|
render(conn, "relationship.json", user: muter, target: muted)
|
|
|
|
else
|
2019-09-30 01:28:12 -06:00
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/accounts/:id/unmute"
|
|
|
|
def unmute(%{assigns: %{user: muter, account: muted}} = conn, _params) do
|
2019-11-19 13:22:10 -07:00
|
|
|
with {:ok, _user_relationships} <- User.unmute(muter, muted) do
|
2019-09-30 06:10:54 -06:00
|
|
|
render(conn, "relationship.json", user: muter, target: muted)
|
|
|
|
else
|
2019-09-30 01:28:12 -06:00
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/accounts/:id/block"
|
|
|
|
def block(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do
|
2020-06-25 03:44:04 -06:00
|
|
|
with {:ok, _activity} <- CommonAPI.block(blocker, blocked) do
|
2019-09-30 06:10:54 -06:00
|
|
|
render(conn, "relationship.json", user: blocker, target: blocked)
|
|
|
|
else
|
2019-09-30 01:28:12 -06:00
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/accounts/:id/unblock"
|
|
|
|
def unblock(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do
|
2020-05-05 10:00:37 -06:00
|
|
|
with {:ok, _activity} <- CommonAPI.unblock(blocker, blocked) do
|
2019-09-30 06:10:54 -06:00
|
|
|
render(conn, "relationship.json", user: blocker, target: blocked)
|
|
|
|
else
|
2019-09-30 01:28:12 -06:00
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|
|
|
|
end
|
2019-10-02 11:42:40 -06:00
|
|
|
|
2021-11-21 08:53:30 -07:00
|
|
|
@doc "POST /api/v1/accounts/:id/note"
|
2021-11-21 10:17:06 -07:00
|
|
|
def note(
|
|
|
|
%{assigns: %{user: noter, account: target}, body_params: %{comment: comment}} = conn,
|
|
|
|
_params
|
|
|
|
) do
|
2021-11-21 08:53:30 -07:00
|
|
|
with {:ok, _user_note} <- UserNote.create(noter, target, comment) do
|
|
|
|
render(conn, "relationship.json", user: noter, target: target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-19 04:01:14 -06:00
|
|
|
@doc "POST /api/v1/accounts/:id/remove_from_followers"
|
|
|
|
def remove_from_followers(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
|
|
|
|
{:error, "Can not unfollow yourself"}
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_followers(%{assigns: %{user: followed, account: follower}} = conn, _params) do
|
|
|
|
with {:ok, follower} <- CommonAPI.reject_follow_request(follower, followed) do
|
|
|
|
render(conn, "relationship.json", user: followed, target: follower)
|
|
|
|
else
|
|
|
|
nil ->
|
|
|
|
render_error(conn, :not_found, "Record not found")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-01 04:15:58 -06:00
|
|
|
@doc "POST /api/v1/follows"
|
2020-04-28 10:56:20 -06:00
|
|
|
def follow_by_uri(%{body_params: %{uri: uri}} = conn, _) do
|
2020-04-22 07:04:26 -06:00
|
|
|
case User.get_cached_by_nickname(uri) do
|
|
|
|
%User{} = user ->
|
|
|
|
conn
|
|
|
|
|> assign(:account, user)
|
|
|
|
|> follow(%{})
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :not_found}
|
2019-10-01 04:15:58 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/mutes"
|
2020-10-13 11:58:18 -06:00
|
|
|
def mutes(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
users =
|
|
|
|
user
|
|
|
|
|> User.muted_users_relation(_restrict_deactivated = true)
|
2023-08-04 08:18:07 -06:00
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
2020-10-13 11:58:18 -06:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(users)
|
2021-01-28 22:41:21 -07:00
|
|
|
|> render("index.json",
|
|
|
|
users: users,
|
|
|
|
for: user,
|
|
|
|
as: :user,
|
|
|
|
embed_relationships: embed_relationships?(params)
|
|
|
|
)
|
2019-10-01 04:15:58 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/blocks"
|
2020-10-13 11:39:41 -06:00
|
|
|
def blocks(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
users =
|
|
|
|
user
|
|
|
|
|> User.blocked_users_relation(_restrict_deactivated = true)
|
2023-08-04 08:18:07 -06:00
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
2020-10-13 11:39:41 -06:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(users)
|
2023-02-28 20:16:01 -07:00
|
|
|
|> render("index.json",
|
|
|
|
users: users,
|
|
|
|
for: user,
|
|
|
|
as: :user,
|
|
|
|
embed_relationships: embed_relationships?(params)
|
|
|
|
)
|
2019-10-01 04:15:58 -06:00
|
|
|
end
|
2019-10-06 02:43:49 -06:00
|
|
|
|
2021-12-28 08:11:17 -07:00
|
|
|
@doc "GET /api/v1/accounts/lookup"
|
2022-12-19 13:32:16 -07:00
|
|
|
def lookup(%{assigns: %{user: for_user}} = conn, %{acct: nickname} = _params) do
|
|
|
|
with %User{} = user <- User.get_by_nickname(nickname),
|
|
|
|
:visible <- User.visible_for(user, for_user) do
|
2021-12-28 08:11:17 -07:00
|
|
|
render(conn, "show.json",
|
|
|
|
user: user,
|
2021-12-28 10:24:48 -07:00
|
|
|
skip_visibility_check: true
|
2021-12-28 08:11:17 -07:00
|
|
|
)
|
|
|
|
else
|
|
|
|
error -> user_visibility_error(conn, error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
@doc "GET /api/v1/endorsements"
|
2020-04-06 01:20:44 -06:00
|
|
|
def endorsements(conn, params), do: MastodonAPIController.empty_array(conn, params)
|
|
|
|
|
|
|
|
@doc "GET /api/v1/identity_proofs"
|
|
|
|
def identity_proofs(conn, params), do: MastodonAPIController.empty_array(conn, params)
|
2019-09-30 06:10:54 -06:00
|
|
|
end
|