Add id to objects, too.

This commit is contained in:
Roger Braun 2017-03-23 21:22:49 +01:00
parent 1f0f79d959
commit 74112ef99e
2 changed files with 26 additions and 0 deletions

View File

@ -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)

View File

@ -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