Notifications: Add emoji reaction notifications
authorlain <lain@soykaf.club>
Wed, 22 Jan 2020 19:06:12 +0000 (20:06 +0100)
committerlain <lain@soykaf.club>
Wed, 22 Jan 2020 19:06:12 +0000 (20:06 +0100)
lib/pleroma/activity.ex
lib/pleroma/notification.ex
lib/pleroma/web/mastodon_api/views/notification_view.ex
test/notification_test.exs
test/web/mastodon_api/views/notification_view_test.exs

index 896cbb3c5558d8bdd2b4431bc7cd4636f58ef9a3..0f8fce774e81b5904a24e19c0404cf85596e777d 100644 (file)
@@ -30,7 +30,8 @@ defmodule Pleroma.Activity do
     "Follow" => "follow",
     "Announce" => "reblog",
     "Like" => "favourite",
-    "Move" => "move"
+    "Move" => "move",
+    "EmojiReaction" => "pleroma:emoji_reaction"
   }
 
   @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
index 8f3e46af98d187e52949b4f9bdf4eb3dcda49fc1..d04a65a1e6c011828c41686d6ff87a027bbbd569 100644 (file)
@@ -294,7 +294,7 @@ defmodule Pleroma.Notification do
   end
 
   def create_notifications(%Activity{data: %{"type" => type}} = activity)
-      when type in ["Like", "Announce", "Follow", "Move"] do
+      when type in ["Like", "Announce", "Follow", "Move", "EmojiReaction"] do
     notifications =
       activity
       |> get_notified_from_activity()
@@ -322,7 +322,7 @@ defmodule Pleroma.Notification do
   def get_notified_from_activity(activity, local_only \\ true)
 
   def get_notified_from_activity(%Activity{data: %{"type" => type}} = activity, local_only)
-      when type in ["Create", "Like", "Announce", "Follow", "Move"] do
+      when type in ["Create", "Like", "Announce", "Follow", "Move", "EmojiReaction"] do
     []
     |> Utils.maybe_notify_to_recipients(activity)
     |> Utils.maybe_notify_mentioned_recipients(activity)
index ddd7f531826d4bb18bc944770abd31b83be66b7a..360ec10f0cdc864b25f91dd628231b433ed49fc6 100644 (file)
@@ -37,18 +37,37 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
       }
 
       case mastodon_type do
-        "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
+        "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
+
+        "pleroma:emoji_reaction" ->
+          put_status(response, parent_activity, user) |> put_emoji(activity)
+
+        _ ->
+          nil
       end
     else
       _ -> nil
     end
   end
 
+  defp put_emoji(response, activity) do
+    response
+    |> Map.put(:emoji, activity.data["content"])
+  end
+
   defp put_status(response, activity, user) do
     Map.put(response, :status, StatusView.render("show.json", %{activity: activity, for: user}))
   end
index 9a1c2f2b5ea969a416c5d352e0639ea834406c0e..04bf5b41aaf62883934167a3cca48978c7b658c8 100644 (file)
@@ -15,6 +15,18 @@ defmodule Pleroma.NotificationTest do
   alias Pleroma.Web.Streamer
 
   describe "create_notifications" do
+    test "creates a notification for an emoji reaction" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "yeah"})
+      {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
+
+      {:ok, [notification]} = Notification.create_notifications(activity)
+
+      assert notification.user_id == user.id
+    end
+
     test "notifies someone when they are directly addressed" do
       user = insert(:user)
       other_user = insert(:user)
index ba1721e06dd5b9ebf5ec884e5df9d0e9ffffc53e..1fe83cb2cab6d54415d50ea6892242ced2ebda78 100644 (file)
@@ -134,4 +134,31 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
     assert [expected] ==
              NotificationView.render("index.json", %{notifications: [notification], for: follower})
   end
+
+  test "EmojiReaction notification" do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
+    {:ok, _activity, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
+
+    activity = Repo.get(Activity, activity.id)
+
+    [notification] = Notification.for_user(user)
+
+    assert notification
+
+    expected = %{
+      id: to_string(notification.id),
+      pleroma: %{is_seen: false},
+      type: "pleroma:emoji_reaction",
+      emoji: "☕",
+      account: AccountView.render("show.json", %{user: other_user, for: user}),
+      status: StatusView.render("show.json", %{activity: activity, for: user}),
+      created_at: Utils.to_masto_date(notification.inserted_at)
+    }
+
+    assert expected ==
+             NotificationView.render("show.json", %{notification: notification, for: user})
+  end
 end