Merge branch 'endorsements-api' into 'develop'
Support new Mastodon API for endorsed accounts See merge request pleroma/pleroma!4361
This commit is contained in:
commit
e81e0d64c1
8 changed files with 84 additions and 84 deletions
1
changelog.d/endorsements-api.change
Normal file
1
changelog.d/endorsements-api.change
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Support new Mastodon API for endorsed accounts
|
||||||
|
|
@ -398,6 +398,28 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
|
||||||
}
|
}
|
||||||
end
|
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
|
def remove_from_followers_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
tags: ["Account actions"],
|
tags: ["Account actions"],
|
||||||
|
|
@ -500,11 +522,11 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def endorsements_operation do
|
def own_endorsements_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
tags: ["Retrieve account information"],
|
tags: ["Retrieve account information"],
|
||||||
summary: "Endorsements",
|
summary: "Endorsements",
|
||||||
operationId: "AccountController.endorsements",
|
operationId: "AccountController.own_endorsements",
|
||||||
description: "Returns endorsed accounts",
|
description: "Returns endorsed accounts",
|
||||||
security: [%{"oAuth" => ["read:accounts"]}],
|
security: [%{"oAuth" => ["read:accounts"]}],
|
||||||
responses: %{
|
responses: %{
|
||||||
|
|
|
||||||
|
|
@ -64,25 +64,6 @@ defmodule Pleroma.Web.ApiSpec.PleromaAccountOperation do
|
||||||
}
|
}
|
||||||
end
|
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
|
def subscribe_operation do
|
||||||
%Operation{
|
%Operation{
|
||||||
deprecated: true,
|
deprecated: true,
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|
||||||
plug(
|
plug(
|
||||||
OAuthScopesPlug,
|
OAuthScopesPlug,
|
||||||
%{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
|
%{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
|
||||||
when action in [:show, :followers, :following]
|
when action in [:show, :followers, :following, :endorsements]
|
||||||
)
|
)
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
|
|
@ -50,7 +50,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|
||||||
plug(
|
plug(
|
||||||
OAuthScopesPlug,
|
OAuthScopesPlug,
|
||||||
%{scopes: ["read:accounts"]}
|
%{scopes: ["read:accounts"]}
|
||||||
when action in [:verify_credentials, :endorsements]
|
when action in [:verify_credentials, :endorsements, :own_endorsements]
|
||||||
)
|
)
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
|
|
@ -89,7 +89,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|
||||||
@relationship_actions [:follow, :unfollow, :remove_from_followers]
|
@relationship_actions [:follow, :unfollow, :remove_from_followers]
|
||||||
@needs_account ~W(
|
@needs_account ~W(
|
||||||
followers following lists follow unfollow mute unmute block unblock
|
followers following lists follow unfollow mute unmute block unblock
|
||||||
note endorse unendorse remove_from_followers
|
note endorse unendorse endorsements remove_from_followers
|
||||||
)a
|
)a
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
|
|
@ -555,6 +555,22 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|
||||||
end
|
end
|
||||||
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"
|
@doc "POST /api/v1/accounts/:id/remove_from_followers"
|
||||||
def remove_from_followers(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
|
def remove_from_followers(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
|
||||||
{:error, "Can not unfollow yourself"}
|
{:error, "Can not unfollow yourself"}
|
||||||
|
|
@ -631,7 +647,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc "GET /api/v1/endorsements"
|
@doc "GET /api/v1/endorsements"
|
||||||
def endorsements(%{assigns: %{user: user}} = conn, params) do
|
def own_endorsements(%{assigns: %{user: user}} = conn, params) do
|
||||||
users =
|
users =
|
||||||
user
|
user
|
||||||
|> User.endorsed_users_relation(_restrict_deactivated = true)
|
|> User.endorsed_users_relation(_restrict_deactivated = true)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
|
||||||
only: [
|
only: [
|
||||||
json_response: 3,
|
json_response: 3,
|
||||||
add_link_headers: 2,
|
add_link_headers: 2,
|
||||||
embed_relationships?: 1,
|
|
||||||
assign_account_by_id: 2
|
assign_account_by_id: 2
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -45,12 +44,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
|
||||||
%{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
|
%{scopes: ["read:favourites"], fallback: :proceed_unauthenticated} when action == :favourites
|
||||||
)
|
)
|
||||||
|
|
||||||
plug(
|
|
||||||
OAuthScopesPlug,
|
|
||||||
%{fallback: :proceed_unauthenticated, scopes: ["read:accounts"]}
|
|
||||||
when action == :endorsements
|
|
||||||
)
|
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
OAuthScopesPlug,
|
OAuthScopesPlug,
|
||||||
%{scopes: ["read:accounts"]} when action == :birthdays
|
%{scopes: ["read:accounts"]} when action == :birthdays
|
||||||
|
|
@ -60,7 +53,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
:assign_account_by_id
|
: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
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaAccountOperation
|
||||||
|
|
@ -109,22 +102,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
|
||||||
)
|
)
|
||||||
end
|
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"
|
@doc "POST /api/v1/pleroma/accounts/:id/subscribe"
|
||||||
def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
|
def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do
|
||||||
with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
|
with {:ok, _subscription} <- User.subscribe(user, subscription_target) do
|
||||||
|
|
|
||||||
|
|
@ -608,7 +608,6 @@ defmodule Pleroma.Web.Router do
|
||||||
scope [] do
|
scope [] do
|
||||||
pipe_through(:api)
|
pipe_through(:api)
|
||||||
get("/accounts/:id/favourites", AccountController, :favourites)
|
get("/accounts/:id/favourites", AccountController, :favourites)
|
||||||
get("/accounts/:id/endorsements", AccountController, :endorsements)
|
|
||||||
|
|
||||||
get("/statuses/:id/quotes", StatusController, :quotes)
|
get("/statuses/:id/quotes", StatusController, :quotes)
|
||||||
end
|
end
|
||||||
|
|
@ -637,6 +636,11 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/accounts/:id/scrobbles", ScrobbleController, :index)
|
get("/accounts/:id/scrobbles", ScrobbleController, :index)
|
||||||
end
|
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 "/api/v2/pleroma", Pleroma.Web.PleromaAPI do
|
||||||
scope [] do
|
scope [] do
|
||||||
pipe_through(:authenticated_api)
|
pipe_through(:authenticated_api)
|
||||||
|
|
@ -653,7 +657,7 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/accounts/relationships", AccountController, :relationships)
|
get("/accounts/relationships", AccountController, :relationships)
|
||||||
get("/accounts/familiar_followers", AccountController, :familiar_followers)
|
get("/accounts/familiar_followers", AccountController, :familiar_followers)
|
||||||
get("/accounts/:id/lists", AccountController, :lists)
|
get("/accounts/:id/lists", AccountController, :lists)
|
||||||
get("/endorsements", AccountController, :endorsements)
|
get("/endorsements", AccountController, :own_endorsements)
|
||||||
get("/blocks", AccountController, :blocks)
|
get("/blocks", AccountController, :blocks)
|
||||||
get("/mutes", AccountController, :mutes)
|
get("/mutes", AccountController, :mutes)
|
||||||
|
|
||||||
|
|
@ -667,6 +671,8 @@ defmodule Pleroma.Web.Router do
|
||||||
post("/accounts/:id/note", AccountController, :note)
|
post("/accounts/:id/note", AccountController, :note)
|
||||||
post("/accounts/:id/pin", AccountController, :endorse)
|
post("/accounts/:id/pin", AccountController, :endorse)
|
||||||
post("/accounts/:id/unpin", AccountController, :unendorse)
|
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)
|
post("/accounts/:id/remove_from_followers", AccountController, :remove_from_followers)
|
||||||
|
|
||||||
get("/conversations", ConversationController, :index)
|
get("/conversations", ConversationController, :index)
|
||||||
|
|
@ -782,6 +788,7 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/accounts/:id/statuses", AccountController, :statuses)
|
get("/accounts/:id/statuses", AccountController, :statuses)
|
||||||
get("/accounts/:id/followers", AccountController, :followers)
|
get("/accounts/:id/followers", AccountController, :followers)
|
||||||
get("/accounts/:id/following", AccountController, :following)
|
get("/accounts/:id/following", AccountController, :following)
|
||||||
|
get("/accounts/:id/endorsements", AccountController, :endorsements)
|
||||||
get("/accounts/:id", AccountController, :show)
|
get("/accounts/:id", AccountController, :show)
|
||||||
|
|
||||||
post("/accounts", AccountController, :create)
|
post("/accounts", AccountController, :create)
|
||||||
|
|
|
||||||
|
|
@ -2134,7 +2134,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
||||||
assert %{"id" => ^id1, "endorsed" => true} =
|
assert %{"id" => ^id1, "endorsed" => true} =
|
||||||
conn
|
conn
|
||||||
|> put_req_header("content-type", "application/json")
|
|> 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)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert [%{"id" => ^id1}] =
|
assert [%{"id" => ^id1}] =
|
||||||
|
|
@ -2153,7 +2153,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
||||||
assert %{"id" => ^id1, "endorsed" => false} =
|
assert %{"id" => ^id1, "endorsed" => false} =
|
||||||
conn
|
conn
|
||||||
|> put_req_header("content-type", "application/json")
|
|> 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)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert [] =
|
assert [] =
|
||||||
|
|
@ -2172,15 +2172,40 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_req_header("content-type", "application/json")
|
|> 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)
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
assert %{"error" => "You have already pinned the maximum number of users"} =
|
assert %{"error" => "You have already pinned the maximum number of users"} =
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> post("/api/v1/accounts/#{id2}/pin")
|
|> post("/api/v1/accounts/#{id2}/endorse")
|
||||||
|> json_response_and_validate_schema(400)
|
|> json_response_and_validate_schema(400)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "familiar followers" do
|
describe "familiar followers" do
|
||||||
|
|
|
||||||
|
|
@ -280,35 +280,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
|
||||||
end
|
end
|
||||||
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)
|
|
||||||
|
|
||||||
response =
|
|
||||||
conn
|
|
||||||
|> get("/api/v1/pleroma/accounts/#{id1}/endorsements")
|
|
||||||
|> json_response_and_validate_schema(200)
|
|
||||||
|
|
||||||
assert length(response) == 2
|
|
||||||
assert Enum.any?(response, fn user -> user["id"] == id2 end)
|
|
||||||
assert Enum.any?(response, fn user -> user["id"] == id3 end)
|
|
||||||
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
|
describe "birthday reminders" do
|
||||||
test "returns a list of friends having birthday on specified day" do
|
test "returns a list of friends having birthday on specified day" do
|
||||||
%{user: user, conn: conn} = oauth_access(["read:accounts"])
|
%{user: user, conn: conn} = oauth_access(["read:accounts"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue