Add utility functions for objects and activities.
authorRoger Braun <roger@rogerbraun.net>
Thu, 13 Apr 2017 13:49:42 +0000 (15:49 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 13 Apr 2017 13:49:42 +0000 (15:49 +0200)
lib/pleroma/activity.ex
test/activity_test.exs [new file with mode: 0644]
test/object_test.exs [new file with mode: 0644]

index c4efc62839bc659cccd64591b3ba6770fe1978f5..46568bb13eb288d6322b145db776e5427aa5e9c8 100644 (file)
@@ -1,9 +1,21 @@
 defmodule Pleroma.Activity do
   use Ecto.Schema
+  alias Pleroma.{Repo, Activity}
+  import Ecto.Query
 
   schema "activities" do
     field :data, :map
 
     timestamps()
   end
+
+  def get_by_ap_id(ap_id) do
+    Repo.one(from activity in Activity,
+      where: fragment("? @> ?", activity.data, ^%{id: ap_id}))
+  end
+
+  def all_by_object_ap_id(ap_id) do
+    Repo.all(from activity in Activity,
+      where: fragment("? @> ?", activity.data, ^%{object: %{id: ap_id}}))
+  end
 end
diff --git a/test/activity_test.exs b/test/activity_test.exs
new file mode 100644 (file)
index 0000000..ce6eb15
--- /dev/null
@@ -0,0 +1,18 @@
+defmodule Pleroma.ActivityTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+
+  test "returns an activity by it's AP id" do
+    activity = insert(:note_activity)
+    found_activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
+
+    assert activity == found_activity
+  end
+
+  test "returns activities by it's objects AP ids" do
+    activity = insert(:note_activity)
+    [found_activity] = Pleroma.Activity.all_by_object_ap_id(activity.data["object"]["id"])
+
+    assert activity == found_activity
+  end
+end
diff --git a/test/object_test.exs b/test/object_test.exs
new file mode 100644 (file)
index 0000000..3ffddbb
--- /dev/null
@@ -0,0 +1,11 @@
+defmodule Pleroma.ObjectTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+
+  test "returns an object by it's AP id" do
+    object = insert(:note)
+    found_object = Pleroma.Object.get_by_ap_id(object.data["id"])
+
+    assert object == found_object
+  end
+end