Add actor column to activities.
authorRoger Braun <roger@rogerbraun.net>
Thu, 9 Nov 2017 09:41:19 +0000 (10:41 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 9 Nov 2017 09:41:19 +0000 (10:41 +0100)
lib/pleroma/activity.ex
lib/pleroma/web/activity_pub/activity_pub.ex
priv/repo/migrations/20171109091239_add_actor_to_activity.exs [new file with mode: 0644]
test/support/factory.ex
test/web/activity_pub/activity_pub_test.exs

index f43b32b565a502c8338b2d9a655577fc906b1a2b..a35ccc9b415cff3ab73cb21c8f852b55a9cccdc1 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Activity do
   schema "activities" do
     field :data, :map
     field :local, :boolean, default: true
+    field :actor, :string
     has_many :notifications, Notification
 
     timestamps()
index e7d4c48bd1443c90742728ff7b7ce63e07f70daa..1624c6545b31e0af5fd1a7683431a3c1eb13a751 100644 (file)
@@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     with nil <- Activity.get_by_ap_id(map["id"]),
          map <- lazy_put_activity_defaults(map),
          :ok <- insert_full_object(map) do
-      {:ok, activity} = Repo.insert(%Activity{data: map, local: local})
+      {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"]})
       Notification.create_notifications(activity)
       {:ok, activity}
     else
@@ -137,7 +137,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   defp restrict_actor(query, %{"actor_id" => actor_id}) do
     from activity in query,
-      where: fragment("?->>'actor' = ?", activity.data, ^actor_id)
+      where: activity.actor == ^actor_id
   end
   defp restrict_actor(query, _), do: query
 
@@ -168,14 +168,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
     blocks = info["blocks"] || []
     from activity in query,
-      where: fragment("not (?->>'actor' = ANY(?))", activity.data, ^blocks)
+      where: fragment("not (? = ANY(?))", activity.actor, ^blocks)
   end
   defp restrict_blocked(query, _), do: query
 
   def fetch_activities(recipients, opts \\ %{}) do
     base_query = from activity in Activity,
       limit: 20,
-      order_by: [desc: :id]
+      order_by: [fragment("? desc nulls last", activity.id)]
 
     base_query
     |> restrict_recipients(recipients)
diff --git a/priv/repo/migrations/20171109091239_add_actor_to_activity.exs b/priv/repo/migrations/20171109091239_add_actor_to_activity.exs
new file mode 100644 (file)
index 0000000..bac5397
--- /dev/null
@@ -0,0 +1,24 @@
+defmodule Pleroma.Repo.Migrations.AddActorToActivity do
+  use Ecto.Migration
+
+  @disable_ddl_transaction true
+
+  def up do
+    alter table(:activities) do
+      add :actor, :string
+    end
+
+    execute """
+      update activities set actor = data->>'actor';
+    """
+
+    create index(:activities, [:actor, "id DESC NULLS LAST"], concurrently: true)
+  end
+
+  def down do
+    drop index(:activities, [:actor, "id DESC NULLS LAST"])
+    alter table(:activities) do
+      remove :actor
+    end
+  end
+end
index dabb951415ff1bc882ad3c350c16f707e370ad29..9bbf11f08f12af98fd378bdf810712c8c26d7670 100644 (file)
@@ -51,7 +51,8 @@ defmodule Pleroma.Factory do
     }
 
     %Pleroma.Activity{
-      data: data
+      data: data,
+      actor: data["actor"]
     }
   end
 
index 2cd33d623ee42a7c29b2df54494ce7e91ea91900..f50509b630522f22b5df2744202a0bdd0c558418 100644 (file)
@@ -52,6 +52,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     test "removes doubled 'to' recipients" do
       {:ok, activity} = ActivityPub.create(["user1", "user1", "user2"], %User{ap_id: "1"}, "", %{})
       assert activity.data["to"] == ["user1", "user2"]
+      assert activity.actor == "1"
     end
   end