Formatting and add tests for blocks/unblocks
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index 6a36a6c10673a938bb7fa44ad0e07e975a6a29fd..846dd97c22d9f2889048ace60589b673f338e3e2 100644 (file)
@@ -175,7 +175,8 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   def update_element_in_object(property, element, object) do
     with new_data <-
-           object.data |> Map.put("#{property}_count", length(element))
+           object.data
+           |> Map.put("#{property}_count", length(element))
            |> Map.put("#{property}s", element),
          changeset <- Changeset.change(object, data: new_data),
          {:ok, object} <- Repo.update(changeset),
@@ -237,7 +238,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   #### Announce-related helpers
 
   @doc """
-  Retruns an existing announce activity if the notice has already been announced 
+  Retruns an existing announce activity if the notice has already been announced
   """
   def get_existing_announce(actor, %{data: %{"id" => id}}) do
     query =
@@ -278,6 +279,26 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     if activity_id, do: Map.put(data, "id", activity_id), else: data
   end
 
+  @doc """
+  Make unannounce activity data for the given actor and object
+  """
+  def make_unannounce_data(
+        %User{ap_id: ap_id} = user,
+        %Activity{data: %{"context" => context}} = activity,
+        activity_id
+      ) do
+    data = %{
+      "type" => "Undo",
+      "actor" => ap_id,
+      "object" => activity.data,
+      "to" => [user.follower_address, activity.data["actor"]],
+      "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
+      "context" => context
+    }
+
+    if activity_id, do: Map.put(data, "id", activity_id), else: data
+  end
+
   def add_announce_to_object(%Activity{data: %{"actor" => actor}}, object) do
     with announcements <- [actor | object.data["announcements"] || []] |> Enum.uniq() do
       update_element_in_object("announcement", announcements, object)
@@ -301,6 +322,42 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     }
   end
 
+  #### Block-related helpers
+  def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
+    query =
+      from(
+        activity in Activity,
+        where:
+          fragment(
+            "? @> ?",
+            activity.data,
+            ^%{type: "Block", actor: blocker_id, object: blocked_id}
+          ),
+        order_by: [desc: :id],
+        limit: 1
+      )
+
+    Repo.one(query)
+  end
+
+  def make_block_data(blocker, blocked) do
+    %{
+      "type" => "Block",
+      "actor" => blocker.ap_id,
+      "to" => [blocked.ap_id],
+      "object" => blocked.ap_id
+    }
+  end
+
+  def make_unblock_data(blocker, blocked, block_activity) do
+    %{
+      "type" => "Undo",
+      "actor" => blocker.ap_id,
+      "to" => [blocked.ap_id],
+      "object" => block_activity.data
+    }
+  end
+
   #### Create-related helpers
 
   def make_create_data(params, additional) do