From da53c079db91ce5d7ba14809f5e99b10d3ae307a Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Fri, 15 Mar 2019 14:06:58 +0100 Subject: [PATCH] Refactor to store user ap_id, add tests --- lib/pleroma/user.ex | 2 +- lib/pleroma/user/info.ex | 8 ++--- lib/pleroma/web/common_api/common_api.ex | 16 ++++++---- .../mastodon_api/mastodon_api_controller.ex | 11 +++---- lib/pleroma/web/streamer.ex | 2 +- test/web/activity_pub/activity_pub_test.exs | 13 ++++++++ test/web/common_api/common_api_test.exs | 23 ++++++++++++++ test/web/streamer_test.exs | 30 +++++++++++++++++++ 8 files changed, 88 insertions(+), 17 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 467cb910b..692ae836c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1387,6 +1387,6 @@ defp paginate(query, page, page_size) do end def showing_reblogs?(%User{} = user, %User{} = target) do - target.id not in user.info.muted_reblogs + target.ap_id not in user.info.muted_reblogs end end diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 35586e212..70b72710b 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -261,14 +261,14 @@ def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do } end - def add_reblog_mute(info, id) do - params = %{muted_reblogs: info.muted_reblogs ++ [id]} + def add_reblog_mute(info, ap_id) do + params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]} cast(info, params, [:muted_reblogs]) end - def remove_reblog_mute(info, id) do - params = %{muted_reblogs: List.delete(info.muted_reblogs, id)} + def remove_reblog_mute(info, ap_id) do + params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)} cast(info, params, [:muted_reblogs]) end diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 035c59387..84d66efc9 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -300,17 +300,21 @@ def report(user, data) do end end - def hide_reblogs(user, id) do - if id not in user.info.muted_reblogs do - info_changeset = User.Info.add_reblog_mute(user.info, id) + def hide_reblogs(user, muted) do + ap_id = muted.ap_id + + if ap_id not in user.info.muted_reblogs do + info_changeset = User.Info.add_reblog_mute(user.info, ap_id) changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset) User.update_and_set_cache(changeset) end end - def show_reblogs(user, id) do - if id in user.info.muted_reblogs do - info_changeset = User.Info.remove_reblog_mute(user.info, id) + def show_reblogs(user, muted) do + ap_id = muted.ap_id + + if ap_id in user.info.muted_reblogs do + info_changeset = User.Info.remove_reblog_mute(user.info, ap_id) changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset) User.update_and_set_cache(changeset) end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 68b7d8b49..952aa2453 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -730,13 +730,14 @@ def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do |> render("relationship.json", %{user: follower, target: followed}) else true -> - case conn.params["reblogs"] do - true -> CommonAPI.show_reblogs(follower, id) - false -> CommonAPI.hide_reblogs(follower, id) - end - followed = User.get_cached_by_id(id) + {:ok, follower} = + case conn.params["reblogs"] do + true -> CommonAPI.show_reblogs(follower, followed) + false -> CommonAPI.hide_reblogs(follower, followed) + end + conn |> put_view(AccountView) |> render("relationship.json", %{user: follower, target: followed}) diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index fa0fb2ac8..a0863a062 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -200,7 +200,7 @@ def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = ite user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id) blocks = user.info.blocks || [] mutes = user.info.mutes || [] - reblog_mutes = user.info.reblog_mutes || [] + reblog_mutes = user.info.muted_reblogs || [] parent = Object.normalize(item.data["object"]) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 2b83bfb1d..035778218 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -424,6 +424,19 @@ test "retrieves ids up to max_id" do assert length(activities) == 20 assert last == last_expected end + + 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, activity, _} = CommonAPI.repeat(activity.id, booster) + + activities = ActivityPub.fetch_activities([], %{"muting_user" => user}) + + refute Enum.member?(activities, activity) + end end describe "like an object" do diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 181813c76..f83f80b40 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -221,4 +221,27 @@ test "creates a report" do } = flag_activity end end + + describe "reblog muting" do + setup do + muter = insert(:user) + + muted = insert(:user) + + [muter: muter, muted: muted] + end + + test "add a reblog mute", %{muter: muter, muted: muted} do + {:ok, muter} = CommonAPI.hide_reblogs(muter, muted) + + assert Pleroma.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) + + assert Pleroma.User.showing_reblogs?(muter, muted) == true + end + end end diff --git a/test/web/streamer_test.exs b/test/web/streamer_test.exs index 0a2e91298..f00577a8a 100644 --- a/test/web/streamer_test.exs +++ b/test/web/streamer_test.exs @@ -202,4 +202,34 @@ test "it send wanted private posts to list" do Task.await(task) end + + test "it doesn't send muted reblogs" do + user1 = insert(:user) + user2 = insert(:user) + user3 = insert(:user) + CommonAPI.hide_reblogs(user1, user2.ap_id) + + task = + Task.async(fn -> + refute_receive {:text, _}, 1_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user1 + } + } + + {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"}) + {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", announce_activity) + + Task.await(task) + end end