From 74112ef99e84f9f19d278faaa8e231b9ea867b2b Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 23 Mar 2017 21:22:49 +0100 Subject: [PATCH] Add id to objects, too. --- lib/pleroma/web/activity_pub/activity_pub.ex | 15 +++++++++++++++ test/web/activity_pub/activity_pub_test.exs | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 0f5a78e8a..e7427affb 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -6,6 +6,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do def insert(map) when is_map(map) do map = Map.put_new_lazy(map, "id", &generate_activity_id/0) + map = if map["object"] do + object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0) + Map.put(map, "object", object) + else + map + end + Repo.insert(%Activity{data: map}) end @@ -17,6 +24,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do "https://#{host}/activities/#{Ecto.UUID.generate}" end + def generate_object_id do + host = + Application.get_env(:pleroma, Pleroma.Web.Endpoint) + |> Keyword.fetch!(:url) + |> Keyword.fetch!(:host) + "https://#{host}/objects/#{Ecto.UUID.generate}" + end + def fetch_public_activities(opts \\ %{}) do public = ["https://www.w3.org/ns/activitystreams#Public"] fetch_activities(public, opts) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 613c5a04f..6a5138012 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -24,6 +24,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert activity.data["ok"] == data["ok"] assert activity.data["id"] == given_id end + + test "adds an id to a given object if it lacks one" do + data = %{ + "object" => %{ + "ok" => true + } + } + + {:ok, %Activity{} = activity} = ActivityPub.insert(data) + assert is_binary(activity.data["object"]["id"]) + end end describe "fetch activities for recipients" do -- 2.45.2