Refactor to store user ap_id, add tests
authorKaren Konou <konoukaren@gmail.com>
Fri, 15 Mar 2019 13:06:58 +0000 (14:06 +0100)
committerKaren Konou <konoukaren@gmail.com>
Fri, 15 Mar 2019 13:20:08 +0000 (14:20 +0100)
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/streamer.ex
test/web/activity_pub/activity_pub_test.exs
test/web/common_api/common_api_test.exs
test/web/streamer_test.exs

index 467cb910b65692421bbef1bfe446fd8b3c13d9df..692ae836c36ff0d265c6fa0e39a053e48eb56d0b 100644 (file)
@@ -1387,6 +1387,6 @@ defmodule Pleroma.User 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
index 35586e212de7df93d3f47c206b19ec1c71190a44..70b72710bfb844d0aaf9c63f60d446f91219a118 100644 (file)
@@ -261,14 +261,14 @@ defmodule Pleroma.User.Info 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
index 035c59387a2d97a890f9edb321c8c312f901415a..84d66efc9edec8564b9af8499cddd5d05706c368 100644 (file)
@@ -300,17 +300,21 @@ defmodule Pleroma.Web.CommonAPI 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
index 68b7d8b49dd62ffc67857ad25218c98b597d1c95..952aa2453b4a87163d23b7f6f0fc80ab304ddc13 100644 (file)
@@ -730,13 +730,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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})
index fa0fb2ac8deacb37af48ae793f7e9504e5e291fe..a0863a062f227448ff6e99b3bf1f7082c5d1daab 100644 (file)
@@ -200,7 +200,7 @@ defmodule Pleroma.Web.Streamer do
         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"])
 
index 2b83bfb1d05c6cf5ab4c944b5eb78d1cef6bd8e2..03577821836bcd8e35e64ea288157612d84a7cb6 100644 (file)
@@ -424,6 +424,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest 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
index 181813c76492442fb99ec328ed939e81e642223d..f83f80b4037502b9d23cf186b754598c7b356c43 100644 (file)
@@ -221,4 +221,27 @@ defmodule Pleroma.Web.CommonAPITest 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
index 0a2e91298155fb49d72d78a146b8436e45bb9a6c..f00577a8a328149715dc51b4d9ebe20e3bcae089 100644 (file)
@@ -202,4 +202,34 @@ defmodule Pleroma.Web.StreamerTest 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