Object: Automatically set ap_id from the data.

This commit is contained in:
lain 2019-10-08 11:57:28 +02:00
parent 9038556064
commit 202678216a
7 changed files with 55 additions and 4 deletions

View File

@ -19,14 +19,21 @@ defmodule Pleroma.Object do
schema "objects" do schema "objects" do
field(:data, :map) field(:data, :map)
# This is set by a database side trigger on insert and update
field(:ap_id, :string) field(:ap_id, :string)
timestamps() timestamps()
end end
# Add the default 'returning' options, so we get the generated ap_id column
def insert(cng, options \\ []) do
cng
|> Repo.insert(Keyword.put(options, :returning, true))
end
def create(data) do def create(data) do
Object.change(%Object{}, %{data: data}) Object.change(%Object{}, %{data: data})
|> Repo.insert() |> insert()
end end
def change(struct, params \\ %{}) do def change(struct, params \\ %{}) do

View File

@ -976,7 +976,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
data data
end end
Repo.insert(%Object{data: obj_data}) Object.insert(%Object{data: obj_data})
end end
end end

View File

@ -153,7 +153,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
with true <- is_nil(object), with true <- is_nil(object),
changeset <- Object.context_mapping(context), changeset <- Object.context_mapping(context),
{:ok, inserted_object} <- Repo.insert(changeset) do {:ok, inserted_object} <- Object.insert(changeset) do
inserted_object inserted_object
else else
_ -> _ ->

View File

@ -6,6 +6,6 @@ defmodule Pleroma.Repo.Migrations.AddApIdColumnToObjects do
add :ap_id, :string add :ap_id, :string
end end
create unique_index(:objects, [:ap_id], concurrently: true) create unique_index(:objects, [:ap_id])
end end
end end

View File

@ -0,0 +1,7 @@
defmodule Pleroma.Repo.Migrations.FillObjectApIdField do
use Ecto.Migration
def change do
execute("update objects set ap_id = data->>'id'")
end
end

View File

@ -0,0 +1,25 @@
defmodule Pleroma.Repo.Migrations.AddObjectApIdTrigger do
use Ecto.Migration
def change do
execute("""
CREATE OR REPLACE FUNCTION set_ap_id()
RETURNS trigger
LANGUAGE plpgsql
AS $BODY$
BEGIN
NEW.ap_id = NEW.data->>'id';
RETURN NEW;
END
$BODY$;
""")
execute("""
CREATE TRIGGER object_ap_id_extraction
BEFORE INSERT OR UPDATE
ON objects
FOR EACH ROW
EXECUTE PROCEDURE set_ap_id();
""")
end
end

View File

@ -19,6 +19,7 @@ defmodule Pleroma.ObjectTest do
test "returns an object by it's AP id" do test "returns an object by it's AP id" do
object = insert(:note) object = insert(:note)
object = Object.get_by_id(object.id)
found_object = Object.get_by_ap_id(object.data["id"]) found_object = Object.get_by_ap_id(object.data["id"])
assert object == found_object assert object == found_object
@ -32,11 +33,21 @@ defmodule Pleroma.ObjectTest do
{:error, _result} = Repo.insert(cs) {:error, _result} = Repo.insert(cs)
end end
test "it automatically sets the ap_id field" do
{:ok, object} =
%Object{}
|> Object.change(%{data: %{id: "myid"}})
|> Object.insert()
assert object.ap_id == "myid"
end
end end
describe "deletion function" do describe "deletion function" do
test "deletes an object" do test "deletes an object" do
object = insert(:note) object = insert(:note)
object = Object.get_by_id(object.id)
found_object = Object.get_by_ap_id(object.data["id"]) found_object = Object.get_by_ap_id(object.data["id"])
assert object == found_object assert object == found_object
@ -52,6 +63,7 @@ defmodule Pleroma.ObjectTest do
test "ensures cache is cleared for the object" do test "ensures cache is cleared for the object" do
object = insert(:note) object = insert(:note)
object = Object.get_by_id(object.id)
cached_object = Object.get_cached_by_ap_id(object.data["id"]) cached_object = Object.get_cached_by_ap_id(object.data["id"])
assert object == cached_object assert object == cached_object