logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/
commit: 065200e92e3917a84ad64d1e9a4e9361cf5e62a6
parent 99fbe0418a1bae703b6ae052c9ffd60c361f0249
Author: Nicole Mikołajczyk <git@mkljczk.pl>
Date:   Tue,  6 May 2025 12:37:37 +0200

Support new Mastodon API for endorsed accounts

Signed-off-by: Nicole Mikołajczyk <git@mkljczk.pl>

Diffstat:

Achangelog.d/endorsements-api.change1+
Mlib/pleroma/web/api_spec/operations/account_operation.ex26++++++++++++++++++++++++--
Mlib/pleroma/web/api_spec/operations/pleroma_account_operation.ex19-------------------
Mlib/pleroma/web/mastodon_api/controllers/account_controller.ex24++++++++++++++++++++----
Mlib/pleroma/web/pleroma_api/controllers/account_controller.ex25+------------------------
Mlib/pleroma/web/router.ex11+++++++++--
Mtest/pleroma/web/mastodon_api/controllers/account_controller_test.exs33+++++++++++++++++++++++++++++----
Mtest/pleroma/web/pleroma_api/controllers/account_controller_test.exs25-------------------------
8 files changed, 84 insertions(+), 80 deletions(-)

diff --git a/changelog.d/endorsements-api.change b/changelog.d/endorsements-api.change @@ -0,0 +1 @@ +Support new Mastodon API for endorsed accounts diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -383,6 +383,28 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do } end + def endorsements_operation do + %Operation{ + tags: ["Retrieve account information"], + summary: "Endorsements", + description: "Returns endorsed accounts", + operationId: "AccountController.endorsements", + parameters: [ + with_relationships_param(), + %Reference{"$ref": "#/components/parameters/accountIdOrNickname"} + ], + responses: %{ + 200 => + Operation.response( + "Array of Accounts", + "application/json", + array_of_accounts() + ), + 404 => Operation.response("Not Found", "application/json", ApiError) + } + } + end + def remove_from_followers_operation do %Operation{ tags: ["Account actions"], @@ -485,11 +507,11 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do } end - def endorsements_operation do + def own_endorsements_operation do %Operation{ tags: ["Retrieve account information"], summary: "Endorsements", - operationId: "AccountController.endorsements", + operationId: "AccountController.own_endorsements", description: "Returns endorsed accounts", security: [%{"oAuth" => ["read:accounts"]}], responses: %{ diff --git a/lib/pleroma/web/api_spec/operations/pleroma_account_operation.ex b/lib/pleroma/web/api_spec/operations/pleroma_account_operation.ex @@ -64,25 +64,6 @@ defmodule Pleroma.Web.ApiSpec.PleromaAccountOperation do } end - def endorsements_operation do - %Operation{ - tags: ["Retrieve account information"], - summary: "Endorsements", - description: "Returns endorsed accounts", - operationId: "PleromaAPI.AccountController.endorsements", - parameters: [with_relationships_param(), id_param()], - responses: %{ - 200 => - Operation.response( - "Array of Accounts", - "application/json", - AccountOperation.array_of_accounts() - ), - 404 => Operation.response("Not Found", "application/json", ApiError) - } - } - end - def subscribe_operation do %Operation{ deprecated: true, diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -38,7 +38,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do plug( OAuthScopesPlug, %{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]} - when action in [:show, :followers, :following] + when action in [:show, :followers, :following, :endorsements] ) plug( @@ -50,7 +50,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do plug( OAuthScopesPlug, %{scopes: ["read:accounts"]} - when action in [:verify_credentials, :endorsements] + when action in [:verify_credentials, :endorsements, :own_endorsements] ) plug( @@ -89,7 +89,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do @relationship_actions [:follow, :unfollow, :remove_from_followers] @needs_account ~W( followers following lists follow unfollow mute unmute block unblock - note endorse unendorse remove_from_followers + note endorse unendorse endorsements remove_from_followers )a plug( @@ -549,6 +549,22 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do end end + @doc "GET /api/v1/accounts/:id/endorsements" + def endorsements(%{assigns: %{user: for_user, account: user}} = conn, params) do + users = + user + |> User.endorsed_users_relation(_restrict_deactivated = true) + |> Pleroma.Repo.all() + + conn + |> render("index.json", + for: for_user, + users: users, + as: :user, + embed_relationships: embed_relationships?(params) + ) + end + @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"} @@ -624,7 +640,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do end @doc "GET /api/v1/endorsements" - def endorsements(%{assigns: %{user: user}} = conn, params) do + def own_endorsements(%{assigns: %{user: user}} = conn, params) do users = user |> User.endorsed_users_relation(_restrict_deactivated = true) diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -9,7 +9,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do only: [ json_response: 3, add_link_headers: 2, - embed_relationships?: 1, assign_account_by_id: 2 ] @@ -47,12 +46,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do plug( OAuthScopesPlug, - %{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]} - when action == :endorsements - ) - - plug( - OAuthScopesPlug, %{scopes: ["read:accounts"]} when action == :birthdays ) @@ -60,7 +53,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do plug( :assign_account_by_id - when action in [:favourites, :endorsements, :subscribe, :unsubscribe] + when action in [:favourites, :subscribe, :unsubscribe] ) defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation @@ -109,22 +102,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do ) end - @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) - |> Pleroma.Repo.all() - - conn - |> render("index.json", - for: for_user, - users: users, - as: :user, - embed_relationships: embed_relationships?(params) - ) - end - @doc "POST /api/v1/pleroma/accounts/:id/subscribe" def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do with {:ok, _subscription} <- User.subscribe(user, subscription_target) do diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex @@ -607,7 +607,6 @@ defmodule Pleroma.Web.Router do scope [] do pipe_through(:api) get("/accounts/:id/favourites", AccountController, :favourites) - get("/accounts/:id/endorsements", AccountController, :endorsements) get("/statuses/:id/quotes", StatusController, :quotes) end @@ -636,6 +635,11 @@ defmodule Pleroma.Web.Router do get("/accounts/:id/scrobbles", ScrobbleController, :index) end + scope "/api/v1/pleroma", Pleroma.Web.MastodonAPI do + pipe_through(:api) + get("/accounts/:id/endorsements", AccountController, :endorsements) + end + scope "/api/v2/pleroma", Pleroma.Web.PleromaAPI do scope [] do pipe_through(:authenticated_api) @@ -652,7 +656,7 @@ defmodule Pleroma.Web.Router do get("/accounts/relationships", AccountController, :relationships) get("/accounts/familiar_followers", AccountController, :familiar_followers) get("/accounts/:id/lists", AccountController, :lists) - get("/endorsements", AccountController, :endorsements) + get("/endorsements", AccountController, :own_endorsements) get("/blocks", AccountController, :blocks) get("/mutes", AccountController, :mutes) @@ -666,6 +670,8 @@ defmodule Pleroma.Web.Router do post("/accounts/:id/note", AccountController, :note) post("/accounts/:id/pin", AccountController, :endorse) post("/accounts/:id/unpin", AccountController, :unendorse) + post("/accounts/:id/endorse", AccountController, :endorse) + post("/accounts/:id/unendorse", AccountController, :unendorse) post("/accounts/:id/remove_from_followers", AccountController, :remove_from_followers) get("/conversations", ConversationController, :index) @@ -781,6 +787,7 @@ defmodule Pleroma.Web.Router do get("/accounts/:id/statuses", AccountController, :statuses) get("/accounts/:id/followers", AccountController, :followers) get("/accounts/:id/following", AccountController, :following) + get("/accounts/:id/endorsements", AccountController, :endorsements) get("/accounts/:id", AccountController, :show) post("/accounts", AccountController, :create) diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -2123,7 +2123,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert %{"id" => ^id1, "endorsed" => true} = conn |> put_req_header("content-type", "application/json") - |> post("/api/v1/accounts/#{id1}/pin") + |> post("/api/v1/accounts/#{id1}/endorse") |> json_response_and_validate_schema(200) assert [%{"id" => ^id1}] = @@ -2142,7 +2142,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert %{"id" => ^id1, "endorsed" => false} = conn |> put_req_header("content-type", "application/json") - |> post("/api/v1/accounts/#{id1}/unpin") + |> post("/api/v1/accounts/#{id1}/unendorse") |> json_response_and_validate_schema(200) assert [] = @@ -2161,15 +2161,40 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do conn |> put_req_header("content-type", "application/json") - |> post("/api/v1/accounts/#{id1}/pin") + |> post("/api/v1/accounts/#{id1}/endorse") |> json_response_and_validate_schema(200) assert %{"error" => "You have already pinned the maximum number of users"} = conn |> assign(:user, user) - |> post("/api/v1/accounts/#{id2}/pin") + |> post("/api/v1/accounts/#{id2}/endorse") |> json_response_and_validate_schema(400) end + + test "returns a list of pinned accounts", %{conn: conn} do + clear_config([:instance, :max_endorsed_users], 3) + + %{id: id1} = user1 = insert(:user) + %{id: id2} = user2 = insert(:user) + %{id: id3} = user3 = insert(:user) + + CommonAPI.follow(user2, user1) + CommonAPI.follow(user3, user1) + + User.endorse(user1, user2) + User.endorse(user1, user3) + + [%{"id" => ^id2}, %{"id" => ^id3}] = + conn + |> get("/api/v1/accounts/#{id1}/endorsements") + |> json_response_and_validate_schema(200) + end + + test "returns 404 error when specified user is not exist", %{conn: conn} do + conn = get(conn, "/api/v1/accounts/test/endorsements") + + assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"} + end end describe "familiar followers" do diff --git a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs @@ -280,31 +280,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do end end - describe "account endorsements" do - test "returns a list of pinned accounts", %{conn: conn} do - %{id: id1} = user1 = insert(:user) - %{id: id2} = user2 = insert(:user) - %{id: id3} = user3 = insert(:user) - - CommonAPI.follow(user2, user1) - CommonAPI.follow(user3, user1) - - User.endorse(user1, user2) - User.endorse(user1, user3) - - [%{"id" => ^id2}, %{"id" => ^id3}] = - conn - |> get("/api/v1/pleroma/accounts/#{id1}/endorsements") - |> json_response_and_validate_schema(200) - end - - test "returns 404 error when specified user is not exist", %{conn: conn} do - conn = get(conn, "/api/v1/pleroma/accounts/test/endorsements") - - assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"} - end - end - describe "birthday reminders" do test "returns a list of friends having birthday on specified day" do %{user: user, conn: conn} = oauth_access(["read:accounts"])