2019-09-30 01:28:12 -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 01:28:12 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.PleromaAPI.AccountController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper,
|
2021-11-29 04:43:29 -07:00
|
|
|
only: [
|
|
|
|
json_response: 3,
|
|
|
|
add_link_headers: 2,
|
|
|
|
embed_relationships?: 1,
|
|
|
|
assign_account_by_id: 2
|
|
|
|
]
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2020-06-24 04:07:47 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
|
|
|
alias Pleroma.Web.Plugs.RateLimiter
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
require Pleroma.Constants
|
|
|
|
|
2020-06-16 11:00:54 -06:00
|
|
|
plug(
|
|
|
|
Majic.Plug,
|
|
|
|
[pool: Pleroma.MajicPool] when action in [:update_avatar, :update_background, :update_banner]
|
|
|
|
)
|
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
plug(
|
|
|
|
OpenApiSpex.Plug.PutApiSpec,
|
|
|
|
[module: Pleroma.Web.ApiSpec] when action == :confirmation_resend
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2021-06-08 18:14:12 -06:00
|
|
|
plug(:skip_auth when action == :confirmation_resend)
|
2020-04-21 07:29:19 -06:00
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["follow", "write:follows"]} when action in [:subscribe, :unsubscribe]
|
|
|
|
)
|
|
|
|
|
2020-04-24 13:25:27 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
|
|
|
|
)
|
2019-10-02 11:42:40 -06:00
|
|
|
|
2021-11-29 04:43:29 -07:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
|
|
|
|
when action == :endorsements
|
|
|
|
)
|
|
|
|
|
2022-01-18 06:57:48 -07:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:accounts"]} when action == :birthdays
|
|
|
|
)
|
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
|
2020-04-21 07:29:19 -06:00
|
|
|
|
2022-01-10 13:35:55 -07:00
|
|
|
plug(
|
|
|
|
:assign_account_by_id
|
|
|
|
when action in [:favourites, :endorsements, :subscribe, :unsubscribe]
|
|
|
|
)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
|
|
|
|
|
2019-09-30 01:28:12 -06:00
|
|
|
@doc "POST /api/v1/pleroma/accounts/confirmation_resend"
|
|
|
|
def confirmation_resend(conn, params) do
|
2020-05-13 09:06:25 -06:00
|
|
|
nickname_or_email = params[:email] || params[:nickname]
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
|
2021-02-04 13:33:49 -07:00
|
|
|
{:ok, _} <- User.maybe_send_confirmation_email(user) do
|
2019-09-30 01:28:12 -06:00
|
|
|
json_response(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/pleroma/accounts/:id/favourites"
|
2019-10-16 12:59:21 -06:00
|
|
|
def favourites(%{assigns: %{account: %{hide_favorites: true}}} = conn, _params) do
|
2019-09-30 01:28:12 -06: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 11:33:16 -06:00
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:favorited_by, user.ap_id)
|
|
|
|
|> Map.put(:blocking_user, for_user)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
recipients =
|
|
|
|
if for_user do
|
2019-10-10 13:35:32 -06:00
|
|
|
[Pleroma.Constants.as_public()] ++ [for_user.ap_id | User.following(for_user)]
|
2019-09-30 01:28:12 -06: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 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: for_user,
|
2020-05-09 09:05:44 -06:00
|
|
|
as: :activity
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
|
2021-11-29 04:43:29 -07:00
|
|
|
@doc "GET /api/v1/pleroma/accounts/:id/endorsements"
|
|
|
|
def endorsements(%{assigns: %{user: for_user, account: user}} = conn, params) do
|
|
|
|
users =
|
|
|
|
user
|
|
|
|
|> User.endorsed_users_relation(_restrict_deactivated = true)
|
2022-01-10 13:35:55 -07:00
|
|
|
|> Pleroma.Repo.all()
|
2021-11-29 04:43:29 -07:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> render("index.json",
|
|
|
|
for: for_user,
|
|
|
|
users: users,
|
|
|
|
as: :user,
|
|
|
|
embed_relationships: embed_relationships?(params)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-30 01:28:12 -06:00
|
|
|
@doc "POST /api/v1/pleroma/accounts/:id/subscribe"
|
|
|
|
def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
|
2019-11-20 05:46:11 -07:00
|
|
|
with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
|
2019-09-30 01:28:12 -06: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 05:46:11 -07:00
|
|
|
with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do
|
2019-09-30 01:28:12 -06:00
|
|
|
render(conn, "relationship.json", user: user, target: subscription_target)
|
|
|
|
else
|
|
|
|
{:error, message} -> json_response(conn, :forbidden, %{error: message})
|
|
|
|
end
|
|
|
|
end
|
2022-01-18 06:57:48 -07:00
|
|
|
|
|
|
|
@doc "GET /api/v1/pleroma/birthday_reminders"
|
|
|
|
def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do
|
|
|
|
birthdays =
|
2022-01-18 11:29:21 -07:00
|
|
|
User.get_friends_birthdays_query(user, day, month)
|
2022-01-18 06:57:48 -07:00
|
|
|
|> Pleroma.Repo.all()
|
|
|
|
|
2022-01-18 10:02:01 -07:00
|
|
|
conn
|
|
|
|
|> render("index.json",
|
|
|
|
for: user,
|
|
|
|
users: birthdays,
|
|
|
|
as: :user
|
|
|
|
)
|
2022-01-18 06:57:48 -07:00
|
|
|
end
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|