Merge branch 'develop' into 'patch-2'
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index 7362a3ccfaa4a99b8d4ab32c51d421a5699da776..8b41a3becbb26cad7af645d8bd6480316bd2a8c0 100644 (file)
@@ -4,6 +4,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   alias Pleroma.Web.Endpoint
   alias Ecto.{Changeset, UUID}
   import Ecto.Query
+  require Logger
 
   # Some implementations send the actor URI as the actor field, others send the entire actor object,
   # so figure out what the actor's URI is based on what we have.
@@ -127,7 +128,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   Inserts a full object if it is contained in an activity.
   """
   def insert_full_object(%{"object" => %{"type" => type} = object_data})
-      when is_map(object_data) and type in ["Note"] do
+      when is_map(object_data) and type in ["Article", "Note"] do
     with {:ok, _} <- Object.create(object_data) do
       :ok
     end
@@ -216,10 +217,27 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   #### Follow-related helpers
 
+  @doc """
+  Updates a follow activity's state (for locked accounts).
+  """
+  def update_follow_state(%Activity{} = activity, state) do
+    with new_data <-
+           activity.data
+           |> Map.put("state", state),
+         changeset <- Changeset.change(activity, data: new_data),
+         {:ok, activity} <- Repo.update(changeset) do
+      {:ok, activity}
+    end
+  end
+
   @doc """
   Makes a follow activity data for the given follower and followed
   """
-  def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activity_id) do
+  def make_follow_data(
+        %User{ap_id: follower_id},
+        %User{ap_id: followed_id} = followed,
+        activity_id
+      ) do
     data = %{
       "type" => "Follow",
       "actor" => follower_id,
@@ -228,20 +246,28 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "object" => followed_id
     }
 
-    if activity_id, do: Map.put(data, "id", activity_id), else: data
+    data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+    data = if User.locked?(followed), do: Map.put(data, "state", "pending"), else: data
+
+    data
   end
 
   def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
     query =
       from(
         activity in Activity,
+        where:
+          fragment(
+            "? ->> 'type' = 'Follow'",
+            activity.data
+          ),
+        where: activity.actor == ^follower_id,
         where:
           fragment(
             "? @> ?",
             activity.data,
-            ^%{type: "Follow", object: followed_id}
+            ^%{object: followed_id}
           ),
-        where: activity.actor == ^follower_id,
         order_by: [desc: :id],
         limit: 1
       )
@@ -360,13 +386,18 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     query =
       from(
         activity in Activity,
+        where:
+          fragment(
+            "? ->> 'type' = 'Block'",
+            activity.data
+          ),
+        where: activity.actor == ^blocker_id,
         where:
           fragment(
             "? @> ?",
             activity.data,
-            ^%{type: "Block", object: blocked_id}
+            ^%{object: blocked_id}
           ),
-        where: activity.actor == ^blocker_id,
         order_by: [desc: :id],
         limit: 1
       )