Generate ids for activities.

This commit is contained in:
Roger Braun 2017-03-23 17:56:49 +01:00
parent 9ad045fd58
commit 1f0f79d959
2 changed files with 24 additions and 4 deletions

View File

@ -4,9 +4,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
import Ecto.Query import Ecto.Query
def insert(map) when is_map(map) do def insert(map) when is_map(map) do
map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
Repo.insert(%Activity{data: map}) Repo.insert(%Activity{data: map})
end 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 def fetch_public_activities(opts \\ %{}) do
public = ["https://www.w3.org/ns/activitystreams#Public"] public = ["https://www.w3.org/ns/activitystreams#Public"]
fetch_activities(public, opts) fetch_activities(public, opts)

View File

@ -5,17 +5,27 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
alias Pleroma.Builders.ActivityBuilder alias Pleroma.Builders.ActivityBuilder
describe "insertion" do 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 = %{ data = %{
ok: true "ok" => true
} }
{:ok, %Activity{} = activity} = ActivityPub.insert(data) {: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
end end
describe "fetch activities for recipients" do describe "fetch activities for recipients" do
test "retrieve the activities for certain recipients" do test "retrieve the activities for certain recipients" do
{:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]}) {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})