Add `with_move` query param to the notifications API
authorEgor Kislitsyn <egor@kislitsyn.com>
Tue, 3 Dec 2019 15:13:38 +0000 (22:13 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Tue, 3 Dec 2019 15:13:38 +0000 (22:13 +0700)
lib/pleroma/notification.ex
lib/pleroma/web/mastodon_api/mastodon_api.ex
test/notification_test.exs
test/web/activity_pub/activity_pub_test.exs
test/web/mastodon_api/controllers/notification_controller_test.exs
test/web/mastodon_api/views/notification_view_test.exs

index f37e7ec671ee88c39848cc92a7f715621766999e..6b6e1c2e1bb38d6206d3a4e85ee5d662f95c659d 100644 (file)
@@ -57,6 +57,7 @@ defmodule Pleroma.Notification 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 @@ defmodule Pleroma.Notification 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})
index d875a578840b4bfa6ec3af391c537aea05f7378c..3e7dc4617288b2890d5860e9c75a2f5ea56f9f50 100644 (file)
@@ -73,7 +73,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI 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))
index dcbffeafe39b16c1afdf7505fc4744cf2119ce25..9fe34d18fb6067a626f9ef104ed7b7097450e6b5 100644 (file)
@@ -643,13 +643,17 @@ defmodule Pleroma.NotificationTest do
       Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
       ObanHelpers.perform_all()
 
+      assert [] = Notification.for_user(follower)
+
       assert [
                %{
                  activity: %{
                    data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
                  }
                }
-             ] = Notification.for_user(follower)
+             ] = Notification.for_user(follower, %{with_move: true})
+
+      assert [] = Notification.for_user(other_follower)
 
       assert [
                %{
@@ -657,7 +661,7 @@ defmodule Pleroma.NotificationTest do
                    data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
                  }
                }
-             ] = Notification.for_user(other_follower)
+             ] = Notification.for_user(other_follower, %{with_move: true})
     end
   end
 
index 2677b9e36bbd48b1a700a2dc9f5811f997dc237a..93eabde858e90c411c62d2811b2417787b7e75fd 100644 (file)
@@ -1619,10 +1619,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest 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
index fa55a7cf927e33313dd00b074bcf607f1fb8f04b..349cca682a4dae74a793fc468ba6cec584ca0706 100644 (file)
@@ -341,6 +341,32 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
     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)
index 80b6d414cf178a5494b0fa209a6964ac0e502e3a..cd36cb5384cca0bfd0c7f3bb61ad105794a15a56 100644 (file)
@@ -109,22 +109,22 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest 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)
     }