summaryrefslogtreecommitdiff
path: root/test/support/builders/activity_builder.ex
blob: f1ca3951bcd33d18d7e60c9a6c355cc46c20f40b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Builders.ActivityBuilder do
  alias Pleroma.Web.ActivityPub.ActivityPub

  def build(data \\ %{}, opts \\ %{}) do
    user = opts[:user] || Pleroma.Factory.insert(:user)

    activity = %{
      "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
      "actor" => user.ap_id,
      "to" => ["https://www.w3.org/ns/activitystreams#Public"],
      "type" => "Create",
      "object" => %{
        "type" => "Note",
        "content" => "test",
        "to" => ["https://www.w3.org/ns/activitystreams#Public"]
      }
    }

    Map.merge(activity, data)
  end

  def insert(data \\ %{}, opts \\ %{}) do
    activity = build(data, opts)

    case ActivityPub.insert(activity) do
      ok = {:ok, activity} ->
        ActivityPub.notify_and_stream(activity)
        ok

      error ->
        error
    end
  end

  def insert_list(times, data \\ %{}, opts \\ %{}) do
    Enum.map(1..times, fn _n ->
      {:ok, activity} = insert(data, opts)
      activity
    end)
  end

  def public_and_non_public do
    user = Pleroma.Factory.insert(:user)

    public = build(%{"id" => 1}, %{user: user})
    non_public = build(%{"id" => 2, "to" => [user.follower_address]}, %{user: user})

    {:ok, public} = ActivityPub.insert(public)
    {:ok, non_public} = ActivityPub.insert(non_public)

    %{
      public: public,
      non_public: non_public,
      user: user
    }
  end
end