Return "audio" info in mastodon api.
[akkoma] / lib / pleroma / web / activity_pub / utils.ex
index ef58b88d203ca483995cba0e072ef6369b078583..a25b27aabde0b3035c57693aaeadf4ca1962bcf3 100644 (file)
@@ -5,6 +5,26 @@ defmodule Pleroma.Web.ActivityPub.Utils do
   alias Ecto.{Changeset, UUID}
   import Ecto.Query
 
+  def make_json_ld_header do
+    %{
+      "@context" => [
+        "https://www.w3.org/ns/activitystreams",
+        "https://w3id.org/security/v1",
+        %{
+          "manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
+          "sensitive" => "as:sensitive",
+          "Hashtag" => "as:Hashtag",
+          "ostatus" => "http://ostatus.org#",
+          "atomUri" => "ostatus:atomUri",
+          "inReplyToAtomUri" => "ostatus:inReplyToAtomUri",
+          "conversation" => "ostatus:conversation",
+          "toot" => "http://joinmastodon.org/ns#",
+          "Emoji" => "toot:Emoji"
+        }
+      ]
+    }
+  end
+
   def make_date do
     DateTime.utc_now() |> DateTime.to_iso8601
   end
@@ -29,7 +49,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 +88,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 +113,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 +129,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 +171,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 +183,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 +199,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 +228,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,