Add a view for the move notification
authorEgor Kislitsyn <egor@kislitsyn.com>
Tue, 26 Nov 2019 11:48:56 +0000 (18:48 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 27 Nov 2019 10:52:02 +0000 (17:52 +0700)
docs/API/differences_in_mastoapi_responses.md
lib/pleroma/activity.ex
lib/pleroma/web/mastodon_api/views/notification_view.ex
lib/pleroma/web/push/impl.ex
test/notification_test.exs
test/web/mastodon_api/views/notification_view_test.exs

index e649bdf2465d6614f1f90672d3948027a1c2a4bc..006d17c1bc28abd63ae6331f0cf9e9ab99dbac9a 100644 (file)
@@ -92,6 +92,12 @@ Has these additional fields under the `pleroma` object:
 
 - `is_seen`: true if the notification was read by the user
 
+### Move Notification
+
+The `type` value is `move`. Has an additional field:
+
+- `target`: new account
+
 ## GET `/api/v1/notifications`
 
 Accepts additional parameters:
index c1065611bf31d9eccc5e8eba005ac8cc210c55e4..992298c2de9af354f82fe660349c8664b59cfeea 100644 (file)
@@ -28,7 +28,8 @@ defmodule Pleroma.Activity do
     "Create" => "mention",
     "Follow" => "follow",
     "Announce" => "reblog",
-    "Like" => "favourite"
+    "Like" => "favourite",
+    "Move" => "move"
   }
 
   @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
index 5e3dbe728335d09dbd70b641bcb22c0e63349dc6..ddd7f531826d4bb18bc944770abd31b83be66b7a 100644 (file)
@@ -37,32 +37,24 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
       }
 
       case mastodon_type do
-        "mention" ->
-          response
-          |> Map.merge(%{
-            status: StatusView.render("show.json", %{activity: activity, for: user})
-          })
-
-        "favourite" ->
-          response
-          |> Map.merge(%{
-            status: StatusView.render("show.json", %{activity: parent_activity, for: user})
-          })
-
-        "reblog" ->
-          response
-          |> Map.merge(%{
-            status: StatusView.render("show.json", %{activity: parent_activity, for: user})
-          })
-
-        "follow" ->
-          response
-
-        _ ->
-          nil
+        "mention" -> put_status(response, activity, user)
+        "favourite" -> put_status(response, parent_activity, user)
+        "reblog" -> put_status(response, parent_activity, user)
+        "move" -> put_target(response, activity, user)
+        "follow" -> response
+        _ -> nil
       end
     else
       _ -> nil
     end
   end
+
+  defp put_status(response, activity, user) do
+    Map.put(response, :status, StatusView.render("show.json", %{activity: activity, for: user}))
+  end
+
+  defp put_target(response, activity, user) do
+    target = User.get_cached_by_ap_id(activity.data["target"])
+    Map.put(response, :target, AccountView.render("show.json", %{user: target, for: user}))
+  end
 end
index dd445e8bfe2d0877ebbe850b304b5ef6952f2979..8c131409d1343a3c486bb3bd1b84b7bac986ceeb 100644 (file)
@@ -16,7 +16,7 @@ defmodule Pleroma.Web.Push.Impl do
   require Logger
   import Ecto.Query
 
-  @types ["Create", "Follow", "Announce", "Like"]
+  @types ["Create", "Follow", "Announce", "Like", "Move"]
 
   @doc "Performs sending notifications for user subscriptions"
   @spec perform(Notification.t()) :: list(any) | :error
index f8d42922322353a2d509dc8fc2c4c33313db186a..dcbffeafe39b16c1afdf7505fc4744cf2119ce25 100644 (file)
@@ -630,6 +630,35 @@ defmodule Pleroma.NotificationTest do
 
       assert Enum.empty?(Notification.for_user(local_user))
     end
+
+    test "move activity generates a 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])
+      follower = insert(:user)
+      other_follower = insert(:user, %{allow_following_move: false})
+
+      User.follow(follower, old_user)
+      User.follow(other_follower, old_user)
+
+      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 [
+               %{
+                 activity: %{
+                   data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
+                 }
+               }
+             ] = Notification.for_user(other_follower)
+    end
   end
 
   describe "for_user" do
index c9043a69ada032c61f404dcf7c96800c9c8ecdd5..80b6d414cf178a5494b0fa209a6964ac0e502e3a 100644 (file)
@@ -107,4 +107,28 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
     assert [] ==
              NotificationView.render("index.json", %{notifications: [notification], for: followed})
   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])
+    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)
+
+    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}),
+      created_at: Utils.to_masto_date(notification.inserted_at)
+    }
+
+    assert [expected] ==
+             NotificationView.render("index.json", %{notifications: [notification], for: follower})
+  end
 end