From b5b0a0d3327c86e7dc48ca5023e6bef81c6f89c7 Mon Sep 17 00:00:00 2001 From: Fox Date: Thu, 9 Mar 2023 12:48:06 +0900 Subject: [PATCH] some fixes --- lib/pleroma/web/activity_pub/activity_pub.ex | 4 +- .../object_validators/common_fields.ex | 2 +- .../web/api_spec/operations/chat_operation.ex | 22 -- lib/pleroma/web/api_spec/schemas/account.ex | 8 +- .../web/mastodon_api/views/instance_view.ex | 8 + test/pleroma/web/node_info_test.exs | 1 + .../controllers/chat_controller_test.exs | 264 ++++++++---------- 7 files changed, 140 insertions(+), 169 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4180a8b8b..72a775e51 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1289,7 +1289,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - defp exclude_chat_messages(query, %{include_chat_messages: true}), do: query defp exclude_chat_messages(query, _) do @@ -1627,8 +1626,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do shared_inbox: shared_inbox, pinned_objects: pinned_objects, nickname: nickname, - accepts_chat_messages: accepts_chat_messages, - pinned_objects: pinned_objects + accepts_chat_messages: accepts_chat_messages } end diff --git a/lib/pleroma/web/activity_pub/object_validators/common_fields.ex b/lib/pleroma/web/activity_pub/object_validators/common_fields.ex index 6527153b2..1eda0e130 100644 --- a/lib/pleroma/web/activity_pub/object_validators/common_fields.ex +++ b/lib/pleroma/web/activity_pub/object_validators/common_fields.ex @@ -27,7 +27,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFields do end end - # All objects except Answer and CHatMessage + # All objects except Answer and ChatMessage defmacro object_fields do quote bind_quoted: binding() do field(:content, :string) diff --git a/lib/pleroma/web/api_spec/operations/chat_operation.ex b/lib/pleroma/web/api_spec/operations/chat_operation.ex index 23cb66392..31fd150de 100644 --- a/lib/pleroma/web/api_spec/operations/chat_operation.ex +++ b/lib/pleroma/web/api_spec/operations/chat_operation.ex @@ -128,28 +128,6 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do } end - def index_operation do - %Operation{ - tags: ["Chats"], - summary: "Retrieve list of chats (unpaginated)", - deprecated: true, - description: - "Deprecated due to no support for pagination. Using [/api/v2/pleroma/chats](#operation/ChatController.index2) instead is recommended.", - operationId: "ChatController.index", - parameters: [ - Operation.parameter(:with_muted, :query, BooleanLike, "Include chats from muted users") - ], - responses: %{ - 200 => Operation.response("The chats of the user", "application/json", chats_response()) - }, - security: [ - %{ - "oAuth" => ["read:chats"] - } - ] - } - end - def index2_operation do %Operation{ tags: ["Chats"], diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index 566cbd357..9d8ea50f6 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -46,8 +46,12 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do type: :boolean, description: "whether the user allows automatically follow moved following accounts" }, - background_image: %Schema{type: :string, nullable: true, format: :uri}, - chat_token: %Schema{type: :string}, + background_image: %Schema{ + type: :string, nullable: true, format: :uri + }, + chat_token: %Schema{ + type: :string + }, is_confirmed: %Schema{ type: :boolean, description: diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index 6e2702984..530fbbbfe 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -70,6 +70,13 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do if Config.get([:media_proxy, :enabled]) do "media_proxy" end, + # backwards compat + if Config.get([:shout, :enabled]) do + "chat" + end, + if Config.get([:shout, :enabled]) do + "shout" + end, if Config.get([:instance, :allow_relay]) do "relay" end, @@ -77,6 +84,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do "safe_dm_mentions" end, "pleroma_emoji_reactions", + "pleroma_chat_messages", if Config.get([:instance, :show_reactions]) do "exposable_reactions" end, diff --git a/test/pleroma/web/node_info_test.exs b/test/pleroma/web/node_info_test.exs index 94a3ccf03..370d159a6 100644 --- a/test/pleroma/web/node_info_test.exs +++ b/test/pleroma/web/node_info_test.exs @@ -133,6 +133,7 @@ defmodule Pleroma.Web.NodeInfoTest do default_features = [ "pleroma_api", + "akkoma_api", "mastodon_api", "mastodon_api_streaming", "polls", diff --git a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs index a817a2dba..1114da242 100644 --- a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs @@ -307,165 +307,147 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do end end - for tested_endpoint <- ["/api/v1/pleroma/chats", "/api/v2/pleroma/chats"] do - describe "GET #{tested_endpoint}" do - setup do: oauth_access(["read:chats"]) + describe "GET /api/v2/pleroma/chats" do + setup do: oauth_access(["read:chats"]) - test "it does not return chats with deleted users", %{conn: conn, user: user} do + test "it does not return chats with deleted users", %{conn: conn, user: user} do + recipient = insert(:user) + {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + + Pleroma.Repo.delete(recipient) + User.invalidate_cache(recipient) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 0 + end + + test "it does not return chats with users you blocked", %{conn: conn, user: user} do + recipient = insert(:user) + + {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 1 + + User.block(user, recipient) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 0 + end + + test "it does not return chats with users you muted", %{conn: conn, user: user} do + recipient = insert(:user) + + {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 1 + + User.mute(user, recipient) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 0 + + result = + conn + |> get("/api/v2/pleroma/chats?with_muted=true") + |> json_response_and_validate_schema(200) + + assert length(result) == 1 + end + + test "it paginates chats", %{conn: conn, user: user} do + Enum.each(1..30, fn _ -> recipient = insert(:user) {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + end) - Pleroma.Repo.delete(recipient) - User.invalidate_cache(recipient) + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + assert length(result) == 20 + last_id = List.last(result)["id"] - assert length(result) == 0 - end + result = + conn + |> get("/api/v2/pleroma/chats?max_id=#{last_id}") + |> json_response_and_validate_schema(200) - test "it does not return chats with users you blocked", %{conn: conn, user: user} do - recipient = insert(:user) + assert length(result) == 10 + end - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + test "it return a list of chats the current user is participating in, in descending order of updates", + %{conn: conn, user: user} do + har = insert(:user) + jafnhar = insert(:user) + tridi = insert(:user) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id) + {:ok, chat_1} = time_travel(chat_1, -3) + {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id) + {:ok, _chat_2} = time_travel(chat_2, -2) + {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id) + {:ok, chat_3} = time_travel(chat_3, -1) - assert length(result) == 1 + # bump the second one + {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id) - User.block(user, recipient) + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + ids = Enum.map(result, & &1["id"]) - assert length(result) == 0 - end + assert ids == [ + chat_2.id |> to_string(), + chat_3.id |> to_string(), + chat_1.id |> to_string() + ] + end - test "it does not return chats with users you muted", %{conn: conn, user: user} do - recipient = insert(:user) + test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{ + conn: conn, + user: user + } do + clear_config([:restrict_unauthenticated, :profiles, :local], true) + clear_config([:restrict_unauthenticated, :profiles, :remote], true) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + user2 = insert(:user) + user3 = insert(:user, local: false) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id) + {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id) - assert length(result) == 1 + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) - User.mute(user, recipient) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 0 - - result = - conn - |> get("#{unquote(tested_endpoint)}?with_muted=true") - |> json_response_and_validate_schema(200) - - assert length(result) == 1 - end - - if tested_endpoint == "/api/v1/pleroma/chats" do - test "it returns all chats", %{conn: conn, user: user} do - Enum.each(1..30, fn _ -> - recipient = insert(:user) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) - end) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 30 - end - else - test "it paginates chats", %{conn: conn, user: user} do - Enum.each(1..30, fn _ -> - recipient = insert(:user) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) - end) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 20 - last_id = List.last(result)["id"] - - result = - conn - |> get(unquote(tested_endpoint) <> "?max_id=#{last_id}") - |> json_response_and_validate_schema(200) - - assert length(result) == 10 - end - end - - test "it return a list of chats the current user is participating in, in descending order of updates", - %{conn: conn, user: user} do - har = insert(:user) - jafnhar = insert(:user) - tridi = insert(:user) - - {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id) - {:ok, chat_1} = time_travel(chat_1, -3) - {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id) - {:ok, _chat_2} = time_travel(chat_2, -2) - {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id) - {:ok, chat_3} = time_travel(chat_3, -1) - - # bump the second one - {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - ids = Enum.map(result, & &1["id"]) - - assert ids == [ - chat_2.id |> to_string(), - chat_3.id |> to_string(), - chat_1.id |> to_string() - ] - end - - test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{ - conn: conn, - user: user - } do - clear_config([:restrict_unauthenticated, :profiles, :local], true) - clear_config([:restrict_unauthenticated, :profiles, :remote], true) - - user2 = insert(:user) - user3 = insert(:user, local: false) - - {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id) - {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - account_ids = Enum.map(result, &get_in(&1, ["account", "id"])) - assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id]) - end + account_ids = Enum.map(result, &get_in(&1, ["account", "id"])) + assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id]) end end end