activitypub: transmogrifier: ensure as:Public activities are delivered to followers
[akkoma] / lib / pleroma / activity.ex
index 66854dc2dd2920208261f4da1849420bd60c874a..de0e6668129841387eaa19e6a95d0af6bf3991ac 100644 (file)
@@ -5,9 +5,9 @@
 defmodule Pleroma.Activity do
   use Ecto.Schema
 
-  alias Pleroma.Repo
   alias Pleroma.Activity
   alias Pleroma.Notification
+  alias Pleroma.Repo
 
   import Ecto.Query
 
@@ -22,6 +22,10 @@ defmodule Pleroma.Activity do
     "Like" => "favourite"
   }
 
+  @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
+                                         into: %{},
+                                         do: {v, k}
+
   schema "activities" do
     field(:data, :map)
     field(:local, :boolean, default: true)
@@ -107,6 +111,18 @@ defmodule Pleroma.Activity do
 
   def get_in_reply_to_activity(_), do: nil
 
+  def delete_by_ap_id(id) when is_binary(id) do
+    by_object_ap_id(id)
+    |> Repo.delete_all(returning: true)
+    |> elem(1)
+    |> Enum.find(fn
+      %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
+      _ -> nil
+    end)
+  end
+
+  def delete_by_ap_id(_), do: nil
+
   for {ap_type, type} <- @mastodon_notification_types do
     def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
       do: unquote(type)
@@ -114,6 +130,10 @@ defmodule Pleroma.Activity do
 
   def mastodon_notification_type(%Activity{}), do: nil
 
+  def from_mastodon_notification_type(type) do
+    Map.get(@mastodon_to_ap_notification_types, type)
+  end
+
   def all_by_actor_and_id(actor, status_ids \\ [])
   def all_by_actor_and_id(_actor, []), do: []