doc fixes
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index ac3a57e035414e3b3502bd2cfa118db52e2409bf..6a36a6c10673a938bb7fa44ad0e07e975a6a29fd 100644 (file)
@@ -50,10 +50,13 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     changeset = Object.context_mapping(context)
 
     case Repo.insert(changeset) do
-      {:ok, object} -> object
+      {:ok, object} ->
+        object
+
       # This should be solved by an upsert, but it seems ecto
       # has problems accessing the constraint inside the jsonb.
-      {:error, _} -> Object.get_cached_by_ap_id(context)
+      {:error, _} ->
+        Object.get_cached_by_ap_id(context)
     end
   end
 
@@ -233,6 +236,28 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   #### Announce-related helpers
 
+  @doc """
+  Retruns an existing announce activity if the notice has already been announced 
+  """
+  def get_existing_announce(actor, %{data: %{"id" => id}}) do
+    query =
+      from(
+        activity in Activity,
+        where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
+        # this is to use the index
+        where:
+          fragment(
+            "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
+            activity.data,
+            activity.data,
+            ^id
+          ),
+        where: fragment("(?)->>'type' = 'Announce'", activity.data)
+      )
+
+    Repo.one(query)
+  end
+
   @doc """
   Make announce activity data for the given actor and object
   """
@@ -259,6 +284,12 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     end
   end
 
+  def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
+    with announcements <- (object.data["announcements"] || []) |> List.delete(actor) do
+      update_element_in_object("announcement", announcements, object)
+    end
+  end
+
   #### Unfollow-related helpers
 
   def make_unfollow_data(follower, followed, follow_activity) do