Merge develop
[akkoma] / lib / pleroma / web / push / impl.ex
index 33f912d346bddb8a0c60211eda04d456fa2f62c7..35d3ff07cbb4946b411eac8f040efb454931dd76 100644 (file)
@@ -5,13 +5,13 @@
 defmodule Pleroma.Web.Push.Impl do
   @moduledoc "The module represents implementation push web notification"
 
-  alias Pleroma.Repo
-  alias Pleroma.User
   alias Pleroma.Activity
+  alias Pleroma.Notification
   alias Pleroma.Object
-  alias Pleroma.Web.Push.Subscription
+  alias Pleroma.Repo
+  alias Pleroma.User
   alias Pleroma.Web.Metadata.Utils
-  alias Pleroma.Notification
+  alias Pleroma.Web.Push.Subscription
 
   require Logger
   import Ecto.Query
@@ -19,32 +19,41 @@ defmodule Pleroma.Web.Push.Impl do
   @types ["Create", "Follow", "Announce", "Like"]
 
   @doc "Performs sending notifications for user subscriptions"
-  @spec perform_send(Notification.t()) :: list(any)
-  def perform_send(%{activity: %{data: %{"type" => activity_type}}, user_id: user_id} = notif)
+  @spec perform(Notification.t()) :: list(any) | :error
+  def perform(
+        %{
+          activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
+          user_id: user_id
+        } = notif
+      )
       when activity_type in @types do
     actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
 
     type = Activity.mastodon_notification_type(notif.activity)
     gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
     avatar_url = User.avatar_url(actor)
+    object = Object.normalize(activity)
 
     for subscription <- fetch_subsriptions(user_id),
         get_in(subscription.data, ["alerts", type]) do
       %{
         title: format_title(notif),
         access_token: subscription.token.token,
-        body: format_body(notif, actor),
+        body: format_body(notif, actor, object),
         notification_id: notif.id,
         notification_type: type,
         icon: avatar_url,
-        preferred_locale: "en"
+        preferred_locale: "en",
+        pleroma: %{
+          activity_id: activity_id
+        }
       }
       |> Jason.encode!()
       |> push_message(build_sub(subscription), gcm_api_key, subscription)
     end
   end
 
-  def perform_send(_) do
+  def perform(_) do
     Logger.warn("Unknown notification type")
     :error
   end
@@ -89,25 +98,25 @@ defmodule Pleroma.Web.Push.Impl do
   end
 
   def format_body(
-        %{activity: %{data: %{"type" => "Create", "object" => %{"content" => content}}}},
-        actor
+        %{activity: %{data: %{"type" => "Create"}}},
+        actor,
+        %{data: %{"content" => content}}
       ) do
     "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
   end
 
   def format_body(
-        %{activity: %{data: %{"type" => "Announce", "object" => activity_id}}},
-        actor
+        %{activity: %{data: %{"type" => "Announce"}}},
+        actor,
+        %{data: %{"content" => content}}
       ) do
-    %Activity{data: %{"object" => %{"id" => object_id}}} = Activity.get_by_ap_id(activity_id)
-    %Object{data: %{"content" => content}} = Object.get_by_ap_id(object_id)
-
     "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
   end
 
   def format_body(
         %{activity: %{data: %{"type" => type}}},
-        actor
+        actor,
+        _object
       )
       when type in ["Follow", "Like"] do
     case type do