2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.JsonLD.ObjectView do
|
|
|
|
use Mobilizon.Web, :view
|
2019-03-04 18:38:30 +01:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Addresses.Address
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Posts.Post
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
|
|
|
alias Mobilizon.Web.MediaProxy
|
2019-03-04 18:38:30 +01:00
|
|
|
|
|
|
|
def render("event.json", %{event: %Event{} = event}) do
|
2020-07-09 17:24:28 +02:00
|
|
|
organizer = %{
|
|
|
|
"@type" => if(event.organizer_actor.type == :Group, do: "Organization", else: "Person"),
|
|
|
|
"name" => Actor.display_name(event.organizer_actor)
|
|
|
|
}
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2019-03-04 18:38:30 +01:00
|
|
|
json_ld = %{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "Event",
|
|
|
|
"name" => event.title,
|
|
|
|
"description" => event.description,
|
2020-07-09 17:24:28 +02:00
|
|
|
# We assume for now performer == organizer
|
|
|
|
"performer" => organizer,
|
|
|
|
"organizer" => organizer,
|
|
|
|
"location" => render_one(event.physical_address, ObjectView, "place.json", as: :address),
|
|
|
|
"eventStatus" =>
|
|
|
|
if(event.status == :cancelled,
|
|
|
|
do: "https://schema.org/EventCancelled",
|
|
|
|
else: "https://schema.org/EventScheduled"
|
|
|
|
)
|
2019-03-04 18:38:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
json_ld =
|
|
|
|
if event.picture do
|
|
|
|
Map.put(json_ld, "image", [
|
2019-05-28 10:51:02 +02:00
|
|
|
event.picture.file.url |> MediaProxy.url()
|
2019-05-22 14:12:11 +02:00
|
|
|
])
|
|
|
|
else
|
|
|
|
json_ld
|
|
|
|
end
|
|
|
|
|
2019-03-04 18:38:30 +01:00
|
|
|
json_ld =
|
|
|
|
if event.begins_on,
|
|
|
|
do: Map.put(json_ld, "startDate", DateTime.to_iso8601(event.begins_on)),
|
|
|
|
else: json_ld
|
|
|
|
|
|
|
|
json_ld =
|
|
|
|
if event.ends_on,
|
|
|
|
do: Map.put(json_ld, "endDate", DateTime.to_iso8601(event.ends_on)),
|
|
|
|
else: json_ld
|
|
|
|
|
|
|
|
json_ld
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("place.json", %{address: %Address{} = address}) do
|
|
|
|
%{
|
|
|
|
"@type" => "Place",
|
|
|
|
"name" => address.description,
|
|
|
|
"address" => %{
|
|
|
|
"@type" => "PostalAddress",
|
2019-03-22 15:51:23 +01:00
|
|
|
"streetAddress" => address.street,
|
|
|
|
"addressLocality" => address.locality,
|
|
|
|
"postalCode" => address.postal_code,
|
|
|
|
"addressRegion" => address.region,
|
|
|
|
"addressCountry" => address.country
|
2019-03-04 18:38:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("place.json", nil), do: %{}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
def render("post.json", %{post: %Post{} = post}) do
|
|
|
|
%{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "Article",
|
|
|
|
"name" => post.title,
|
|
|
|
"author" => %{
|
|
|
|
"@type" => "Organization",
|
|
|
|
"name" => Actor.display_name(post.attributed_to)
|
|
|
|
},
|
|
|
|
"datePublished" => post.publish_at,
|
|
|
|
"dateModified" => post.updated_at
|
|
|
|
}
|
|
|
|
end
|
2019-03-04 18:38:30 +01:00
|
|
|
end
|