2019-09-30 07:28:12 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 07:28:12 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.PleromaAPI.AccountController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper,
|
2020-05-09 15:05:44 +00:00
|
|
|
only: [json_response: 3, add_link_headers: 2, assign_account_by_id: 2]
|
2019-09-30 07:28:12 +00:00
|
|
|
|
|
|
|
alias Ecto.Changeset
|
2020-04-24 13:52:38 +00:00
|
|
|
alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
|
2019-10-02 17:42:40 +00:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-09-30 07:28:12 +00:00
|
|
|
alias Pleroma.Plugs.RateLimiter
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
|
|
|
|
|
|
|
require Pleroma.Constants
|
|
|
|
|
2020-05-13 15:06:25 +00:00
|
|
|
plug(
|
|
|
|
OpenApiSpex.Plug.PutApiSpec,
|
|
|
|
[module: Pleroma.Web.ApiSpec] when action == :confirmation_resend
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2020-04-21 13:29:19 +00:00
|
|
|
plug(
|
|
|
|
:skip_plug,
|
2020-04-24 13:52:38 +00:00
|
|
|
[OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :confirmation_resend
|
2020-04-21 13:29:19 +00:00
|
|
|
)
|
|
|
|
|
2019-10-02 17:42:40 +00:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:accounts"]}
|
|
|
|
# Note: the following actions are not permission-secured in Mastodon:
|
|
|
|
when action in [
|
|
|
|
:update_avatar,
|
|
|
|
:update_banner,
|
|
|
|
:update_background
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-04-24 19:25:27 +00:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
|
|
|
|
)
|
2019-10-02 17:42:40 +00:00
|
|
|
|
2019-11-11 12:13:06 +00:00
|
|
|
plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
|
2020-04-21 13:29:19 +00:00
|
|
|
|
2019-09-30 07:28:12 +00:00
|
|
|
plug(:assign_account_by_id when action in [:favourites, :subscribe, :unsubscribe])
|
|
|
|
plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
|
|
|
|
|
2020-05-13 15:06:25 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
|
|
|
|
|
2019-09-30 07:28:12 +00:00
|
|
|
@doc "POST /api/v1/pleroma/accounts/confirmation_resend"
|
|
|
|
def confirmation_resend(conn, params) do
|
2020-05-13 15:06:25 +00:00
|
|
|
nickname_or_email = params[:email] || params[:nickname]
|
2019-09-30 07:28:12 +00:00
|
|
|
|
|
|
|
with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
|
|
|
|
{:ok, _} <- User.try_send_confirmation_email(user) do
|
|
|
|
json_response(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "PATCH /api/v1/pleroma/accounts/update_avatar"
|
2020-05-13 15:06:25 +00:00
|
|
|
def update_avatar(%{assigns: %{user: user}, body_params: %{img: ""}} = conn, _) do
|
2020-04-03 11:03:32 +00:00
|
|
|
{:ok, _user} =
|
2019-09-30 07:28:12 +00:00
|
|
|
user
|
|
|
|
|> Changeset.change(%{avatar: nil})
|
|
|
|
|> User.update_and_set_cache()
|
|
|
|
|
|
|
|
json(conn, %{url: nil})
|
|
|
|
end
|
|
|
|
|
2020-05-13 15:06:25 +00:00
|
|
|
def update_avatar(%{assigns: %{user: user}, body_params: params} = conn, _params) do
|
2019-09-30 07:28:12 +00:00
|
|
|
{:ok, %{data: data}} = ActivityPub.upload(params, type: :avatar)
|
2020-04-03 11:03:32 +00:00
|
|
|
{:ok, _user} = user |> Changeset.change(%{avatar: data}) |> User.update_and_set_cache()
|
2019-09-30 07:28:12 +00:00
|
|
|
%{"url" => [%{"href" => href} | _]} = data
|
|
|
|
|
|
|
|
json(conn, %{url: href})
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "PATCH /api/v1/pleroma/accounts/update_banner"
|
2020-05-13 15:06:25 +00:00
|
|
|
def update_banner(%{assigns: %{user: user}, body_params: %{banner: ""}} = conn, _) do
|
2020-04-03 11:03:32 +00:00
|
|
|
with {:ok, _user} <- User.update_banner(user, %{}) do
|
2019-09-30 07:28:12 +00:00
|
|
|
json(conn, %{url: nil})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-13 15:06:25 +00:00
|
|
|
def update_banner(%{assigns: %{user: user}, body_params: params} = conn, _) do
|
|
|
|
with {:ok, object} <- ActivityPub.upload(%{img: params[:banner]}, type: :banner),
|
2020-04-03 11:03:32 +00:00
|
|
|
{:ok, _user} <- User.update_banner(user, object.data) do
|
2019-09-30 07:28:12 +00:00
|
|
|
%{"url" => [%{"href" => href} | _]} = object.data
|
|
|
|
|
|
|
|
json(conn, %{url: href})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "PATCH /api/v1/pleroma/accounts/update_background"
|
2020-05-13 15:06:25 +00:00
|
|
|
def update_background(%{assigns: %{user: user}, body_params: %{img: ""}} = conn, _) do
|
2019-10-16 18:59:21 +00:00
|
|
|
with {:ok, _user} <- User.update_background(user, %{}) do
|
2019-09-30 07:28:12 +00:00
|
|
|
json(conn, %{url: nil})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-13 15:06:25 +00:00
|
|
|
def update_background(%{assigns: %{user: user}, body_params: params} = conn, _) do
|
2019-09-30 07:28:12 +00:00
|
|
|
with {:ok, object} <- ActivityPub.upload(params, type: :background),
|
2019-10-16 18:59:21 +00:00
|
|
|
{:ok, _user} <- User.update_background(user, object.data) do
|
2019-09-30 07:28:12 +00:00
|
|
|
%{"url" => [%{"href" => href} | _]} = object.data
|
|
|
|
|
|
|
|
json(conn, %{url: href})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/pleroma/accounts/:id/favourites"
|
2019-10-16 18:59:21 +00:00
|
|
|
def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
|
2019-09-30 07:28:12 +00:00
|
|
|
render_error(conn, :forbidden, "Can't get favorites")
|
|
|
|
end
|
|
|
|
|
|
|
|
def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
|
|
|
|
params =
|
|
|
|
params
|
2020-06-04 17:33:16 +00:00
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:favorited_by, user.ap_id)
|
|
|
|
|> Map.put(:blocking_user, for_user)
|
2019-09-30 07:28:12 +00:00
|
|
|
|
|
|
|
recipients =
|
|
|
|
if for_user do
|
2019-10-10 19:35:32 +00:00
|
|
|
[Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
|
2019-09-30 07:28:12 +00:00
|
|
|
else
|
|
|
|
[Pleroma.Constants.as_public()]
|
|
|
|
end
|
|
|
|
|
|
|
|
activities =
|
|
|
|
recipients
|
|
|
|
|> ActivityPub.fetch_activities(params)
|
|
|
|
|> Enum.reverse()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
|
|
|
|> put_view(StatusView)
|
2020-04-01 16:49:09 +00:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: for_user,
|
2020-05-09 15:05:44 +00:00
|
|
|
as: :activity
|
2020-04-01 16:49:09 +00:00
|
|
|
)
|
2019-09-30 07:28:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/pleroma/accounts/:id/subscribe"
|
|
|
|
def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
|
2019-11-20 12:46:11 +00:00
|
|
|
with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
|
2019-09-30 07:28:12 +00:00
|
|
|
render(conn, "relationship.json", user: user, target: subscription_target)
|
|
|
|
else
|
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/pleroma/accounts/:id/unsubscribe"
|
|
|
|
def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
|
2019-11-20 12:46:11 +00:00
|
|
|
with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do
|
2019-09-30 07:28:12 +00:00
|
|
|
render(conn, "relationship.json", user: user, target: subscription_target)
|
|
|
|
else
|
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|