From 3db988250bcd279f20bd1742ca454aa187d89368 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sun, 10 Nov 2019 16:30:21 +0300 Subject: [PATCH 01/24] [#1335] User: refactored :blocks field into :blocked_users relation. Introduced UserBlock. --- lib/pleroma/notification.ex | 4 +- lib/pleroma/user.ex | 57 +++++++++------ lib/pleroma/user/search.ex | 15 +++- lib/pleroma/user_block.ex | 72 +++++++++++++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 8 +-- .../controllers/account_controller.ex | 4 +- lib/pleroma/web/streamer/worker.ex | 2 +- .../20191108161911_create_user_blocks.exs | 14 ++++ ...11_data_migration_populate_user_blocks.exs | 50 +++++++++++++ test/conversation/participation_test.exs | 6 +- test/notification_test.exs | 6 +- test/user_test.exs | 27 +++---- test/web/activity_pub/activity_pub_test.exs | 12 ++-- .../transmogrifier/follow_handling_test.exs | 2 +- .../controllers/account_controller_test.exs | 2 +- .../controllers/status_controller_test.exs | 4 +- .../controllers/timeline_controller_test.exs | 2 +- .../mastodon_api/views/account_view_test.exs | 6 +- test/web/streamer/streamer_test.exs | 6 +- test/web/twitter_api/util_controller_test.exs | 4 +- 20 files changed, 234 insertions(+), 69 deletions(-) create mode 100644 lib/pleroma/user_block.ex create mode 100644 priv/repo/migrations/20191108161911_create_user_blocks.exs create mode 100644 priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index b7ecf51e4..82faef85e 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -60,8 +60,10 @@ def for_user_query(user, opts \\ []) do end defp exclude_blocked(query, user) do + blocked_ap_ids = User.blocked_ap_ids(user) + query - |> where([n, a], a.actor not in ^user.blocks) + |> where([n, a], a.actor not in ^blocked_ap_ids) |> where( [n, a], fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.domain_blocks diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f8c2db1e1..d4fc1c093 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -7,6 +7,7 @@ defmodule Pleroma.User do import Ecto.Changeset import Ecto.Query + import Ecto, only: [assoc: 2] alias Comeonin.Pbkdf2 alias Ecto.Multi @@ -21,6 +22,7 @@ defmodule Pleroma.User do alias Pleroma.Repo alias Pleroma.RepoStreamer alias Pleroma.User + alias Pleroma.UserBlock alias Pleroma.Web alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils @@ -74,7 +76,6 @@ defmodule Pleroma.User do field(:password_reset_pending, :boolean, default: false) field(:confirmation_token, :string, default: nil) field(:default_scope, :string, default: "public") - field(:blocks, {:array, :string}, default: []) field(:domain_blocks, {:array, :string}, default: []) field(:mutes, {:array, :string}, default: []) field(:muted_reblogs, {:array, :string}, default: []) @@ -118,9 +119,16 @@ defmodule Pleroma.User do has_many(:notifications, Notification) has_many(:registrations, Registration) has_many(:deliveries, Delivery) + has_many(:blocker_blocks, UserBlock, foreign_key: :blocker_id) + has_many(:blockee_blocks, UserBlock, foreign_key: :blockee_id) + has_many(:blocked_users, through: [:blocker_blocks, :blockee]) + has_many(:blocker_users, through: [:blockee_blocks, :blocker]) field(:info, :map, default: %{}) + # `:blocks` is deprecated (replaced with `blocked_users` relation) + field(:blocks, {:array, :string}, default: []) + timestamps() end @@ -986,7 +994,7 @@ def unsubscribe(unsubscriber, %{ap_id: ap_id}) do end end - def block(blocker, %User{ap_id: ap_id} = blocked) do + def block(blocker, %User{} = blocked) do # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213) blocker = if following?(blocker, blocked) do @@ -1015,7 +1023,7 @@ def block(blocker, %User{ap_id: ap_id} = blocked) do {:ok, blocker} = update_follower_count(blocker) {:ok, blocker, _} = Participation.mark_all_as_read(blocker, blocked) - add_to_block(blocker, ap_id) + add_to_block(blocker, blocked) end # helper to handle the block given only an actor's AP id @@ -1023,8 +1031,13 @@ def block(blocker, %{ap_id: ap_id}) do block(blocker, get_cached_by_ap_id(ap_id)) end + def unblock(blocker, %User{} = blocked) do + remove_from_block(blocker, blocked) + end + + # helper to handle the block given only an actor's AP id def unblock(blocker, %{ap_id: ap_id}) do - remove_from_block(blocker, ap_id) + unblock(blocker, get_cached_by_ap_id(ap_id)) end def mutes?(nil, _), do: false @@ -1043,7 +1056,7 @@ def blocks?(%User{} = user, %User{} = target) do def blocks?(nil, _), do: false def blocks_ap_id?(%User{} = user, %User{} = target) do - Enum.member?(user.blocks, target.ap_id) + UserBlock.exists?(user, target) end def blocks_ap_id?(_, _), do: false @@ -1070,10 +1083,20 @@ def muted_users(user) do @spec blocked_users(User.t()) :: [User.t()] def blocked_users(user) do - User.Query.build(%{ap_id: user.blocks, deactivated: false}) + user + |> assoc(:blocked_users) + |> restrict_deactivated() |> Repo.all() end + def blocked_ap_ids(user) do + Repo.all( + from(u in assoc(user, :blocked_users), + select: u.ap_id + ) + ) + end + @spec subscribers(User.t()) :: [User.t()] def subscribers(user) do User.Query.build(%{ap_id: user.subscribers, deactivated: false}) @@ -1179,7 +1202,7 @@ def perform(:blocks_import, %User{} = blocker, blocked_identifiers) blocked_identifiers, fn blocked_identifier -> with {:ok, %User{} = blocked} <- get_or_fetch(blocked_identifier), - {:ok, blocker} <- block(blocker, blocked), + {:ok, _user_block} <- block(blocker, blocked), {:ok, _} <- ActivityPub.block(blocker, blocked) do blocked else @@ -1844,21 +1867,15 @@ def unblock_domain(user, domain_blocked) do set_domain_blocks(user, List.delete(user.domain_blocks, domain_blocked)) end - defp set_blocks(user, blocks) do - params = %{blocks: blocks} - - user - |> cast(params, [:blocks]) - |> validate_required([:blocks]) - |> update_and_set_cache() + @spec add_to_block(User.t(), User.t()) :: {:ok, UserBlock.t()} | {:error, Ecto.Changeset.t()} + defp add_to_block(%User{} = user, %User{} = blocked) do + UserBlock.create(user, blocked) end - def add_to_block(user, blocked) do - set_blocks(user, Enum.uniq([blocked | user.blocks])) - end - - def remove_from_block(user, blocked) do - set_blocks(user, List.delete(user.blocks, blocked)) + @spec add_to_block(User.t(), User.t()) :: + {:ok, UserBlock.t()} | {:ok, nil} | {:error, Ecto.Changeset.t()} + defp remove_from_block(%User{} = user, %User{} = blocked) do + UserBlock.delete(user, blocked) end defp set_mutes(user, mutes) do diff --git a/lib/pleroma/user/search.ex b/lib/pleroma/user/search.ex index 09664db76..8fa1e2e97 100644 --- a/lib/pleroma/user/search.ex +++ b/lib/pleroma/user/search.ex @@ -98,9 +98,18 @@ defp trigram_rank(query, query_string) do defp base_query(_user, false), do: User defp base_query(user, true), do: User.get_followers_query(user) - defp filter_blocked_user(query, %User{blocks: blocks}) - when length(blocks) > 0 do - from(q in query, where: not (q.ap_id in ^blocks)) + defp filter_blocked_user(query, %User{} = blocker) do + blocker_id = FlakeId.from_string(blocker.id) + + from( + q in query, + where: + fragment( + "? NOT IN (SELECT blockee_id FROM user_blocks WHERE user_blocks.blocker_id = ?)", + q.id, + ^blocker_id + ) + ) end defp filter_blocked_user(query, _), do: query diff --git a/lib/pleroma/user_block.ex b/lib/pleroma/user_block.ex new file mode 100644 index 000000000..1cdb15886 --- /dev/null +++ b/lib/pleroma/user_block.ex @@ -0,0 +1,72 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.UserBlock do + use Ecto.Schema + + import Ecto.Changeset + import Ecto.Query + + alias Pleroma.Repo + alias Pleroma.User + alias Pleroma.UserBlock + + schema "user_blocks" do + belongs_to(:blocker, User, type: FlakeId.Ecto.CompatType) + belongs_to(:blockee, User, type: FlakeId.Ecto.CompatType) + + timestamps(updated_at: false) + end + + def changeset(%UserBlock{} = user_block, params \\ %{}) do + user_block + |> cast(params, [:blocker_id, :blockee_id]) + |> validate_required([:blocker_id, :blockee_id]) + |> unique_constraint(:blockee_id, name: :user_blocks_blocker_id_blockee_id_index) + |> validate_not_self_block() + end + + def exists?(%User{} = blocker, %User{} = blockee) do + UserBlock + |> where(blocker_id: ^blocker.id, blockee_id: ^blockee.id) + |> Repo.exists?() + end + + def create(%User{} = blocker, %User{} = blockee) do + %UserBlock{} + |> changeset(%{blocker_id: blocker.id, blockee_id: blockee.id}) + |> Repo.insert( + on_conflict: :replace_all_except_primary_key, + conflict_target: [:blocker_id, :blockee_id] + ) + end + + def delete(%User{} = blocker, %User{} = blockee) do + attrs = %{blocker_id: blocker.id, blockee_id: blockee.id} + + if is_nil(existing_record = Repo.get_by(UserBlock, attrs)) do + {:ok, nil} + else + Repo.delete(existing_record) + end + end + + defp validate_not_self_block(%Ecto.Changeset{} = changeset) do + changeset + |> validate_change(:blockee_id, fn _, blockee_id -> + if blockee_id == changeset.changes[:blocker_id] || changeset.data.blocker_id do + [blockee_id: "can't be equal to blocker_id"] + else + [] + end + end) + |> validate_change(:blocker_id, fn _, blocker_id -> + if blocker_id == changeset.changes[:blockee_id] || changeset.data.blockee_id do + [blocker_id: "can't be equal to blockee_id"] + else + [] + end + end) + end +end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 65dd251f3..82203ef5c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -876,7 +876,7 @@ defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do defp restrict_muted(query, _), do: query defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do - blocks = user.blocks || [] + blocked_ap_ids = User.blocked_ap_ids(user) domain_blocks = user.domain_blocks || [] query = @@ -884,14 +884,14 @@ defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do from( [activity, object: o] in query, - where: fragment("not (? = ANY(?))", activity.actor, ^blocks), - where: fragment("not (? && ?)", activity.recipients, ^blocks), + where: fragment("not (? = ANY(?))", activity.actor, ^blocked_ap_ids), + where: fragment("not (? && ?)", activity.recipients, ^blocked_ap_ids), where: fragment( "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)", activity.data, activity.data, - ^blocks + ^blocked_ap_ids ), where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks), where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks) diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index 73fad519e..7c94a35d9 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -341,7 +341,7 @@ def unmute(%{assigns: %{user: muter, account: muted}} = conn, _params) do @doc "POST /api/v1/accounts/:id/block" def block(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do - with {:ok, blocker} <- User.block(blocker, blocked), + with {:ok, _user_block} <- User.block(blocker, blocked), {:ok, _activity} <- ActivityPub.block(blocker, blocked) do render(conn, "relationship.json", user: blocker, target: blocked) else @@ -351,7 +351,7 @@ def block(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do @doc "POST /api/v1/accounts/:id/unblock" def unblock(%{assigns: %{user: blocker, account: blocked}} = conn, _params) do - with {:ok, blocker} <- User.unblock(blocker, blocked), + with {:ok, _user_block} <- User.unblock(blocker, blocked), {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do render(conn, "relationship.json", user: blocker, target: blocked) else diff --git a/lib/pleroma/web/streamer/worker.ex b/lib/pleroma/web/streamer/worker.ex index 33b24840d..672dd52ee 100644 --- a/lib/pleroma/web/streamer/worker.ex +++ b/lib/pleroma/web/streamer/worker.ex @@ -129,7 +129,7 @@ defp do_stream(%{topic: topic, item: item}) do end defp should_send?(%User{} = user, %Activity{} = item) do - blocks = user.blocks || [] + blocks = User.blocked_ap_ids(user) mutes = user.mutes || [] reblog_mutes = user.muted_reblogs || [] recipient_blocks = MapSet.new(blocks ++ mutes) diff --git a/priv/repo/migrations/20191108161911_create_user_blocks.exs b/priv/repo/migrations/20191108161911_create_user_blocks.exs new file mode 100644 index 000000000..c882d2bd6 --- /dev/null +++ b/priv/repo/migrations/20191108161911_create_user_blocks.exs @@ -0,0 +1,14 @@ +defmodule Pleroma.Repo.Migrations.CreateUserBlocks do + use Ecto.Migration + + def change do + create_if_not_exists table(:user_blocks) do + add(:blocker_id, references(:users, type: :uuid, on_delete: :delete_all)) + add(:blockee_id, references(:users, type: :uuid, on_delete: :delete_all)) + + timestamps(updated_at: false) + end + + create_if_not_exists(unique_index(:user_blocks, [:blocker_id, :blockee_id])) + end +end diff --git a/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs b/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs new file mode 100644 index 000000000..728da8211 --- /dev/null +++ b/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs @@ -0,0 +1,50 @@ +defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserBlocks do + use Ecto.Migration + + alias Ecto.Adapters.SQL + alias Pleroma.Repo + + require Logger + + def up do + {:ok, %{rows: block_rows}} = + SQL.query(Repo, "SELECT id, blocks FROM users WHERE blocks != '{}'") + + blockee_ap_ids = + Enum.flat_map( + block_rows, + fn [_, ap_ids] -> ap_ids end + ) + |> Enum.uniq() + + # Selecting ids of all blockees at once in order to reduce the number of SELECT queries + {:ok, %{rows: blockee_ap_id_id}} = + SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [blockee_ap_ids]) + + blockee_id_by_ap_id = Enum.into(blockee_ap_id_id, %{}, fn [k, v] -> {k, v} end) + + Enum.each( + block_rows, + fn [blocker_id, blockee_ap_ids] -> + blocker_uuid = Ecto.UUID.cast!(blocker_id) + + for blockee_ap_id <- blockee_ap_ids do + blockee_id = blockee_id_by_ap_id[blockee_ap_id] + blockee_uuid = blockee_id && Ecto.UUID.cast!(blockee_id) + + with {:ok, blockee_uuid} <- Ecto.UUID.cast(blockee_id) do + execute( + "INSERT INTO user_blocks(blocker_id, blockee_id, inserted_at) " <> + "VALUES('#{blocker_uuid}'::uuid, '#{blockee_uuid}'::uuid, now()) " <> + "ON CONFLICT (blocker_id, blockee_id) DO NOTHING" + ) + else + _ -> Logger.warn("Missing reference: (#{blocker_uuid}, #{blockee_id})") + end + end + end + ) + end + + def down, do: :noop +end diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index 863270022..a36f45a5a 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -252,7 +252,7 @@ test "when the user blocks a recipient, the existing conversations with them are assert User.get_cached_by_id(blocker.id).unread_conversation_count == 4 - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) # The conversations with the blocked user are marked as read assert [%{read: true}, %{read: true}, %{read: true}, %{read: false}] = @@ -274,7 +274,7 @@ test "the new conversation with the blocked user is not marked as unread " do blocked = insert(:user) third_user = insert(:user) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) # When the blocked user is the author {:ok, _direct1} = @@ -311,7 +311,7 @@ test "the conversation with the blocked user is not marked as unread on a reply" "visibility" => "direct" }) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) assert [%{read: true}] = Participation.for_user(blocker) assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0 diff --git a/test/notification_test.exs b/test/notification_test.exs index f8d429223..5388214ba 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -93,7 +93,7 @@ test "it creates a notification for user if the user blocks the activity author" activity = insert(:note_activity) author = User.get_cached_by_ap_id(activity.data["actor"]) user = insert(:user) - {:ok, user} = User.block(user, author) + {:ok, _user_block} = User.block(user, author) assert Notification.create_notification(activity, user) end @@ -656,7 +656,7 @@ test "it doesn't return notifications for muted user with notifications" do test "it doesn't return notifications for blocked user" do user = insert(:user) blocked = insert(:user) - {:ok, user} = User.block(user, blocked) + {:ok, _user_block} = User.block(user, blocked) {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"}) @@ -696,7 +696,7 @@ test "it returns notifications from a muted user when with_muted is set" do test "it doesn't return notifications from a blocked user when with_muted is set" do user = insert(:user) blocked = insert(:user) - {:ok, user} = User.block(user, blocked) + {:ok, _user_block} = User.block(user, blocked) {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"}) diff --git a/test/user_test.exs b/test/user_test.exs index 6b1b24ce5..fa8d336f2 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -100,7 +100,7 @@ test "clears follow requests when requester is blocked" do CommonAPI.follow(follower, followed) assert [_activity] = User.get_follow_requests(followed) - {:ok, _follower} = User.block(followed, follower) + {:ok, _user_block} = User.block(followed, follower) assert [] = User.get_follow_requests(followed) end @@ -113,8 +113,8 @@ test "follow_all follows mutliple users" do not_followed = insert(:user) reverse_blocked = insert(:user) - {:ok, user} = User.block(user, blocked) - {:ok, reverse_blocked} = User.block(reverse_blocked, user) + {:ok, _user_block} = User.block(user, blocked) + {:ok, _user_block} = User.block(reverse_blocked, user) {:ok, user} = User.follow(user, followed_zero) @@ -166,7 +166,7 @@ test "can't follow a user who blocked us" do blocker = insert(:user) blockee = insert(:user) - {:ok, blocker} = User.block(blocker, blockee) + {:ok, _user_block} = User.block(blocker, blockee) {:error, _} = User.follow(blockee, blocker) end @@ -175,7 +175,7 @@ test "can't subscribe to a user who blocked us" do blocker = insert(:user) blocked = insert(:user) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) {:error, _} = User.subscribe(blocked, blocker) end @@ -711,7 +711,7 @@ test "it blocks people" do refute User.blocks?(user, blocked_user) - {:ok, user} = User.block(user, blocked_user) + {:ok, _user_block} = User.block(user, blocked_user) assert User.blocks?(user, blocked_user) end @@ -720,8 +720,8 @@ test "it unblocks users" do user = insert(:user) blocked_user = insert(:user) - {:ok, user} = User.block(user, blocked_user) - {:ok, user} = User.unblock(user, blocked_user) + {:ok, _user_block} = User.block(user, blocked_user) + {:ok, _user_block} = User.unblock(user, blocked_user) refute User.blocks?(user, blocked_user) end @@ -736,7 +736,7 @@ test "blocks tear down cyclical follow relationships" do assert User.following?(blocker, blocked) assert User.following?(blocked, blocker) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) blocked = User.get_cached_by_id(blocked.id) assert User.blocks?(blocker, blocked) @@ -754,7 +754,7 @@ test "blocks tear down blocker->blocked follow relationships" do assert User.following?(blocker, blocked) refute User.following?(blocked, blocker) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) blocked = User.get_cached_by_id(blocked.id) assert User.blocks?(blocker, blocked) @@ -772,7 +772,7 @@ test "blocks tear down blocked->blocker follow relationships" do refute User.following?(blocker, blocked) assert User.following?(blocked, blocker) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) blocked = User.get_cached_by_id(blocked.id) assert User.blocks?(blocker, blocked) @@ -790,7 +790,7 @@ test "blocks tear down blocked->blocker subscription relationships" do assert User.subscribed_to?(blocked, blocker) refute User.subscribed_to?(blocker, blocked) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) assert User.blocks?(blocker, blocked) refute User.subscribed_to?(blocker, blocked) @@ -1313,7 +1313,8 @@ test "follower count is updated when a follower is blocked" do {:ok, _follower2} = User.follow(follower2, user) {:ok, _follower3} = User.follow(follower3, user) - {:ok, user} = User.block(user, follower) + {:ok, _user_block} = User.block(user, follower) + user = refresh_record(user) assert User.user_info(user).follower_count == 2 end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 0d0281faf..17d283954 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -484,7 +484,7 @@ test "retrieves activities that have a given context" do activity_five = insert(:note_activity) user = insert(:user) - {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]}) + {:ok, _user_block} = User.block(user, %{ap_id: activity_five.data["actor"]}) activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user}) assert activities == [activity_two, activity] @@ -497,7 +497,7 @@ test "doesn't return blocked activities" do activity_three = insert(:note_activity) user = insert(:user) booster = insert(:user) - {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]}) + {:ok, _user_block} = User.block(user, %{ap_id: activity_one.data["actor"]}) activities = ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true}) @@ -506,7 +506,7 @@ test "doesn't return blocked activities" do assert Enum.member?(activities, activity_three) refute Enum.member?(activities, activity_one) - {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]}) + {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]}) activities = ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true}) @@ -515,7 +515,7 @@ test "doesn't return blocked activities" do assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_one) - {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]}) + {:ok, _user_block} = User.block(user, %{ap_id: activity_three.data["actor"]}) {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster) %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id) activity_three = Activity.get_by_id(activity_three.id) @@ -542,7 +542,7 @@ test "doesn't return transitive interactions concerning blocked users" do blockee = insert(:user) friend = insert(:user) - {:ok, blocker} = User.block(blocker, blockee) + {:ok, _user_block} = User.block(blocker, blockee) {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"}) @@ -565,7 +565,7 @@ test "doesn't return announce activities concerning blocked users" do blockee = insert(:user) friend = insert(:user) - {:ok, blocker} = User.block(blocker, blockee) + {:ok, _user_block} = User.block(blocker, blockee) {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"}) diff --git a/test/web/activity_pub/transmogrifier/follow_handling_test.exs b/test/web/activity_pub/transmogrifier/follow_handling_test.exs index 75cfbea2e..5c67d41c5 100644 --- a/test/web/activity_pub/transmogrifier/follow_handling_test.exs +++ b/test/web/activity_pub/transmogrifier/follow_handling_test.exs @@ -128,7 +128,7 @@ test "it rejects incoming follow requests from blocked users when deny_follow_bl user = insert(:user) {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin") - {:ok, user} = User.block(user, target) + {:ok, _user_block} = User.block(user, target) data = File.read!("test/fixtures/mastodon-follow-activity.json") diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index 8fc2d9300..b98d9c8e3 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -883,7 +883,7 @@ test "getting a list of blocks", %{conn: conn} do user = insert(:user) other_user = insert(:user) - {:ok, user} = User.block(user, other_user) + {:ok, _user_block} = User.block(user, other_user) conn = conn diff --git a/test/web/mastodon_api/controllers/status_controller_test.exs b/test/web/mastodon_api/controllers/status_controller_test.exs index a96fd860b..e8a1054ac 100644 --- a/test/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/web/mastodon_api/controllers/status_controller_test.exs @@ -1108,7 +1108,7 @@ test "does not return users who have favorited the status but are blocked", %{ activity: activity } do other_user = insert(:user) - {:ok, user} = User.block(user, other_user) + {:ok, _user_block} = User.block(user, other_user) {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) @@ -1205,7 +1205,7 @@ test "does not return users who have reblogged the status but are blocked", %{ activity: activity } do other_user = insert(:user) - {:ok, user} = User.block(user, other_user) + {:ok, _user_block} = User.block(user, other_user) {:ok, _, _} = CommonAPI.repeat(activity.id, other_user) diff --git a/test/web/mastodon_api/controllers/timeline_controller_test.exs b/test/web/mastodon_api/controllers/timeline_controller_test.exs index 61b6cea75..524529d1f 100644 --- a/test/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/web/mastodon_api/controllers/timeline_controller_test.exs @@ -194,7 +194,7 @@ test "doesn't include DMs from blocked users", %{conn: conn} do blocker = insert(:user) blocked = insert(:user) user = insert(:user) - {:ok, blocker} = User.block(blocker, blocked) + {:ok, _user_block} = User.block(blocker, blocked) {:ok, _blocked_direct} = CommonAPI.post(blocked, %{ diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index af88841ed..c34bf7ba9 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -219,8 +219,8 @@ test "represent a relationship for the blocking and blocked user" do {:ok, user} = User.follow(user, other_user) {:ok, other_user} = User.subscribe(user, other_user) - {:ok, user} = User.block(user, other_user) - {:ok, other_user} = User.block(other_user, user) + {:ok, _user_block} = User.block(user, other_user) + {:ok, _user_block} = User.block(other_user, user) expected = %{ id: to_string(other_user.id), @@ -291,7 +291,7 @@ test "represent an embedded relationship" do other_user = insert(:user) {:ok, other_user} = User.follow(other_user, user) - {:ok, other_user} = User.block(other_user, user) + {:ok, _user_block} = User.block(other_user, user) {:ok, _} = User.follow(insert(:user), user) expected = %{ diff --git a/test/web/streamer/streamer_test.exs b/test/web/streamer/streamer_test.exs index 80a7541b2..6461fc4c3 100644 --- a/test/web/streamer/streamer_test.exs +++ b/test/web/streamer/streamer_test.exs @@ -59,7 +59,7 @@ test "it doesn't send notify to the 'user:notification' stream when a user is bl user: user } do blocked = insert(:user) - {:ok, user} = User.block(user, blocked) + {:ok, _user_block} = User.block(user, blocked) task = Task.async(fn -> refute_receive {:text, _}, 4_000 end) @@ -259,7 +259,7 @@ test "it sends message if recipients invalid and thread containment is enabled b test "it doesn't send messages involving blocked users" do user = insert(:user) blocked_user = insert(:user) - {:ok, user} = User.block(user, blocked_user) + {:ok, _user_block} = User.block(user, blocked_user) task = Task.async(fn -> @@ -301,7 +301,7 @@ test "it doesn't send messages transitively involving blocked users" do "public" => [fake_socket] } - {:ok, blocker} = User.block(blocker, blockee) + {:ok, _user_block} = User.block(blocker, blockee) {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"}) diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index f0211f59c..986ee01f3 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -387,7 +387,7 @@ test "returns error when user is blocked", %{conn: conn} do user = insert(:user) user2 = insert(:user) - {:ok, _user} = Pleroma.User.block(user2, user) + {:ok, _user_block} = Pleroma.User.block(user2, user) response = conn @@ -485,7 +485,7 @@ test "returns error when user is blocked", %{conn: conn} do Pleroma.Config.put([:user, :deny_follow_blocked], true) user = insert(:user) user2 = insert(:user) - {:ok, _user} = Pleroma.User.block(user2, user) + {:ok, _user_block} = Pleroma.User.block(user2, user) response = conn From 5cf2c7422b006d477498af8d7618a0d6f7659290 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 11 Nov 2019 14:16:57 +0300 Subject: [PATCH 02/24] [#1335] Applied code review suggestions. --- lib/pleroma/user.ex | 9 ++++----- lib/pleroma/user/search.ex | 15 +++++---------- lib/pleroma/user_block.ex | 11 +++++------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d4fc1c093..9dd1ad8fc 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1090,11 +1090,10 @@ def blocked_users(user) do end def blocked_ap_ids(user) do - Repo.all( - from(u in assoc(user, :blocked_users), - select: u.ap_id - ) - ) + user + |> assoc(:blocked_users) + |> select([u], u.ap_id) + |> Repo.all() end @spec subscribers(User.t()) :: [User.t()] diff --git a/lib/pleroma/user/search.ex b/lib/pleroma/user/search.ex index 8fa1e2e97..049e1a634 100644 --- a/lib/pleroma/user/search.ex +++ b/lib/pleroma/user/search.ex @@ -99,17 +99,12 @@ defp base_query(_user, false), do: User defp base_query(user, true), do: User.get_followers_query(user) defp filter_blocked_user(query, %User{} = blocker) do - blocker_id = FlakeId.from_string(blocker.id) - - from( - q in query, - where: - fragment( - "? NOT IN (SELECT blockee_id FROM user_blocks WHERE user_blocks.blocker_id = ?)", - q.id, - ^blocker_id - ) + query + |> join(:left, [u], b in Pleroma.UserBlock, + as: :blocks, + on: b.blocker_id == ^blocker.id and u.id == b.blockee_id ) + |> where([blocks: b], is_nil(b.blockee_id)) end defp filter_blocked_user(query, _), do: query diff --git a/lib/pleroma/user_block.ex b/lib/pleroma/user_block.ex index 1cdb15886..bcf4b64d9 100644 --- a/lib/pleroma/user_block.ex +++ b/lib/pleroma/user_block.ex @@ -45,24 +45,23 @@ def create(%User{} = blocker, %User{} = blockee) do def delete(%User{} = blocker, %User{} = blockee) do attrs = %{blocker_id: blocker.id, blockee_id: blockee.id} - if is_nil(existing_record = Repo.get_by(UserBlock, attrs)) do - {:ok, nil} - else - Repo.delete(existing_record) + case Repo.get_by(UserBlock, attrs) do + %UserBlock{} = existing_record -> Repo.delete(existing_record) + nil -> {:ok, nil} end end defp validate_not_self_block(%Ecto.Changeset{} = changeset) do changeset |> validate_change(:blockee_id, fn _, blockee_id -> - if blockee_id == changeset.changes[:blocker_id] || changeset.data.blocker_id do + if blockee_id == get_field(changeset, :blocker_id) do [blockee_id: "can't be equal to blocker_id"] else [] end end) |> validate_change(:blocker_id, fn _, blocker_id -> - if blocker_id == changeset.changes[:blockee_id] || changeset.data.blockee_id do + if blocker_id == get_field(changeset, :blockee_id) do [blocker_id: "can't be equal to blockee_id"] else [] From c31ddce51ea18f052c1c3ad30a221b77c7a94e71 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Fri, 15 Nov 2019 21:38:54 +0300 Subject: [PATCH 03/24] [#1335] Reorganized `users.mutes` as relation to UserMute entity. --- lib/pleroma/user.ex | 114 ++++++++++++------ lib/pleroma/user_mute.ex | 71 +++++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 6 +- .../controllers/account_controller.ex | 8 +- .../web/mastodon_api/views/account_view.ex | 4 +- lib/pleroma/web/streamer/worker.ex | 3 +- ...11_data_migration_populate_user_blocks.exs | 3 +- .../20191112151559_create_user_mutes.exs | 14 +++ ...614_data_migration_populate_user_mutes.exs | 48 ++++++++ test/notification_test.exs | 11 +- test/user_test.exs | 11 +- test/web/activity_pub/activity_pub_test.exs | 9 +- .../controllers/account_controller_test.exs | 2 +- .../notification_controller_test.exs | 9 +- .../mastodon_api/views/account_view_test.exs | 5 +- .../mastodon_api/views/status_view_test.exs | 4 +- 16 files changed, 258 insertions(+), 64 deletions(-) create mode 100644 lib/pleroma/user_mute.ex create mode 100644 priv/repo/migrations/20191112151559_create_user_mutes.exs create mode 100644 priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 9dd1ad8fc..383c45992 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -23,6 +23,7 @@ defmodule Pleroma.User do alias Pleroma.RepoStreamer alias Pleroma.User alias Pleroma.UserBlock + alias Pleroma.UserMute alias Pleroma.Web alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils @@ -119,11 +120,17 @@ defmodule Pleroma.User do has_many(:notifications, Notification) has_many(:registrations, Registration) has_many(:deliveries, Delivery) + has_many(:blocker_blocks, UserBlock, foreign_key: :blocker_id) has_many(:blockee_blocks, UserBlock, foreign_key: :blockee_id) has_many(:blocked_users, through: [:blocker_blocks, :blockee]) has_many(:blocker_users, through: [:blockee_blocks, :blocker]) + has_many(:muter_mutes, UserMute, foreign_key: :muter_id) + has_many(:mutee_mutes, UserMute, foreign_key: :mutee_id) + has_many(:muted_users, through: [:muter_mutes, :mutee]) + has_many(:muter_users, through: [:mutee_mutes, :muter]) + field(:info, :map, default: %{}) # `:blocks` is deprecated (replaced with `blocked_users` relation) @@ -968,12 +975,12 @@ def get_recipients_from_activity(%Activity{recipients: to}) do end @spec mute(User.t(), User.t(), boolean()) :: {:ok, User.t()} | {:error, String.t()} - def mute(muter, %User{ap_id: ap_id}, notifications? \\ true) do - add_to_mutes(muter, ap_id, notifications?) + def mute(muter, %User{} = mutee, notifications? \\ true) do + add_to_mutes(muter, mutee, notifications?) end - def unmute(muter, %{ap_id: ap_id}) do - remove_from_mutes(muter, ap_id) + def unmute(muter, %User{} = mutee) do + remove_from_mutes(muter, mutee) end def subscribe(subscriber, %{ap_id: ap_id}) do @@ -1041,7 +1048,11 @@ def unblock(blocker, %{ap_id: ap_id}) do end def mutes?(nil, _), do: false - def mutes?(user, %{ap_id: ap_id}), do: Enum.member?(user.mutes, ap_id) + def mutes?(%User{} = user, %User{} = target), do: mutes_user?(user, target) + + def mutes_user?(%User{} = user, %User{} = target) do + UserMute.exists?(user, target) + end @spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean() def muted_notifications?(nil, _), do: false @@ -1049,17 +1060,17 @@ def muted_notifications?(nil, _), do: false def muted_notifications?(user, %{ap_id: ap_id}), do: Enum.member?(user.muted_notifications, ap_id) - def blocks?(%User{} = user, %User{} = target) do - blocks_ap_id?(user, target) || blocks_domain?(user, target) - end - def blocks?(nil, _), do: false - def blocks_ap_id?(%User{} = user, %User{} = target) do + def blocks?(%User{} = user, %User{} = target) do + blocks_user?(user, target) || blocks_domain?(user, target) + end + + def blocks_user?(%User{} = user, %User{} = target) do UserBlock.exists?(user, target) end - def blocks_ap_id?(_, _), do: false + def blocks_user?(_, _), do: false def blocks_domain?(%User{} = user, %User{} = target) do domain_blocks = Pleroma.Web.ActivityPub.MRF.subdomains_regex(user.domain_blocks) @@ -1077,7 +1088,16 @@ def subscribed_to?(user, %{ap_id: ap_id}) do @spec muted_users(User.t()) :: [User.t()] def muted_users(user) do - User.Query.build(%{ap_id: user.mutes, deactivated: false}) + user + |> assoc(:muted_users) + |> restrict_deactivated() + |> Repo.all() + end + + def muted_ap_ids(user) do + user + |> assoc(:muted_users) + |> select([u], u.ap_id) |> Repo.all() end @@ -1096,6 +1116,35 @@ def blocked_ap_ids(user) do |> Repo.all() end + defp related_ap_ids_sql(join_table, source_column, target_column) do + "(SELECT array_agg(u.ap_id) FROM users as u " <> + "INNER JOIN #{join_table} AS join_table " <> + "ON join_table.#{source_column} = $1 " <> + "WHERE u.id = join_table.#{target_column})" + end + + @related_ap_ids_sql_params %{ + blocked_users: ["user_blocks", "blocker_id", "blockee_id"], + muted_users: ["user_mutes", "muter_id", "mutee_id"] + } + + def related_ap_ids(user, relations) when is_list(relations) do + query = + relations + |> Enum.map(fn r -> @related_ap_ids_sql_params[r] end) + |> Enum.filter(& &1) + |> Enum.map(fn [join_table, source_column, target_column] -> + related_ap_ids_sql(join_table, source_column, target_column) + end) + |> Enum.join(", ") + + with {:ok, %{rows: [ap_ids_arrays]}} <- + Repo.query("SELECT #{query}", [FlakeId.from_string(user.id)]) do + ap_ids_arrays = Enum.map(ap_ids_arrays, &(&1 || [])) + {:ok, ap_ids_arrays} + end + end + @spec subscribers(User.t()) :: [User.t()] def subscribers(user) do User.Query.build(%{ap_id: user.subscribers, deactivated: false}) @@ -1877,32 +1926,27 @@ defp remove_from_block(%User{} = user, %User{} = blocked) do UserBlock.delete(user, blocked) end - defp set_mutes(user, mutes) do - params = %{mutes: mutes} - - user - |> cast(params, [:mutes]) - |> validate_required([:mutes]) - |> update_and_set_cache() - end - - def add_to_mutes(user, muted, notifications?) do - with {:ok, user} <- set_mutes(user, Enum.uniq([muted | user.mutes])) do - set_notification_mutes( - user, - Enum.uniq([muted | user.muted_notifications]), - notifications? - ) + defp add_to_mutes(%User{} = user, %User{ap_id: ap_id} = muted_user, notifications?) do + with {:ok, user_mute} <- UserMute.create(user, muted_user), + {:ok, _user} <- + set_notification_mutes( + user, + Enum.uniq([ap_id | user.muted_notifications]), + notifications? + ) do + {:ok, user_mute} end end - def remove_from_mutes(user, muted) do - with {:ok, user} <- set_mutes(user, List.delete(user.mutes, muted)) do - set_notification_mutes( - user, - List.delete(user.muted_notifications, muted), - true - ) + defp remove_from_mutes(user, %User{ap_id: ap_id} = muted_user) do + with {:ok, user_mute} <- UserMute.delete(user, muted_user), + {:ok, _user} <- + set_notification_mutes( + user, + List.delete(user.muted_notifications, ap_id), + true + ) do + {:ok, user_mute} end end diff --git a/lib/pleroma/user_mute.ex b/lib/pleroma/user_mute.ex new file mode 100644 index 000000000..417a5ff84 --- /dev/null +++ b/lib/pleroma/user_mute.ex @@ -0,0 +1,71 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.UserMute do + use Ecto.Schema + + import Ecto.Changeset + import Ecto.Query + + alias Pleroma.Repo + alias Pleroma.User + alias Pleroma.UserMute + + schema "user_mutes" do + belongs_to(:muter, User, type: FlakeId.Ecto.CompatType) + belongs_to(:mutee, User, type: FlakeId.Ecto.CompatType) + + timestamps(updated_at: false) + end + + def changeset(%UserMute{} = user_mute, params \\ %{}) do + user_mute + |> cast(params, [:muter_id, :mutee_id]) + |> validate_required([:muter_id, :mutee_id]) + |> unique_constraint(:mutee_id, name: :user_mutes_muter_id_mutee_id_index) + |> validate_not_self_mute() + end + + def exists?(%User{} = muter, %User{} = mutee) do + UserMute + |> where(muter_id: ^muter.id, mutee_id: ^mutee.id) + |> Repo.exists?() + end + + def create(%User{} = muter, %User{} = mutee) do + %UserMute{} + |> changeset(%{muter_id: muter.id, mutee_id: mutee.id}) + |> Repo.insert( + on_conflict: :replace_all_except_primary_key, + conflict_target: [:muter_id, :mutee_id] + ) + end + + def delete(%User{} = muter, %User{} = mutee) do + attrs = %{muter_id: muter.id, mutee_id: mutee.id} + + case Repo.get_by(UserMute, attrs) do + %UserMute{} = existing_record -> Repo.delete(existing_record) + nil -> {:ok, nil} + end + end + + defp validate_not_self_mute(%Ecto.Changeset{} = changeset) do + changeset + |> validate_change(:mutee_id, fn _, mutee_id -> + if mutee_id == get_field(changeset, :muter_id) do + [mutee_id: "can't be equal to muter_id"] + else + [] + end + end) + |> validate_change(:muter_id, fn _, muter_id -> + if muter_id == get_field(changeset, :mutee_id) do + [muter_id: "can't be equal to mutee_id"] + else + [] + end + end) + end +end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 82203ef5c..cba2abfc3 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -858,7 +858,7 @@ defp restrict_reblogs(query, _), do: query defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do - mutes = user.mutes + mutes = opts["muted_ap_ids"] || User.muted_ap_ids(user) query = from([activity] in query, @@ -875,8 +875,8 @@ defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do defp restrict_muted(query, _), do: query - defp restrict_blocked(query, %{"blocking_user" => %User{} = user}) do - blocked_ap_ids = User.blocked_ap_ids(user) + defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do + blocked_ap_ids = opts["blocked_ap_ids"] || User.blocked_ap_ids(user) domain_blocks = user.domain_blocks || [] query = diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index 7c94a35d9..e37d3bb07 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -323,7 +323,9 @@ def unfollow(%{assigns: %{user: follower, account: followed}} = conn, _params) d def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do notifications? = params |> Map.get("notifications", true) |> truthy_param?() - with {:ok, muter} <- User.mute(muter, muted, notifications?) do + with {:ok, _user_mute} <- User.mute(muter, muted, notifications?) do + # TODO: remove `muter` refresh once `muted_notifications` field is deprecated + muter = User.get_cached_by_id(muter.id) render(conn, "relationship.json", user: muter, target: muted) else {:error, message} -> json_response(conn, :forbidden, %{error: message}) @@ -332,7 +334,9 @@ def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do @doc "POST /api/v1/accounts/:id/unmute" def unmute(%{assigns: %{user: muter, account: muted}} = conn, _params) do - with {:ok, muter} <- User.unmute(muter, muted) do + with {:ok, _user_mute} <- User.unmute(muter, muted) do + # TODO: remove `muter` refresh once `muted_notifications` field is deprecated + muter = User.get_cached_by_id(muter.id) render(conn, "relationship.json", user: muter, target: muted) else {:error, message} -> json_response(conn, :forbidden, %{error: message}) diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index e30fed610..37dc8e194 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -50,8 +50,8 @@ def render("relationship.json", %{user: %User{} = user, target: %User{} = target id: to_string(target.id), following: User.following?(user, target), followed_by: User.following?(target, user), - blocking: User.blocks_ap_id?(user, target), - blocked_by: User.blocks_ap_id?(target, user), + blocking: User.blocks_user?(user, target), + blocked_by: User.blocks_user?(target, user), muting: User.mutes?(user, target), muting_notifications: User.muted_notifications?(user, target), subscribing: User.subscribed_to?(user, target), diff --git a/lib/pleroma/web/streamer/worker.ex b/lib/pleroma/web/streamer/worker.ex index 672dd52ee..45f9c7c53 100644 --- a/lib/pleroma/web/streamer/worker.ex +++ b/lib/pleroma/web/streamer/worker.ex @@ -129,8 +129,7 @@ defp do_stream(%{topic: topic, item: item}) do end defp should_send?(%User{} = user, %Activity{} = item) do - blocks = User.blocked_ap_ids(user) - mutes = user.mutes || [] + {:ok, [blocks, mutes]} = User.related_ap_ids(user, [:blocked_users, :muted_users]) reblog_mutes = user.muted_reblogs || [] recipient_blocks = MapSet.new(blocks ++ mutes) recipients = MapSet.new(item.recipients) diff --git a/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs b/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs index 728da8211..fe537679d 100644 --- a/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs +++ b/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs @@ -30,9 +30,8 @@ def up do for blockee_ap_id <- blockee_ap_ids do blockee_id = blockee_id_by_ap_id[blockee_ap_id] - blockee_uuid = blockee_id && Ecto.UUID.cast!(blockee_id) - with {:ok, blockee_uuid} <- Ecto.UUID.cast(blockee_id) do + with {:ok, blockee_uuid} <- blockee_id && Ecto.UUID.cast(blockee_id) do execute( "INSERT INTO user_blocks(blocker_id, blockee_id, inserted_at) " <> "VALUES('#{blocker_uuid}'::uuid, '#{blockee_uuid}'::uuid, now()) " <> diff --git a/priv/repo/migrations/20191112151559_create_user_mutes.exs b/priv/repo/migrations/20191112151559_create_user_mutes.exs new file mode 100644 index 000000000..eaa285de2 --- /dev/null +++ b/priv/repo/migrations/20191112151559_create_user_mutes.exs @@ -0,0 +1,14 @@ +defmodule Pleroma.Repo.Migrations.CreateUserMutes do + use Ecto.Migration + + def change do + create_if_not_exists table(:user_mutes) do + add(:muter_id, references(:users, type: :uuid, on_delete: :delete_all)) + add(:mutee_id, references(:users, type: :uuid, on_delete: :delete_all)) + + timestamps(updated_at: false) + end + + create_if_not_exists(unique_index(:user_mutes, [:muter_id, :mutee_id])) + end +end diff --git a/priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs b/priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs new file mode 100644 index 000000000..a8bdd072e --- /dev/null +++ b/priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs @@ -0,0 +1,48 @@ +defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserMutes do + use Ecto.Migration + + alias Ecto.Adapters.SQL + alias Pleroma.Repo + + require Logger + + def up do + {:ok, %{rows: mute_rows}} = SQL.query(Repo, "SELECT id, mutes FROM users WHERE mutes != '{}'") + + mutee_ap_ids = + Enum.flat_map( + mute_rows, + fn [_, ap_ids] -> ap_ids end + ) + |> Enum.uniq() + + # Selecting ids of all mutees at once in order to reduce the number of SELECT queries + {:ok, %{rows: mutee_ap_id_id}} = + SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [mutee_ap_ids]) + + mutee_id_by_ap_id = Enum.into(mutee_ap_id_id, %{}, fn [k, v] -> {k, v} end) + + Enum.each( + mute_rows, + fn [muter_id, mutee_ap_ids] -> + muter_uuid = Ecto.UUID.cast!(muter_id) + + for mutee_ap_id <- mutee_ap_ids do + mutee_id = mutee_id_by_ap_id[mutee_ap_id] + + with {:ok, mutee_uuid} <- mutee_id && Ecto.UUID.cast(mutee_id) do + execute( + "INSERT INTO user_mutes(muter_id, mutee_id, inserted_at) " <> + "VALUES('#{muter_uuid}'::uuid, '#{mutee_uuid}'::uuid, now()) " <> + "ON CONFLICT (muter_id, mutee_id) DO NOTHING" + ) + else + _ -> Logger.warn("Missing reference: (#{muter_uuid}, #{mutee_id})") + end + end + end + ) + end + + def down, do: :noop +end diff --git a/test/notification_test.exs b/test/notification_test.exs index 5388214ba..d3d409c68 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -112,7 +112,7 @@ test "notification created if user is muted without notifications" do muter = insert(:user) muted = insert(:user) - {:ok, muter} = User.mute(muter, muted, false) + {:ok, _user_mute} = User.mute(muter, muted, false) {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"}) @@ -636,7 +636,7 @@ test "notifications are deleted if a remote user is deleted" do test "it returns notifications for muted user without notifications" do user = insert(:user) muted = insert(:user) - {:ok, user} = User.mute(user, muted, false) + {:ok, _user_mute} = User.mute(user, muted, false) {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"}) @@ -646,7 +646,10 @@ test "it returns notifications for muted user without notifications" do test "it doesn't return notifications for muted user with notifications" do user = insert(:user) muted = insert(:user) - {:ok, user} = User.mute(user, muted) + {:ok, _user_mute} = User.mute(user, muted) + + # Refreshing to reflect embedded ap id relation fields (remove once removed) + user = refresh_record(user) {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"}) @@ -686,7 +689,7 @@ test "it doesn't return notifications for muted thread" do test "it returns notifications from a muted user when with_muted is set" do user = insert(:user) muted = insert(:user) - {:ok, user} = User.mute(user, muted) + {:ok, _user_mute} = User.mute(user, muted) {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"}) diff --git a/test/user_test.exs b/test/user_test.exs index fa8d336f2..15b4f67f6 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -673,7 +673,10 @@ test "it mutes people" do refute User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) - {:ok, user} = User.mute(user, muted_user) + {:ok, _user_mute} = User.mute(user, muted_user) + + # Refreshing to reflect embedded ap id relation fields (remove once removed) + user = refresh_record(user) assert User.mutes?(user, muted_user) assert User.muted_notifications?(user, muted_user) @@ -683,8 +686,8 @@ test "it unmutes users" do user = insert(:user) muted_user = insert(:user) - {:ok, user} = User.mute(user, muted_user) - {:ok, user} = User.unmute(user, muted_user) + {:ok, _user_mute} = User.mute(user, muted_user) + {:ok, _user_mute} = User.unmute(user, muted_user) refute User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) @@ -697,7 +700,7 @@ test "it mutes user without notifications" do refute User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) - {:ok, user} = User.mute(user, muted_user, false) + {:ok, _user_mute} = User.mute(user, muted_user, false) assert User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 17d283954..de8472691 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -611,7 +611,9 @@ test "doesn't return muted activities" do activity_three = insert(:note_activity) user = insert(:user) booster = insert(:user) - {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]}) + + activity_one_actor = User.get_by_ap_id(activity_one.data["actor"]) + {:ok, _user_mute} = User.mute(user, activity_one_actor) activities = ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true}) @@ -632,7 +634,7 @@ test "doesn't return muted activities" do assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_one) - {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]}) + {:ok, _user_mute} = User.unmute(user, activity_one_actor) activities = ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true}) @@ -641,7 +643,8 @@ test "doesn't return muted activities" do assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_one) - {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]}) + activity_three_actor = User.get_by_ap_id(activity_three.data["actor"]) + {:ok, _user_mute} = User.mute(user, activity_three_actor) {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster) %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id) activity_three = Activity.get_by_id(activity_three.id) diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index b98d9c8e3..c5ef96bb7 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -868,7 +868,7 @@ test "getting a list of mutes", %{conn: conn} do user = insert(:user) other_user = insert(:user) - {:ok, user} = User.mute(user, other_user) + {:ok, _user_mute} = User.mute(user, other_user) conn = conn diff --git a/test/web/mastodon_api/controllers/notification_controller_test.exs b/test/web/mastodon_api/controllers/notification_controller_test.exs index fa55a7cf9..75b871a43 100644 --- a/test/web/mastodon_api/controllers/notification_controller_test.exs +++ b/test/web/mastodon_api/controllers/notification_controller_test.exs @@ -289,7 +289,10 @@ test "doesn't see notifications after muting user with notifications", %{conn: c assert length(json_response(conn, 200)) == 1 - {:ok, user} = User.mute(user, user2) + {:ok, _user_mute} = User.mute(user, user2) + + # Refreshing to reflect embedded ap id relation fields (remove once removed) + user = refresh_record(user) conn = assign(build_conn(), :user, user) conn = get(conn, "/api/v1/notifications") @@ -310,7 +313,7 @@ test "see notifications after muting user without notifications", %{conn: conn} assert length(json_response(conn, 200)) == 1 - {:ok, user} = User.mute(user, user2, false) + {:ok, _user_mute} = User.mute(user, user2, false) conn = assign(build_conn(), :user, user) conn = get(conn, "/api/v1/notifications") @@ -333,7 +336,7 @@ test "see notifications after muting user with notifications and with_muted para assert length(json_response(conn, 200)) == 1 - {:ok, user} = User.mute(user, user2) + {:ok, _user_mute} = User.mute(user, user2) conn = assign(build_conn(), :user, user) conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"}) diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index c34bf7ba9..f8b74553b 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -191,9 +191,12 @@ test "represent a relationship for the following and followed user" do {:ok, user} = User.follow(user, other_user) {:ok, other_user} = User.follow(other_user, user) {:ok, other_user} = User.subscribe(user, other_user) - {:ok, user} = User.mute(user, other_user, true) + {:ok, _user_mute} = User.mute(user, other_user, true) {:ok, user} = CommonAPI.hide_reblogs(user, other_user) + # Refreshing to reflect embedded ap id relation fields (remove once removed) + user = refresh_record(user) + expected = %{ id: to_string(other_user.id), following: true, diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index d46ecc646..89b146e1e 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -183,7 +183,7 @@ test "tells if the message is muted for some reason" do user = insert(:user) other_user = insert(:user) - {:ok, user} = User.mute(user, other_user) + {:ok, _user_mute} = User.mute(user, other_user) {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"}) status = StatusView.render("show.json", %{activity: activity}) @@ -199,7 +199,7 @@ test "tells if the message is thread muted" do user = insert(:user) other_user = insert(:user) - {:ok, user} = User.mute(user, other_user) + {:ok, _user_mute} = User.mute(user, other_user) {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"}) status = StatusView.render("show.json", %{activity: activity, for: user}) From aad6576130c3e47a5fcc102c736ce6414c0efd7a Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 18 Nov 2019 20:38:56 +0300 Subject: [PATCH 04/24] [#1335] Refactored UserMute and UserBlock into UserRelationship, introduced EctoEnum. --- lib/pleroma/ecto_enums.ex | 7 ++ lib/pleroma/user.ex | 99 ++++++++++--------- lib/pleroma/user/search.ex | 6 +- lib/pleroma/user_block.ex | 71 ------------- lib/pleroma/user_mute.ex | 71 ------------- lib/pleroma/user_relationship.ex | 90 +++++++++++++++++ lib/pleroma/web/streamer/worker.ex | 4 +- mix.exs | 1 + mix.lock | 1 + .../20191108161911_create_user_blocks.exs | 14 --- ...11_data_migration_populate_user_blocks.exs | 49 --------- .../20191112151559_create_user_mutes.exs | 14 --- ...614_data_migration_populate_user_mutes.exs | 48 --------- ...191118084425_create_user_relationships.exs | 17 ++++ ..._migration_populate_user_relationships.exs | 64 ++++++++++++ 15 files changed, 239 insertions(+), 317 deletions(-) create mode 100644 lib/pleroma/ecto_enums.ex delete mode 100644 lib/pleroma/user_block.ex delete mode 100644 lib/pleroma/user_mute.ex create mode 100644 lib/pleroma/user_relationship.ex delete mode 100644 priv/repo/migrations/20191108161911_create_user_blocks.exs delete mode 100644 priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs delete mode 100644 priv/repo/migrations/20191112151559_create_user_mutes.exs delete mode 100644 priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs create mode 100644 priv/repo/migrations/20191118084425_create_user_relationships.exs create mode 100644 priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs diff --git a/lib/pleroma/ecto_enums.ex b/lib/pleroma/ecto_enums.ex new file mode 100644 index 000000000..bad5ec523 --- /dev/null +++ b/lib/pleroma/ecto_enums.ex @@ -0,0 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +import EctoEnum + +defenum(UserRelationshipTypeEnum, block: 1, mute: 2, reblog_mute: 3, notification_mute: 4) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d6bc02d04..4dd03c6c6 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -22,8 +22,7 @@ defmodule Pleroma.User do alias Pleroma.Repo alias Pleroma.RepoStreamer alias Pleroma.User - alias Pleroma.UserBlock - alias Pleroma.UserMute + alias Pleroma.UserRelationship alias Pleroma.Web alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils @@ -78,7 +77,6 @@ defmodule Pleroma.User do field(:confirmation_token, :string, default: nil) field(:default_scope, :string, default: "public") field(:domain_blocks, {:array, :string}, default: []) - field(:mutes, {:array, :string}, default: []) field(:muted_reblogs, {:array, :string}, default: []) field(:muted_notifications, {:array, :string}, default: []) field(:subscribers, {:array, :string}, default: []) @@ -121,20 +119,41 @@ defmodule Pleroma.User do has_many(:registrations, Registration) has_many(:deliveries, Delivery) - has_many(:blocker_blocks, UserBlock, foreign_key: :blocker_id) - has_many(:blockee_blocks, UserBlock, foreign_key: :blockee_id) - has_many(:blocked_users, through: [:blocker_blocks, :blockee]) - has_many(:blocker_users, through: [:blockee_blocks, :blocker]) + has_many(:outgoing_relationships, UserRelationship, foreign_key: :source_id) + has_many(:incoming_relationships, UserRelationship, foreign_key: :target_id) - has_many(:muter_mutes, UserMute, foreign_key: :muter_id) - has_many(:mutee_mutes, UserMute, foreign_key: :mutee_id) - has_many(:muted_users, through: [:muter_mutes, :mutee]) - has_many(:muter_users, through: [:mutee_mutes, :muter]) + has_many(:blocker_blocks, UserRelationship, + foreign_key: :source_id, + where: [relationship_type: :block] + ) + + has_many(:blockee_blocks, UserRelationship, + foreign_key: :target_id, + where: [relationship_type: :block] + ) + + has_many(:blocked_users, through: [:blocker_blocks, :target]) + has_many(:blocker_users, through: [:blockee_blocks, :source]) + + has_many(:muter_mutes, UserRelationship, + foreign_key: :source_id, + where: [relationship_type: :mute] + ) + + has_many(:mutee_mutes, UserRelationship, + foreign_key: :target_id, + where: [relationship_type: :mute] + ) + + has_many(:muted_users, through: [:muter_mutes, :target]) + has_many(:muter_users, through: [:mutee_mutes, :source]) field(:info, :map, default: %{}) # `:blocks` is deprecated (replaced with `blocked_users` relation) field(:blocks, {:array, :string}, default: []) + # `:mutes` is deprecated (replaced with `muted_users` relation) + field(:mutes, {:array, :string}, default: []) timestamps() end @@ -1054,7 +1073,7 @@ def mutes?(nil, _), do: false def mutes?(%User{} = user, %User{} = target), do: mutes_user?(user, target) def mutes_user?(%User{} = user, %User{} = target) do - UserMute.exists?(user, target) + UserRelationship.mute_exists?(user, target) end @spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean() @@ -1070,7 +1089,7 @@ def blocks?(%User{} = user, %User{} = target) do end def blocks_user?(%User{} = user, %User{} = target) do - UserBlock.exists?(user, target) + UserRelationship.block_exists?(user, target) end def blocks_user?(_, _), do: false @@ -1119,33 +1138,20 @@ def blocked_ap_ids(user) do |> Repo.all() end - defp related_ap_ids_sql(join_table, source_column, target_column) do - "(SELECT array_agg(u.ap_id) FROM users as u " <> - "INNER JOIN #{join_table} AS join_table " <> - "ON join_table.#{source_column} = $1 " <> - "WHERE u.id = join_table.#{target_column})" - end - - @related_ap_ids_sql_params %{ - blocked_users: ["user_blocks", "blocker_id", "blockee_id"], - muted_users: ["user_mutes", "muter_id", "mutee_id"] - } - - def related_ap_ids(user, relations) when is_list(relations) do - query = - relations - |> Enum.map(fn r -> @related_ap_ids_sql_params[r] end) - |> Enum.filter(& &1) - |> Enum.map(fn [join_table, source_column, target_column] -> - related_ap_ids_sql(join_table, source_column, target_column) - end) - |> Enum.join(", ") - - with {:ok, %{rows: [ap_ids_arrays]}} <- - Repo.query("SELECT #{query}", [FlakeId.from_string(user.id)]) do - ap_ids_arrays = Enum.map(ap_ids_arrays, &(&1 || [])) - {:ok, ap_ids_arrays} - end + @doc """ + Returns map of related AP IDs list by relation type. + E.g. `related_ap_ids(user, [:blocks])` -> `%{blocks: ["https://some.site/users/userapid"]}` + """ + @spec related_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())} + def related_ap_ids(%User{} = user, relationship_types) when is_list(relationship_types) do + user + |> assoc(:outgoing_relationships) + |> join(:inner, [user_rel], u in assoc(user_rel, :target)) + |> where([user_rel, u], user_rel.relationship_type in ^relationship_types) + |> select([user_rel, u], [user_rel.relationship_type, fragment("array_agg(?)", u.ap_id)]) + |> group_by([user_rel, u], user_rel.relationship_type) + |> Repo.all() + |> Enum.into(%{}, fn [k, v] -> {k, v} end) end @spec subscribers(User.t()) :: [User.t()] @@ -1918,19 +1924,20 @@ def unblock_domain(user, domain_blocked) do set_domain_blocks(user, List.delete(user.domain_blocks, domain_blocked)) end - @spec add_to_block(User.t(), User.t()) :: {:ok, UserBlock.t()} | {:error, Ecto.Changeset.t()} + @spec add_to_block(User.t(), User.t()) :: + {:ok, UserRelationship.t()} | {:error, Ecto.Changeset.t()} defp add_to_block(%User{} = user, %User{} = blocked) do - UserBlock.create(user, blocked) + UserRelationship.create_block(user, blocked) end @spec add_to_block(User.t(), User.t()) :: - {:ok, UserBlock.t()} | {:ok, nil} | {:error, Ecto.Changeset.t()} + {:ok, UserRelationship.t()} | {:ok, nil} | {:error, Ecto.Changeset.t()} defp remove_from_block(%User{} = user, %User{} = blocked) do - UserBlock.delete(user, blocked) + UserRelationship.delete_block(user, blocked) end defp add_to_mutes(%User{} = user, %User{ap_id: ap_id} = muted_user, notifications?) do - with {:ok, user_mute} <- UserMute.create(user, muted_user), + with {:ok, user_mute} <- UserRelationship.create_mute(user, muted_user), {:ok, _user} <- set_notification_mutes( user, @@ -1942,7 +1949,7 @@ defp add_to_mutes(%User{} = user, %User{ap_id: ap_id} = muted_user, notification end defp remove_from_mutes(user, %User{ap_id: ap_id} = muted_user) do - with {:ok, user_mute} <- UserMute.delete(user, muted_user), + with {:ok, user_mute} <- UserRelationship.delete_mute(user, muted_user), {:ok, _user} <- set_notification_mutes( user, diff --git a/lib/pleroma/user/search.ex b/lib/pleroma/user/search.ex index 049e1a634..2b3c7b5b4 100644 --- a/lib/pleroma/user/search.ex +++ b/lib/pleroma/user/search.ex @@ -100,11 +100,11 @@ defp base_query(user, true), do: User.get_followers_query(user) defp filter_blocked_user(query, %User{} = blocker) do query - |> join(:left, [u], b in Pleroma.UserBlock, + |> join(:left, [u], b in Pleroma.UserRelationship, as: :blocks, - on: b.blocker_id == ^blocker.id and u.id == b.blockee_id + on: b.relationship_type == ^:block and b.source_id == ^blocker.id and u.id == b.target_id ) - |> where([blocks: b], is_nil(b.blockee_id)) + |> where([blocks: b], is_nil(b.target_id)) end defp filter_blocked_user(query, _), do: query diff --git a/lib/pleroma/user_block.ex b/lib/pleroma/user_block.ex deleted file mode 100644 index bcf4b64d9..000000000 --- a/lib/pleroma/user_block.ex +++ /dev/null @@ -1,71 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.UserBlock do - use Ecto.Schema - - import Ecto.Changeset - import Ecto.Query - - alias Pleroma.Repo - alias Pleroma.User - alias Pleroma.UserBlock - - schema "user_blocks" do - belongs_to(:blocker, User, type: FlakeId.Ecto.CompatType) - belongs_to(:blockee, User, type: FlakeId.Ecto.CompatType) - - timestamps(updated_at: false) - end - - def changeset(%UserBlock{} = user_block, params \\ %{}) do - user_block - |> cast(params, [:blocker_id, :blockee_id]) - |> validate_required([:blocker_id, :blockee_id]) - |> unique_constraint(:blockee_id, name: :user_blocks_blocker_id_blockee_id_index) - |> validate_not_self_block() - end - - def exists?(%User{} = blocker, %User{} = blockee) do - UserBlock - |> where(blocker_id: ^blocker.id, blockee_id: ^blockee.id) - |> Repo.exists?() - end - - def create(%User{} = blocker, %User{} = blockee) do - %UserBlock{} - |> changeset(%{blocker_id: blocker.id, blockee_id: blockee.id}) - |> Repo.insert( - on_conflict: :replace_all_except_primary_key, - conflict_target: [:blocker_id, :blockee_id] - ) - end - - def delete(%User{} = blocker, %User{} = blockee) do - attrs = %{blocker_id: blocker.id, blockee_id: blockee.id} - - case Repo.get_by(UserBlock, attrs) do - %UserBlock{} = existing_record -> Repo.delete(existing_record) - nil -> {:ok, nil} - end - end - - defp validate_not_self_block(%Ecto.Changeset{} = changeset) do - changeset - |> validate_change(:blockee_id, fn _, blockee_id -> - if blockee_id == get_field(changeset, :blocker_id) do - [blockee_id: "can't be equal to blocker_id"] - else - [] - end - end) - |> validate_change(:blocker_id, fn _, blocker_id -> - if blocker_id == get_field(changeset, :blockee_id) do - [blocker_id: "can't be equal to blockee_id"] - else - [] - end - end) - end -end diff --git a/lib/pleroma/user_mute.ex b/lib/pleroma/user_mute.ex deleted file mode 100644 index 417a5ff84..000000000 --- a/lib/pleroma/user_mute.ex +++ /dev/null @@ -1,71 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.UserMute do - use Ecto.Schema - - import Ecto.Changeset - import Ecto.Query - - alias Pleroma.Repo - alias Pleroma.User - alias Pleroma.UserMute - - schema "user_mutes" do - belongs_to(:muter, User, type: FlakeId.Ecto.CompatType) - belongs_to(:mutee, User, type: FlakeId.Ecto.CompatType) - - timestamps(updated_at: false) - end - - def changeset(%UserMute{} = user_mute, params \\ %{}) do - user_mute - |> cast(params, [:muter_id, :mutee_id]) - |> validate_required([:muter_id, :mutee_id]) - |> unique_constraint(:mutee_id, name: :user_mutes_muter_id_mutee_id_index) - |> validate_not_self_mute() - end - - def exists?(%User{} = muter, %User{} = mutee) do - UserMute - |> where(muter_id: ^muter.id, mutee_id: ^mutee.id) - |> Repo.exists?() - end - - def create(%User{} = muter, %User{} = mutee) do - %UserMute{} - |> changeset(%{muter_id: muter.id, mutee_id: mutee.id}) - |> Repo.insert( - on_conflict: :replace_all_except_primary_key, - conflict_target: [:muter_id, :mutee_id] - ) - end - - def delete(%User{} = muter, %User{} = mutee) do - attrs = %{muter_id: muter.id, mutee_id: mutee.id} - - case Repo.get_by(UserMute, attrs) do - %UserMute{} = existing_record -> Repo.delete(existing_record) - nil -> {:ok, nil} - end - end - - defp validate_not_self_mute(%Ecto.Changeset{} = changeset) do - changeset - |> validate_change(:mutee_id, fn _, mutee_id -> - if mutee_id == get_field(changeset, :muter_id) do - [mutee_id: "can't be equal to muter_id"] - else - [] - end - end) - |> validate_change(:muter_id, fn _, muter_id -> - if muter_id == get_field(changeset, :mutee_id) do - [muter_id: "can't be equal to mutee_id"] - else - [] - end - end) - end -end diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex new file mode 100644 index 000000000..5cb99ae50 --- /dev/null +++ b/lib/pleroma/user_relationship.ex @@ -0,0 +1,90 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.UserRelationship do + use Ecto.Schema + + import Ecto.Changeset + import Ecto.Query + + alias Pleroma.Repo + alias Pleroma.User + alias Pleroma.UserRelationship + + schema "user_relationships" do + belongs_to(:source, User, type: FlakeId.Ecto.CompatType) + belongs_to(:target, User, type: FlakeId.Ecto.CompatType) + field(:relationship_type, UserRelationshipTypeEnum) + + timestamps(updated_at: false) + end + + def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do + user_relationship + |> cast(params, [:relationship_type, :source_id, :target_id]) + |> validate_required([:relationship_type, :source_id, :target_id]) + |> unique_constraint(:relationship_type, + name: :user_relationships_source_id_relationship_type_target_id_index + ) + |> validate_not_self_relationship() + end + + def exists?(relationship_type, %User{} = source, %User{} = target) do + UserRelationship + |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id) + |> Repo.exists?() + end + + def block_exists?(%User{} = blocker, %User{} = blockee), do: exists?(:block, blocker, blockee) + + def mute_exists?(%User{} = muter, %User{} = mutee), do: exists?(:mute, muter, mutee) + + def create(relationship_type, %User{} = source, %User{} = target) do + %UserRelationship{} + |> changeset(%{ + relationship_type: relationship_type, + source_id: source.id, + target_id: target.id + }) + |> Repo.insert( + on_conflict: :replace_all_except_primary_key, + conflict_target: [:source_id, :relationship_type, :target_id] + ) + end + + def create_block(%User{} = blocker, %User{} = blockee), do: create(:block, blocker, blockee) + + def create_mute(%User{} = muter, %User{} = mutee), do: create(:mute, muter, mutee) + + def delete(relationship_type, %User{} = source, %User{} = target) do + attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id} + + case Repo.get_by(UserRelationship, attrs) do + %UserRelationship{} = existing_record -> Repo.delete(existing_record) + nil -> {:ok, nil} + end + end + + def delete_block(%User{} = blocker, %User{} = blockee), do: delete(:block, blocker, blockee) + + def delete_mute(%User{} = muter, %User{} = mutee), do: delete(:mute, muter, mutee) + + defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do + changeset + |> validate_change(:target_id, fn _, target_id -> + if target_id == get_field(changeset, :source_id) do + [target_id: "can't be equal to source_id"] + else + [] + end + end) + |> validate_change(:source_id, fn _, source_id -> + if source_id == get_field(changeset, :target_id) do + [source_id: "can't be equal to target_id"] + else + [] + end + end) + end +end diff --git a/lib/pleroma/web/streamer/worker.ex b/lib/pleroma/web/streamer/worker.ex index 45f9c7c53..020112949 100644 --- a/lib/pleroma/web/streamer/worker.ex +++ b/lib/pleroma/web/streamer/worker.ex @@ -129,7 +129,9 @@ defp do_stream(%{topic: topic, item: item}) do end defp should_send?(%User{} = user, %Activity{} = item) do - {:ok, [blocks, mutes]} = User.related_ap_ids(user, [:blocked_users, :muted_users]) + related_ap_ids = User.related_ap_ids(user, [:block, :mute]) + blocks = related_ap_ids[:block] || [] + mutes = related_ap_ids[:mute] || [] reblog_mutes = user.muted_reblogs || [] recipient_blocks = MapSet.new(blocks ++ mutes) recipients = MapSet.new(item.recipients) diff --git a/mix.exs b/mix.exs index 81ce4f25c..c870b330f 100644 --- a/mix.exs +++ b/mix.exs @@ -100,6 +100,7 @@ defp deps do {:plug_cowboy, "~> 2.0"}, {:phoenix_pubsub, "~> 1.1"}, {:phoenix_ecto, "~> 4.0"}, + {:ecto_enum, "~> 1.4"}, {:ecto_sql, "~> 3.2"}, {:postgrex, ">= 0.13.5"}, {:oban, "~> 0.8.1"}, diff --git a/mix.lock b/mix.lock index d4a80df77..fc092b79f 100644 --- a/mix.lock +++ b/mix.lock @@ -24,6 +24,7 @@ "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm"}, "earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm"}, "ecto": {:hex, :ecto, "3.2.3", "51274df79862845b388733fddcf6f107d0c8c86e27abe7131fa98f8d30761bda", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"}, + "ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_sql": {:hex, :ecto_sql, "3.2.0", "751cea597e8deb616084894dd75cbabfdbe7255ff01e8c058ca13f0353a3921b", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.2.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"}, "esshd": {:hex, :esshd, "0.1.0", "6f93a2062adb43637edad0ea7357db2702a4b80dd9683482fe00f5134e97f4c1", [:mix], [], "hexpm"}, "eternal": {:hex, :eternal, "1.2.1", "d5b6b2499ba876c57be2581b5b999ee9bdf861c647401066d3eeed111d096bc4", [:mix], [], "hexpm"}, diff --git a/priv/repo/migrations/20191108161911_create_user_blocks.exs b/priv/repo/migrations/20191108161911_create_user_blocks.exs deleted file mode 100644 index c882d2bd6..000000000 --- a/priv/repo/migrations/20191108161911_create_user_blocks.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Pleroma.Repo.Migrations.CreateUserBlocks do - use Ecto.Migration - - def change do - create_if_not_exists table(:user_blocks) do - add(:blocker_id, references(:users, type: :uuid, on_delete: :delete_all)) - add(:blockee_id, references(:users, type: :uuid, on_delete: :delete_all)) - - timestamps(updated_at: false) - end - - create_if_not_exists(unique_index(:user_blocks, [:blocker_id, :blockee_id])) - end -end diff --git a/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs b/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs deleted file mode 100644 index fe537679d..000000000 --- a/priv/repo/migrations/20191108173911_data_migration_populate_user_blocks.exs +++ /dev/null @@ -1,49 +0,0 @@ -defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserBlocks do - use Ecto.Migration - - alias Ecto.Adapters.SQL - alias Pleroma.Repo - - require Logger - - def up do - {:ok, %{rows: block_rows}} = - SQL.query(Repo, "SELECT id, blocks FROM users WHERE blocks != '{}'") - - blockee_ap_ids = - Enum.flat_map( - block_rows, - fn [_, ap_ids] -> ap_ids end - ) - |> Enum.uniq() - - # Selecting ids of all blockees at once in order to reduce the number of SELECT queries - {:ok, %{rows: blockee_ap_id_id}} = - SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [blockee_ap_ids]) - - blockee_id_by_ap_id = Enum.into(blockee_ap_id_id, %{}, fn [k, v] -> {k, v} end) - - Enum.each( - block_rows, - fn [blocker_id, blockee_ap_ids] -> - blocker_uuid = Ecto.UUID.cast!(blocker_id) - - for blockee_ap_id <- blockee_ap_ids do - blockee_id = blockee_id_by_ap_id[blockee_ap_id] - - with {:ok, blockee_uuid} <- blockee_id && Ecto.UUID.cast(blockee_id) do - execute( - "INSERT INTO user_blocks(blocker_id, blockee_id, inserted_at) " <> - "VALUES('#{blocker_uuid}'::uuid, '#{blockee_uuid}'::uuid, now()) " <> - "ON CONFLICT (blocker_id, blockee_id) DO NOTHING" - ) - else - _ -> Logger.warn("Missing reference: (#{blocker_uuid}, #{blockee_id})") - end - end - end - ) - end - - def down, do: :noop -end diff --git a/priv/repo/migrations/20191112151559_create_user_mutes.exs b/priv/repo/migrations/20191112151559_create_user_mutes.exs deleted file mode 100644 index eaa285de2..000000000 --- a/priv/repo/migrations/20191112151559_create_user_mutes.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Pleroma.Repo.Migrations.CreateUserMutes do - use Ecto.Migration - - def change do - create_if_not_exists table(:user_mutes) do - add(:muter_id, references(:users, type: :uuid, on_delete: :delete_all)) - add(:mutee_id, references(:users, type: :uuid, on_delete: :delete_all)) - - timestamps(updated_at: false) - end - - create_if_not_exists(unique_index(:user_mutes, [:muter_id, :mutee_id])) - end -end diff --git a/priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs b/priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs deleted file mode 100644 index a8bdd072e..000000000 --- a/priv/repo/migrations/20191112151614_data_migration_populate_user_mutes.exs +++ /dev/null @@ -1,48 +0,0 @@ -defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserMutes do - use Ecto.Migration - - alias Ecto.Adapters.SQL - alias Pleroma.Repo - - require Logger - - def up do - {:ok, %{rows: mute_rows}} = SQL.query(Repo, "SELECT id, mutes FROM users WHERE mutes != '{}'") - - mutee_ap_ids = - Enum.flat_map( - mute_rows, - fn [_, ap_ids] -> ap_ids end - ) - |> Enum.uniq() - - # Selecting ids of all mutees at once in order to reduce the number of SELECT queries - {:ok, %{rows: mutee_ap_id_id}} = - SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [mutee_ap_ids]) - - mutee_id_by_ap_id = Enum.into(mutee_ap_id_id, %{}, fn [k, v] -> {k, v} end) - - Enum.each( - mute_rows, - fn [muter_id, mutee_ap_ids] -> - muter_uuid = Ecto.UUID.cast!(muter_id) - - for mutee_ap_id <- mutee_ap_ids do - mutee_id = mutee_id_by_ap_id[mutee_ap_id] - - with {:ok, mutee_uuid} <- mutee_id && Ecto.UUID.cast(mutee_id) do - execute( - "INSERT INTO user_mutes(muter_id, mutee_id, inserted_at) " <> - "VALUES('#{muter_uuid}'::uuid, '#{mutee_uuid}'::uuid, now()) " <> - "ON CONFLICT (muter_id, mutee_id) DO NOTHING" - ) - else - _ -> Logger.warn("Missing reference: (#{muter_uuid}, #{mutee_id})") - end - end - end - ) - end - - def down, do: :noop -end diff --git a/priv/repo/migrations/20191118084425_create_user_relationships.exs b/priv/repo/migrations/20191118084425_create_user_relationships.exs new file mode 100644 index 000000000..c281f887d --- /dev/null +++ b/priv/repo/migrations/20191118084425_create_user_relationships.exs @@ -0,0 +1,17 @@ +defmodule Pleroma.Repo.Migrations.CreateUserRelationships do + use Ecto.Migration + + def change do + create_if_not_exists table(:user_relationships) do + add(:source_id, references(:users, type: :uuid, on_delete: :delete_all)) + add(:target_id, references(:users, type: :uuid, on_delete: :delete_all)) + add(:relationship_type, :integer, null: false) + + timestamps(updated_at: false) + end + + create_if_not_exists( + unique_index(:user_relationships, [:source_id, :relationship_type, :target_id]) + ) + end +end diff --git a/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs b/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs new file mode 100644 index 000000000..f8dde7626 --- /dev/null +++ b/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs @@ -0,0 +1,64 @@ +defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserRelationships do + use Ecto.Migration + + alias Ecto.Adapters.SQL + alias Pleroma.Repo + + require Logger + + def up do + Enum.each( + [blocks: 1, mutes: 2, muted_reblogs: 3, muted_notifications: 4], + fn {field, relationship_type_code} -> + migrate(field, relationship_type_code) + end + ) + end + + def down, do: :noop + + defp migrate(field, relationship_type_code) do + Logger.info("Processing users.#{field}...") + + {:ok, %{rows: field_rows}} = + SQL.query(Repo, "SELECT id, #{field} FROM users WHERE #{field} != '{}'") + + target_ap_ids = + Enum.flat_map( + field_rows, + fn [_, ap_ids] -> ap_ids end + ) + |> Enum.uniq() + + # Selecting ids of all targets at once in order to reduce the number of SELECT queries + {:ok, %{rows: target_ap_id_id}} = + SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [target_ap_ids]) + + target_id_by_ap_id = Enum.into(target_ap_id_id, %{}, fn [k, v] -> {k, v} end) + + Enum.each( + field_rows, + fn [source_id, target_ap_ids] -> + source_uuid = Ecto.UUID.cast!(source_id) + + for target_ap_id <- target_ap_ids do + target_id = target_id_by_ap_id[target_ap_id] + + with {:ok, target_uuid} <- target_id && Ecto.UUID.cast(target_id) do + execute(""" + INSERT INTO user_relationships( + source_id, target_id, relationship_type, inserted_at + ) + VALUES( + '#{source_uuid}'::uuid, '#{target_uuid}'::uuid, #{relationship_type_code}, now() + ) + ON CONFLICT (source_id, relationship_type, target_id) DO NOTHING + """) + else + _ -> Logger.warn("Unresolved #{field} reference: (#{source_uuid}, #{target_id})") + end + end + end + ) + end +end From ba5cc3016514080b4bf7eaefd3e25936c0e222ba Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Tue, 19 Nov 2019 23:22:10 +0300 Subject: [PATCH 05/24] [#1335] Implemented notification mutes and reblog mutes as UserRelationships. User to UserRelationship relations and functions refactoring. --- lib/pleroma/notification.ex | 6 +- lib/pleroma/user.ex | 216 +++++++++--------- lib/pleroma/user_relationship.ex | 26 ++- lib/pleroma/web/activity_pub/activity_pub.ex | 6 +- lib/pleroma/web/common_api/common_api.ex | 13 +- .../controllers/account_controller.ex | 14 +- lib/pleroma/web/mastodon_api/mastodon_api.ex | 11 +- lib/pleroma/web/streamer/worker.ex | 14 +- test/conversation/participation_test.exs | 6 +- test/notification_test.exs | 17 +- test/user_test.exs | 33 ++- test/web/activity_pub/activity_pub_test.exs | 20 +- .../transmogrifier/follow_handling_test.exs | 2 +- test/web/common_api/common_api_test.exs | 6 +- .../controllers/account_controller_test.exs | 4 +- .../notification_controller_test.exs | 9 +- .../controllers/status_controller_test.exs | 4 +- .../controllers/timeline_controller_test.exs | 2 +- .../mastodon_api/views/account_view_test.exs | 13 +- .../mastodon_api/views/status_view_test.exs | 4 +- test/web/streamer/streamer_test.exs | 6 +- 21 files changed, 203 insertions(+), 229 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 82faef85e..15b3337f4 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -60,7 +60,7 @@ def for_user_query(user, opts \\ []) do end defp exclude_blocked(query, user) do - blocked_ap_ids = User.blocked_ap_ids(user) + blocked_ap_ids = User.blocked_users_ap_ids(user) query |> where([n, a], a.actor not in ^blocked_ap_ids) @@ -75,8 +75,10 @@ defp exclude_muted(query, _, %{with_muted: true}) do end defp exclude_muted(query, user, _opts) do + notification_muted_ap_ids = User.notification_muted_users_ap_ids(user) + query - |> where([n, a], a.actor not in ^user.muted_notifications) + |> where([n, a], a.actor not in ^notification_muted_ap_ids) |> join(:left, [n, a], tm in Pleroma.ThreadMute, on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data) ) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 4dd03c6c6..615f1b725 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -44,6 +44,20 @@ defmodule Pleroma.User do @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/ @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/ + # Format: [rel_type: [outgoing_rel: :outgoing_rel_target, incoming_rel: :incoming_rel_source]] + @user_relationships_config [ + block: [blocker_blocks: :blocked_users, blockee_blocks: :blocker_users], + mute: [muter_mutes: :muted_users, mutee_mutes: :muter_users], + reblog_mute: [ + reblog_muter_mutes: :reblog_muted_users, + reblog_mutee_mutes: :reblog_muter_users + ], + notification_mute: [ + notification_muter_mutes: :notification_muted_users, + notification_mutee_mutes: :notification_muter_users + ] + ] + schema "users" do field(:bio, :string) field(:email, :string) @@ -63,7 +77,6 @@ defmodule Pleroma.User do field(:tags, {:array, :string}, default: []) field(:last_refreshed_at, :naive_datetime_usec) field(:last_digest_emailed_at, :naive_datetime) - field(:banner, :map, default: %{}) field(:background, :map, default: %{}) field(:source_data, :map, default: %{}) @@ -77,8 +90,6 @@ defmodule Pleroma.User do field(:confirmation_token, :string, default: nil) field(:default_scope, :string, default: "public") field(:domain_blocks, {:array, :string}, default: []) - field(:muted_reblogs, {:array, :string}, default: []) - field(:muted_notifications, {:array, :string}, default: []) field(:subscribers, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) @@ -122,31 +133,29 @@ defmodule Pleroma.User do has_many(:outgoing_relationships, UserRelationship, foreign_key: :source_id) has_many(:incoming_relationships, UserRelationship, foreign_key: :target_id) - has_many(:blocker_blocks, UserRelationship, - foreign_key: :source_id, - where: [relationship_type: :block] - ) + for {relationship_type, + [ + {outgoing_relation, outgoing_relation_target}, + {incoming_relation, incoming_relation_source} + ]} <- @user_relationships_config do + # Definitions of `has_many :blocker_blocks`, `has_many :muter_mutes` etc. + has_many(outgoing_relation, UserRelationship, + foreign_key: :source_id, + where: [relationship_type: relationship_type] + ) - has_many(:blockee_blocks, UserRelationship, - foreign_key: :target_id, - where: [relationship_type: :block] - ) + # Definitions of `has_many :blockee_blocks`, `has_many :mutee_mutes` etc. + has_many(incoming_relation, UserRelationship, + foreign_key: :target_id, + where: [relationship_type: relationship_type] + ) - has_many(:blocked_users, through: [:blocker_blocks, :target]) - has_many(:blocker_users, through: [:blockee_blocks, :source]) + # Definitions of `has_many :blocked_users`, `has_many :muted_users` etc. + has_many(outgoing_relation_target, through: [outgoing_relation, :target]) - has_many(:muter_mutes, UserRelationship, - foreign_key: :source_id, - where: [relationship_type: :mute] - ) - - has_many(:mutee_mutes, UserRelationship, - foreign_key: :target_id, - where: [relationship_type: :mute] - ) - - has_many(:muted_users, through: [:muter_mutes, :target]) - has_many(:muter_users, through: [:mutee_mutes, :source]) + # Definitions of `has_many :blocker_users`, `has_many :muter_users` etc. + has_many(incoming_relation_source, through: [incoming_relation, :source]) + end field(:info, :map, default: %{}) @@ -154,10 +163,49 @@ defmodule Pleroma.User do field(:blocks, {:array, :string}, default: []) # `:mutes` is deprecated (replaced with `muted_users` relation) field(:mutes, {:array, :string}, default: []) + # `:muted_reblogs` is deprecated (replaced with `reblog_muted_users` relation) + field(:muted_reblogs, {:array, :string}, default: []) + # `:muted_notifications` is deprecated (replaced with `notification_muted_users` relation) + field(:muted_notifications, {:array, :string}, default: []) timestamps() end + for {_relationship_type, [{_outgoing_relation, outgoing_relation_target}, _]} <- + @user_relationships_config do + # Definitions of `blocked_users_relation/1`, `muted_users_relation/1`, etc. + def unquote(:"#{outgoing_relation_target}_relation")(user, restrict_deactivated? \\ false) do + target_users_query = assoc(user, unquote(outgoing_relation_target)) + + if restrict_deactivated? do + restrict_deactivated(target_users_query) + else + target_users_query + end + end + + # Definitions of `blocked_users/1`, `muted_users/1`, etc. + def unquote(outgoing_relation_target)(user, restrict_deactivated? \\ false) do + __MODULE__ + |> apply(unquote(:"#{outgoing_relation_target}_relation"), [ + user, + restrict_deactivated? + ]) + |> Repo.all() + end + + # Definitions of `blocked_users_ap_ids/1`, `muted_users_ap_ids/1`, etc. + def unquote(:"#{outgoing_relation_target}_ap_ids")(user, restrict_deactivated? \\ false) do + __MODULE__ + |> apply(unquote(:"#{outgoing_relation_target}_relation"), [ + user, + restrict_deactivated? + ]) + |> select([u], u.ap_id) + |> Repo.all() + end + end + @doc "Returns if the user should be allowed to authenticate" def auth_active?(%User{deactivated: true}), do: false @@ -996,7 +1044,8 @@ def get_recipients_from_activity(%Activity{recipients: to}) do |> Repo.all() end - @spec mute(User.t(), User.t(), boolean()) :: {:ok, User.t()} | {:error, String.t()} + @spec mute(User.t(), User.t(), boolean()) :: + {:ok, list(UserRelationship.t())} | {:error, String.t()} def mute(muter, %User{} = mutee, notifications? \\ true) do add_to_mutes(muter, mutee, notifications?) end @@ -1079,8 +1128,8 @@ def mutes_user?(%User{} = user, %User{} = target) do @spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean() def muted_notifications?(nil, _), do: false - def muted_notifications?(user, %{ap_id: ap_id}), - do: Enum.member?(user.muted_notifications, ap_id) + def muted_notifications?(user, %User{} = target), + do: UserRelationship.notification_mute_exists?(user, target) def blocks?(nil, _), do: false @@ -1108,50 +1157,27 @@ def subscribed_to?(user, %{ap_id: ap_id}) do end end - @spec muted_users(User.t()) :: [User.t()] - def muted_users(user) do - user - |> assoc(:muted_users) - |> restrict_deactivated() - |> Repo.all() - end - - def muted_ap_ids(user) do - user - |> assoc(:muted_users) - |> select([u], u.ap_id) - |> Repo.all() - end - - @spec blocked_users(User.t()) :: [User.t()] - def blocked_users(user) do - user - |> assoc(:blocked_users) - |> restrict_deactivated() - |> Repo.all() - end - - def blocked_ap_ids(user) do - user - |> assoc(:blocked_users) - |> select([u], u.ap_id) - |> Repo.all() - end - @doc """ Returns map of related AP IDs list by relation type. - E.g. `related_ap_ids(user, [:blocks])` -> `%{blocks: ["https://some.site/users/userapid"]}` + E.g. `related_ap_ids(user, [:block])` -> `%{block: ["https://some.site/users/userapid"]}` """ @spec related_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())} def related_ap_ids(%User{} = user, relationship_types) when is_list(relationship_types) do - user - |> assoc(:outgoing_relationships) - |> join(:inner, [user_rel], u in assoc(user_rel, :target)) - |> where([user_rel, u], user_rel.relationship_type in ^relationship_types) - |> select([user_rel, u], [user_rel.relationship_type, fragment("array_agg(?)", u.ap_id)]) - |> group_by([user_rel, u], user_rel.relationship_type) - |> Repo.all() - |> Enum.into(%{}, fn [k, v] -> {k, v} end) + db_result = + user + |> assoc(:outgoing_relationships) + |> join(:inner, [user_rel], u in assoc(user_rel, :target)) + |> where([user_rel, u], user_rel.relationship_type in ^relationship_types) + |> select([user_rel, u], [user_rel.relationship_type, fragment("array_agg(?)", u.ap_id)]) + |> group_by([user_rel, u], user_rel.relationship_type) + |> Repo.all() + |> Enum.into(%{}, fn [k, v] -> {k, v} end) + + Enum.into( + relationship_types, + %{}, + fn rel_type -> {rel_type, db_result[rel_type] || []} end + ) end @spec subscribers(User.t()) :: [User.t()] @@ -1572,7 +1598,7 @@ def all_superusers do end def showing_reblogs?(%User{} = user, %User{} = target) do - target.ap_id not in user.muted_reblogs + not UserRelationship.reblog_mute_exists?(user, target) end @doc """ @@ -1936,59 +1962,23 @@ defp remove_from_block(%User{} = user, %User{} = blocked) do UserRelationship.delete_block(user, blocked) end - defp add_to_mutes(%User{} = user, %User{ap_id: ap_id} = muted_user, notifications?) do + defp add_to_mutes(%User{} = user, %User{} = muted_user, notifications?) do with {:ok, user_mute} <- UserRelationship.create_mute(user, muted_user), - {:ok, _user} <- - set_notification_mutes( - user, - Enum.uniq([ap_id | user.muted_notifications]), - notifications? - ) do - {:ok, user_mute} + {:ok, user_notification_mute} <- + (notifications? && UserRelationship.create_notification_mute(user, muted_user)) || + {:ok, nil} do + {:ok, Enum.filter([user_mute, user_notification_mute], & &1)} end end - defp remove_from_mutes(user, %User{ap_id: ap_id} = muted_user) do + defp remove_from_mutes(user, %User{} = muted_user) do with {:ok, user_mute} <- UserRelationship.delete_mute(user, muted_user), - {:ok, _user} <- - set_notification_mutes( - user, - List.delete(user.muted_notifications, ap_id), - true - ) do - {:ok, user_mute} + {:ok, user_notification_mute} <- + UserRelationship.delete_notification_mute(user, muted_user) do + {:ok, [user_mute, user_notification_mute]} end end - defp set_notification_mutes(user, _muted_notifications, false = _notifications?) do - {:ok, user} - end - - defp set_notification_mutes(user, muted_notifications, true = _notifications?) do - params = %{muted_notifications: muted_notifications} - - user - |> cast(params, [:muted_notifications]) - |> validate_required([:muted_notifications]) - |> update_and_set_cache() - end - - def add_reblog_mute(user, ap_id) do - params = %{muted_reblogs: user.muted_reblogs ++ [ap_id]} - - user - |> cast(params, [:muted_reblogs]) - |> update_and_set_cache() - end - - def remove_reblog_mute(user, ap_id) do - params = %{muted_reblogs: List.delete(user.muted_reblogs, ap_id)} - - user - |> cast(params, [:muted_reblogs]) - |> update_and_set_cache() - end - def set_invisible(user, invisible) do params = %{invisible: invisible} diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex index 5cb99ae50..24c724549 100644 --- a/lib/pleroma/user_relationship.ex +++ b/lib/pleroma/user_relationship.ex @@ -20,6 +20,20 @@ defmodule Pleroma.UserRelationship do timestamps(updated_at: false) end + for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do + # Definitions of `create_block/2`, `create_mute/2` etc. + def unquote(:"create_#{relationship_type}")(source, target), + do: create(unquote(relationship_type), source, target) + + # Definitions of `delete_block/2`, `delete_mute/2` etc. + def unquote(:"delete_#{relationship_type}")(source, target), + do: delete(unquote(relationship_type), source, target) + + # Definitions of `block_exists?/2`, `mute_exists?/2` etc. + def unquote(:"#{relationship_type}_exists?")(source, target), + do: exists?(unquote(relationship_type), source, target) + end + def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do user_relationship |> cast(params, [:relationship_type, :source_id, :target_id]) @@ -36,10 +50,6 @@ def exists?(relationship_type, %User{} = source, %User{} = target) do |> Repo.exists?() end - def block_exists?(%User{} = blocker, %User{} = blockee), do: exists?(:block, blocker, blockee) - - def mute_exists?(%User{} = muter, %User{} = mutee), do: exists?(:mute, muter, mutee) - def create(relationship_type, %User{} = source, %User{} = target) do %UserRelationship{} |> changeset(%{ @@ -53,10 +63,6 @@ def create(relationship_type, %User{} = source, %User{} = target) do ) end - def create_block(%User{} = blocker, %User{} = blockee), do: create(:block, blocker, blockee) - - def create_mute(%User{} = muter, %User{} = mutee), do: create(:mute, muter, mutee) - def delete(relationship_type, %User{} = source, %User{} = target) do attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id} @@ -66,10 +72,6 @@ def delete(relationship_type, %User{} = source, %User{} = target) do end end - def delete_block(%User{} = blocker, %User{} = blockee), do: delete(:block, blocker, blockee) - - def delete_mute(%User{} = muter, %User{} = mutee), do: delete(:mute, muter, mutee) - defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do changeset |> validate_change(:target_id, fn _, target_id -> diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 7a4a4791e..61b7f9280 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -884,7 +884,7 @@ defp restrict_reblogs(query, _), do: query defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do - mutes = opts["muted_ap_ids"] || User.muted_ap_ids(user) + mutes = opts["muted_ap_ids"] || User.muted_users_ap_ids(user) query = from([activity] in query, @@ -902,7 +902,7 @@ defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do defp restrict_muted(query, _), do: query defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do - blocked_ap_ids = opts["blocked_ap_ids"] || User.blocked_ap_ids(user) + blocked_ap_ids = opts["blocked_ap_ids"] || User.blocked_users_ap_ids(user) domain_blocks = user.domain_blocks || [] query = @@ -945,7 +945,7 @@ defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) defp restrict_pinned(query, _), do: query defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user}) do - muted_reblogs = user.muted_reblogs || [] + muted_reblogs = User.reblog_muted_users_ap_ids(user) from( activity in query, diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index fe6e26a90..afda9ffce 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Web.CommonAPI do alias Pleroma.Object alias Pleroma.ThreadMute alias Pleroma.User + alias Pleroma.UserRelationship alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Visibility @@ -420,15 +421,11 @@ defp set_visibility(activity, %{"visibility" => visibility}) do defp set_visibility(activity, _), do: {:ok, activity} - def hide_reblogs(user, %{ap_id: ap_id} = _muted) do - if ap_id not in user.muted_reblogs do - User.add_reblog_mute(user, ap_id) - end + def hide_reblogs(%User{} = user, %User{} = target) do + UserRelationship.create_reblog_mute(user, target) end - def show_reblogs(user, %{ap_id: ap_id} = _muted) do - if ap_id in user.muted_reblogs do - User.remove_reblog_mute(user, ap_id) - end + def show_reblogs(%User{} = user, %User{} = target) do + UserRelationship.delete_reblog_mute(user, target) end end diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index 750f5c690..5b6158b93 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -323,9 +323,7 @@ def unfollow(%{assigns: %{user: follower, account: followed}} = conn, _params) d def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do notifications? = params |> Map.get("notifications", true) |> truthy_param?() - with {:ok, _user_mute} <- User.mute(muter, muted, notifications?) do - # TODO: remove `muter` refresh once `muted_notifications` field is deprecated - muter = User.get_cached_by_id(muter.id) + with {:ok, _user_relationships} <- User.mute(muter, muted, notifications?) do render(conn, "relationship.json", user: muter, target: muted) else {:error, message} -> json_response(conn, :forbidden, %{error: message}) @@ -334,9 +332,7 @@ def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do @doc "POST /api/v1/accounts/:id/unmute" def unmute(%{assigns: %{user: muter, account: muted}} = conn, _params) do - with {:ok, _user_mute} <- User.unmute(muter, muted) do - # TODO: remove `muter` refresh once `muted_notifications` field is deprecated - muter = User.get_cached_by_id(muter.id) + with {:ok, _user_relationships} <- User.unmute(muter, muted) do render(conn, "relationship.json", user: muter, target: muted) else {:error, message} -> json_response(conn, :forbidden, %{error: message}) @@ -377,12 +373,14 @@ def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do @doc "GET /api/v1/mutes" def mutes(%{assigns: %{user: user}} = conn, _) do - render(conn, "index.json", users: User.muted_users(user), for: user, as: :user) + users = User.muted_users(user, _restrict_deactivated = true) + render(conn, "index.json", users: users, for: user, as: :user) end @doc "GET /api/v1/blocks" def blocks(%{assigns: %{user: user}} = conn, _) do - render(conn, "index.json", users: User.blocked_users(user), for: user, as: :user) + users = User.blocked_users(user, _restrict_deactivated = true) + render(conn, "index.json", users: users, for: user, as: :user) end @doc "GET /api/v1/endorsements" diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index d875a5788..ee253a342 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -24,19 +24,16 @@ def follow(follower, followed, params \\ %{}) do with {:ok, follower, _followed, _} <- result do options = cast_params(params) - - case reblogs_visibility(options[:reblogs], result) do - {:ok, follower} -> {:ok, follower} - _ -> {:ok, follower} - end + set_reblogs_visibility(options[:reblogs], result) + {:ok, follower} end end - defp reblogs_visibility(false, {:ok, follower, followed, _}) do + defp set_reblogs_visibility(false, {:ok, follower, followed, _}) do CommonAPI.hide_reblogs(follower, followed) end - defp reblogs_visibility(_, {:ok, follower, followed, _}) do + defp set_reblogs_visibility(_, {:ok, follower, followed, _}) do CommonAPI.show_reblogs(follower, followed) end diff --git a/lib/pleroma/web/streamer/worker.ex b/lib/pleroma/web/streamer/worker.ex index 020112949..07e677d29 100644 --- a/lib/pleroma/web/streamer/worker.ex +++ b/lib/pleroma/web/streamer/worker.ex @@ -129,17 +129,17 @@ defp do_stream(%{topic: topic, item: item}) do end defp should_send?(%User{} = user, %Activity{} = item) do - related_ap_ids = User.related_ap_ids(user, [:block, :mute]) - blocks = related_ap_ids[:block] || [] - mutes = related_ap_ids[:mute] || [] - reblog_mutes = user.muted_reblogs || [] - recipient_blocks = MapSet.new(blocks ++ mutes) + %{block: blocked_ap_ids, mute: muted_ap_ids, reblog_mute: reblog_muted_ap_ids} = + User.related_ap_ids(user, [:block, :mute, :reblog_mute]) + + recipient_blocks = MapSet.new(blocked_ap_ids ++ muted_ap_ids) recipients = MapSet.new(item.recipients) domain_blocks = Pleroma.Web.ActivityPub.MRF.subdomains_regex(user.domain_blocks) with parent <- Object.normalize(item) || item, - true <- Enum.all?([blocks, mutes, reblog_mutes], &(item.actor not in &1)), - true <- Enum.all?([blocks, mutes], &(parent.data["actor"] not in &1)), + true <- + Enum.all?([blocked_ap_ids, muted_ap_ids, reblog_muted_ap_ids], &(item.actor not in &1)), + true <- Enum.all?([blocked_ap_ids, muted_ap_ids], &(parent.data["actor"] not in &1)), true <- MapSet.disjoint?(recipients, recipient_blocks), %{host: item_host} <- URI.parse(item.actor), %{host: parent_host} <- URI.parse(parent.data["actor"]), diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs index a36f45a5a..9b2c97963 100644 --- a/test/conversation/participation_test.exs +++ b/test/conversation/participation_test.exs @@ -252,7 +252,7 @@ test "when the user blocks a recipient, the existing conversations with them are assert User.get_cached_by_id(blocker.id).unread_conversation_count == 4 - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) # The conversations with the blocked user are marked as read assert [%{read: true}, %{read: true}, %{read: true}, %{read: false}] = @@ -274,7 +274,7 @@ test "the new conversation with the blocked user is not marked as unread " do blocked = insert(:user) third_user = insert(:user) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) # When the blocked user is the author {:ok, _direct1} = @@ -311,7 +311,7 @@ test "the conversation with the blocked user is not marked as unread on a reply" "visibility" => "direct" }) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) assert [%{read: true}] = Participation.for_user(blocker) assert User.get_cached_by_id(blocker.id).unread_conversation_count == 0 diff --git a/test/notification_test.exs b/test/notification_test.exs index d3d409c68..44f1bb414 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -93,7 +93,7 @@ test "it creates a notification for user if the user blocks the activity author" activity = insert(:note_activity) author = User.get_cached_by_ap_id(activity.data["actor"]) user = insert(:user) - {:ok, _user_block} = User.block(user, author) + {:ok, _user_relationship} = User.block(user, author) assert Notification.create_notification(activity, user) end @@ -112,7 +112,7 @@ test "notification created if user is muted without notifications" do muter = insert(:user) muted = insert(:user) - {:ok, _user_mute} = User.mute(muter, muted, false) + {:ok, _user_relationships} = User.mute(muter, muted, false) {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"}) @@ -636,7 +636,7 @@ test "notifications are deleted if a remote user is deleted" do test "it returns notifications for muted user without notifications" do user = insert(:user) muted = insert(:user) - {:ok, _user_mute} = User.mute(user, muted, false) + {:ok, _user_relationships} = User.mute(user, muted, false) {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"}) @@ -646,10 +646,7 @@ test "it returns notifications for muted user without notifications" do test "it doesn't return notifications for muted user with notifications" do user = insert(:user) muted = insert(:user) - {:ok, _user_mute} = User.mute(user, muted) - - # Refreshing to reflect embedded ap id relation fields (remove once removed) - user = refresh_record(user) + {:ok, _user_relationships} = User.mute(user, muted) {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"}) @@ -659,7 +656,7 @@ test "it doesn't return notifications for muted user with notifications" do test "it doesn't return notifications for blocked user" do user = insert(:user) blocked = insert(:user) - {:ok, _user_block} = User.block(user, blocked) + {:ok, _user_relationship} = User.block(user, blocked) {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"}) @@ -689,7 +686,7 @@ test "it doesn't return notifications for muted thread" do test "it returns notifications from a muted user when with_muted is set" do user = insert(:user) muted = insert(:user) - {:ok, _user_mute} = User.mute(user, muted) + {:ok, _user_relationships} = User.mute(user, muted) {:ok, _activity} = CommonAPI.post(muted, %{"status" => "hey @#{user.nickname}"}) @@ -699,7 +696,7 @@ test "it returns notifications from a muted user when with_muted is set" do test "it doesn't return notifications from a blocked user when with_muted is set" do user = insert(:user) blocked = insert(:user) - {:ok, _user_block} = User.block(user, blocked) + {:ok, _user_relationship} = User.block(user, blocked) {:ok, _activity} = CommonAPI.post(blocked, %{"status" => "hey @#{user.nickname}"}) diff --git a/test/user_test.exs b/test/user_test.exs index 9e3c4b984..b0838e498 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -100,7 +100,7 @@ test "clears follow requests when requester is blocked" do CommonAPI.follow(follower, followed) assert [_activity] = User.get_follow_requests(followed) - {:ok, _user_block} = User.block(followed, follower) + {:ok, _user_relationship} = User.block(followed, follower) assert [] = User.get_follow_requests(followed) end @@ -113,8 +113,8 @@ test "follow_all follows mutliple users" do not_followed = insert(:user) reverse_blocked = insert(:user) - {:ok, _user_block} = User.block(user, blocked) - {:ok, _user_block} = User.block(reverse_blocked, user) + {:ok, _user_relationship} = User.block(user, blocked) + {:ok, _user_relationship} = User.block(reverse_blocked, user) {:ok, user} = User.follow(user, followed_zero) @@ -166,7 +166,7 @@ test "can't follow a user who blocked us" do blocker = insert(:user) blockee = insert(:user) - {:ok, _user_block} = User.block(blocker, blockee) + {:ok, _user_relationship} = User.block(blocker, blockee) {:error, _} = User.follow(blockee, blocker) end @@ -175,7 +175,7 @@ test "can't subscribe to a user who blocked us" do blocker = insert(:user) blocked = insert(:user) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) {:error, _} = User.subscribe(blocked, blocker) end @@ -673,10 +673,7 @@ test "it mutes people" do refute User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) - {:ok, _user_mute} = User.mute(user, muted_user) - - # Refreshing to reflect embedded ap id relation fields (remove once removed) - user = refresh_record(user) + {:ok, _user_relationships} = User.mute(user, muted_user) assert User.mutes?(user, muted_user) assert User.muted_notifications?(user, muted_user) @@ -686,7 +683,7 @@ test "it unmutes users" do user = insert(:user) muted_user = insert(:user) - {:ok, _user_mute} = User.mute(user, muted_user) + {:ok, _user_relationships} = User.mute(user, muted_user) {:ok, _user_mute} = User.unmute(user, muted_user) refute User.mutes?(user, muted_user) @@ -700,7 +697,7 @@ test "it mutes user without notifications" do refute User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) - {:ok, _user_mute} = User.mute(user, muted_user, false) + {:ok, _user_relationships} = User.mute(user, muted_user, false) assert User.mutes?(user, muted_user) refute User.muted_notifications?(user, muted_user) @@ -714,7 +711,7 @@ test "it blocks people" do refute User.blocks?(user, blocked_user) - {:ok, _user_block} = User.block(user, blocked_user) + {:ok, _user_relationship} = User.block(user, blocked_user) assert User.blocks?(user, blocked_user) end @@ -723,7 +720,7 @@ test "it unblocks users" do user = insert(:user) blocked_user = insert(:user) - {:ok, _user_block} = User.block(user, blocked_user) + {:ok, _user_relationship} = User.block(user, blocked_user) {:ok, _user_block} = User.unblock(user, blocked_user) refute User.blocks?(user, blocked_user) @@ -739,7 +736,7 @@ test "blocks tear down cyclical follow relationships" do assert User.following?(blocker, blocked) assert User.following?(blocked, blocker) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) blocked = User.get_cached_by_id(blocked.id) assert User.blocks?(blocker, blocked) @@ -757,7 +754,7 @@ test "blocks tear down blocker->blocked follow relationships" do assert User.following?(blocker, blocked) refute User.following?(blocked, blocker) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) blocked = User.get_cached_by_id(blocked.id) assert User.blocks?(blocker, blocked) @@ -775,7 +772,7 @@ test "blocks tear down blocked->blocker follow relationships" do refute User.following?(blocker, blocked) assert User.following?(blocked, blocker) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) blocked = User.get_cached_by_id(blocked.id) assert User.blocks?(blocker, blocked) @@ -793,7 +790,7 @@ test "blocks tear down blocked->blocker subscription relationships" do assert User.subscribed_to?(blocked, blocker) refute User.subscribed_to?(blocker, blocked) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) assert User.blocks?(blocker, blocked) refute User.subscribed_to?(blocker, blocked) @@ -1323,7 +1320,7 @@ test "follower count is updated when a follower is blocked" do {:ok, _follower2} = User.follow(follower2, user) {:ok, _follower3} = User.follow(follower3, user) - {:ok, _user_block} = User.block(user, follower) + {:ok, _user_relationship} = User.block(user, follower) user = refresh_record(user) assert User.user_info(user).follower_count == 2 diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 4e2321966..1946a603e 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -484,7 +484,7 @@ test "retrieves activities that have a given context" do activity_five = insert(:note_activity) user = insert(:user) - {:ok, _user_block} = User.block(user, %{ap_id: activity_five.data["actor"]}) + {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]}) activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user}) assert activities == [activity_two, activity] @@ -497,7 +497,7 @@ test "doesn't return blocked activities" do activity_three = insert(:note_activity) user = insert(:user) booster = insert(:user) - {:ok, _user_block} = User.block(user, %{ap_id: activity_one.data["actor"]}) + {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]}) activities = ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true}) @@ -515,7 +515,7 @@ test "doesn't return blocked activities" do assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_one) - {:ok, _user_block} = User.block(user, %{ap_id: activity_three.data["actor"]}) + {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]}) {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster) %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id) activity_three = Activity.get_by_id(activity_three.id) @@ -542,7 +542,7 @@ test "doesn't return transitive interactions concerning blocked users" do blockee = insert(:user) friend = insert(:user) - {:ok, _user_block} = User.block(blocker, blockee) + {:ok, _user_relationship} = User.block(blocker, blockee) {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"}) @@ -565,7 +565,7 @@ test "doesn't return announce activities concerning blocked users" do blockee = insert(:user) friend = insert(:user) - {:ok, _user_block} = User.block(blocker, blockee) + {:ok, _user_relationship} = User.block(blocker, blockee) {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"}) @@ -613,7 +613,7 @@ test "doesn't return muted activities" do booster = insert(:user) activity_one_actor = User.get_by_ap_id(activity_one.data["actor"]) - {:ok, _user_mute} = User.mute(user, activity_one_actor) + {:ok, _user_relationships} = User.mute(user, activity_one_actor) activities = ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true}) @@ -644,7 +644,7 @@ test "doesn't return muted activities" do assert Enum.member?(activities, activity_one) activity_three_actor = User.get_by_ap_id(activity_three.data["actor"]) - {:ok, _user_mute} = User.mute(user, activity_three_actor) + {:ok, _user_relationships} = User.mute(user, activity_three_actor) {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster) %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id) activity_three = Activity.get_by_id(activity_three.id) @@ -791,7 +791,7 @@ test "doesn't return reblogs for users for whom reblogs have been muted" do activity = insert(:note_activity) user = insert(:user) booster = insert(:user) - {:ok, user} = CommonAPI.hide_reblogs(user, booster) + {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster) {:ok, activity, _} = CommonAPI.repeat(activity.id, booster) @@ -804,8 +804,8 @@ test "returns reblogs for users for whom reblogs have not been muted" do activity = insert(:note_activity) user = insert(:user) booster = insert(:user) - {:ok, user} = CommonAPI.hide_reblogs(user, booster) - {:ok, user} = CommonAPI.show_reblogs(user, booster) + {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster) + {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster) {:ok, activity, _} = CommonAPI.repeat(activity.id, booster) diff --git a/test/web/activity_pub/transmogrifier/follow_handling_test.exs b/test/web/activity_pub/transmogrifier/follow_handling_test.exs index 5c67d41c5..7d6d0814d 100644 --- a/test/web/activity_pub/transmogrifier/follow_handling_test.exs +++ b/test/web/activity_pub/transmogrifier/follow_handling_test.exs @@ -128,7 +128,7 @@ test "it rejects incoming follow requests from blocked users when deny_follow_bl user = insert(:user) {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin") - {:ok, _user_block} = User.block(user, target) + {:ok, _user_relationship} = User.block(user, target) data = File.read!("test/fixtures/mastodon-follow-activity.json") diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 138488d44..667ca89b9 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -509,14 +509,14 @@ test "updates state of multiple reports" do end test "add a reblog mute", %{muter: muter, muted: muted} do - {:ok, muter} = CommonAPI.hide_reblogs(muter, muted) + {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted) assert User.showing_reblogs?(muter, muted) == false end test "remove a reblog mute", %{muter: muter, muted: muted} do - {:ok, muter} = CommonAPI.hide_reblogs(muter, muted) - {:ok, muter} = CommonAPI.show_reblogs(muter, muted) + {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted) + {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted) assert User.showing_reblogs?(muter, muted) == true end diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index c5ef96bb7..9ff008a50 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -868,7 +868,7 @@ test "getting a list of mutes", %{conn: conn} do user = insert(:user) other_user = insert(:user) - {:ok, _user_mute} = User.mute(user, other_user) + {:ok, _user_relationships} = User.mute(user, other_user) conn = conn @@ -883,7 +883,7 @@ test "getting a list of blocks", %{conn: conn} do user = insert(:user) other_user = insert(:user) - {:ok, _user_block} = User.block(user, other_user) + {:ok, _user_relationship} = User.block(user, other_user) conn = conn diff --git a/test/web/mastodon_api/controllers/notification_controller_test.exs b/test/web/mastodon_api/controllers/notification_controller_test.exs index 75b871a43..00a85169e 100644 --- a/test/web/mastodon_api/controllers/notification_controller_test.exs +++ b/test/web/mastodon_api/controllers/notification_controller_test.exs @@ -289,10 +289,7 @@ test "doesn't see notifications after muting user with notifications", %{conn: c assert length(json_response(conn, 200)) == 1 - {:ok, _user_mute} = User.mute(user, user2) - - # Refreshing to reflect embedded ap id relation fields (remove once removed) - user = refresh_record(user) + {:ok, _user_relationships} = User.mute(user, user2) conn = assign(build_conn(), :user, user) conn = get(conn, "/api/v1/notifications") @@ -313,7 +310,7 @@ test "see notifications after muting user without notifications", %{conn: conn} assert length(json_response(conn, 200)) == 1 - {:ok, _user_mute} = User.mute(user, user2, false) + {:ok, _user_relationships} = User.mute(user, user2, false) conn = assign(build_conn(), :user, user) conn = get(conn, "/api/v1/notifications") @@ -336,7 +333,7 @@ test "see notifications after muting user with notifications and with_muted para assert length(json_response(conn, 200)) == 1 - {:ok, _user_mute} = User.mute(user, user2) + {:ok, _user_relationships} = User.mute(user, user2) conn = assign(build_conn(), :user, user) conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"}) diff --git a/test/web/mastodon_api/controllers/status_controller_test.exs b/test/web/mastodon_api/controllers/status_controller_test.exs index e8a1054ac..5fbe947ba 100644 --- a/test/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/web/mastodon_api/controllers/status_controller_test.exs @@ -1108,7 +1108,7 @@ test "does not return users who have favorited the status but are blocked", %{ activity: activity } do other_user = insert(:user) - {:ok, _user_block} = User.block(user, other_user) + {:ok, _user_relationship} = User.block(user, other_user) {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) @@ -1205,7 +1205,7 @@ test "does not return users who have reblogged the status but are blocked", %{ activity: activity } do other_user = insert(:user) - {:ok, _user_block} = User.block(user, other_user) + {:ok, _user_relationship} = User.block(user, other_user) {:ok, _, _} = CommonAPI.repeat(activity.id, other_user) diff --git a/test/web/mastodon_api/controllers/timeline_controller_test.exs b/test/web/mastodon_api/controllers/timeline_controller_test.exs index 524529d1f..dc17cc963 100644 --- a/test/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/web/mastodon_api/controllers/timeline_controller_test.exs @@ -194,7 +194,7 @@ test "doesn't include DMs from blocked users", %{conn: conn} do blocker = insert(:user) blocked = insert(:user) user = insert(:user) - {:ok, _user_block} = User.block(blocker, blocked) + {:ok, _user_relationship} = User.block(blocker, blocked) {:ok, _blocked_direct} = CommonAPI.post(blocked, %{ diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index f8b74553b..4e5da6b06 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -191,11 +191,8 @@ test "represent a relationship for the following and followed user" do {:ok, user} = User.follow(user, other_user) {:ok, other_user} = User.follow(other_user, user) {:ok, other_user} = User.subscribe(user, other_user) - {:ok, _user_mute} = User.mute(user, other_user, true) - {:ok, user} = CommonAPI.hide_reblogs(user, other_user) - - # Refreshing to reflect embedded ap id relation fields (remove once removed) - user = refresh_record(user) + {:ok, _user_relationships} = User.mute(user, other_user, true) + {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user) expected = %{ id: to_string(other_user.id), @@ -222,8 +219,8 @@ test "represent a relationship for the blocking and blocked user" do {:ok, user} = User.follow(user, other_user) {:ok, other_user} = User.subscribe(user, other_user) - {:ok, _user_block} = User.block(user, other_user) - {:ok, _user_block} = User.block(other_user, user) + {:ok, _user_relationship} = User.block(user, other_user) + {:ok, _user_relationship} = User.block(other_user, user) expected = %{ id: to_string(other_user.id), @@ -294,7 +291,7 @@ test "represent an embedded relationship" do other_user = insert(:user) {:ok, other_user} = User.follow(other_user, user) - {:ok, _user_block} = User.block(other_user, user) + {:ok, _user_relationship} = User.block(other_user, user) {:ok, _} = User.follow(insert(:user), user) expected = %{ diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index 89b146e1e..bdd87a79e 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -183,7 +183,7 @@ test "tells if the message is muted for some reason" do user = insert(:user) other_user = insert(:user) - {:ok, _user_mute} = User.mute(user, other_user) + {:ok, _user_relationships} = User.mute(user, other_user) {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"}) status = StatusView.render("show.json", %{activity: activity}) @@ -199,7 +199,7 @@ test "tells if the message is thread muted" do user = insert(:user) other_user = insert(:user) - {:ok, _user_mute} = User.mute(user, other_user) + {:ok, _user_relationships} = User.mute(user, other_user) {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"}) status = StatusView.render("show.json", %{activity: activity, for: user}) diff --git a/test/web/streamer/streamer_test.exs b/test/web/streamer/streamer_test.exs index 6461fc4c3..802c22e5d 100644 --- a/test/web/streamer/streamer_test.exs +++ b/test/web/streamer/streamer_test.exs @@ -59,7 +59,7 @@ test "it doesn't send notify to the 'user:notification' stream when a user is bl user: user } do blocked = insert(:user) - {:ok, _user_block} = User.block(user, blocked) + {:ok, _user_relationship} = User.block(user, blocked) task = Task.async(fn -> refute_receive {:text, _}, 4_000 end) @@ -259,7 +259,7 @@ test "it sends message if recipients invalid and thread containment is enabled b test "it doesn't send messages involving blocked users" do user = insert(:user) blocked_user = insert(:user) - {:ok, _user_block} = User.block(user, blocked_user) + {:ok, _user_relationship} = User.block(user, blocked_user) task = Task.async(fn -> @@ -301,7 +301,7 @@ test "it doesn't send messages transitively involving blocked users" do "public" => [fake_socket] } - {:ok, _user_block} = User.block(blocker, blockee) + {:ok, _user_relationship} = User.block(blocker, blockee) {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"}) From 555edd01abf62b74c14c30caf82d77a80a93e72c Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 20 Nov 2019 12:19:07 +0300 Subject: [PATCH 06/24] [#1335] User AP ID relations fetching performance optimizations. --- lib/pleroma/notification.ex | 37 ++++++++++++++---- lib/pleroma/user.ex | 11 ++++-- lib/pleroma/web/activity_pub/activity_pub.ex | 40 ++++++++++++++++---- lib/pleroma/web/streamer/worker.ex | 2 +- 4 files changed, 70 insertions(+), 20 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 15b3337f4..d4b6eb3b8 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -21,6 +21,8 @@ defmodule Pleroma.Notification do @type t :: %__MODULE__{} + @include_muted_option :with_muted + schema "notifications" do field(:seen, :boolean, default: false) belongs_to(:user, User, type: FlakeId.Ecto.CompatType) @@ -34,7 +36,25 @@ def changeset(%Notification{} = notification, attrs) do |> cast(attrs, [:seen]) end - def for_user_query(user, opts \\ []) do + defp for_user_query_ap_id_opts(user, opts) do + ap_id_relations = + [:block] ++ + if opts[@include_muted_option], do: [], else: [:notification_mute] + + preloaded_ap_ids = User.outgoing_relations_ap_ids(user, ap_id_relations) + + exclude_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts) + + exclude_notification_muted_opts = + Map.merge(%{notification_muted_users_ap_ids: preloaded_ap_ids[:notification_mute]}, opts) + + {exclude_blocked_opts, exclude_notification_muted_opts} + end + + def for_user_query(user, opts \\ %{}) do + {exclude_blocked_opts, exclude_notification_muted_opts} = + for_user_query_ap_id_opts(user, opts) + Notification |> where(user_id: ^user.id) |> where( @@ -54,13 +74,13 @@ def for_user_query(user, opts \\ []) do ) ) |> preload([n, a, o], activity: {a, object: o}) - |> exclude_muted(user, opts) - |> exclude_blocked(user) + |> exclude_notification_muted(user, exclude_notification_muted_opts) + |> exclude_blocked(user, exclude_blocked_opts) |> exclude_visibility(opts) end - defp exclude_blocked(query, user) do - blocked_ap_ids = User.blocked_users_ap_ids(user) + defp exclude_blocked(query, user, opts) do + blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user) query |> where([n, a], a.actor not in ^blocked_ap_ids) @@ -70,12 +90,13 @@ defp exclude_blocked(query, user) do ) end - defp exclude_muted(query, _, %{with_muted: true}) do + defp exclude_notification_muted(query, _, %{@include_muted_option => true}) do query end - defp exclude_muted(query, user, _opts) do - notification_muted_ap_ids = User.notification_muted_users_ap_ids(user) + defp exclude_notification_muted(query, user, opts) do + notification_muted_ap_ids = + opts[:notification_muted_users_ap_ids] || User.notification_muted_users_ap_ids(user) query |> where([n, a], a.actor not in ^notification_muted_ap_ids) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 615f1b725..4b53dce13 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1158,11 +1158,14 @@ def subscribed_to?(user, %{ap_id: ap_id}) do end @doc """ - Returns map of related AP IDs list by relation type. - E.g. `related_ap_ids(user, [:block])` -> `%{block: ["https://some.site/users/userapid"]}` + Returns map of outgoing (blocked, muted etc.) relations' user AP IDs by relation type. + E.g. `outgoing_relations_ap_ids(user, [:block])` -> `%{block: ["https://some.site/users/userapid"]}` """ - @spec related_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())} - def related_ap_ids(%User{} = user, relationship_types) when is_list(relationship_types) do + @spec outgoing_relations_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())} + def outgoing_relations_ap_ids(_, []), do: %{} + + def outgoing_relations_ap_ids(%User{} = user, relationship_types) + when is_list(relationship_types) do db_result = user |> assoc(:outgoing_relationships) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 61b7f9280..b24139117 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -884,7 +884,7 @@ defp restrict_reblogs(query, _), do: query defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do - mutes = opts["muted_ap_ids"] || User.muted_users_ap_ids(user) + mutes = opts["muted_users_ap_ids"] || User.muted_users_ap_ids(user) query = from([activity] in query, @@ -902,7 +902,7 @@ defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do defp restrict_muted(query, _), do: query defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do - blocked_ap_ids = opts["blocked_ap_ids"] || User.blocked_users_ap_ids(user) + blocked_ap_ids = opts["blocked_users_ap_ids"] || User.blocked_users_ap_ids(user) domain_blocks = user.domain_blocks || [] query = @@ -944,8 +944,8 @@ defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) defp restrict_pinned(query, _), do: query - defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user}) do - muted_reblogs = User.reblog_muted_users_ap_ids(user) + defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user} = opts) do + muted_reblogs = opts["reblog_muted_users_ap_ids"] || User.reblog_muted_users_ap_ids(user) from( activity in query, @@ -1012,7 +1012,33 @@ defp maybe_order(query, %{order: :asc}) do defp maybe_order(query, _), do: query + defp fetch_activities_query_ap_ids_ops(opts) do + source_user = opts["muting_user"] + ap_id_relations = if source_user, do: [:mute, :reblog_mute], else: [] + + ap_id_relations = + ap_id_relations ++ + if opts["blocking_user"] && opts["blocking_user"] == source_user do + [:block] + else + [] + end + + preloaded_ap_ids = User.outgoing_relations_ap_ids(source_user, ap_id_relations) + + restrict_blocked_opts = Map.merge(%{"blocked_users_ap_ids" => preloaded_ap_ids[:block]}, opts) + restrict_muted_opts = Map.merge(%{"muted_users_ap_ids" => preloaded_ap_ids[:mute]}, opts) + + restrict_muted_reblogs_opts = + Map.merge(%{"reblog_muted_users_ap_ids" => preloaded_ap_ids[:reblog_mute]}, opts) + + {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} + end + def fetch_activities_query(recipients, opts \\ %{}) do + {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} = + fetch_activities_query_ap_ids_ops(opts) + config = %{ skip_thread_containment: Config.get([:instance, :skip_thread_containment]) } @@ -1032,15 +1058,15 @@ def fetch_activities_query(recipients, opts \\ %{}) do |> restrict_type(opts) |> restrict_state(opts) |> restrict_favorited_by(opts) - |> restrict_blocked(opts) - |> restrict_muted(opts) + |> restrict_blocked(restrict_blocked_opts) + |> restrict_muted(restrict_muted_opts) |> restrict_media(opts) |> restrict_visibility(opts) |> restrict_thread_visibility(opts, config) |> restrict_replies(opts) |> restrict_reblogs(opts) |> restrict_pinned(opts) - |> restrict_muted_reblogs(opts) + |> restrict_muted_reblogs(restrict_muted_reblogs_opts) |> Activity.restrict_deactivated_users() |> exclude_poll_votes(opts) |> exclude_visibility(opts) diff --git a/lib/pleroma/web/streamer/worker.ex b/lib/pleroma/web/streamer/worker.ex index 07e677d29..a1b445f2f 100644 --- a/lib/pleroma/web/streamer/worker.ex +++ b/lib/pleroma/web/streamer/worker.ex @@ -130,7 +130,7 @@ defp do_stream(%{topic: topic, item: item}) do defp should_send?(%User{} = user, %Activity{} = item) do %{block: blocked_ap_ids, mute: muted_ap_ids, reblog_mute: reblog_muted_ap_ids} = - User.related_ap_ids(user, [:block, :mute, :reblog_mute]) + User.outgoing_relations_ap_ids(user, [:block, :mute, :reblog_mute]) recipient_blocks = MapSet.new(blocked_ap_ids ++ muted_ap_ids) recipients = MapSet.new(item.recipients) From de892d2fe1e70054aaf946b4cd11fb39111fe937 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 20 Nov 2019 15:46:11 +0300 Subject: [PATCH 07/24] [#1335] Reorganized users.subscribers as UserRelationship. Added tests for UserRelationship-related functionality. --- lib/pleroma/ecto_enums.ex | 8 +- lib/pleroma/user.ex | 103 +++++++------- lib/pleroma/web/common_api/common_api.ex | 2 +- lib/pleroma/web/common_api/utils.ex | 2 +- .../controllers/account_controller.ex | 4 +- ..._migration_populate_user_relationships.exs | 6 +- test/support/factory.ex | 12 ++ test/user_relationship_test.exs | 130 ++++++++++++++++++ test/user_test.exs | 52 ++++++- test/web/common_api/common_api_test.exs | 2 +- .../mastodon_api/views/account_view_test.exs | 4 +- 11 files changed, 263 insertions(+), 62 deletions(-) create mode 100644 test/user_relationship_test.exs diff --git a/lib/pleroma/ecto_enums.ex b/lib/pleroma/ecto_enums.ex index bad5ec523..b86229312 100644 --- a/lib/pleroma/ecto_enums.ex +++ b/lib/pleroma/ecto_enums.ex @@ -4,4 +4,10 @@ import EctoEnum -defenum(UserRelationshipTypeEnum, block: 1, mute: 2, reblog_mute: 3, notification_mute: 4) +defenum(UserRelationshipTypeEnum, + block: 1, + mute: 2, + reblog_mute: 3, + notification_mute: 4, + inverse_subscription: 5 +) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 4b53dce13..d97c19d38 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -44,10 +44,17 @@ defmodule Pleroma.User do @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/ @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/ + # AP ID user relationships (blocks, mutes etc.) # Format: [rel_type: [outgoing_rel: :outgoing_rel_target, incoming_rel: :incoming_rel_source]] @user_relationships_config [ - block: [blocker_blocks: :blocked_users, blockee_blocks: :blocker_users], - mute: [muter_mutes: :muted_users, mutee_mutes: :muter_users], + block: [ + blocker_blocks: :blocked_users, + blockee_blocks: :blocker_users + ], + mute: [ + muter_mutes: :muted_users, + mutee_mutes: :muter_users + ], reblog_mute: [ reblog_muter_mutes: :reblog_muted_users, reblog_mutee_mutes: :reblog_muter_users @@ -55,6 +62,11 @@ defmodule Pleroma.User do notification_mute: [ notification_muter_mutes: :notification_muted_users, notification_mutee_mutes: :notification_muter_users + ], + # Note: `inverse_subscription` relationship is inverse: subscriber acts as relationship target + inverse_subscription: [ + subscribee_subscriptions: :subscriber_users, + subscriber_subscriptions: :subscribee_users ] ] @@ -90,7 +102,6 @@ defmodule Pleroma.User do field(:confirmation_token, :string, default: nil) field(:default_scope, :string, default: "public") field(:domain_blocks, {:array, :string}, default: []) - field(:subscribers, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) @@ -167,6 +178,8 @@ defmodule Pleroma.User do field(:muted_reblogs, {:array, :string}, default: []) # `:muted_notifications` is deprecated (replaced with `notification_muted_users` relation) field(:muted_notifications, {:array, :string}, default: []) + # `:subscribers` is deprecated (replaced with `subscriber_users` relation) + field(:subscribers, {:array, :string}, default: []) timestamps() end @@ -1046,33 +1059,43 @@ def get_recipients_from_activity(%Activity{recipients: to}) do @spec mute(User.t(), User.t(), boolean()) :: {:ok, list(UserRelationship.t())} | {:error, String.t()} - def mute(muter, %User{} = mutee, notifications? \\ true) do + def mute(%User{} = muter, %User{} = mutee, notifications? \\ true) do add_to_mutes(muter, mutee, notifications?) end - def unmute(muter, %User{} = mutee) do + def unmute(%User{} = muter, %User{} = mutee) do remove_from_mutes(muter, mutee) end - def subscribe(subscriber, %{ap_id: ap_id}) do - with %User{} = subscribed <- get_cached_by_ap_id(ap_id) do - deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked]) + def subscribe(%User{} = subscriber, %User{} = target) do + deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked]) - if blocks?(subscribed, subscriber) and deny_follow_blocked do - {:error, "Could not subscribe: #{subscribed.nickname} is blocking you"} - else - User.add_to_subscribers(subscribed, subscriber.ap_id) - end + if blocks?(target, subscriber) and deny_follow_blocked do + {:error, "Could not subscribe: #{target.nickname} is blocking you"} + else + # Note: the relationship is inverse: subscriber acts as relationship target + UserRelationship.create_inverse_subscription(target, subscriber) end end - def unsubscribe(unsubscriber, %{ap_id: ap_id}) do + def subscribe(%User{} = subscriber, %{ap_id: ap_id}) do + with %User{} = subscribee <- get_cached_by_ap_id(ap_id) do + subscribe(subscriber, subscribee) + end + end + + def unsubscribe(%User{} = unsubscriber, %User{} = target) do + # Note: the relationship is inverse: subscriber acts as relationship target + UserRelationship.delete_inverse_subscription(target, unsubscriber) + end + + def unsubscribe(%User{} = unsubscriber, %{ap_id: ap_id}) do with %User{} = user <- get_cached_by_ap_id(ap_id) do - User.remove_from_subscribers(user, unsubscriber.ap_id) + unsubscribe(unsubscriber, user) end end - def block(blocker, %User{} = blocked) do + def block(%User{} = blocker, %User{} = blocked) do # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213) blocker = if following?(blocker, blocked) do @@ -1089,13 +1112,7 @@ def block(blocker, %User{} = blocked) do nil -> blocked end - blocker = - if subscribed_to?(blocked, blocker) do - {:ok, blocker} = unsubscribe(blocked, blocker) - blocker - else - blocker - end + unsubscribe(blocked, blocker) if following?(blocked, blocker), do: unfollow(blocked, blocker) @@ -1105,16 +1122,16 @@ def block(blocker, %User{} = blocked) do end # helper to handle the block given only an actor's AP id - def block(blocker, %{ap_id: ap_id}) do + def block(%User{} = blocker, %{ap_id: ap_id}) do block(blocker, get_cached_by_ap_id(ap_id)) end - def unblock(blocker, %User{} = blocked) do + def unblock(%User{} = blocker, %User{} = blocked) do remove_from_block(blocker, blocked) end # helper to handle the block given only an actor's AP id - def unblock(blocker, %{ap_id: ap_id}) do + def unblock(%User{} = blocker, %{ap_id: ap_id}) do unblock(blocker, get_cached_by_ap_id(ap_id)) end @@ -1128,7 +1145,7 @@ def mutes_user?(%User{} = user, %User{} = target) do @spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean() def muted_notifications?(nil, _), do: false - def muted_notifications?(user, %User{} = target), + def muted_notifications?(%User{} = user, %User{} = target), do: UserRelationship.notification_mute_exists?(user, target) def blocks?(nil, _), do: false @@ -1151,9 +1168,14 @@ def blocks_domain?(%User{} = user, %User{} = target) do def blocks_domain?(_, _), do: false - def subscribed_to?(user, %{ap_id: ap_id}) do + def subscribed_to?(%User{} = user, %User{} = target) do + # Note: the relationship is inverse: subscriber acts as relationship target + UserRelationship.inverse_subscription_exists?(target, user) + end + + def subscribed_to?(%User{} = user, %{ap_id: ap_id}) do with %User{} = target <- get_cached_by_ap_id(ap_id) do - Enum.member?(target.subscribers, user.ap_id) + subscribed_to?(user, target) end end @@ -1183,12 +1205,6 @@ def outgoing_relations_ap_ids(%User{} = user, relationship_types) ) end - @spec subscribers(User.t()) :: [User.t()] - def subscribers(user) do - User.Query.build(%{ap_id: user.subscribers, deactivated: false}) - |> Repo.all() - end - def deactivate_async(user, status \\ true) do BackgroundWorker.enqueue("deactivate_user", %{"user_id" => user.id, "status" => status}) end @@ -1919,23 +1935,6 @@ def update_email_notifications(user, settings) do |> update_and_set_cache() end - defp set_subscribers(user, subscribers) do - params = %{subscribers: subscribers} - - user - |> cast(params, [:subscribers]) - |> validate_required([:subscribers]) - |> update_and_set_cache() - end - - def add_to_subscribers(user, subscribed) do - set_subscribers(user, Enum.uniq([subscribed | user.subscribers])) - end - - def remove_from_subscribers(user, subscribed) do - set_subscribers(user, List.delete(user.subscribers, subscribed)) - end - defp set_domain_blocks(user, domain_blocks) do params = %{domain_blocks: domain_blocks} diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index afda9ffce..2f3bcfc3c 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -33,7 +33,7 @@ def follow(follower, followed) do def unfollow(follower, unfollowed) do with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed), {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed), - {:ok, _unfollowed} <- User.unsubscribe(follower, unfollowed) do + {:ok, _subscription} <- User.unsubscribe(follower, unfollowed) do {:ok, follower} end end diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 88a5f434a..f77076906 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -492,7 +492,7 @@ def maybe_notify_subscribers( with %User{} = user <- User.get_cached_by_ap_id(actor) do subscriber_ids = user - |> User.subscribers() + |> User.subscriber_users() |> Enum.filter(&Visibility.visible_for_user?(activity, &1)) |> Enum.map(& &1.ap_id) diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index bc2f1017c..773cd9a97 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -144,7 +144,7 @@ def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do @doc "POST /api/v1/pleroma/accounts/:id/subscribe" def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do - with {:ok, subscription_target} <- User.subscribe(user, subscription_target) do + with {:ok, _subscription} <- User.subscribe(user, subscription_target) do render(conn, "relationship.json", user: user, target: subscription_target) else {:error, message} -> json_response(conn, :forbidden, %{error: message}) @@ -153,7 +153,7 @@ def subscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _p @doc "POST /api/v1/pleroma/accounts/:id/unsubscribe" def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, _params) do - with {:ok, subscription_target} <- User.unsubscribe(user, subscription_target) do + with {:ok, _subscription} <- User.unsubscribe(user, subscription_target) do render(conn, "relationship.json", user: user, target: subscription_target) else {:error, message} -> json_response(conn, :forbidden, %{error: message}) diff --git a/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs b/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs index f8dde7626..990e9f3b8 100644 --- a/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs +++ b/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs @@ -8,9 +8,13 @@ defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserRelationships do def up do Enum.each( - [blocks: 1, mutes: 2, muted_reblogs: 3, muted_notifications: 4], + [blocks: 1, mutes: 2, muted_reblogs: 3, muted_notifications: 4, subscribers: 5], fn {field, relationship_type_code} -> migrate(field, relationship_type_code) + + if field == :subscribers do + drop_if_exists(index(:users, [:subscribers])) + end end ) end diff --git a/test/support/factory.ex b/test/support/factory.ex index e3f797f64..c2ceb5836 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -43,6 +43,18 @@ def user_factory do } end + def user_relationship_factory(attrs \\ %{}) do + source = attrs[:source] || insert(:user) + target = attrs[:target] || insert(:user) + relationship_type = attrs[:relationship_type] || :block + + %Pleroma.UserRelationship{ + source_id: source.id, + target_id: target.id, + relationship_type: relationship_type + } + end + def note_factory(attrs \\ %{}) do text = sequence(:text, &"This is :moominmamma: note #{&1}") diff --git a/test/user_relationship_test.exs b/test/user_relationship_test.exs new file mode 100644 index 000000000..437450bc3 --- /dev/null +++ b/test/user_relationship_test.exs @@ -0,0 +1,130 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.UserRelationshipTest do + alias Pleroma.UserRelationship + + use Pleroma.DataCase + + import Pleroma.Factory + + describe "*_exists?/2" do + setup do + {:ok, users: insert_list(2, :user)} + end + + test "returns false if record doesn't exist", %{users: [user1, user2]} do + refute UserRelationship.block_exists?(user1, user2) + refute UserRelationship.mute_exists?(user1, user2) + refute UserRelationship.notification_mute_exists?(user1, user2) + refute UserRelationship.reblog_mute_exists?(user1, user2) + refute UserRelationship.inverse_subscription_exists?(user1, user2) + end + + test "returns true if record exists", %{users: [user1, user2]} do + for relationship_type <- [ + :block, + :mute, + :notification_mute, + :reblog_mute, + :inverse_subscription + ] do + insert(:user_relationship, + source: user1, + target: user2, + relationship_type: relationship_type + ) + end + + assert UserRelationship.block_exists?(user1, user2) + assert UserRelationship.mute_exists?(user1, user2) + assert UserRelationship.notification_mute_exists?(user1, user2) + assert UserRelationship.reblog_mute_exists?(user1, user2) + assert UserRelationship.inverse_subscription_exists?(user1, user2) + end + end + + describe "create_*/2" do + setup do + {:ok, users: insert_list(2, :user)} + end + + test "creates user relationship record if it doesn't exist", %{users: [user1, user2]} do + for relationship_type <- [ + :block, + :mute, + :notification_mute, + :reblog_mute, + :inverse_subscription + ] do + insert(:user_relationship, + source: user1, + target: user2, + relationship_type: relationship_type + ) + end + + UserRelationship.create_block(user1, user2) + UserRelationship.create_mute(user1, user2) + UserRelationship.create_notification_mute(user1, user2) + UserRelationship.create_reblog_mute(user1, user2) + UserRelationship.create_inverse_subscription(user1, user2) + + assert UserRelationship.block_exists?(user1, user2) + assert UserRelationship.mute_exists?(user1, user2) + assert UserRelationship.notification_mute_exists?(user1, user2) + assert UserRelationship.reblog_mute_exists?(user1, user2) + assert UserRelationship.inverse_subscription_exists?(user1, user2) + end + + test "if record already exists, returns it", %{users: [user1, user2]} do + user_block = UserRelationship.create_block(user1, user2) + assert user_block == UserRelationship.create_block(user1, user2) + end + end + + describe "delete_*/2" do + setup do + {:ok, users: insert_list(2, :user)} + end + + test "deletes user relationship record if it exists", %{users: [user1, user2]} do + for relationship_type <- [ + :block, + :mute, + :notification_mute, + :reblog_mute, + :inverse_subscription + ] do + insert(:user_relationship, + source: user1, + target: user2, + relationship_type: relationship_type + ) + end + + assert {:ok, %UserRelationship{}} = UserRelationship.delete_block(user1, user2) + assert {:ok, %UserRelationship{}} = UserRelationship.delete_mute(user1, user2) + assert {:ok, %UserRelationship{}} = UserRelationship.delete_notification_mute(user1, user2) + assert {:ok, %UserRelationship{}} = UserRelationship.delete_reblog_mute(user1, user2) + + assert {:ok, %UserRelationship{}} = + UserRelationship.delete_inverse_subscription(user1, user2) + + refute UserRelationship.block_exists?(user1, user2) + refute UserRelationship.mute_exists?(user1, user2) + refute UserRelationship.notification_mute_exists?(user1, user2) + refute UserRelationship.reblog_mute_exists?(user1, user2) + refute UserRelationship.inverse_subscription_exists?(user1, user2) + end + + test "if record does not exist, returns {:ok, nil}", %{users: [user1, user2]} do + assert {:ok, nil} = UserRelationship.delete_block(user1, user2) + assert {:ok, nil} = UserRelationship.delete_mute(user1, user2) + assert {:ok, nil} = UserRelationship.delete_notification_mute(user1, user2) + assert {:ok, nil} = UserRelationship.delete_reblog_mute(user1, user2) + assert {:ok, nil} = UserRelationship.delete_inverse_subscription(user1, user2) + end + end +end diff --git a/test/user_test.exs b/test/user_test.exs index b0838e498..ae90edbe9 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -25,6 +25,56 @@ defmodule Pleroma.UserTest do clear_config([:instance, :account_activation_required]) + describe "AP ID user relationships" do + setup do + {:ok, user: insert(:user)} + end + + test "outgoing_relations_ap_ids/1", %{user: user} do + rel_types = [:block, :mute, :notification_mute, :reblog_mute, :inverse_subscription] + + ap_ids_by_rel = + Enum.into( + rel_types, + %{}, + fn rel_type -> + rel_records = + insert_list(2, :user_relationship, %{source: user, relationship_type: rel_type}) + + ap_ids = Enum.map(rel_records, fn rr -> Repo.preload(rr, :target).target.ap_id end) + {rel_type, Enum.sort(ap_ids)} + end + ) + + assert ap_ids_by_rel[:block] == Enum.sort(User.blocked_users_ap_ids(user)) + assert ap_ids_by_rel[:block] == Enum.sort(Enum.map(User.blocked_users(user), & &1.ap_id)) + + assert ap_ids_by_rel[:mute] == Enum.sort(User.muted_users_ap_ids(user)) + assert ap_ids_by_rel[:mute] == Enum.sort(Enum.map(User.muted_users(user), & &1.ap_id)) + + assert ap_ids_by_rel[:notification_mute] == + Enum.sort(User.notification_muted_users_ap_ids(user)) + + assert ap_ids_by_rel[:notification_mute] == + Enum.sort(Enum.map(User.notification_muted_users(user), & &1.ap_id)) + + assert ap_ids_by_rel[:reblog_mute] == Enum.sort(User.reblog_muted_users_ap_ids(user)) + + assert ap_ids_by_rel[:reblog_mute] == + Enum.sort(Enum.map(User.reblog_muted_users(user), & &1.ap_id)) + + assert ap_ids_by_rel[:inverse_subscription] == Enum.sort(User.subscriber_users_ap_ids(user)) + + assert ap_ids_by_rel[:inverse_subscription] == + Enum.sort(Enum.map(User.subscriber_users(user), & &1.ap_id)) + + outgoing_relations_ap_ids = User.outgoing_relations_ap_ids(user, rel_types) + + assert ap_ids_by_rel == + Enum.into(outgoing_relations_ap_ids, %{}, fn {k, v} -> {k, Enum.sort(v)} end) + end + end + describe "when tags are nil" do test "tagging a user" do user = insert(:user, %{tags: nil}) @@ -785,7 +835,7 @@ test "blocks tear down blocked->blocker subscription relationships" do blocker = insert(:user) blocked = insert(:user) - {:ok, blocker} = User.subscribe(blocked, blocker) + {:ok, _subscription} = User.subscribe(blocked, blocker) assert User.subscribed_to?(blocked, blocker) refute User.subscribed_to?(blocker, blocked) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 667ca89b9..b5d6d4055 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -526,7 +526,7 @@ test "remove a reblog mute", %{muter: muter, muted: muted} do test "also unsubscribes a user" do [follower, followed] = insert_pair(:user) {:ok, follower, followed, _} = CommonAPI.follow(follower, followed) - {:ok, followed} = User.subscribe(follower, followed) + {:ok, _subscription} = User.subscribe(follower, followed) assert User.subscribed_to?(follower, followed) diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index 4e5da6b06..53cd26a69 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -190,7 +190,7 @@ test "represent a relationship for the following and followed user" do {:ok, user} = User.follow(user, other_user) {:ok, other_user} = User.follow(other_user, user) - {:ok, other_user} = User.subscribe(user, other_user) + {:ok, _subscription} = User.subscribe(user, other_user) {:ok, _user_relationships} = User.mute(user, other_user, true) {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user) @@ -218,7 +218,7 @@ test "represent a relationship for the blocking and blocked user" do other_user = insert(:user) {:ok, user} = User.follow(user, other_user) - {:ok, other_user} = User.subscribe(user, other_user) + {:ok, _subscription} = User.subscribe(user, other_user) {:ok, _user_relationship} = User.block(user, other_user) {:ok, _user_relationship} = User.block(other_user, user) From 81f1c6bc4725f6d97d42ff89f24328ae5b505a2d Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 20 Nov 2019 15:57:51 +0300 Subject: [PATCH 08/24] [#1335] Added CHANGELOG.md entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bd835a3d..3a259ef63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - OStatus: Extract RSS functionality - Deprecated `User.Info` embedded schema (fields moved to `User`) - Store status data inside Flag activity +- Deprecated (reorganized as `UserRelationship` entity) User fields with user AP IDs (`blocks`, `mutes`, `muted_reblogs`, `muted_notifications`, `subscribers`).
API Changes From 2b341627da5d592bdedc66a331409f5228ab28cf Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 25 Nov 2019 00:04:29 +0900 Subject: [PATCH 09/24] Admin API: Render whole status in grouped reports --- CHANGELOG.md | 1 + lib/pleroma/web/activity_pub/utils.ex | 23 +++++++++++++++---- .../web/admin_api/views/report_view.ex | 8 ++++++- .../admin_api/admin_api_controller_test.exs | 16 ++++--------- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bd835a3d..443e5f3c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Mastodon API: `pleroma.thread_muted` to the Status entity - Mastodon API: Mark the direct conversation as read for the author when they send a new direct message - Mastodon API, streaming: Add `pleroma.direct_conversation_id` to the `conversation` stream event payload. +- Admin API: Render whole status in grouped reports
### Added diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index c45662359..277ca3c7c 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -822,20 +822,33 @@ def parse_report_group(activity) do reports = get_reports_by_status_id(activity["id"]) max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"])) actors = Enum.map(reports, & &1.user_actor) + {deleted, status} = get_status_data(activity) %{ date: max_date.data["published"], account: activity["actor"], - status: %{ - id: activity["id"], - content: activity["content"], - published: activity["published"] - }, + status: status, + status_deleted: deleted, actors: Enum.uniq(actors), reports: reports } end + defp get_status_data(activity) do + case Activity.get_by_ap_id(activity["id"]) do + %Activity{} = act -> + {false, act} + + _ -> + {true, + %{ + id: activity["id"], + content: activity["content"], + published: activity["published"] + }} + end + end + def get_reports_by_status_id(ap_id) do from(a in Activity, where: fragment("(?)->>'type' = 'Flag'", a.data), diff --git a/lib/pleroma/web/admin_api/views/report_view.ex b/lib/pleroma/web/admin_api/views/report_view.ex index ca88595c7..0ba94def9 100644 --- a/lib/pleroma/web/admin_api/views/report_view.ex +++ b/lib/pleroma/web/admin_api/views/report_view.ex @@ -45,10 +45,16 @@ def render("show.json", %{report: report, user: user, account: account, statuses def render("index_grouped.json", %{groups: groups}) do reports = Enum.map(groups, fn group -> + status = + if group[:status_deleted], + do: group[:status], + else: StatusView.render("show.json", %{activity: group[:status]}) + %{ date: group[:date], account: group[:account], - status: group[:status], + status: status, + status_deleted: status_deleted, actors: Enum.map(group[:actors], &merge_account_views/1), reports: group[:reports] diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 3a4c4d65c..ea1b4c48c 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -15,6 +15,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do alias Pleroma.UserInviteToken alias Pleroma.Web.ActivityPub.Relay alias Pleroma.Web.CommonAPI + alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MediaProxy import Pleroma.Factory @@ -1616,14 +1617,11 @@ test "returns reports grouped by status", %{ assert length(response["reports"]) == 3 - first_group = - Enum.find(response["reports"], &(&1["status"]["id"] == first_status.data["id"])) + first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id)) - second_group = - Enum.find(response["reports"], &(&1["status"]["id"] == second_status.data["id"])) + second_group = Enum.find(response["reports"], &(&1["status"]["id"] == second_status.id)) - third_group = - Enum.find(response["reports"], &(&1["status"]["id"] == third_status.data["id"])) + third_group = Enum.find(response["reports"], &(&1["status"]["id"] == third_status.id)) assert length(first_group["reports"]) == 3 assert length(second_group["reports"]) == 2 @@ -1634,11 +1632,7 @@ test "returns reports grouped by status", %{ NaiveDateTime.from_iso8601!(act.data["published"]) end).data["published"] - assert first_group["status"] == %{ - "id" => first_status.data["id"], - "content" => first_status.object.data["content"], - "published" => first_status.object.data["published"] - } + assert first_group["status"] == StatusView.render("show.json", %{activity: first_status}) assert first_group["account"]["id"] == target_user.id From 40059c9b1e404412da103aaf74333bec364d3099 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 25 Nov 2019 00:05:52 +0900 Subject: [PATCH 10/24] Typo --- lib/pleroma/web/admin_api/views/report_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/admin_api/views/report_view.ex b/lib/pleroma/web/admin_api/views/report_view.ex index 0ba94def9..45ce75272 100644 --- a/lib/pleroma/web/admin_api/views/report_view.ex +++ b/lib/pleroma/web/admin_api/views/report_view.ex @@ -54,7 +54,7 @@ def render("index_grouped.json", %{groups: groups}) do date: group[:date], account: group[:account], status: status, - status_deleted: status_deleted, + status_deleted: group[:status_deleted], actors: Enum.map(group[:actors], &merge_account_views/1), reports: group[:reports] From 1364d303f8e4dcd4d9f7913d4755c58b0f4b87ef Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 25 Nov 2019 01:39:35 +0900 Subject: [PATCH 11/24] AdminAPI: Fix grouped reports for closed/resolved reports --- lib/pleroma/web/activity_pub/utils.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 277ca3c7c..9e460b604 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -852,7 +852,8 @@ defp get_status_data(activity) do def get_reports_by_status_id(ap_id) do from(a in Activity, where: fragment("(?)->>'type' = 'Flag'", a.data), - where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}]) + where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}]), + or_where: fragment("(?)->'object' @> ?", a.data, ^[ap_id]) ) |> Activity.with_preloaded_user_actor() |> Repo.all() From b3b4e5ca805780f278b6239973d6d497b1697fbd Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Fri, 22 Nov 2019 13:35:21 +0900 Subject: [PATCH 12/24] AdminAPI: Grouped reports old/new fix If some status received reports both in the "new" format and "old" format it was considered reports on two different statuses (in the context of grouped reports) --- CHANGELOG.md | 1 + lib/pleroma/web/activity_pub/utils.ex | 78 ++++++++++++++------------- test/web/activity_pub/utils_test.exs | 43 --------------- 3 files changed, 41 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 443e5f3c3..60512c6b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Mastodon API: Fix private and direct statuses not being filtered out from the public timeline for an authenticated user (`GET /api/v1/timelines/public`) - Mastodon API: Inability to get some local users by nickname in `/api/v1/accounts/:id_or_nickname` +- AdminAPI: If some status received reports both in the "new" format and "old" format it was considered reports on two different statuses (in the context of grouped reports) ## [1.1.6] - 2019-11-19 diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 9e460b604..718e3328d 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -788,36 +788,6 @@ def get_reports(params, page, page_size) do ActivityPub.fetch_activities([], params, :offset) end - @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{ - required(:groups) => [ - %{ - required(:date) => String.t(), - required(:account) => %{}, - required(:status) => %{}, - required(:actors) => [%User{}], - required(:reports) => [%Activity{}] - } - ], - required(:total) => integer - } - def get_reports_grouped_by_status(groups) do - parsed_groups = - groups - |> Enum.map(fn entry -> - activity = - case Jason.decode(entry.activity) do - {:ok, activity} -> activity - _ -> build_flag_object(entry.activity) - end - - parse_report_group(activity) - end) - - %{ - groups: parsed_groups - } - end - def parse_report_group(activity) do reports = get_reports_by_status_id(activity["id"]) max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"])) @@ -859,6 +829,32 @@ def get_reports_by_status_id(ap_id) do |> Repo.all() end + @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{ + required(:groups) => [ + %{ + required(:date) => String.t(), + required(:account) => %{}, + required(:status) => %{}, + required(:actors) => [%User{}], + required(:reports) => [%Activity{}] + } + ], + required(:total) => integer + } + def get_reports_grouped_by_status(activity_ids) do + parsed_groups = + activity_ids + |> Enum.map(fn id -> + id + |> build_flag_object() + |> parse_report_group() + end) + + %{ + groups: parsed_groups + } + end + @spec get_reported_activities() :: [ %{ required(:activity) => String.t(), @@ -866,17 +862,23 @@ def get_reports_by_status_id(ap_id) do } ] def get_reported_activities do - from(a in Activity, - where: fragment("(?)->>'type' = 'Flag'", a.data), + reported_activities_query = + from(a in Activity, + where: fragment("(?)->>'type' = 'Flag'", a.data), + select: %{ + activity: fragment("jsonb_array_elements((? #- '{object,0}')->'object')", a.data) + }, + group_by: fragment("activity") + ) + + from(a in subquery(reported_activities_query), + distinct: true, select: %{ - date: fragment("max(?->>'published') date", a.data), - activity: - fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity", a.data) - }, - group_by: fragment("activity"), - order_by: fragment("date DESC") + id: fragment("COALESCE(?->>'id'::text, ? #>> '{}')", a.activity, a.activity) + } ) |> Repo.all() + |> Enum.map(& &1.id) end def update_report_state(%Activity{} = activity, state) diff --git a/test/web/activity_pub/utils_test.exs b/test/web/activity_pub/utils_test.exs index 1feb076ba..586eb1d2f 100644 --- a/test/web/activity_pub/utils_test.exs +++ b/test/web/activity_pub/utils_test.exs @@ -636,47 +636,4 @@ test "removes actor from announcements" do assert updated_object.data["announcement_count"] == 1 end end - - describe "get_reports_grouped_by_status/1" do - setup do - [reporter, target_user] = insert_pair(:user) - first_status = insert(:note_activity, user: target_user) - second_status = insert(:note_activity, user: target_user) - - CommonAPI.report(reporter, %{ - "account_id" => target_user.id, - "comment" => "I feel offended", - "status_ids" => [first_status.id] - }) - - CommonAPI.report(reporter, %{ - "account_id" => target_user.id, - "comment" => "I feel offended2", - "status_ids" => [second_status.id] - }) - - data = [%{activity: first_status.data["id"]}, %{activity: second_status.data["id"]}] - - {:ok, - %{ - first_status: first_status, - second_status: second_status, - data: data - }} - end - - test "works for deprecated reports format", %{ - first_status: first_status, - second_status: second_status, - data: data - } do - groups = Utils.get_reports_grouped_by_status(data).groups - - first_group = Enum.find(groups, &(&1.status.id == first_status.data["id"])) - second_group = Enum.find(groups, &(&1.status.id == second_status.data["id"])) - - assert first_group.status.id == first_status.data["id"] - assert second_group.status.id == second_status.data["id"] - end - end end From 5869a43fe722c878fca1d87816cd1437f2bb49fe Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Tue, 26 Nov 2019 01:06:54 +0900 Subject: [PATCH 13/24] Fix tests --- test/support/helpers.ex | 17 +++++++++++++++++ .../admin_api/admin_api_controller_test.exs | 19 +++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/test/support/helpers.ex b/test/support/helpers.ex index ce39dd9d8..af2b2eddf 100644 --- a/test/support/helpers.ex +++ b/test/support/helpers.ex @@ -75,6 +75,23 @@ def render_json(view, template, assigns) do |> Poison.decode!() end + def stringify_keys(nil), do: nil + + def stringify_keys(key) when key in [true, false], do: key + def stringify_keys(key) when is_atom(key), do: Atom.to_string(key) + + def stringify_keys(map) when is_map(map) do + map + |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end) + |> Enum.into(%{}) + end + + def stringify_keys([head | rest] = list) when is_list(list) do + [stringify_keys(head) | stringify_keys(rest)] + end + + def stringify_keys(key), do: key + defmacro guards_config(config_path) do quote do initial_setting = Pleroma.Config.get(config_path) diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 12dba7773..c6ff1a065 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1654,9 +1654,10 @@ test "returns reports grouped by status", %{ NaiveDateTime.from_iso8601!(act.data["published"]) end).data["published"] - assert first_group["status"] == StatusView.render("show.json", %{activity: first_status}) + assert first_group["status"] == + stringify_keys(StatusView.render("show.json", %{activity: first_status})) - assert first_group["account"]["id"] == target_user.id + assert(first_group["account"]["id"] == target_user.id) assert length(first_group["actors"]) == 1 assert hd(first_group["actors"])["id"] == reporter.id @@ -1669,11 +1670,8 @@ test "returns reports grouped by status", %{ NaiveDateTime.from_iso8601!(act.data["published"]) end).data["published"] - assert second_group["status"] == %{ - "id" => second_status.data["id"], - "content" => second_status.object.data["content"], - "published" => second_status.object.data["published"] - } + assert second_group["status"] == + stringify_keys(StatusView.render("show.json", %{activity: second_status})) assert second_group["account"]["id"] == target_user.id @@ -1688,11 +1686,8 @@ test "returns reports grouped by status", %{ NaiveDateTime.from_iso8601!(act.data["published"]) end).data["published"] - assert third_group["status"] == %{ - "id" => third_status.data["id"], - "content" => third_status.object.data["content"], - "published" => third_status.object.data["published"] - } + assert third_group["status"] == + stringify_keys(StatusView.render("show.json", %{activity: third_status})) assert third_group["account"]["id"] == target_user.id From 5135656f579954cf786011b539934c7150e0d0bc Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Wed, 27 Nov 2019 22:54:12 +0900 Subject: [PATCH 14/24] Handle reopened reports with deleted statuses --- lib/pleroma/web/activity_pub/utils.ex | 50 ++++++++------- .../web/admin_api/views/report_view.ex | 11 ++-- .../admin_api/admin_api_controller_test.exs | 64 ++++++++++++++++++- 3 files changed, 93 insertions(+), 32 deletions(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 962f02a05..d91abf7b3 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -722,16 +722,22 @@ defp build_flag_object(act) when is_map(act) or is_binary(act) do act when is_binary(act) -> act end - activity = Activity.get_by_ap_id_with_object(id) - actor = User.get_by_ap_id(activity.object.data["actor"]) + case Activity.get_by_ap_id_with_object(id) do + %Activity{} = activity -> + %{ + "type" => "Note", + "id" => activity.data["id"], + "content" => activity.object.data["content"], + "published" => activity.object.data["published"], + "actor" => + AccountView.render("show.json", %{ + user: User.get_by_ap_id(activity.object.data["actor"]) + }) + } - %{ - "type" => "Note", - "id" => activity.data["id"], - "content" => activity.object.data["content"], - "published" => activity.object.data["published"], - "actor" => AccountView.render("show.json", %{user: actor}) - } + _ -> + %{"id" => id, "deleted" => true} + end end defp build_flag_object(_), do: [] @@ -792,30 +798,27 @@ def parse_report_group(activity) do reports = get_reports_by_status_id(activity["id"]) max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"])) actors = Enum.map(reports, & &1.user_actor) - {deleted, status} = get_status_data(activity) + status = get_status_data(activity) %{ date: max_date.data["published"], account: activity["actor"], status: status, - status_deleted: deleted, actors: Enum.uniq(actors), reports: reports } end - defp get_status_data(activity) do - case Activity.get_by_ap_id(activity["id"]) do - %Activity{} = act -> - {false, act} + defp get_status_data(status) do + case status["deleted"] do + true -> + %{ + "id" => status["id"], + "deleted" => true + } _ -> - {true, - %{ - id: activity["id"], - content: activity["content"], - published: activity["published"] - }} + Activity.get_by_ap_id(status["id"]) end end @@ -829,7 +832,7 @@ def get_reports_by_status_id(ap_id) do |> Repo.all() end - @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{ + @spec get_reports_grouped_by_status([String.t()]) :: %{ required(:groups) => [ %{ required(:date) => String.t(), @@ -838,8 +841,7 @@ def get_reports_by_status_id(ap_id) do required(:actors) => [%User{}], required(:reports) => [%Activity{}] } - ], - required(:total) => integer + ] } def get_reports_grouped_by_status(activity_ids) do parsed_groups = diff --git a/lib/pleroma/web/admin_api/views/report_view.ex b/lib/pleroma/web/admin_api/views/report_view.ex index 45ce75272..13602efd9 100644 --- a/lib/pleroma/web/admin_api/views/report_view.ex +++ b/lib/pleroma/web/admin_api/views/report_view.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.AdminAPI.ReportView do use Pleroma.Web, :view + alias Pleroma.Activity alias Pleroma.HTML alias Pleroma.User alias Pleroma.Web.AdminAPI.Report @@ -46,15 +47,15 @@ def render("index_grouped.json", %{groups: groups}) do reports = Enum.map(groups, fn group -> status = - if group[:status_deleted], - do: group[:status], - else: StatusView.render("show.json", %{activity: group[:status]}) + case group.status do + %Activity{} = activity -> StatusView.render("show.json", %{activity: activity}) + _ -> group.status + end %{ date: group[:date], account: group[:account], - status: status, - status_deleted: group[:status_deleted], + status: Map.put_new(status, "deleted", false), actors: Enum.map(group[:actors], &merge_account_views/1), reports: group[:reports] diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index c6ff1a065..a69fadcdc 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1613,6 +1613,7 @@ test "returns 403 when requested by anonymous" do first_status: Activity.get_by_ap_id_with_object(first_status.data["id"]), second_status: Activity.get_by_ap_id_with_object(second_status.data["id"]), third_status: Activity.get_by_ap_id_with_object(third_status.data["id"]), + first_report: first_report, first_status_reports: [first_report, second_report, third_report], second_status_reports: [first_report, second_report], third_status_reports: [first_report], @@ -1655,7 +1656,11 @@ test "returns reports grouped by status", %{ end).data["published"] assert first_group["status"] == - stringify_keys(StatusView.render("show.json", %{activity: first_status})) + Map.put( + stringify_keys(StatusView.render("show.json", %{activity: first_status})), + "deleted", + false + ) assert(first_group["account"]["id"] == target_user.id) @@ -1671,7 +1676,11 @@ test "returns reports grouped by status", %{ end).data["published"] assert second_group["status"] == - stringify_keys(StatusView.render("show.json", %{activity: second_status})) + Map.put( + stringify_keys(StatusView.render("show.json", %{activity: second_status})), + "deleted", + false + ) assert second_group["account"]["id"] == target_user.id @@ -1687,7 +1696,11 @@ test "returns reports grouped by status", %{ end).data["published"] assert third_group["status"] == - stringify_keys(StatusView.render("show.json", %{activity: third_status})) + Map.put( + stringify_keys(StatusView.render("show.json", %{activity: third_status})), + "deleted", + false + ) assert third_group["account"]["id"] == target_user.id @@ -1697,6 +1710,51 @@ test "returns reports grouped by status", %{ assert Enum.map(third_group["reports"], & &1["id"]) -- Enum.map(third_status_reports, & &1.id) == [] end + + test "reopened report renders status data", %{ + conn: conn, + first_report: first_report, + first_status: first_status + } do + {:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved") + + response = + conn + |> get("/api/pleroma/admin/grouped_reports") + |> json_response(:ok) + + first_group = Enum.find(response["reports"], &(&1["status"]["id"] == first_status.id)) + + assert first_group["status"] == + Map.put( + stringify_keys(StatusView.render("show.json", %{activity: first_status})), + "deleted", + false + ) + end + + test "reopened report does not render status data if status has been deleted", %{ + conn: conn, + first_report: first_report, + first_status: first_status, + target_user: target_user + } do + {:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved") + {:ok, _} = CommonAPI.delete(first_status.id, target_user) + + refute Activity.get_by_ap_id(first_status.id) + + response = + conn + |> get("/api/pleroma/admin/grouped_reports") + |> json_response(:ok) + + assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["status"][ + "deleted" + ] == true + + assert length(Enum.filter(response["reports"], &(&1["status"]["deleted"] == false))) == 2 + end end describe "POST /api/pleroma/admin/reports/:id/respond" do From fcabcab4430b0aa075243bf98630d67c79f3ef9b Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Thu, 28 Nov 2019 00:09:00 +0900 Subject: [PATCH 15/24] Fetch account from report, not from status (it might be deleted) --- lib/pleroma/web/activity_pub/utils.ex | 9 ++++++++- .../web/admin_api/admin_api_controller.ex | 4 ++-- .../admin_api/admin_api_controller_test.exs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index d91abf7b3..2ca805c09 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -798,11 +798,18 @@ def parse_report_group(activity) do reports = get_reports_by_status_id(activity["id"]) max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"])) actors = Enum.map(reports, & &1.user_actor) + [%{data: %{"object" => [account_id | _]}} | _] = reports + + account = + AccountView.render("show.json", %{ + user: User.get_by_ap_id(account_id) + }) + status = get_status_data(activity) %{ date: max_date.data["published"], - account: activity["actor"], + account: account, status: status, actors: Enum.uniq(actors), reports: reports diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index 24fdc3c82..b003d1f35 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -647,11 +647,11 @@ def list_reports(conn, params) do end def list_grouped_reports(conn, _params) do - reports = Utils.get_reported_activities() + statuses = Utils.get_reported_activities() conn |> put_view(ReportView) - |> render("index_grouped.json", Utils.get_reports_grouped_by_status(reports)) + |> render("index_grouped.json", Utils.get_reports_grouped_by_status(statuses)) end def report_show(conn, %{"id" => id}) do diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index a69fadcdc..108baad91 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1755,6 +1755,25 @@ test "reopened report does not render status data if status has been deleted", % assert length(Enum.filter(response["reports"], &(&1["status"]["deleted"] == false))) == 2 end + + test "account not empty if status was deleted", %{ + conn: conn, + first_report: first_report, + first_status: first_status, + target_user: target_user + } do + {:ok, _} = CommonAPI.update_report_state(first_report.id, "resolved") + {:ok, _} = CommonAPI.delete(first_status.id, target_user) + + refute Activity.get_by_ap_id(first_status.id) + + response = + conn + |> get("/api/pleroma/admin/grouped_reports") + |> json_response(:ok) + + assert Enum.find(response["reports"], &(&1["status"]["deleted"] == true))["account"] + end end describe "POST /api/pleroma/admin/reports/:id/respond" do From 624e720aa45fd54575d17a2b5e77c67deef6eb86 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 3 Dec 2019 22:13:38 +0700 Subject: [PATCH 16/24] Add `with_move` query param to the notifications API --- lib/pleroma/notification.ex | 9 +++++++ lib/pleroma/web/mastodon_api/mastodon_api.ex | 3 ++- test/notification_test.exs | 20 ++++++++------ test/web/activity_pub/activity_pub_test.exs | 4 +-- .../notification_controller_test.exs | 26 +++++++++++++++++++ .../views/notification_view_test.exs | 8 +++--- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index f37e7ec67..6b6e1c2e1 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -57,6 +57,7 @@ def for_user_query(user, opts \\ []) do |> exclude_muted(user, opts) |> exclude_blocked(user) |> exclude_visibility(opts) + |> exclude_move(opts) end defp exclude_blocked(query, user) do @@ -81,6 +82,14 @@ defp exclude_muted(query, user, _opts) do |> where([n, a, o, tm], is_nil(tm.user_id)) end + defp exclude_move(query, %{with_move: true}) do + query + end + + defp exclude_move(query, _opts) do + where(query, [n, a], fragment("?->>'type' != 'Move'", a.data)) + end + @valid_visibilities ~w[direct unlisted public private] defp exclude_visibility(query, %{exclude_visibilities: visibility}) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index d875a5788..3e7dc4617 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -73,7 +73,8 @@ defp cast_params(params) do exclude_types: {:array, :string}, exclude_visibilities: {:array, :string}, reblogs: :boolean, - with_muted: :boolean + with_muted: :boolean, + with_move: :boolean } changeset = cast({%{}, param_types}, params, Map.keys(param_types)) diff --git a/test/notification_test.exs b/test/notification_test.exs index dcbffeafe..9fe34d18f 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -643,13 +643,7 @@ test "move activity generates a notification" do Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user) ObanHelpers.perform_all() - assert [ - %{ - activity: %{ - data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id} - } - } - ] = Notification.for_user(follower) + assert [] = Notification.for_user(follower) assert [ %{ @@ -657,7 +651,17 @@ test "move activity generates a notification" do data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id} } } - ] = Notification.for_user(other_follower) + ] = Notification.for_user(follower, %{with_move: true}) + + assert [] = Notification.for_user(other_follower) + + assert [ + %{ + activity: %{ + data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id} + } + } + ] = Notification.for_user(other_follower, %{with_move: true}) end end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 2677b9e36..93eabde85 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1619,10 +1619,10 @@ test "create" do activity = %Activity{activity | object: nil} assert [%Notification{activity: ^activity}] = - Notification.for_user_since(follower, ~N[2019-04-13 11:22:33]) + Notification.for_user(follower, %{with_move: true}) assert [%Notification{activity: ^activity}] = - Notification.for_user_since(follower_move_opted_out, ~N[2019-04-13 11:22:33]) + Notification.for_user(follower_move_opted_out, %{with_move: true}) end test "old user must be in the new user's `also_known_as` list" do diff --git a/test/web/mastodon_api/controllers/notification_controller_test.exs b/test/web/mastodon_api/controllers/notification_controller_test.exs index fa55a7cf9..349cca682 100644 --- a/test/web/mastodon_api/controllers/notification_controller_test.exs +++ b/test/web/mastodon_api/controllers/notification_controller_test.exs @@ -341,6 +341,32 @@ test "see notifications after muting user with notifications and with_muted para assert length(json_response(conn, 200)) == 1 end + test "see move notifications with `with_move` parameter", %{ + conn: conn + } do + old_user = insert(:user) + new_user = insert(:user, also_known_as: [old_user.ap_id]) + follower = insert(:user) + + User.follow(follower, old_user) + Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user) + Pleroma.Tests.ObanHelpers.perform_all() + + conn = + conn + |> assign(:user, follower) + |> get("/api/v1/notifications") + + assert json_response(conn, 200) == [] + + conn = + build_conn() + |> assign(:user, follower) + |> get("/api/v1/notifications", %{"with_move" => "true"}) + + assert length(json_response(conn, 200)) == 1 + end + defp get_notification_id_by_activity(%{id: id}) do Notification |> Repo.get_by(activity_id: id) diff --git a/test/web/mastodon_api/views/notification_view_test.exs b/test/web/mastodon_api/views/notification_view_test.exs index 80b6d414c..cd36cb538 100644 --- a/test/web/mastodon_api/views/notification_view_test.exs +++ b/test/web/mastodon_api/views/notification_view_test.exs @@ -109,22 +109,22 @@ test "Follow notification" do end test "Move notification" do - %{ap_id: old_ap_id} = old_user = insert(:user) - %{ap_id: _new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id]) + old_user = insert(:user) + new_user = insert(:user, also_known_as: [old_user.ap_id]) follower = insert(:user) User.follow(follower, old_user) Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user) Pleroma.Tests.ObanHelpers.perform_all() - [notification] = Notification.for_user(follower) + [notification] = Notification.for_user(follower, %{with_move: true}) expected = %{ id: to_string(notification.id), pleroma: %{is_seen: false}, type: "move", account: AccountView.render("show.json", %{user: old_user, for: follower}), - target: AccountView.render("show.json", %{user: new_user, for: follower}), + target: AccountView.render("show.json", %{user: refresh_record(new_user), for: follower}), created_at: Utils.to_masto_date(notification.inserted_at) } From 05fb8d0084553a3ed7d1f10212f49771ce05570f Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 3 Dec 2019 22:31:12 +0700 Subject: [PATCH 17/24] Fix Pleroma.FollowingRelationship.move_following/2 --- lib/pleroma/following_relationship.ex | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/following_relationship.ex b/lib/pleroma/following_relationship.ex index a03c9bd30..0b0219b82 100644 --- a/lib/pleroma/following_relationship.ex +++ b/lib/pleroma/following_relationship.ex @@ -121,8 +121,12 @@ def move_following(origin, target) do Pleroma.Web.CommonAPI.follow(following_relationship.follower, target) end) |> case do - [] -> :ok - _ -> move_following(origin, target) + [] -> + User.update_follower_count(origin) + :ok + + _ -> + move_following(origin, target) end end end From aa97fe8f175c3618a04bb45e6aa7c37e26259577 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 4 Dec 2019 12:48:34 +0100 Subject: [PATCH 18/24] ChatChannel: Ignore messages that are too long. --- config/config.exs | 1 + lib/pleroma/application.ex | 2 -- lib/pleroma/web/chat_channel.ex | 2 +- test/support/channel_case.ex | 1 + test/web/chat_channel_test.exs | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/web/chat_channel_test.exs diff --git a/config/config.exs b/config/config.exs index b60ffef7d..4624bded2 100644 --- a/config/config.exs +++ b/config/config.exs @@ -225,6 +225,7 @@ notify_email: "noreply@example.com", description: "A Pleroma instance, an alternative fediverse server", limit: 5_000, + chat_limit: 5_000, remote_limit: 100_000, upload_limit: 16_000_000, avatar_upload_limit: 2_000_000, diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 9dbd1e26b..57462740c 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -147,8 +147,6 @@ defp oauth_cleanup_child(true), defp oauth_cleanup_child(_), do: [] - defp chat_child(:test, _), do: [] - defp chat_child(_env, true) do [Pleroma.Web.ChatChannel.ChatChannelState] end diff --git a/lib/pleroma/web/chat_channel.ex b/lib/pleroma/web/chat_channel.ex index 08841a3e8..840414933 100644 --- a/lib/pleroma/web/chat_channel.ex +++ b/lib/pleroma/web/chat_channel.ex @@ -20,7 +20,7 @@ def handle_info(:after_join, socket) do def handle_in("new_msg", %{"text" => text}, %{assigns: %{user_name: user_name}} = socket) do text = String.trim(text) - if String.length(text) > 0 do + if String.length(text) in 1..Pleroma.Config.get([:instance, :chat_limit]) do author = User.get_cached_by_nickname(user_name) author = Pleroma.Web.MastodonAPI.AccountView.render("show.json", user: author) message = ChatChannelState.add_message(%{text: text, author: author}) diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex index 466d8986f..4a4585844 100644 --- a/test/support/channel_case.ex +++ b/test/support/channel_case.ex @@ -23,6 +23,7 @@ defmodule Pleroma.Web.ChannelCase do quote do # Import conveniences for testing with channels use Phoenix.ChannelTest + use Pleroma.Tests.Helpers # The default endpoint for testing @endpoint Pleroma.Web.Endpoint diff --git a/test/web/chat_channel_test.exs b/test/web/chat_channel_test.exs new file mode 100644 index 000000000..68c24a9f9 --- /dev/null +++ b/test/web/chat_channel_test.exs @@ -0,0 +1,37 @@ +defmodule Pleroma.Web.ChatChannelTest do + use Pleroma.Web.ChannelCase + alias Pleroma.Web.ChatChannel + alias Pleroma.Web.UserSocket + + import Pleroma.Factory + + setup do + user = insert(:user) + + {:ok, _, socket} = + socket(UserSocket, "", %{user_name: user.nickname}) + |> subscribe_and_join(ChatChannel, "chat:public") + + {:ok, socket: socket} + end + + test "it broadcasts a message", %{socket: socket} do + push(socket, "new_msg", %{"text" => "why is tenshi eating a corndog so cute?"}) + assert_broadcast("new_msg", %{text: "why is tenshi eating a corndog so cute?"}) + end + + describe "message lengths" do + clear_config([:instance, :chat_limit]) + + test "it ignores messages of length zero", %{socket: socket} do + push(socket, "new_msg", %{"text" => ""}) + refute_broadcast("new_msg", %{text: ""}) + end + + test "it ignores messages above a certain length", %{socket: socket} do + Pleroma.Config.put([:instance, :chat_limit], 2) + push(socket, "new_msg", %{"text" => "123"}) + refute_broadcast("new_msg", %{text: "123"}) + end + end +end From 21353a8ee900fc5b48d7de5168f0b81e7b15dc97 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 4 Dec 2019 12:49:46 +0100 Subject: [PATCH 19/24] Changelog: Add information about chat limit. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a06ea211e..b162026e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added +- `:chat_limit` option to limit chat characters. - Refreshing poll results for remote polls - Authentication: Added rate limit for password-authorized actions / login existence checks - Static Frontend: Add the ability to render user profiles and notices server-side without requiring JS app. From 9487995e8b2172bd720d17d639d5d782d38a45a2 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 4 Dec 2019 12:51:06 +0100 Subject: [PATCH 20/24] Cheat Sheet: Addg chat_limit information. --- docs/configuration/cheatsheet.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index dc2f55229..ef2711e3c 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -12,6 +12,7 @@ You shouldn't edit the base config directly to avoid breakages and merge conflic * `notify_email`: Email used for notifications. * `description`: The instance’s description, can be seen in nodeinfo and ``/api/v1/instance``. * `limit`: Posts character limit (CW/Subject included in the counter). +* `chat_limit`: Character limit of the instance chat messages. * `remote_limit`: Hard character limit beyond which remote posts will be dropped. * `upload_limit`: File size limit of uploads (except for avatar, background, banner). * `avatar_upload_limit`: File size limit of user’s profile avatars. From d3bce89edddf72e2eb78e7c84a93c5bd4b6c8ff8 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 4 Dec 2019 19:41:14 +0300 Subject: [PATCH 21/24] [#1335] Merge issue fix. --- lib/pleroma/user.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index a68524377..b7f50e5ac 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -169,8 +169,6 @@ defmodule Pleroma.User do has_many(incoming_relation_source, through: [incoming_relation, :source]) end - field(:info, :map, default: %{}) - # `:blocks` is deprecated (replaced with `blocked_users` relation) field(:blocks, {:array, :string}, default: []) # `:mutes` is deprecated (replaced with `muted_users` relation) From 9f99640cfc81f28c0b6cdb5f6ef065b3e46f5f23 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Thu, 5 Dec 2019 02:50:38 +0300 Subject: [PATCH 22/24] ActivityPub: Fix deletes being exempt from MRF Closes #1461 --- CHANGELOG.md | 1 + lib/pleroma/activity.ex | 5 +++-- lib/pleroma/activity/queries.ex | 8 ++++++++ lib/pleroma/object.ex | 2 +- lib/pleroma/web/activity_pub/activity_pub.ex | 5 +++-- .../activity_pub/activity_pub_controller_test.exs | 2 +- test/web/activity_pub/activity_pub_test.exs | 15 +++++++++++++++ 7 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a06ea211e..3ec34f452 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed - Report emails now include functional links to profiles of remote user accounts - Not being able to log in to some third-party apps when logged in to MastoFE +- MRF: `Delete` activities being exempt from MRF policies
API Changes diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index f180c1e33..480b261cf 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -241,9 +241,10 @@ def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"]) def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id) def normalize(_), do: nil - def delete_by_ap_id(id) when is_binary(id) do + def delete_all_by_object_ap_id(id) when is_binary(id) do id |> Queries.by_object_id() + |> Queries.exclude_type("Delete") |> select([u], u) |> Repo.delete_all() |> elem(1) @@ -255,7 +256,7 @@ def delete_by_ap_id(id) when is_binary(id) do |> purge_web_resp_cache() end - def delete_by_ap_id(_), do: nil + def delete_all_by_object_ap_id(_), do: nil defp purge_web_resp_cache(%Activity{} = activity) do %{path: path} = URI.parse(activity.data["id"]) diff --git a/lib/pleroma/activity/queries.ex b/lib/pleroma/activity/queries.ex index 949f010a8..26bc1099d 100644 --- a/lib/pleroma/activity/queries.ex +++ b/lib/pleroma/activity/queries.ex @@ -64,4 +64,12 @@ def by_type(query \\ Activity, activity_type) do where: fragment("(?)->>'type' = ?", activity.data, ^activity_type) ) end + + @spec exclude_type(query, String.t()) :: query + def exclude_type(query \\ Activity, activity_type) do + from( + activity in query, + where: fragment("(?)->>'type' != ?", activity.data, ^activity_type) + ) + end end diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index b4ed3a9b2..ff0e59241 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -147,7 +147,7 @@ def swap_object_with_tombstone(object) do def delete(%Object{data: %{"id" => id}} = object) do with {:ok, _obj} = swap_object_with_tombstone(object), - deleted_activity = Activity.delete_by_ap_id(id), + deleted_activity = Activity.delete_all_by_object_ap_id(id), {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"), {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do {:ok, object, deleted_activity} diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index d6a425d8b..c8160b0cf 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -456,17 +456,18 @@ def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, options \\ [ user = User.get_cached_by_ap_id(actor) to = (object.data["to"] || []) ++ (object.data["cc"] || []) - with {:ok, object, activity} <- Object.delete(object), + with create_activity <- Activity.get_create_by_object_ap_id(id), data <- %{ "type" => "Delete", "actor" => actor, "object" => id, "to" => to, - "deleted_activity_id" => activity && activity.id + "deleted_activity_id" => create_activity && create_activity.id } |> maybe_put("id", activity_id), {:ok, activity} <- insert(data, local, false), + {:ok, object, _create_activity} <- Object.delete(object), stream_out_participations(object, user), _ <- decrease_replies_count_if_reply(object), {:ok, _actor} <- decrease_note_count_if_public(user, object), diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index 1aa73d75c..ba2ce1dd9 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -298,7 +298,7 @@ test "cached purged after activity deletion", %{conn: conn} do assert json_response(conn1, :ok) assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"})) - Activity.delete_by_ap_id(activity.object.data["id"]) + Activity.delete_all_by_object_ap_id(activity.object.data["id"]) conn2 = conn diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 2677b9e36..23dffb150 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1256,6 +1256,21 @@ test "decreases reply count" do assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id) assert object.data["repliesCount"] == 0 end + + test "it passes delete activity through MRF before deleting the object" do + rewrite_policy = Pleroma.Config.get([:instance, :rewrite_policy]) + Pleroma.Config.put([:instance, :rewrite_policy], Pleroma.Web.ActivityPub.MRF.DropPolicy) + + on_exit(fn -> Pleroma.Config.put([:instance, :rewrite_policy], rewrite_policy) end) + + note = insert(:note_activity) + object = Object.normalize(note) + + {:error, {:reject, _}} = ActivityPub.delete(object) + + assert Activity.get_by_id(note.id) + assert Repo.get(Object, object.id).data["type"] == object.data["type"] + end end describe "timeline post-processing" do From e8cee4d9a0ea13db8d087e42eb17939bb8b11f0b Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 6 Dec 2019 14:25:13 +0100 Subject: [PATCH 23/24] ActivityPub: For user timelines, respects blocks. Unless the timeline belongs to a blocked user. --- lib/pleroma/web/activity_pub/activity_pub.ex | 9 ++++ .../controllers/account_controller.ex | 6 ++- .../controllers/account_controller_test.exs | 44 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index b07a94701..c5bc1ef0d 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -748,6 +748,15 @@ def fetch_user_activities(user, reading_user, params \\ %{}) do |> Map.put("whole_db", true) |> Map.put("pinned_activity_ids", user.pinned_activities) + params = + if User.blocks?(reading_user, user) do + params + else + params + |> Map.put("blocking_user", reading_user) + |> Map.put("muting_user", reading_user) + end + recipients = user_activities_recipients(%{ "godmode" => params["godmode"], diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index d71a14434..d19029cb5 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -249,7 +249,11 @@ def show(%{assigns: %{user: for_user}} = conn, %{"id" => nickname_or_id}) do @doc "GET /api/v1/accounts/:id/statuses" def statuses(%{assigns: %{user: reading_user}} = conn, params) do with %User{} = user <- User.get_cached_by_nickname_or_id(params["id"], for: reading_user) do - params = Map.put(params, "tag", params["tagged"]) + params = + params + |> Map.put("tag", params["tagged"]) + |> Map.delete("godmode") + activities = ActivityPub.fetch_user_activities(user, reading_user, params) conn diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index 444693404..fa08ae4df 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -144,6 +144,50 @@ test "returns 404 for internal.fetch actor", %{conn: conn} do end describe "user timelines" do + test "respects blocks", %{conn: conn} do + user_one = insert(:user) + user_two = insert(:user) + user_three = insert(:user) + + User.block(user_one, user_two) + + {:ok, activity} = CommonAPI.post(user_two, %{"status" => "User one sux0rz"}) + {:ok, repeat, _} = CommonAPI.repeat(activity.id, user_three) + + resp = + conn + |> get("/api/v1/accounts/#{user_two.id}/statuses") + + assert [%{"id" => id}] = json_response(resp, 200) + assert id == activity.id + + # Even a blocked user will deliver the full user timeline, there would be + # no point in looking at a blocked users timeline otherwise + resp = + conn + |> assign(:user, user_one) + |> get("/api/v1/accounts/#{user_two.id}/statuses") + + assert [%{"id" => id}] = json_response(resp, 200) + assert id == activity.id + + resp = + conn + |> get("/api/v1/accounts/#{user_three.id}/statuses") + + assert [%{"id" => id}] = json_response(resp, 200) + assert id == repeat.id + + # When viewing a third user's timeline, the blocked users will NOT be + # shown. + resp = + conn + |> assign(:user, user_one) + |> get("/api/v1/accounts/#{user_three.id}/statuses") + + assert [] = json_response(resp, 200) + end + test "gets a users statuses", %{conn: conn} do user_one = insert(:user) user_two = insert(:user) From a21340caa192b41051abe3b5905fbcb2fde281d7 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Sun, 8 Dec 2019 16:36:22 +0300 Subject: [PATCH 24/24] Fix never matching clause `length/1` is only used with lists. --- lib/pleroma/clippy.ex | 1 + lib/pleroma/html.ex | 1 + lib/pleroma/web/activity_pub/transmogrifier.ex | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/clippy.ex b/lib/pleroma/clippy.ex index bd20952a6..6e6121d4e 100644 --- a/lib/pleroma/clippy.ex +++ b/lib/pleroma/clippy.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Clippy do @moduledoc false + # No software is complete until they have a Clippy implementation. # A ballmer peak _may_ be required to change this module. diff --git a/lib/pleroma/html.ex b/lib/pleroma/html.ex index 997e965f0..71c53ce0e 100644 --- a/lib/pleroma/html.ex +++ b/lib/pleroma/html.ex @@ -163,6 +163,7 @@ defmodule Pleroma.HTML.Scrubber.Default do require FastSanitize.Sanitizer.Meta alias FastSanitize.Sanitizer.Meta + # credo:disable-for-previous-line # No idea how to fix this one… diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index ce95fb6ba..ecba27bef 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -387,7 +387,7 @@ def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = def handle_incoming(%{"id" => nil}, _options), do: :error def handle_incoming(%{"id" => ""}, _options), do: :error # length of https:// = 8, should validate better, but good enough for now. - def handle_incoming(%{"id" => id}, _options) when not (is_binary(id) and length(id) > 8), + def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8, do: :error # TODO: validate those with a Ecto scheme