Use correct logic to determine "attentions" list
authorMaxim Filippov <colixer@gmail.com>
Thu, 24 Jan 2019 20:30:43 +0000 (23:30 +0300)
committerMaxim Filippov <colixer@gmail.com>
Thu, 24 Jan 2019 20:30:43 +0000 (23:30 +0300)
lib/pleroma/notification.ex
lib/pleroma/web/common_api/utils.ex
lib/pleroma/web/twitter_api/representers/activity_representer.ex
lib/pleroma/web/twitter_api/views/activity_view.ex
test/web/twitter_api/views/activity_view_test.exs

index c7d01f63b83517c36b15779ec9a9772524d5b6d4..4659e14ef8bd667f50d4b06ea3757b6d4d1f73d3 100644 (file)
@@ -4,7 +4,8 @@
 
 defmodule Pleroma.Notification do
   use Ecto.Schema
-  alias Pleroma.{User, Activity, Notification, Repo, Object}
+  alias Pleroma.{User, Activity, Notification, Repo}
+  alias Pleroma.Web.CommonAPI.Utils
   import Ecto.Query
 
   schema "notifications" do
@@ -132,54 +133,12 @@ defmodule Pleroma.Notification do
       when type in ["Create", "Like", "Announce", "Follow"] do
     recipients =
       []
-      |> maybe_notify_to_recipients(activity)
-      |> maybe_notify_mentioned_recipients(activity)
+      |> Utils.maybe_notify_to_recipients(activity)
+      |> Utils.maybe_notify_mentioned_recipients(activity)
       |> Enum.uniq()
 
     User.get_users_from_set(recipients, local_only)
   end
 
   def get_notified_from_activity(_, _local_only), do: []
-
-  defp maybe_notify_to_recipients(
-         recipients,
-         %Activity{data: %{"to" => to, "type" => _type}} = _activity
-       ) do
-    recipients ++ to
-  end
-
-  defp maybe_notify_mentioned_recipients(
-         recipients,
-         %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
-       )
-       when type == "Create" do
-    object = Object.normalize(data["object"])
-
-    object_data =
-      cond do
-        !is_nil(object) ->
-          object.data
-
-        is_map(data["object"]) ->
-          data["object"]
-
-        true ->
-          %{}
-      end
-
-    tagged_mentions = maybe_extract_mentions(object_data)
-
-    recipients ++ tagged_mentions
-  end
-
-  defp maybe_notify_mentioned_recipients(recipients, _), do: recipients
-
-  defp maybe_extract_mentions(%{"tag" => tag}) do
-    tag
-    |> Enum.filter(fn x -> is_map(x) end)
-    |> Enum.filter(fn x -> x["type"] == "Mention" end)
-    |> Enum.map(fn x -> x["href"] end)
-  end
-
-  defp maybe_extract_mentions(_), do: []
 end
index 7e30d224cef348d201ad67d3fb1da2dadaff6cf5..d36875705727f56a1a9e7662515aafc6d1b658b7 100644 (file)
@@ -259,4 +259,46 @@ defmodule Pleroma.Web.CommonAPI.Utils do
       }
     end)
   end
+
+  def maybe_notify_to_recipients(
+        recipients,
+        %Activity{data: %{"to" => to, "type" => _type}} = _activity
+      ) do
+    recipients ++ to
+  end
+
+  def maybe_notify_mentioned_recipients(
+        recipients,
+        %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
+      )
+      when type == "Create" do
+    object = Object.normalize(data["object"])
+
+    object_data =
+      cond do
+        !is_nil(object) ->
+          object.data
+
+        is_map(data["object"]) ->
+          data["object"]
+
+        true ->
+          %{}
+      end
+
+    tagged_mentions = maybe_extract_mentions(object_data)
+
+    recipients ++ tagged_mentions
+  end
+
+  def maybe_notify_mentioned_recipients(recipients, _), do: recipients
+
+  def maybe_extract_mentions(%{"tag" => tag}) do
+    tag
+    |> Enum.filter(fn x -> is_map(x) end)
+    |> Enum.filter(fn x -> x["type"] == "Mention" end)
+    |> Enum.map(fn x -> x["href"] end)
+  end
+
+  def maybe_extract_mentions(_), do: []
 end
index 0ddbef63422fe3d143ae9f2c852a14839e4871a3..19b723586bd804f8382156594ad469d2531b6147 100644 (file)
@@ -155,10 +155,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
     repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
     pinned = activity.id in user.info.pinned_activities
 
-    mentions = get_mentioned_users(opts[:mentioned] || [], user)
+    mentions = opts[:mentioned] || []
 
     attentions =
-      activity.recipients
+      []
+      |> Utils.maybe_notify_to_recipients(activity)
+      |> Utils.maybe_notify_mentioned_recipients(activity)
       |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
       |> Enum.filter(& &1)
       |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
@@ -224,10 +226,6 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
     end
   end
 
-  defp get_mentioned_users(mentioned, user) do
-    mentioned ++ [user]
-  end
-
   defp to_boolean(false) do
     false
   end
index 108e7bfc5208d3d19def3aefc4c525c1a3669b6b..9ae7846c072a837872a5fa4bf1b63869d6dcf092 100644 (file)
@@ -246,7 +246,9 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
     pinned = activity.id in user.info.pinned_activities
 
     attentions =
-      activity.recipients
+      []
+      |> Utils.maybe_notify_to_recipients(activity)
+      |> Utils.maybe_notify_mentioned_recipients(activity)
       |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
       |> Enum.filter(& &1)
       |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
index 0f514dab1ca6af6361ab0d9b212a52b199dd0379..8b5a16add9b12d30f0cf0be0c067620e01ee469e 100644 (file)
@@ -118,8 +118,9 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
     expected = %{
       "activity_type" => "post",
       "attachments" => [],
-      "attentions" =>
-        Enum.map([other_user, user], fn u -> UserView.render("show.json", %{user: u}) end),
+      "attentions" => [
+        UserView.render("show.json", %{user: other_user})
+      ],
       "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
       "external_url" => activity.data["object"]["id"],
       "fave_num" => 0,