ActivityPub: Refactor create function.
This commit is contained in:
parent
38b61fddfe
commit
ae26604378
@ -1,6 +1,5 @@
|
||||
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||
alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
|
||||
alias Pleroma.Web.OStatus
|
||||
import Ecto.Query
|
||||
import Pleroma.Web.ActivityPub.Utils
|
||||
require Logger
|
||||
@ -37,7 +36,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||
end
|
||||
end
|
||||
|
||||
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
|
||||
def create(%{to: to, actor: actor, context: context, object: object} = params) do
|
||||
additional = params[:additional] || %{}
|
||||
local = !(params[:local] == false) # only accept false as false value
|
||||
published = params[:published]
|
||||
|
||||
with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
|
||||
{:ok, activity} <- insert(create_data, local),
|
||||
:ok <- maybe_federate(activity) do
|
||||
@ -247,18 +250,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||
|> Map.put("@context", "https://www.w3.org/ns/activitystreams")
|
||||
end
|
||||
|
||||
def prepare_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
|
||||
with {:ok, user} <- OStatus.find_or_make_user(data["actor"]) do
|
||||
{:ok, data}
|
||||
else
|
||||
_e -> :error
|
||||
end
|
||||
end
|
||||
|
||||
def prepare_incoming(_) do
|
||||
:error
|
||||
end
|
||||
|
||||
def publish(actor, activity) do
|
||||
remote_users = Pleroma.Web.Salmon.remote_users(activity)
|
||||
data = sanitize_outgoing_activity_data(activity.data)
|
||||
|
@ -61,7 +61,7 @@ defmodule Pleroma.Web.CommonAPI do
|
||||
cw <- data["spoiler_text"],
|
||||
object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw),
|
||||
object <- Map.put(object, "emoji", Formatter.get_emoji(status) |> Enum.reduce(%{}, fn({name, file}, acc) -> Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url}#{file}") end)) do
|
||||
res = ActivityPub.create(to, user, context, object)
|
||||
res = ActivityPub.create(%{to: to, actor: user, context: context, object: object})
|
||||
User.increase_note_count(user)
|
||||
res
|
||||
end
|
||||
|
@ -112,7 +112,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do
|
||||
# TODO: Handle this case in make_note_data
|
||||
note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
|
||||
do
|
||||
res = ActivityPub.create(to, actor, context, note, %{}, date, false)
|
||||
res = ActivityPub.create(%{to: to, actor: actor, context: context, object: note, published: date, local: false})
|
||||
User.increase_note_count(actor)
|
||||
res
|
||||
else
|
||||
|
@ -62,7 +62,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||
|
||||
describe "create activities" do
|
||||
test "removes doubled 'to' recipients" do
|
||||
{:ok, activity} = ActivityPub.create(["user1", "user1", "user2"], %User{ap_id: "1"}, "", %{})
|
||||
{:ok, activity} = ActivityPub.create(%{to: ["user1", "user1", "user2"], actor: %User{ap_id: "1"}, context: "", object: %{}})
|
||||
assert activity.data["to"] == ["user1", "user2"]
|
||||
assert activity.actor == "1"
|
||||
assert activity.recipients == ["user1", "user2"]
|
||||
|
Loading…
Reference in New Issue
Block a user