Move Utils json-ld header and date to Builder

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-09-25 15:52:48 +02:00
parent bb5cc8b390
commit def4fd9a15
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
7 changed files with 48 additions and 57 deletions

View File

@ -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

View File

@ -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

View File

@ -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",

View File

@ -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 =

View File

@ -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

View File

@ -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

View File

@ -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)