NotificationController: Don't return chat_mentions by default.
authorlain <lain@soykaf.club>
Tue, 2 Jun 2020 12:05:53 +0000 (14:05 +0200)
committerlain <lain@soykaf.club>
Tue, 2 Jun 2020 12:05:53 +0000 (14:05 +0200)
docs/API/chats.md
lib/pleroma/web/mastodon_api/controllers/notification_controller.ex
lib/pleroma/web/mastodon_api/mastodon_api.ex
test/web/mastodon_api/controllers/notification_controller_test.exs

index 2eca5adf6c11762b0ba3b0ce872ae6ce700afd91..d1d39f495481ca1d5ff9322439d6a9bf08acab6e 100644 (file)
@@ -204,7 +204,7 @@ Returned data is the deleted message.
 
 ### Notifications
 
-There's a new `pleroma:chat_mention` notification, which has this form:
+There's a new `pleroma:chat_mention` notification, which has this form. It is not given out in the notifications endpoint by default, you need to explicitly request it with `include_types[]=pleroma:chat_mention`:
 
 ```json
 {
index bcd12c73f74678e36897152c6d982691520a30af..e25cef30bbf80f3f9a405ee2f2211b5deb18c51f 100644 (file)
@@ -42,8 +42,20 @@ defmodule Pleroma.Web.MastodonAPI.NotificationController do
     end
   end
 
+  @default_notification_types ~w{
+    mention
+    follow
+    follow_request
+    reblog
+    favourite
+    move
+    pleroma:emoji_reaction
+  }
   def index(%{assigns: %{user: user}} = conn, params) do
-    params = Map.new(params, fn {k, v} -> {to_string(k), v} end)
+    params =
+      Map.new(params, fn {k, v} -> {to_string(k), v} end)
+      |> Map.put_new("include_types", @default_notification_types)
+
     notifications = MastodonAPI.get_notifications(user, params)
 
     conn
index 70da64a7a8f0fe8d7e7e36181f263a942109e1e9..694bf5ca8b9216dc593577ee0243b032065ba06b 100644 (file)
@@ -6,7 +6,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
   import Ecto.Query
   import Ecto.Changeset
 
-  alias Pleroma.Activity
   alias Pleroma.Notification
   alias Pleroma.Pagination
   alias Pleroma.ScheduledActivity
@@ -82,15 +81,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
   end
 
   defp restrict(query, :include_types, %{include_types: mastodon_types = [_ | _]}) do
-    ap_types = convert_and_filter_mastodon_types(mastodon_types)
-
-    where(query, [q, a], fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
+    where(query, [n], n.type in ^mastodon_types)
   end
 
   defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
-    ap_types = convert_and_filter_mastodon_types(mastodon_types)
-
-    where(query, [q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
+    where(query, [n], n.type not in ^mastodon_types)
   end
 
   defp restrict(query, :account_ap_id, %{account_ap_id: account_ap_id}) do
@@ -98,10 +93,4 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
   end
 
   defp restrict(query, _, _), do: query
-
-  defp convert_and_filter_mastodon_types(types) do
-    types
-    |> Enum.map(&Activity.from_mastodon_notification_type/1)
-    |> Enum.filter(& &1)
-  end
 end
index e278d61f59edd46f3671707ea1cad2f487507b89..698c99711b9ccc1ba69fe7fb746876b0674c300a 100644 (file)
@@ -54,6 +54,27 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
     assert response == expected_response
   end
 
+  test "by default, does not contain pleroma:chat_mention" do
+    %{user: user, conn: conn} = oauth_access(["read:notifications"])
+    other_user = insert(:user)
+
+    {:ok, _activity} = CommonAPI.post_chat_message(other_user, user, "hey")
+
+    result =
+      conn
+      |> get("/api/v1/notifications")
+      |> json_response_and_validate_schema(200)
+
+    assert [] == result
+
+    result =
+      conn
+      |> get("/api/v1/notifications?include_types[]=pleroma:chat_mention")
+      |> json_response_and_validate_schema(200)
+
+    assert [_] = result
+  end
+
   test "getting a single notification" do
     %{user: user, conn: conn} = oauth_access(["read:notifications"])
     other_user = insert(:user)