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
--- /dev/null
+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
--- /dev/null
+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