2022-02-25 22:11:42 -08:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 12:38:50 -08:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2022-02-25 22:11:42 -08:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-03-21 09:53:20 -07:00
|
|
|
defmodule Pleroma.Builders.ActivityBuilder do
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
|
2017-03-21 12:22:05 -07:00
|
|
|
def build(data \\ %{}, opts \\ %{}) do
|
2017-04-16 06:28:28 -07:00
|
|
|
user = opts[:user] || Pleroma.Factory.insert(:user)
|
2018-03-30 06:01:53 -07:00
|
|
|
|
2017-03-21 12:22:05 -07:00
|
|
|
activity = %{
|
2018-03-30 06:01:53 -07:00
|
|
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
|
2017-03-21 09:53:20 -07:00
|
|
|
"actor" => user.ap_id,
|
|
|
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
2018-02-25 08:48:31 -08:00
|
|
|
"type" => "Create",
|
2017-03-21 09:53:20 -07:00
|
|
|
"object" => %{
|
|
|
|
"type" => "Note",
|
2018-02-25 08:48:31 -08:00
|
|
|
"content" => "test",
|
2018-03-30 06:01:53 -07:00
|
|
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"]
|
2017-03-21 09:53:20 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 06:01:53 -07:00
|
|
|
|
2017-03-21 12:22:05 -07:00
|
|
|
Map.merge(activity, data)
|
|
|
|
end
|
2017-03-21 09:53:20 -07:00
|
|
|
|
2017-03-21 12:22:05 -07:00
|
|
|
def insert(data \\ %{}, opts \\ %{}) do
|
|
|
|
activity = build(data, opts)
|
2020-05-07 02:13:32 -07:00
|
|
|
|
|
|
|
case ActivityPub.insert(activity) do
|
|
|
|
ok = {:ok, activity} ->
|
|
|
|
ActivityPub.notify_and_stream(activity)
|
|
|
|
ok
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end
|
2017-03-21 12:22:05 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def insert_list(times, data \\ %{}, opts \\ %{}) do
|
2018-05-04 13:59:01 -07:00
|
|
|
Enum.map(1..times, fn _n ->
|
2018-02-25 08:48:31 -08:00
|
|
|
{:ok, activity} = insert(data, opts)
|
2017-03-21 12:22:05 -07:00
|
|
|
activity
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_and_non_public do
|
2017-04-16 06:28:28 -07:00
|
|
|
user = Pleroma.Factory.insert(:user)
|
2017-03-21 12:22:05 -07:00
|
|
|
|
|
|
|
public = build(%{"id" => 1}, %{user: user})
|
2018-02-25 08:48:31 -08:00
|
|
|
non_public = build(%{"id" => 2, "to" => [user.follower_address]}, %{user: user})
|
2017-03-21 09:53:20 -07:00
|
|
|
|
|
|
|
{:ok, public} = ActivityPub.insert(public)
|
|
|
|
{:ok, non_public} = ActivityPub.insert(non_public)
|
|
|
|
|
|
|
|
%{
|
|
|
|
public: public,
|
|
|
|
non_public: non_public,
|
|
|
|
user: user
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|