From 1f0f79d95911769f1a99aebc5df47aa98b38c360 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 23 Mar 2017 17:56:49 +0100 Subject: [PATCH] Generate ids for activities. --- lib/pleroma/web/activity_pub/activity_pub.ex | 10 ++++++++++ test/web/activity_pub/activity_pub_test.exs | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 121c46c51..0f5a78e8a 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -4,9 +4,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do import Ecto.Query def insert(map) when is_map(map) do + map = Map.put_new_lazy(map, "id", &generate_activity_id/0) + Repo.insert(%Activity{data: map}) end + def generate_activity_id do + host = + Application.get_env(:pleroma, Pleroma.Web.Endpoint) + |> Keyword.fetch!(:url) + |> Keyword.fetch!(:host) + "https://#{host}/activities/#{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 d220f91c3..613c5a04f 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -5,17 +5,27 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do alias Pleroma.Builders.ActivityBuilder describe "insertion" do - test "inserts a given map into the activity database" do + test "inserts a given map into the activity database, giving it an id if it has none." do data = %{ - ok: true + "ok" => true } {:ok, %Activity{} = activity} = ActivityPub.insert(data) - assert activity.data == data + assert activity.data["ok"] == data["ok"] + assert is_binary(activity.data["id"]) + + given_id = "bla" + data = %{ + "ok" => true, + "id" => given_id + } + + {:ok, %Activity{} = activity} = ActivityPub.insert(data) + assert activity.data["ok"] == data["ok"] + assert activity.data["id"] == given_id end end - describe "fetch activities for recipients" do test "retrieve the activities for certain recipients" do {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})