From 6aa1523bb3d8dfe12ce5143d27023fd833b0c1aa Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 30 Mar 2017 18:07:01 +0200 Subject: [PATCH] Put objects in inserted activties into the db. --- lib/pleroma/object.ex | 7 +++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 1 + test/web/activity_pub/activity_pub_test.exs | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index a31f40d07..f932034d7 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -1,9 +1,16 @@ defmodule Pleroma.Object do use Ecto.Schema + alias Pleroma.{Repo, Object} + import Ecto.Query schema "objects" do field :data, :map timestamps() end + + def get_by_ap_id(ap_id) do + Repo.one(from object in Object, + where: fragment("? @> ?", object.data, ^%{id: ap_id})) + end end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 6c8250de8..b01def693 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -8,6 +8,7 @@ def insert(map) when is_map(map) do map = if map["object"] do object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0) + Repo.insert!(%Object{data: object}) Map.put(map, "object", object) else map diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 0e778d887..2c6f67621 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -25,7 +25,7 @@ test "inserts a given map into the activity database, giving it an id if it has assert activity.data["id"] == given_id end - test "adds an id to a given object if it lacks one" do + test "adds an id to a given object if it lacks one and inserts it to the object database" do data = %{ "object" => %{ "ok" => true @@ -34,6 +34,7 @@ test "adds an id to a given object if it lacks one" do {:ok, %Activity{} = activity} = ActivityPub.insert(data) assert is_binary(activity.data["object"]["id"]) + assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"]) end end