From ae266043787ca4b9bcbe5162f12598286a44cae2 Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 15 Feb 2018 19:59:03 +0100 Subject: [PATCH] ActivityPub: Refactor create function. --- lib/pleroma/web/activity_pub/activity_pub.ex | 19 +++++-------------- lib/pleroma/web/common_api/common_api.ex | 2 +- .../web/ostatus/handlers/note_handler.ex | 2 +- test/web/activity_pub/activity_pub_test.exs | 2 +- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 6e29768d1..a7b2988b9 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -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 @@ def stream_out(activity) 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 @@ def sanitize_outgoing_activity_data(data) 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) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index d3a9f7b85..f3060bd89 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -61,7 +61,7 @@ def post(user, %{"status" => status} = data) 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 diff --git a/lib/pleroma/web/ostatus/handlers/note_handler.ex b/lib/pleroma/web/ostatus/handlers/note_handler.ex index 8747dbb67..7b7ed2d5a 100644 --- a/lib/pleroma/web/ostatus/handlers/note_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/note_handler.ex @@ -112,7 +112,7 @@ def handle_note(entry, doc \\ nil) 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 diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 01e5362ec..4817a9f38 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -62,7 +62,7 @@ test "adds an id to a given object if it lacks one and inserts it to the object 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"]