From 7e1caddc58bc6850f9780a9cd432b4b839f02e90 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 25 Nov 2021 14:57:36 -0600 Subject: [PATCH 01/10] v2 Suggestions: return empty array --- .../controllers/suggestion_controller.ex | 17 ++++++++++++++++- lib/pleroma/web/router.ex | 2 ++ .../controllers/suggestion_controller_test.exs | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex index 01e122dd9..b941849f5 100644 --- a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex @@ -8,7 +8,7 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do require Logger plug(Pleroma.Web.ApiSpec.CastAndValidate) - plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action == :index) + plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:index, :index2]) def open_api_operation(action) do operation = String.to_existing_atom("#{action}_operation") @@ -26,7 +26,22 @@ def index_operation do } end + def index2_operation do + %OpenApiSpex.Operation{ + tags: ["Suggestions"], + summary: "Follow suggestions (Not implemented)", + operationId: "SuggestionController.index2", + responses: %{ + 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response() + } + } + end + @doc "GET /api/v1/suggestions" def index(conn, params), do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) + + @doc "GET /api/v2/suggestions" + def index2(conn, params), + do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index efca7078a..acf9ce6c2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -594,6 +594,8 @@ defmodule Pleroma.Web.Router do get("/search", SearchController, :search2) post("/media", MediaController, :create2) + + get("/suggestions", SuggestionController, :index2) end scope "/api", Pleroma.Web do diff --git a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs index 168966fc9..5a9aea680 100644 --- a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs @@ -15,4 +15,13 @@ test "returns empty result", %{conn: conn} do assert res == [] end + + test "returns empty result (v2)", %{conn: conn} do + res = + conn + |> get("/api/v2/suggestions") + |> json_response_and_validate_schema(200) + + assert res == [] + end end From b17360cd7c92d8b2337fa4fd175c3e1312eb352e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 14:33:27 -0600 Subject: [PATCH 02/10] v2 Suggestions: rudimentary API response --- lib/pleroma/user.ex | 1 + lib/pleroma/user/query.ex | 4 +++ .../controllers/suggestion_controller.ex | 15 ++++++-- .../web/mastodon_api/views/suggestion_view.ex | 28 +++++++++++++++ .../20211126191138_add_suggestions.exs | 9 +++++ test/pleroma/user/query_test.exs | 10 ++++++ .../suggestion_controller_test.exs | 7 ++-- .../views/suggestion_view_test.exs | 34 +++++++++++++++++++ 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 lib/pleroma/web/mastodon_api/views/suggestion_view.ex create mode 100644 priv/repo/migrations/20211126191138_add_suggestions.exs create mode 100644 test/pleroma/web/mastodon_api/views/suggestion_view_test.exs diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 62506f37a..6d62e9b43 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -149,6 +149,7 @@ defmodule Pleroma.User do field(:last_active_at, :naive_datetime) field(:disclose_client, :boolean, default: true) field(:pinned_objects, :map, default: %{}) + field(:is_suggested, :boolean, default: false) embeds_one( :notification_settings, diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index ac807fc79..334e395fb 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -167,6 +167,10 @@ defp compose_query({:unconfirmed, _}, query) do where(query, [u], u.is_confirmed == false) end + defp compose_query({:is_suggested, bool}, query) do + where(query, [u], u.is_suggested == ^bool) + end + defp compose_query({:followers, %User{id: id}}, query) do query |> where([u], u.id != ^id) diff --git a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex index b941849f5..a34da98df 100644 --- a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do use Pleroma.Web, :controller + alias Pleroma.User require Logger @@ -29,7 +30,7 @@ def index_operation do def index2_operation do %OpenApiSpex.Operation{ tags: ["Suggestions"], - summary: "Follow suggestions (Not implemented)", + summary: "Follow suggestions", operationId: "SuggestionController.index2", responses: %{ 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response() @@ -42,6 +43,14 @@ def index(conn, params), do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) @doc "GET /api/v2/suggestions" - def index2(conn, params), - do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) + def index2(conn, params) do + limit = Map.get(params, :limit, 40) |> min(80) + + users = + %{is_suggested: true, limit: limit} + |> User.Query.build() + |> Pleroma.Repo.all() + + render(conn, "index.json", %{users: users, source: :staff, skip_visibility_check: true}) + end end diff --git a/lib/pleroma/web/mastodon_api/views/suggestion_view.ex b/lib/pleroma/web/mastodon_api/views/suggestion_view.ex new file mode 100644 index 000000000..865229a88 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/views/suggestion_view.ex @@ -0,0 +1,28 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.MastodonAPI.SuggestionView do + use Pleroma.Web, :view + alias Pleroma.Web.MastodonAPI.AccountView + + @source_types [:staff, :global, :past_interactions] + + def render("index.json", %{users: users} = opts) do + Enum.map(users, fn user -> + opts = + opts + |> Map.put(:user, user) + |> Map.delete(:users) + + render("show.json", opts) + end) + end + + def render("show.json", %{source: source, user: _user} = opts) when source in @source_types do + %{ + source: source, + account: AccountView.render("show.json", opts) + } + end +end diff --git a/priv/repo/migrations/20211126191138_add_suggestions.exs b/priv/repo/migrations/20211126191138_add_suggestions.exs new file mode 100644 index 000000000..5ad604e9d --- /dev/null +++ b/priv/repo/migrations/20211126191138_add_suggestions.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddSuggestions do + use Ecto.Migration + + def change do + alter table(:users) do + add(:is_suggested, :boolean, default: false, null: false) + end + end +end diff --git a/test/pleroma/user/query_test.exs b/test/pleroma/user/query_test.exs index 357016e3e..363da7665 100644 --- a/test/pleroma/user/query_test.exs +++ b/test/pleroma/user/query_test.exs @@ -34,4 +34,14 @@ test "it returns internal users when enabled" do assert %{internal: true} |> Query.build() |> Repo.aggregate(:count) == 2 end end + + test "is_suggested param" do + _user1 = insert(:user, is_suggested: false) + user2 = insert(:user, is_suggested: true) + + assert [^user2] = + %{is_suggested: true} + |> User.Query.build() + |> Repo.all() + end end diff --git a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs index 5a9aea680..407063fa1 100644 --- a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs @@ -4,6 +4,7 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do use Pleroma.Web.ConnCase, async: true + import Pleroma.Factory setup do: oauth_access(["read"]) @@ -16,12 +17,14 @@ test "returns empty result", %{conn: conn} do assert res == [] end - test "returns empty result (v2)", %{conn: conn} do + test "returns v2 suggestions", %{conn: conn} do + %{id: user_id} = insert(:user, is_suggested: true) + res = conn |> get("/api/v2/suggestions") |> json_response_and_validate_schema(200) - assert res == [] + assert [%{"source" => "staff", "account" => %{"id" => ^user_id}}] = res end end diff --git a/test/pleroma/web/mastodon_api/views/suggestion_view_test.exs b/test/pleroma/web/mastodon_api/views/suggestion_view_test.exs new file mode 100644 index 000000000..5aae36ce9 --- /dev/null +++ b/test/pleroma/web/mastodon_api/views/suggestion_view_test.exs @@ -0,0 +1,34 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.MastodonAPI.SuggestionViewTest do + use Pleroma.DataCase, async: true + import Pleroma.Factory + alias Pleroma.Web.MastodonAPI.SuggestionView, as: View + + test "show.json" do + user = insert(:user, is_suggested: true) + json = View.render("show.json", %{user: user, source: :staff, skip_visibility_check: true}) + + assert json.source == :staff + assert json.account.id == user.id + end + + test "index.json" do + user1 = insert(:user, is_suggested: true) + user2 = insert(:user, is_suggested: true) + user3 = insert(:user, is_suggested: true) + + [suggestion1, suggestion2, suggestion3] = + View.render("index.json", %{ + users: [user1, user2, user3], + source: :staff, + skip_visibility_check: true + }) + + assert suggestion1.source == :staff + assert suggestion2.account.id == user2.id + assert suggestion3.account.url == user3.ap_id + end +end From e28d990ecba287d5c44ed04c0039b43c8f309e50 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 14:46:29 -0600 Subject: [PATCH 03/10] v2 Suggestions: don't skip visibility check --- .../web/mastodon_api/controllers/suggestion_controller.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex index a34da98df..4f92c1f46 100644 --- a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex @@ -43,7 +43,7 @@ def index(conn, params), do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) @doc "GET /api/v2/suggestions" - def index2(conn, params) do + def index2(%{assigns: %{user: user}} = conn, params) do limit = Map.get(params, :limit, 40) |> min(80) users = @@ -51,6 +51,6 @@ def index2(conn, params) do |> User.Query.build() |> Pleroma.Repo.all() - render(conn, "index.json", %{users: users, source: :staff, skip_visibility_check: true}) + render(conn, "index.json", %{users: users, source: :staff, for: user}) end end From 6c0484d571e4ed4e39fa3f88e6e1d2d7b8de96fa Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 15:19:01 -0600 Subject: [PATCH 04/10] AdminAPI: suggest a user through the API --- docs/development/API/admin_api.md | 40 +++++++++++ lib/pleroma/moderation_log.ex | 20 ++++++ lib/pleroma/user.ex | 16 +++++ .../admin_api/controllers/user_controller.ex | 30 ++++++++- .../operations/admin/user_operation.ex | 66 ++++++++++++++++++- lib/pleroma/web/router.ex | 3 + test/pleroma/user_test.exs | 32 +++++++++ .../controllers/user_controller_test.exs | 52 +++++++++++++++ 8 files changed, 257 insertions(+), 2 deletions(-) diff --git a/docs/development/API/admin_api.md b/docs/development/API/admin_api.md index 8f855d251..79531c45b 100644 --- a/docs/development/API/admin_api.md +++ b/docs/development/API/admin_api.md @@ -261,6 +261,46 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret } ``` +## `PATCH /api/v1/pleroma/admin/users/suggest` + +### Suggest a user + +Adds the user(s) to follower recommendations. + +- Params: + - `nicknames`: nicknames array +- Response: + +```json +{ + users: [ + { + // user object + } + ] +} +``` + +## `PATCH /api/v1/pleroma/admin/users/unsuggest` + +### Unsuggest a user + +Removes the user(s) from follower recommendations. + +- Params: + - `nicknames`: nicknames array +- Response: + +```json +{ + users: [ + { + // user object + } + ] +} +``` + ## `GET /api/v1/pleroma/admin/users/:nickname_or_id` ### Retrive the details of a user diff --git a/lib/pleroma/moderation_log.ex b/lib/pleroma/moderation_log.ex index 1849cacc8..ca032657c 100644 --- a/lib/pleroma/moderation_log.ex +++ b/lib/pleroma/moderation_log.ex @@ -338,6 +338,26 @@ def get_log_entry_message(%ModerationLog{ "@#{actor_nickname} approved users: #{users_to_nicknames_string(users)}" end + def get_log_entry_message(%ModerationLog{ + data: %{ + "actor" => %{"nickname" => actor_nickname}, + "action" => "add_suggestion", + "subject" => users + } + }) do + "@#{actor_nickname} added suggested users: #{users_to_nicknames_string(users)}" + end + + def get_log_entry_message(%ModerationLog{ + data: %{ + "actor" => %{"nickname" => actor_nickname}, + "action" => "remove_suggestion", + "subject" => users + } + }) do + "@#{actor_nickname} removed suggested users: #{users_to_nicknames_string(users)}" + end + def get_log_entry_message(%ModerationLog{ data: %{ "actor" => %{"nickname" => actor_nickname}, diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6d62e9b43..880f027bc 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1678,6 +1678,22 @@ def confirm(%User{is_confirmed: false} = user) do def confirm(%User{} = user), do: {:ok, user} + def set_suggestion(users, is_suggested) when is_list(users) do + Repo.transaction(fn -> + Enum.map(users, fn user -> + with {:ok, user} <- set_suggestion(user, is_suggested), do: user + end) + end) + end + + def set_suggestion(%User{is_suggested: is_suggested} = user, is_suggested), do: {:ok, user} + + def set_suggestion(%User{} = user, is_suggested) when is_boolean(is_suggested) do + user + |> change(is_suggested: is_suggested) + |> update_and_set_cache() + end + def update_notification_settings(%User{} = user, settings) do user |> cast(%{notification_settings: settings}, []) diff --git a/lib/pleroma/web/admin_api/controllers/user_controller.ex b/lib/pleroma/web/admin_api/controllers/user_controller.ex index 637a0e702..50208a8b7 100644 --- a/lib/pleroma/web/admin_api/controllers/user_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/user_controller.ex @@ -35,7 +35,9 @@ defmodule Pleroma.Web.AdminAPI.UserController do :toggle_activation, :activate, :deactivate, - :approve + :approve, + :suggest, + :unsuggest ] ) @@ -239,6 +241,32 @@ def approve(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = c render(conn, "index.json", users: updated_users) end + def suggest(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do + users = Enum.map(nicknames, &User.get_cached_by_nickname/1) + {:ok, updated_users} = User.set_suggestion(users, true) + + ModerationLog.insert_log(%{ + actor: admin, + subject: users, + action: "add_suggestion" + }) + + render(conn, "index.json", users: updated_users) + end + + def unsuggest(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do + users = Enum.map(nicknames, &User.get_cached_by_nickname/1) + {:ok, updated_users} = User.set_suggestion(users, false) + + ModerationLog.insert_log(%{ + actor: admin, + subject: users, + action: "remove_suggestion" + }) + + render(conn, "index.json", users: updated_users) + end + def index(conn, params) do {page, page_size} = page_params(params) filters = maybe_parse_filters(params[:filters]) diff --git a/lib/pleroma/web/api_spec/operations/admin/user_operation.ex b/lib/pleroma/web/api_spec/operations/admin/user_operation.ex index c9d0bfd7c..57fb1ad65 100644 --- a/lib/pleroma/web/api_spec/operations/admin/user_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/user_operation.ex @@ -216,7 +216,71 @@ def approve_operation do request_body( "Parameters", %Schema{ - description: "POST body for deleting multiple users", + description: "POST body for approving multiple users", + type: :object, + properties: %{ + nicknames: %Schema{ + type: :array, + items: %Schema{type: :string} + } + } + } + ), + responses: %{ + 200 => + Operation.response("Response", "application/json", %Schema{ + type: :object, + properties: %{user: %Schema{type: :array, items: user()}} + }), + 403 => Operation.response("Forbidden", "application/json", ApiError) + } + } + end + + def suggest_operation do + %Operation{ + tags: ["User administration"], + summary: "Suggest multiple users", + operationId: "AdminAPI.UserController.suggest", + security: [%{"oAuth" => ["admin:write:accounts"]}], + parameters: admin_api_params(), + requestBody: + request_body( + "Parameters", + %Schema{ + description: "POST body for adding multiple suggested users", + type: :object, + properties: %{ + nicknames: %Schema{ + type: :array, + items: %Schema{type: :string} + } + } + } + ), + responses: %{ + 200 => + Operation.response("Response", "application/json", %Schema{ + type: :object, + properties: %{user: %Schema{type: :array, items: user()}} + }), + 403 => Operation.response("Forbidden", "application/json", ApiError) + } + } + end + + def unsuggest_operation do + %Operation{ + tags: ["User administration"], + summary: "Unsuggest multiple users", + operationId: "AdminAPI.UserController.unsuggest", + security: [%{"oAuth" => ["admin:write:accounts"]}], + parameters: admin_api_params(), + requestBody: + request_body( + "Parameters", + %Schema{ + description: "POST body for removing multiple suggested users", type: :object, properties: %{ nicknames: %Schema{ diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index acf9ce6c2..1f51bf456 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -194,6 +194,9 @@ defmodule Pleroma.Web.Router do patch("/users/deactivate", UserController, :deactivate) patch("/users/approve", UserController, :approve) + patch("/users/suggest", UserController, :suggest) + patch("/users/unsuggest", UserController, :unsuggest) + get("/relay", RelayController, :index) post("/relay", RelayController, :follow) delete("/relay", RelayController, :unfollow) diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 4021a565d..c6282db78 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -1720,6 +1720,38 @@ test "delete/1 purges a remote user" do assert user.banner == %{} end + describe "set_suggestion" do + test "suggests a user" do + user = insert(:user, is_suggested: false) + refute user.is_suggested + {:ok, user} = User.set_suggestion(user, true) + assert user.is_suggested + end + + test "suggests a list of users" do + unsuggested_users = [ + insert(:user, is_suggested: false), + insert(:user, is_suggested: false), + insert(:user, is_suggested: false) + ] + + {:ok, users} = User.set_suggestion(unsuggested_users, true) + + assert Enum.count(users) == 3 + + Enum.each(users, fn user -> + assert user.is_suggested + end) + end + + test "unsuggests a user" do + user = insert(:user, is_suggested: true) + assert user.is_suggested + {:ok, user} = User.set_suggestion(user, false) + refute user.is_suggested + end + end + test "get_public_key_for_ap_id fetches a user that's not in the db" do assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin") end diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index d9da34f6e..df13f00e6 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -873,6 +873,58 @@ test "PATCH /api/pleroma/admin/users/approve", %{admin: admin, conn: conn} do "@#{admin.nickname} approved users: @#{user_one.nickname}, @#{user_two.nickname}" end + test "PATCH /api/pleroma/admin/users/suggest", %{admin: admin, conn: conn} do + user1 = insert(:user, is_suggested: false) + user2 = insert(:user, is_suggested: false) + + _response = + conn + |> put_req_header("content-type", "application/json") + |> patch( + "/api/pleroma/admin/users/suggest", + %{nicknames: [user1.nickname, user2.nickname]} + ) + |> json_response_and_validate_schema(200) + + [user1, user2] = Repo.reload!([user1, user2]) + + assert user1.is_suggested + assert user2.is_suggested + + log_entry = Repo.one(ModerationLog) + + assert ModerationLog.get_log_entry_message(log_entry) == + "@#{admin.nickname} added suggested users: @#{user1.nickname}, @#{ + user2.nickname + }" + end + + test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do + user1 = insert(:user, is_suggested: true) + user2 = insert(:user, is_suggested: true) + + _response = + conn + |> put_req_header("content-type", "application/json") + |> patch( + "/api/pleroma/admin/users/unsuggest", + %{nicknames: [user1.nickname, user2.nickname]} + ) + |> json_response_and_validate_schema(200) + + [user1, user2] = Repo.reload!([user1, user2]) + + refute user1.is_suggested + refute user2.is_suggested + + log_entry = Repo.one(ModerationLog) + + assert ModerationLog.get_log_entry_message(log_entry) == + "@#{admin.nickname} removed suggested users: @#{user1.nickname}, @#{ + user2.nickname + }" + end + test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do user = insert(:user) From da06e1a17fe45407cd82f83223dc68b8920e1fe8 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 15:32:01 -0600 Subject: [PATCH 05/10] v2 Suggestions: add index on is_suggested column --- priv/repo/migrations/20211126191138_add_suggestions.exs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/priv/repo/migrations/20211126191138_add_suggestions.exs b/priv/repo/migrations/20211126191138_add_suggestions.exs index 5ad604e9d..7cc67d8ef 100644 --- a/priv/repo/migrations/20211126191138_add_suggestions.exs +++ b/priv/repo/migrations/20211126191138_add_suggestions.exs @@ -5,5 +5,7 @@ def change do alter table(:users) do add(:is_suggested, :boolean, default: false, null: false) end + + create_if_not_exists(index(:users, [:is_suggested])) end end From aee55b9a8bc3e643377d5843a1ff5d379aecf0e3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 20:19:29 -0600 Subject: [PATCH 06/10] v2 Suggestions: dismiss a suggestion --- lib/pleroma/ecto_enums.ex | 3 +- .../controllers/suggestion_controller.ex | 30 +++++++++++++++++++ lib/pleroma/web/router.ex | 1 + .../controllers/user_controller_test.exs | 8 ++--- .../suggestion_controller_test.exs | 15 +++++++++- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/ecto_enums.ex b/lib/pleroma/ecto_enums.ex index 2a9addabc..0e3e1e5de 100644 --- a/lib/pleroma/ecto_enums.ex +++ b/lib/pleroma/ecto_enums.ex @@ -9,7 +9,8 @@ mute: 2, reblog_mute: 3, notification_mute: 4, - inverse_subscription: 5 + inverse_subscription: 5, + suggestion_dismiss: 6 ) defenum(Pleroma.FollowingRelationship.State, diff --git a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex index 4f92c1f46..4ebfc737c 100644 --- a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex @@ -5,11 +5,13 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do use Pleroma.Web, :controller alias Pleroma.User + alias Pleroma.UserRelationship require Logger plug(Pleroma.Web.ApiSpec.CastAndValidate) plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:index, :index2]) + plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["write"]} when action in [:dismiss]) def open_api_operation(action) do operation = String.to_existing_atom("#{action}_operation") @@ -38,6 +40,26 @@ def index2_operation do } end + def dismiss_operation do + %OpenApiSpex.Operation{ + tags: ["Suggestions"], + summary: "Remove a suggestion", + operationId: "SuggestionController.dismiss", + parameters: [ + OpenApiSpex.Operation.parameter( + :account_id, + :path, + %OpenApiSpex.Schema{type: :string}, + "Account to dismiss", + required: true + ) + ], + responses: %{ + 200 => Pleroma.Web.ApiSpec.Helpers.empty_object_response() + } + } + end + @doc "GET /api/v1/suggestions" def index(conn, params), do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) @@ -53,4 +75,12 @@ def index2(%{assigns: %{user: user}} = conn, params) do render(conn, "index.json", %{users: users, source: :staff, for: user}) end + + @doc "DELETE /api/v1/suggestions/:account_id" + def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do + with %User{} = target <- User.get_cached_by_id(user_id), + {:ok, _} <- UserRelationship.create(:suggestion_dismiss, source, target) do + json(conn, %{}) + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1f51bf456..5d3a17b98 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -539,6 +539,7 @@ defmodule Pleroma.Web.Router do delete("/push/subscription", SubscriptionController, :delete) get("/suggestions", SuggestionController, :index) + delete("/suggestions/:account_id", SuggestionController, :dismiss) get("/timelines/home", TimelineController, :home) get("/timelines/direct", TimelineController, :direct) diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index df13f00e6..1580ca448 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -894,9 +894,7 @@ test "PATCH /api/pleroma/admin/users/suggest", %{admin: admin, conn: conn} do log_entry = Repo.one(ModerationLog) assert ModerationLog.get_log_entry_message(log_entry) == - "@#{admin.nickname} added suggested users: @#{user1.nickname}, @#{ - user2.nickname - }" + "@#{admin.nickname} added suggested users: @#{user1.nickname}, @#{user2.nickname}" end test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do @@ -920,9 +918,7 @@ test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do log_entry = Repo.one(ModerationLog) assert ModerationLog.get_log_entry_message(log_entry) == - "@#{admin.nickname} removed suggested users: @#{user1.nickname}, @#{ - user2.nickname - }" + "@#{admin.nickname} removed suggested users: @#{user1.nickname}, @#{user2.nickname}" end test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do diff --git a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs index 407063fa1..803a38c67 100644 --- a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs @@ -4,9 +4,10 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do use Pleroma.Web.ConnCase, async: true + alias Pleroma.UserRelationship import Pleroma.Factory - setup do: oauth_access(["read"]) + setup do: oauth_access(["read", "write"]) test "returns empty result", %{conn: conn} do res = @@ -27,4 +28,16 @@ test "returns v2 suggestions", %{conn: conn} do assert [%{"source" => "staff", "account" => %{"id" => ^user_id}}] = res end + + test "dismiss suggestion", %{conn: conn, user: source} do + target = insert(:user, is_suggested: true) + + res = + conn + |> delete("/api/v1/suggestions/#{target.id}") + |> json_response_and_validate_schema(200) + + assert res == %{} + assert UserRelationship.exists?(:suggestion_dismiss, source, target) + end end From 437c1a5a52d6fdde3dd8ce62b3eb4c8d8507b05e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 21:02:14 -0600 Subject: [PATCH 07/10] v2 Suggestions: actually flter out dismissed suggestions --- lib/pleroma/user/query.ex | 1 + .../controllers/suggestion_controller.ex | 20 +++++++++++--- .../suggestion_controller_test.exs | 27 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index 334e395fb..6d4a4ead6 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -46,6 +46,7 @@ defmodule Pleroma.User.Query do unconfirmed: boolean(), is_admin: boolean(), is_moderator: boolean(), + is_suggested: boolean(), super_users: boolean(), invisible: boolean(), internal: boolean(), diff --git a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex index 4ebfc737c..3c5a07b7d 100644 --- a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do use Pleroma.Web, :controller + import Ecto.Query alias Pleroma.User alias Pleroma.UserRelationship @@ -65,15 +66,28 @@ def index(conn, params), do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) @doc "GET /api/v2/suggestions" - def index2(%{assigns: %{user: user}} = conn, params) do + def index2(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do limit = Map.get(params, :limit, 40) |> min(80) users = - %{is_suggested: true, limit: limit} + %{is_suggested: true, invisible: false, limit: limit} |> User.Query.build() + |> where([u], u.id != ^user_id) + |> join(:left, [u], r in UserRelationship, + as: :relationships, + on: + r.target_id == u.id and r.source_id == ^user_id and + r.relationship_type in [:block, :mute, :suggestion_dismiss] + ) + |> where([relationships: r], is_nil(r.target_id)) |> Pleroma.Repo.all() - render(conn, "index.json", %{users: users, source: :staff, for: user}) + render(conn, "index.json", %{ + users: users, + source: :staff, + for: user, + skip_visibility_check: true + }) end @doc "DELETE /api/v1/suggestions/:account_id" diff --git a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs index 803a38c67..8948a52de 100644 --- a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs @@ -29,6 +29,33 @@ test "returns v2 suggestions", %{conn: conn} do assert [%{"source" => "staff", "account" => %{"id" => ^user_id}}] = res end + test "returns v2 suggestions excluding dismissed accounts", %{conn: conn} do + %{id: user_id} = insert(:user, is_suggested: true) + + conn + |> delete("/api/v1/suggestions/#{user_id}") + |> json_response_and_validate_schema(200) + + res = + conn + |> get("/api/v2/suggestions") + |> json_response_and_validate_schema(200) + + assert [] = res + end + + test "returns v2 suggestions excluding blocked accounts", %{conn: conn, user: blocker} do + blocked = insert(:user, is_suggested: true) + {:ok, _} = Pleroma.Web.CommonAPI.block(blocker, blocked) + + res = + conn + |> get("/api/v2/suggestions") + |> json_response_and_validate_schema(200) + + assert [] = res + end + test "dismiss suggestion", %{conn: conn, user: source} do target = insert(:user, is_suggested: true) From e5a7547fbe1c8eccafabc458e1cbec1461bcbc9c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 21:42:28 -0600 Subject: [PATCH 08/10] v2 Suggestions: also filter out users you follow --- .../controllers/suggestion_controller.ex | 38 ++++++++++++++----- .../suggestion_controller_test.exs | 15 +++++++- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex index 3c5a07b7d..e913fcf4b 100644 --- a/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do use Pleroma.Web, :controller import Ecto.Query + alias Pleroma.FollowingRelationship alias Pleroma.User alias Pleroma.UserRelationship @@ -66,20 +67,15 @@ def index(conn, params), do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) @doc "GET /api/v2/suggestions" - def index2(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do + def index2(%{assigns: %{user: user}} = conn, params) do limit = Map.get(params, :limit, 40) |> min(80) users = %{is_suggested: true, invisible: false, limit: limit} |> User.Query.build() - |> where([u], u.id != ^user_id) - |> join(:left, [u], r in UserRelationship, - as: :relationships, - on: - r.target_id == u.id and r.source_id == ^user_id and - r.relationship_type in [:block, :mute, :suggestion_dismiss] - ) - |> where([relationships: r], is_nil(r.target_id)) + |> exclude_user(user) + |> exclude_relationships(user, [:block, :mute, :suggestion_dismiss]) + |> exclude_following(user) |> Pleroma.Repo.all() render(conn, "index.json", %{ @@ -90,6 +86,30 @@ def index2(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do }) end + defp exclude_user(query, %User{id: user_id}) do + where(query, [u], u.id != ^user_id) + end + + defp exclude_relationships(query, %User{id: user_id}, relationship_types) do + query + |> join(:left, [u], r in UserRelationship, + as: :user_relationships, + on: + r.target_id == u.id and r.source_id == ^user_id and + r.relationship_type in ^relationship_types + ) + |> where([user_relationships: r], is_nil(r.target_id)) + end + + defp exclude_following(query, %User{id: user_id}) do + query + |> join(:left, [u], r in FollowingRelationship, + as: :following_relationships, + on: r.following_id == u.id and r.follower_id == ^user_id and r.state == :follow_accept + ) + |> where([following_relationships: r], is_nil(r.following_id)) + end + @doc "DELETE /api/v1/suggestions/:account_id" def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do with %User{} = target <- User.get_cached_by_id(user_id), diff --git a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs index 8948a52de..89273e67b 100644 --- a/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs @@ -5,6 +5,7 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do use Pleroma.Web.ConnCase, async: true alias Pleroma.UserRelationship + alias Pleroma.Web.CommonAPI import Pleroma.Factory setup do: oauth_access(["read", "write"]) @@ -46,7 +47,19 @@ test "returns v2 suggestions excluding dismissed accounts", %{conn: conn} do test "returns v2 suggestions excluding blocked accounts", %{conn: conn, user: blocker} do blocked = insert(:user, is_suggested: true) - {:ok, _} = Pleroma.Web.CommonAPI.block(blocker, blocked) + {:ok, _} = CommonAPI.block(blocker, blocked) + + res = + conn + |> get("/api/v2/suggestions") + |> json_response_and_validate_schema(200) + + assert [] = res + end + + test "returns v2 suggestions excluding followed accounts", %{conn: conn, user: follower} do + followed = insert(:user, is_suggested: true) + {:ok, _, _, _} = CommonAPI.follow(follower, followed) res = conn From 8dc1d2201a21d88090c114b59e1d06f76db66897 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 22:45:49 -0600 Subject: [PATCH 09/10] Instance: add v2_suggestions feature --- lib/pleroma/web/mastodon_api/views/instance_view.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index 3528185d5..8284f93f5 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -59,6 +59,7 @@ def features do "mastodon_api", "mastodon_api_streaming", "polls", + "v2_suggestions", "pleroma_explicit_addressing", "shareable_emoji_packs", "multifetch", From 6519f59d91d858273f929dc1c2a36752f6db07a9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 26 Nov 2021 23:10:01 -0600 Subject: [PATCH 10/10] v2 Suggestions: return `is_suggested` through the API --- lib/pleroma/web/admin_api/views/account_view.ex | 1 + lib/pleroma/web/mastodon_api/views/account_view.ex | 1 + .../web/admin_api/controllers/user_controller_test.exs | 7 +++++-- test/pleroma/web/mastodon_api/views/account_view_test.exs | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/admin_api/views/account_view.ex b/lib/pleroma/web/admin_api/views/account_view.ex index fae0c07f0..2f1f7e627 100644 --- a/lib/pleroma/web/admin_api/views/account_view.ex +++ b/lib/pleroma/web/admin_api/views/account_view.ex @@ -80,6 +80,7 @@ def render("show.json", %{user: user}) do "tags" => user.tags || [], "is_confirmed" => user.is_confirmed, "is_approved" => user.is_approved, + "is_suggested" => user.is_suggested, "url" => user.uri || user.ap_id, "registration_reason" => user.registration_reason, "actor_type" => user.actor_type, diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 9e9de33f6..6114e12b1 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -269,6 +269,7 @@ defp do_render("show.json", %{user: user} = opts) do ap_id: user.ap_id, also_known_as: user.also_known_as, is_confirmed: user.is_confirmed, + is_suggested: user.is_suggested, tags: user.tags, hide_followers_count: user.hide_followers_count, hide_follows_count: user.hide_follows_count, diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index 1580ca448..b199fa704 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -877,7 +877,7 @@ test "PATCH /api/pleroma/admin/users/suggest", %{admin: admin, conn: conn} do user1 = insert(:user, is_suggested: false) user2 = insert(:user, is_suggested: false) - _response = + response = conn |> put_req_header("content-type", "application/json") |> patch( @@ -886,6 +886,7 @@ test "PATCH /api/pleroma/admin/users/suggest", %{admin: admin, conn: conn} do ) |> json_response_and_validate_schema(200) + assert Enum.map(response["users"], & &1["is_suggested"]) == [true, true] [user1, user2] = Repo.reload!([user1, user2]) assert user1.is_suggested @@ -901,7 +902,7 @@ test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do user1 = insert(:user, is_suggested: true) user2 = insert(:user, is_suggested: true) - _response = + response = conn |> put_req_header("content-type", "application/json") |> patch( @@ -910,6 +911,7 @@ test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do ) |> json_response_and_validate_schema(200) + assert Enum.map(response["users"], & &1["is_suggested"]) == [false, false] [user1, user2] = Repo.reload!([user1, user2]) refute user1.is_suggested @@ -954,6 +956,7 @@ defp user_response(user, attrs \\ %{}) do "display_name" => HTML.strip_tags(user.name || user.nickname), "is_confirmed" => true, "is_approved" => true, + "is_suggested" => false, "url" => user.ap_id, "registration_reason" => nil, "actor_type" => "Person", diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index 60881756d..9af588778 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -83,6 +83,7 @@ test "Represent a user account" do tags: [], is_admin: false, is_moderator: false, + is_suggested: false, hide_favorites: true, hide_followers: false, hide_follows: false, @@ -183,6 +184,7 @@ test "Represent a Service(bot) account" do tags: [], is_admin: false, is_moderator: false, + is_suggested: false, hide_favorites: true, hide_followers: false, hide_follows: false,