Merge branch 'instance-deletion' into 'develop'
[akkoma] / test / support / builders / activity_builder.ex
1 defmodule Pleroma.Builders.ActivityBuilder do
2 alias Pleroma.Web.ActivityPub.ActivityPub
3
4 def build(data \\ %{}, opts \\ %{}) do
5 user = opts[:user] || Pleroma.Factory.insert(:user)
6
7 activity = %{
8 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
9 "actor" => user.ap_id,
10 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
11 "type" => "Create",
12 "object" => %{
13 "type" => "Note",
14 "content" => "test",
15 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
16 }
17 }
18
19 Map.merge(activity, data)
20 end
21
22 def insert(data \\ %{}, opts \\ %{}) do
23 activity = build(data, opts)
24
25 case ActivityPub.insert(activity) do
26 ok = {:ok, activity} ->
27 ActivityPub.notify_and_stream(activity)
28 ok
29
30 error ->
31 error
32 end
33 end
34
35 def insert_list(times, data \\ %{}, opts \\ %{}) do
36 Enum.map(1..times, fn _n ->
37 {:ok, activity} = insert(data, opts)
38 activity
39 end)
40 end
41
42 def public_and_non_public do
43 user = Pleroma.Factory.insert(:user)
44
45 public = build(%{"id" => 1}, %{user: user})
46 non_public = build(%{"id" => 2, "to" => [user.follower_address]}, %{user: user})
47
48 {:ok, public} = ActivityPub.insert(public)
49 {:ok, non_public} = ActivityPub.insert(non_public)
50
51 %{
52 public: public,
53 non_public: non_public,
54 user: user
55 }
56 end
57 end