2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.JsonLD.ObjectView do
|
|
|
|
use Mobilizon.Web, :view
|
2019-03-04 17:38:30 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Addresses.Address
|
2021-10-13 10:57:54 +00:00
|
|
|
alias Mobilizon.Events.{Event, Participant, ParticipantRole}
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Posts.Post
|
2020-12-15 16:17:42 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2021-10-13 10:57:54 +00:00
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2019-03-04 17:38:30 +00:00
|
|
|
|
2021-09-27 07:41:36 +00:00
|
|
|
@spec render(String.t(), map()) :: map()
|
2020-09-02 08:00:39 +00:00
|
|
|
def render("group.json", %{group: %Actor{} = group}) do
|
|
|
|
%{
|
|
|
|
"@context" => "http://schema.org",
|
|
|
|
"@type" => "Organization",
|
|
|
|
"url" => group.url,
|
|
|
|
"name" => group.name || group.preferred_username,
|
|
|
|
"address" => render_address(group)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-04 17:38:30 +00:00
|
|
|
def render("event.json", %{event: %Event{} = event}) do
|
2020-07-09 15:24:28 +00:00
|
|
|
organizer = %{
|
|
|
|
"@type" => if(event.organizer_actor.type == :Group, do: "Organization", else: "Person"),
|
|
|
|
"name" => Actor.display_name(event.organizer_actor)
|
|
|
|
}
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2021-10-13 10:57:54 +00:00
|
|
|
organizer =
|
|
|
|
if event.organizer_actor.avatar do
|
|
|
|
Map.put(organizer, "image", event.organizer_actor.avatar.url)
|
|
|
|
else
|
|
|
|
organizer
|
|
|
|
end
|
|
|
|
|
2019-03-04 17:38:30 +00:00
|
|
|
json_ld = %{
|
|
|
|
"@context" => "https://schema.org",
|
|
|
|
"@type" => "Event",
|
|
|
|
"name" => event.title,
|
|
|
|
"description" => event.description,
|
2020-07-09 15:24:28 +00:00
|
|
|
# We assume for now performer == organizer
|
|
|
|
"performer" => organizer,
|
|
|
|
"organizer" => organizer,
|
2020-08-10 14:22:15 +00:00
|
|
|
"location" => render_location(event),
|
2020-07-09 15:24:28 +00:00
|
|
|
"eventStatus" =>
|
|
|
|
if(event.status == :cancelled,
|
|
|
|
do: "https://schema.org/EventCancelled",
|
|
|
|
else: "https://schema.org/EventScheduled"
|
2020-09-02 15:42:17 +00:00
|
|
|
),
|
|
|
|
"image" =>
|
|
|
|
if(event.picture,
|
|
|
|
do: [
|
2020-12-15 16:17:42 +00:00
|
|
|
event.picture.file.url
|
2020-09-02 15:42:17 +00:00
|
|
|
],
|
|
|
|
else: ["#{Endpoint.url()}/img/mobilizon_default_card.png"]
|
2020-07-09 15:24:28 +00:00
|
|
|
)
|
2019-03-04 17:38:30 +00: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,
|
2020-09-02 08:00:39 +00:00
|
|
|
"address" => render_one(address, ObjectView, "address.json", as: :address)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("address.json", %{address: %Address{} = address}) do
|
|
|
|
%{
|
|
|
|
"@type" => "PostalAddress",
|
|
|
|
"streetAddress" => address.street,
|
|
|
|
"addressLocality" => address.locality,
|
|
|
|
"postalCode" => address.postal_code,
|
|
|
|
"addressRegion" => address.region,
|
|
|
|
"addressCountry" => address.country
|
2019-03-04 17:38:30 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00: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
|
2020-08-10 14:22:15 +00:00
|
|
|
|
2021-10-13 10:57:54 +00:00
|
|
|
def render("participation.json", %{
|
|
|
|
participant: %Participant{} = participant
|
|
|
|
}) do
|
|
|
|
res = %{
|
|
|
|
"@context" => "http://schema.org",
|
|
|
|
"@type" => "EventReservation",
|
|
|
|
"underName" => %{
|
|
|
|
"@type" => "Person",
|
|
|
|
"name" => participant.actor.name || participant.actor.preferred_username
|
|
|
|
},
|
|
|
|
"reservationFor" => render("event.json", %{event: participant.event}),
|
|
|
|
"reservationStatus" => reservation_status(participant.role),
|
|
|
|
"modifiedTime" => participant.updated_at,
|
|
|
|
"modifyReservationUrl" => Routes.page_url(Endpoint, :event, participant.event.uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if participant.code do
|
|
|
|
Map.put(res, "reservationNumber", participant.code)
|
|
|
|
else
|
|
|
|
res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec reservation_status(ParticipantRole.t()) :: String.t()
|
|
|
|
defp reservation_status(:rejected), do: "https://schema.org/ReservationCancelled"
|
|
|
|
defp reservation_status(:not_confirmed), do: "https://schema.org/ReservationPending"
|
|
|
|
defp reservation_status(:not_approved), do: "https://schema.org/ReservationHold"
|
|
|
|
defp reservation_status(_), do: "https://schema.org/ReservationConfirmed"
|
|
|
|
|
2021-09-27 07:41:36 +00:00
|
|
|
@spec render_location(map()) :: map() | nil
|
2020-09-02 08:00:39 +00:00
|
|
|
defp render_location(%{physical_address: %Address{} = address}),
|
2020-08-10 14:22:15 +00:00
|
|
|
do: render_one(address, ObjectView, "place.json", as: :address)
|
|
|
|
|
|
|
|
# For now the Virtual Location of an event is it's own URL,
|
|
|
|
# but in the future it will be a special field
|
|
|
|
defp render_location(%Event{url: event_url}) do
|
|
|
|
%{
|
|
|
|
"@type" => "VirtualLocation",
|
|
|
|
"url" => event_url
|
|
|
|
}
|
|
|
|
end
|
2020-09-02 08:00:39 +00:00
|
|
|
|
|
|
|
defp render_location(_), do: nil
|
|
|
|
|
|
|
|
defp render_address(%{physical_address: %Address{} = address}),
|
|
|
|
do: render_one(address, ObjectView, "address.json", as: :address)
|
|
|
|
|
|
|
|
defp render_address(_), do: nil
|
2019-03-04 17:38:30 +00:00
|
|
|
end
|