From def4fd9a1545d67ddd87c83d4dff6861ad543faf Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Fri, 25 Sep 2020 15:52:48 +0200 Subject: [PATCH] Move Utils json-ld header and date to Builder --- lib/pleroma/web/activity_pub/builder.ex | 26 +++++++++++++++++---- lib/pleroma/web/activity_pub/transmogrifier.ex | 10 ++++---- lib/pleroma/web/activity_pub/utils.ex | 27 +++++----------------- lib/pleroma/web/activity_pub/views/object_view.ex | 7 +++--- lib/pleroma/web/activity_pub/views/user_view.ex | 20 ++++++++-------- .../web/activity_pub/transmogrifier_test.exs | 3 ++- test/pleroma/web/activity_pub/utils_test.exs | 12 ---------- 7 files changed, 48 insertions(+), 57 deletions(-) diff --git a/lib/pleroma/web/activity_pub/builder.ex b/lib/pleroma/web/activity_pub/builder.ex index f54d3c575..7d52f6c4c 100644 --- a/lib/pleroma/web/activity_pub/builder.ex +++ b/lib/pleroma/web/activity_pub/builder.ex @@ -120,7 +120,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do "to" => recipients, "object" => object, "type" => "Create", - "published" => DateTime.utc_now() |> DateTime.to_iso8601() + "published" => date() } |> Pleroma.Maps.put_if_present("context", context), []} end @@ -132,7 +132,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do "type" => "ChatMessage", "to" => [recipient], "content" => content, - "published" => DateTime.utc_now() |> DateTime.to_iso8601(), + "published" => date(), "emoji" => Emoji.Formatter.get_emoji_map(content) } @@ -160,7 +160,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do "name" => name, "inReplyTo" => object.data["id"], "context" => object.data["context"], - "published" => DateTime.utc_now() |> DateTime.to_iso8601(), + "published" => date(), "id" => Utils.generate_object_id() }, []} end @@ -240,7 +240,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do "to" => to, "context" => object.data["context"], "type" => "Announce", - "published" => Utils.make_date() + "published" => date() }, []} end @@ -313,9 +313,25 @@ defmodule Pleroma.Web.ActivityPub.Builder do "to" => params.to |> Enum.uniq(), "actor" => params.actor.ap_id, "object" => params.object, - "published" => params.published || Utils.make_date(), + "published" => params.published || date(), "context" => params.context } |> Map.merge(additional) end + + def json_ld_header do + %{ + "@context" => [ + "https://www.w3.org/ns/activitystreams", + "#{Pleroma.Web.Endpoint.url()}/schemas/litepub-0.1.jsonld", + %{ + "@language" => "und" + } + ] + } + end + + def date do + DateTime.utc_now() |> DateTime.to_iso8601() + end end diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index f7c0343ec..d1d5a3184 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -685,7 +685,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do data = data |> Map.put("object", object) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) |> Map.delete("bcc") {:ok, data} @@ -706,7 +706,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do data = data |> strip_internal_fields - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) |> Map.delete("bcc") {:ok, data} @@ -726,7 +726,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do data = data |> Map.put("object", object) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) {:ok, data} end @@ -744,7 +744,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do data = data |> Map.put("object", object) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) {:ok, data} end @@ -755,7 +755,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do data |> strip_internal_fields |> maybe_fix_object_url - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) {:ok, data} end diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 0b1cb464e..170fc38c6 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -13,6 +13,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do alias Pleroma.Repo alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Builder alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.AdminAPI.AccountView alias Pleroma.Web.Endpoint @@ -107,22 +108,6 @@ defmodule Pleroma.Web.ActivityPub.Utils do end end - def make_json_ld_header do - %{ - "@context" => [ - "https://www.w3.org/ns/activitystreams", - "#{Endpoint.url()}/schemas/litepub-0.1.jsonld", - %{ - "@language" => "und" - } - ] - } - end - - def make_date do - DateTime.utc_now() |> DateTime.to_iso8601() - end - def generate_activity_id do generate_id("activities") end @@ -199,7 +184,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do def lazy_put_activity_defaults(map, true) do map |> Map.put_new("id", "pleroma:fakeid") - |> Map.put_new_lazy("published", &make_date/0) + |> Map.put_new_lazy("published", &Builder.date/0) |> Map.put_new("context", "pleroma:fakecontext") |> Map.put_new("context_id", -1) |> lazy_put_object_defaults(true) @@ -210,7 +195,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do map |> Map.put_new_lazy("id", &generate_activity_id/0) - |> Map.put_new_lazy("published", &make_date/0) + |> Map.put_new_lazy("published", &Builder.date/0) |> Map.put_new("context", context) |> Map.put_new("context_id", context_id) |> lazy_put_object_defaults(false) @@ -224,7 +209,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do object = map |> Map.put_new("id", "pleroma:fake_object_id") - |> Map.put_new_lazy("published", &make_date/0) + |> Map.put_new_lazy("published", &Builder.date/0) |> Map.put_new("context", activity["context"]) |> Map.put_new("context_id", activity["context_id"]) |> Map.put_new("fake", true) @@ -237,7 +222,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do object = map |> Map.put_new_lazy("id", &generate_object_id/0) - |> Map.put_new_lazy("published", &make_date/0) + |> Map.put_new_lazy("published", &Builder.date/0) |> Map.put_new("context", activity["context"]) |> Map.put_new("context_id", activity["context_id"]) @@ -662,7 +647,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do #### Create-related helpers def make_create_data(params, additional) do - published = params.published || make_date() + published = params.published || Builder.date() %{ "type" => "Create", diff --git a/lib/pleroma/web/activity_pub/views/object_view.ex b/lib/pleroma/web/activity_pub/views/object_view.ex index 8a3e4d77b..f0a81b904 100644 --- a/lib/pleroma/web/activity_pub/views/object_view.ex +++ b/lib/pleroma/web/activity_pub/views/object_view.ex @@ -6,10 +6,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do use Pleroma.Web, :view alias Pleroma.Activity alias Pleroma.Object + alias Pleroma.Web.ActivityPub.Builder alias Pleroma.Web.ActivityPub.Transmogrifier def render("object.json", %{object: %Object{} = object}) do - base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header() + base = Builder.json_ld_header() additional = Transmogrifier.prepare_object(object.data) Map.merge(base, additional) @@ -17,7 +18,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do def render("object.json", %{object: %Activity{data: %{"type" => activity_type}} = activity}) when activity_type in ["Create", "Listen"] do - base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header() + base = Builder.json_ld_header() object = Object.normalize(activity, fetch: false) additional = @@ -28,7 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do end def render("object.json", %{object: %Activity{} = activity}) do - base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header() + base = Builder.json_ld_header() object = Object.normalize(activity, fetch: false) additional = diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 344da19d3..8bfcf1ace 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -9,9 +9,9 @@ defmodule Pleroma.Web.ActivityPub.UserView do alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User + alias Pleroma.Web.ActivityPub.Builder alias Pleroma.Web.ActivityPub.ObjectView alias Pleroma.Web.ActivityPub.Transmogrifier - alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.Endpoint alias Pleroma.Web.Router.Helpers @@ -60,7 +60,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "endpoints" => endpoints, "invisible" => User.invisible?(user) } - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end # the instance itself is not a Person, but instead an Application @@ -120,7 +120,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do } |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user)) |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user)) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end def render("following.json", %{user: user, page: page} = opts) do @@ -139,7 +139,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do end collection(following, "#{user.ap_id}/following", page, showing_items, total) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end def render("following.json", %{user: user} = opts) do @@ -168,7 +168,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "#{user.ap_id}/following?page=1" end } - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end def render("followers.json", %{user: user, page: page} = opts) do @@ -187,7 +187,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do end collection(followers, "#{user.ap_id}/followers", page, showing_items, total) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end def render("followers.json", %{user: user} = opts) do @@ -216,7 +216,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do end } |> maybe_put_total_items(showing_count, total) - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end def render("activity_collection.json", %{iri: iri}) do @@ -225,7 +225,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "type" => "OrderedCollection", "first" => "#{iri}?page=true" } - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end def render("activity_collection_page.json", %{ @@ -244,7 +244,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "partOf" => iri, "orderedItems" => collection } - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) |> Map.merge(pagination) end @@ -264,7 +264,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "orderedItems" => objects, "totalItems" => length(objects) } - |> Map.merge(Utils.make_json_ld_header()) + |> Map.merge(Builder.json_ld_header()) end defp maybe_put_total_items(map, false, _total), do: map diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 5a3b57acb..b5974d8f6 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -10,6 +10,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do alias Pleroma.Object alias Pleroma.Tests.ObanHelpers alias Pleroma.User + alias Pleroma.Web.ActivityPub.Builder alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.AdminAPI.AccountView @@ -160,7 +161,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do {:ok, activity} = CommonAPI.post(user, %{status: "hey"}) {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) - assert modified["@context"] == Utils.make_json_ld_header()["@context"] + assert modified["@context"] == Builder.json_ld_header()["@context"] assert modified["object"]["conversation"] == modified["context"] end diff --git a/test/pleroma/web/activity_pub/utils_test.exs b/test/pleroma/web/activity_pub/utils_test.exs index ee3e1014e..41d1d9a74 100644 --- a/test/pleroma/web/activity_pub/utils_test.exs +++ b/test/pleroma/web/activity_pub/utils_test.exs @@ -138,18 +138,6 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do end end - test "make_json_ld_header/0" do - assert Utils.make_json_ld_header() == %{ - "@context" => [ - "https://www.w3.org/ns/activitystreams", - "http://localhost:4001/schemas/litepub-0.1.jsonld", - %{ - "@language" => "und" - } - ] - } - end - describe "get_existing_votes" do test "fetches existing votes" do user = insert(:user)