From f97c8e43792f76e84c436e75b0c0893d3c16d7d8 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 13 Apr 2017 15:49:42 +0200 Subject: [PATCH] Add utility functions for objects and activities. --- lib/pleroma/activity.ex | 12 ++++++++++++ test/activity_test.exs | 18 ++++++++++++++++++ test/object_test.exs | 11 +++++++++++ 3 files changed, 41 insertions(+) create mode 100644 test/activity_test.exs create mode 100644 test/object_test.exs diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index c4efc6283..46568bb13 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -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 index 000000000..ce6eb1545 --- /dev/null +++ b/test/activity_test.exs @@ -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 index 000000000..3ffddbbc5 --- /dev/null +++ b/test/object_test.exs @@ -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