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, "to" => recipients,
"object" => object, "object" => object,
"type" => "Create", "type" => "Create",
"published" => DateTime.utc_now() |> DateTime.to_iso8601() "published" => date()
} }
|> Pleroma.Maps.put_if_present("context", context), []} |> Pleroma.Maps.put_if_present("context", context), []}
end end
@ -132,7 +132,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
"type" => "ChatMessage", "type" => "ChatMessage",
"to" => [recipient], "to" => [recipient],
"content" => content, "content" => content,
"published" => DateTime.utc_now() |> DateTime.to_iso8601(), "published" => date(),
"emoji" => Emoji.Formatter.get_emoji_map(content) "emoji" => Emoji.Formatter.get_emoji_map(content)
} }
@ -160,7 +160,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
"name" => name, "name" => name,
"inReplyTo" => object.data["id"], "inReplyTo" => object.data["id"],
"context" => object.data["context"], "context" => object.data["context"],
"published" => DateTime.utc_now() |> DateTime.to_iso8601(), "published" => date(),
"id" => Utils.generate_object_id() "id" => Utils.generate_object_id()
}, []} }, []}
end end
@ -240,7 +240,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
"to" => to, "to" => to,
"context" => object.data["context"], "context" => object.data["context"],
"type" => "Announce", "type" => "Announce",
"published" => Utils.make_date() "published" => date()
}, []} }, []}
end end
@ -313,9 +313,25 @@ defmodule Pleroma.Web.ActivityPub.Builder do
"to" => params.to |> Enum.uniq(), "to" => params.to |> Enum.uniq(),
"actor" => params.actor.ap_id, "actor" => params.actor.ap_id,
"object" => params.object, "object" => params.object,
"published" => params.published || Utils.make_date(), "published" => params.published || date(),
"context" => params.context "context" => params.context
} }
|> Map.merge(additional) |> Map.merge(additional)
end 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 end

View File

@ -685,7 +685,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
data = data =
data data
|> Map.put("object", object) |> Map.put("object", object)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
|> Map.delete("bcc") |> Map.delete("bcc")
{:ok, data} {:ok, data}
@ -706,7 +706,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
data = data =
data data
|> strip_internal_fields |> strip_internal_fields
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
|> Map.delete("bcc") |> Map.delete("bcc")
{:ok, data} {:ok, data}
@ -726,7 +726,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
data = data =
data data
|> Map.put("object", object) |> Map.put("object", object)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
{:ok, data} {:ok, data}
end end
@ -744,7 +744,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
data = data =
data data
|> Map.put("object", object) |> Map.put("object", object)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
{:ok, data} {:ok, data}
end end
@ -755,7 +755,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
data data
|> strip_internal_fields |> strip_internal_fields
|> maybe_fix_object_url |> maybe_fix_object_url
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
{:ok, data} {:ok, data}
end end

View File

@ -13,6 +13,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Builder
alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Web.AdminAPI.AccountView alias Pleroma.Web.AdminAPI.AccountView
alias Pleroma.Web.Endpoint alias Pleroma.Web.Endpoint
@ -107,22 +108,6 @@ defmodule Pleroma.Web.ActivityPub.Utils do
end end
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 def generate_activity_id do
generate_id("activities") generate_id("activities")
end end
@ -199,7 +184,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
def lazy_put_activity_defaults(map, true) do def lazy_put_activity_defaults(map, true) do
map map
|> Map.put_new("id", "pleroma:fakeid") |> 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", "pleroma:fakecontext")
|> Map.put_new("context_id", -1) |> Map.put_new("context_id", -1)
|> lazy_put_object_defaults(true) |> lazy_put_object_defaults(true)
@ -210,7 +195,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
map map
|> Map.put_new_lazy("id", &generate_activity_id/0) |> 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", context)
|> Map.put_new("context_id", context_id) |> Map.put_new("context_id", context_id)
|> lazy_put_object_defaults(false) |> lazy_put_object_defaults(false)
@ -224,7 +209,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
object = object =
map map
|> Map.put_new("id", "pleroma:fake_object_id") |> 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", activity["context"])
|> Map.put_new("context_id", activity["context_id"]) |> Map.put_new("context_id", activity["context_id"])
|> Map.put_new("fake", true) |> Map.put_new("fake", true)
@ -237,7 +222,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
object = object =
map map
|> Map.put_new_lazy("id", &generate_object_id/0) |> 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", activity["context"])
|> Map.put_new("context_id", activity["context_id"]) |> Map.put_new("context_id", activity["context_id"])
@ -662,7 +647,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
#### Create-related helpers #### Create-related helpers
def make_create_data(params, additional) do def make_create_data(params, additional) do
published = params.published || make_date() published = params.published || Builder.date()
%{ %{
"type" => "Create", "type" => "Create",

View File

@ -6,10 +6,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Web.ActivityPub.Builder
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
def render("object.json", %{object: %Object{} = object}) do 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) additional = Transmogrifier.prepare_object(object.data)
Map.merge(base, additional) 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}) def render("object.json", %{object: %Activity{data: %{"type" => activity_type}} = activity})
when activity_type in ["Create", "Listen"] do 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) object = Object.normalize(activity, fetch: false)
additional = additional =
@ -28,7 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
end end
def render("object.json", %{object: %Activity{} = activity}) do 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) object = Object.normalize(activity, fetch: false)
additional = additional =

View File

@ -9,9 +9,9 @@ defmodule Pleroma.Web.ActivityPub.UserView do
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.Builder
alias Pleroma.Web.ActivityPub.ObjectView alias Pleroma.Web.ActivityPub.ObjectView
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.Endpoint alias Pleroma.Web.Endpoint
alias Pleroma.Web.Router.Helpers alias Pleroma.Web.Router.Helpers
@ -60,7 +60,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"endpoints" => endpoints, "endpoints" => endpoints,
"invisible" => User.invisible?(user) "invisible" => User.invisible?(user)
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
# the instance itself is not a Person, but instead an Application # 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.avatar_url/2, "icon", user))
|> Map.merge(maybe_make_image(&User.banner_url/2, "image", 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 end
def render("following.json", %{user: user, page: page} = opts) do def render("following.json", %{user: user, page: page} = opts) do
@ -139,7 +139,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
end end
collection(following, "#{user.ap_id}/following", page, showing_items, total) collection(following, "#{user.ap_id}/following", page, showing_items, total)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
def render("following.json", %{user: user} = opts) do def render("following.json", %{user: user} = opts) do
@ -168,7 +168,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"#{user.ap_id}/following?page=1" "#{user.ap_id}/following?page=1"
end end
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
def render("followers.json", %{user: user, page: page} = opts) do def render("followers.json", %{user: user, page: page} = opts) do
@ -187,7 +187,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
end end
collection(followers, "#{user.ap_id}/followers", page, showing_items, total) collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
def render("followers.json", %{user: user} = opts) do def render("followers.json", %{user: user} = opts) do
@ -216,7 +216,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
end end
} }
|> maybe_put_total_items(showing_count, total) |> maybe_put_total_items(showing_count, total)
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
def render("activity_collection.json", %{iri: iri}) do def render("activity_collection.json", %{iri: iri}) do
@ -225,7 +225,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"type" => "OrderedCollection", "type" => "OrderedCollection",
"first" => "#{iri}?page=true" "first" => "#{iri}?page=true"
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
def render("activity_collection_page.json", %{ def render("activity_collection_page.json", %{
@ -244,7 +244,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"partOf" => iri, "partOf" => iri,
"orderedItems" => collection "orderedItems" => collection
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
|> Map.merge(pagination) |> Map.merge(pagination)
end end
@ -264,7 +264,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"orderedItems" => objects, "orderedItems" => objects,
"totalItems" => length(objects) "totalItems" => length(objects)
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Builder.json_ld_header())
end end
defp maybe_put_total_items(map, false, _total), do: map 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.Object
alias Pleroma.Tests.ObanHelpers alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.Builder
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.AdminAPI.AccountView alias Pleroma.Web.AdminAPI.AccountView
@ -160,7 +161,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
{:ok, activity} = CommonAPI.post(user, %{status: "hey"}) {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
{:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) {: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"] assert modified["object"]["conversation"] == modified["context"]
end end

View File

@ -138,18 +138,6 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
end end
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 describe "get_existing_votes" do
test "fetches existing votes" do test "fetches existing votes" do
user = insert(:user) user = insert(:user)