ActivityPub: Refactor create function.

This commit is contained in:
lain 2018-02-15 19:59:03 +01:00
parent 38b61fddfe
commit ae26604378
4 changed files with 8 additions and 17 deletions

View File

@ -1,6 +1,5 @@
defmodule Pleroma.Web.ActivityPub.ActivityPub do defmodule Pleroma.Web.ActivityPub.ActivityPub do
alias Pleroma.{Activity, Repo, Object, Upload, User, Notification} alias Pleroma.{Activity, Repo, Object, Upload, User, Notification}
alias Pleroma.Web.OStatus
import Ecto.Query import Ecto.Query
import Pleroma.Web.ActivityPub.Utils import Pleroma.Web.ActivityPub.Utils
require Logger require Logger
@ -37,7 +36,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
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), with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
{:ok, activity} <- insert(create_data, local), {:ok, activity} <- insert(create_data, local),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
@ -247,18 +250,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> Map.put("@context", "https://www.w3.org/ns/activitystreams") |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
end 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 def publish(actor, activity) do
remote_users = Pleroma.Web.Salmon.remote_users(activity) remote_users = Pleroma.Web.Salmon.remote_users(activity)
data = sanitize_outgoing_activity_data(activity.data) data = sanitize_outgoing_activity_data(activity.data)

View File

@ -61,7 +61,7 @@ defmodule Pleroma.Web.CommonAPI do
cw <- data["spoiler_text"], cw <- data["spoiler_text"],
object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw), 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 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) User.increase_note_count(user)
res res
end end

View File

@ -112,7 +112,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do
# TODO: Handle this case in make_note_data # TODO: Handle this case in make_note_data
note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note) note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
do 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) User.increase_note_count(actor)
res res
else else

View File

@ -62,7 +62,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
describe "create activities" do describe "create activities" do
test "removes doubled 'to' recipients" 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.data["to"] == ["user1", "user2"]
assert activity.actor == "1" assert activity.actor == "1"
assert activity.recipients == ["user1", "user2"] assert activity.recipients == ["user1", "user2"]