Add support for outgoing update.
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index ef58b88d203ca483995cba0e072ef6369b078583..cda10628387f066bedb69881aff5737a9266835f 100644 (file)
@@ -29,7 +29,12 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   Enqueues an activity for federation if it's local
   """
   def maybe_federate(%Activity{local: true} = activity) do
-    Pleroma.Web.Federator.enqueue(:publish, activity)
+    priority = case activity.data["type"] do
+                 "Delete" -> 10
+                 "Create" -> 1
+                 _ -> 5
+               end
+    Pleroma.Web.Federator.enqueue(:publish, activity, priority)
     :ok
   end
   def maybe_federate(_), do: :ok
@@ -63,8 +68,8 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   @doc """
   Inserts a full object if it is contained in an activity.
   """
-  def insert_full_object(%{"object" => object_data}) when is_map(object_data) do
-    with {:ok, object} <- Object.create(object_data) do
+  def insert_full_object(%{"object" => %{"type" => type} = object_data}) when is_map(object_data) and type in ["Note"] do
+    with {:ok, _} <- Object.create(object_data) do
       :ok
     end
   end
@@ -88,9 +93,13 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   @doc """
   Returns an existing like if a user already liked an object
   """
-  def get_existing_like(actor, %{data: %{"id" => id}} = object) do
+  def get_existing_like(actor, %{data: %{"id" => id}}) do
     query = from activity in Activity,
-      where: fragment("? @> ?", activity.data, ^%{actor: actor, object: id, type: "Like"})
+      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' = 'Like'", activity.data)
+
     Repo.one(query)
   end
 
@@ -100,6 +109,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "actor" => ap_id,
       "object" => id,
       "to" => [actor.follower_address, object.data["actor"]],
+      "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
       "context" => object.data["context"]
     }
 
@@ -141,6 +151,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "type" => "Follow",
       "actor" => follower_id,
       "to" => [followed_id],
+      "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
       "object" => followed_id
     }
 
@@ -152,7 +163,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     query = from activity in Activity,
       where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
                                                   object: followed_id}),
-      order_by: [desc: :inserted_at],
+      order_by: [desc: :id],
       limit: 1
     Repo.one(query)
   end
@@ -168,6 +179,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
       "actor" => ap_id,
       "object" => id,
       "to" => [user.follower_address, object.data["actor"]],
+      "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
       "context" => object.data["context"]
     }
 
@@ -196,8 +208,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
 
   def make_create_data(params, additional) do
     published = params.published || make_date()
-
-    activity = %{
+    %{
       "type" => "Create",
       "to" => params.to |> Enum.uniq,
       "actor" => params.actor.ap_id,