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