implement tracking of follow requests
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index acf23c53e006b9f4589acbb0801e5c54fa8dc18c..3229949c063080bb9e0ac5b5c8fcad9336be6778 100644 (file)
@@ -7,18 +7,15 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   # 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.
-  def normalize_actor(actor) do
-    cond do
-      is_binary(actor) ->
-        actor
-
-      is_map(actor) ->
-        actor["id"]
+  def get_ap_id(object) do
+    case object do
+      %{"id" => id} -> id
+      id -> id
     end
   end
 
   def normalize_params(params) do
-    Map.put(params, "actor", normalize_actor(params["actor"]))
+    Map.put(params, "actor", get_ap_id(params["actor"]))
   end
 
   def make_json_ld_header do
@@ -222,7 +219,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   @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,
@@ -232,17 +229,24 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     }
 
     if activity_id, do: Map.put(data, "id", activity_id), else: data
+    if User.locked?(followed), do: Map.put(data, "state", "pending"), else: 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", actor: follower_id, object: followed_id}
+            ^%{object: followed_id}
           ),
         order_by: [desc: :id],
         limit: 1
@@ -260,7 +264,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     query =
       from(
         activity in Activity,
-        where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
+        where: activity.actor == ^actor,
         # this is to use the index
         where:
           fragment(
@@ -353,6 +357,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "to" => [followed.ap_id],
       "object" => follow_activity.data
     }
+
     if activity_id, do: Map.put(data, "id", activity_id), else: data
   end
 
@@ -361,11 +366,17 @@ 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", actor: blocker_id, object: blocked_id}
+            ^%{object: blocked_id}
           ),
         order_by: [desc: :id],
         limit: 1
@@ -381,6 +392,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "to" => [blocked.ap_id],
       "object" => blocked.ap_id
     }
+
     if activity_id, do: Map.put(data, "id", activity_id), else: data
   end
 
@@ -391,6 +403,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "to" => [blocked.ap_id],
       "object" => block_activity.data
     }
+
     if activity_id, do: Map.put(data, "id", activity_id), else: data
   end