Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds image and image_alternative_text fields to Alerts #670

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions apps/api_web/lib/api_web/controllers/alert_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ defmodule ApiWeb.AlertController do
"""
)

image(
nullable(%Schema{type: :string}, true),
"URL of an image to be displayed alongside alert.",
example: "http://example.com/alert_image.png"
)

image_alternative_text(
nullable(%Schema{type: :string}, true),
"Text describing the appearance of the linked image in the image field.",
example: "Shuttle service beginning at North Quincy and ending at Braintree"
)

informed_entity(
%Schema{
items: Schema.ref(:InformedEntity),
Expand Down Expand Up @@ -525,23 +537,27 @@ defmodule ApiWeb.AlertController do
`#{parent_pointer}/attributes/service_effect`) on a provided service (facility, route, route type, stop and/or \
trip in `/#{parent_pointer}/attributes/informed_entity`) described by a banner \
(`#{parent_pointer}/attributes/banner`), short header (`#{parent_pointer}/attributes/short_header`), header \
`#{parent_pointer}/attributes/header`, and description (`#{parent_pointer}/attributes/description`) that is active \
for one or more periods (`#{parent_pointer}/attributes/active_period`) caused by a cause \
`#{parent_pointer}/attributes/header`, description (`#{parent_pointer}/attributes/description`), \
image (`#{parent_pointer}/attributes/image`), and image alternative text \
(`#{parent_pointer}/attributes/image_alternative_text`) that is active for one or more periods\
(`#{parent_pointer}/attributes/active_period`) caused by a cause \
(`#{parent_pointer}/attribute/cause`) that somewhere in its lifecycle (enumerated in \
`#{parent_pointer}/attributes/lifecycle` and human-readable in `#{parent_pointer}/attributes/timeframe`).

See [GTFS Realtime `FeedMessage` `FeedEntity` `Alert`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-alert)

## Descriptions

There are 5 descriptive attributes.
There are 7 descriptive attributes.

| JSON pointer | Usage |
|---------------------------------------------|---------------------------------------------------------------------------------|
| `#{parent_pointer}/attributes/banner` | Display as alert across application/website |
| `#{parent_pointer}/attributes/short_header` | When `#{parent_pointer}/attributes/header` is too long to display |
| `#{parent_pointer}/attributes/header` | Used before showing and prepended to `#{parent_pointer}/attributes/description` |
| `#{parent_pointer}/attributes/description` | Used when user asks to expand alert. |
| `#{parent_pointer}/attributes/image` | URL to descriptive image. |
| `#{parent_pointer}/attributes/image_alternative_text` | Text that describes image linked in url |

## Effect

Expand Down
4 changes: 3 additions & 1 deletion apps/api_web/lib/api_web/views/alert_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ defmodule ApiWeb.AlertView do
:timeframe,
:lifecycle,
:banner,
:url
:url,
:image,
:image_alternative_text
])

def active_period(%{active_period: periods}, _conn) do
Expand Down
6 changes: 5 additions & 1 deletion apps/api_web/test/api_web/views/alert_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ defmodule ApiWeb.AlertViewTest do
informed_entity: [%{route_type: 0, route: "1"}],
service_effect: "service effect",
timeframe: "timeframe",
lifecycle: "lifecycle"
lifecycle: "lifecycle",
image: "image",
image_alternative_text: "image alternative text"
}

test "can do a basic rendering (does not include relationships)", %{conn: conn} do
Expand All @@ -39,6 +41,8 @@ defmodule ApiWeb.AlertViewTest do
"created_at" => @alert.created_at,
"updated_at" => @alert.updated_at,
"severity" => @alert.severity,
"image" => @alert.image,
"image_alternative_text" => @alert.image_alternative_text,
"active_period" => [
%{
"start" => @alert.active_period |> List.first() |> elem(0),
Expand Down
8 changes: 8 additions & 0 deletions apps/model/lib/model/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ defmodule Model.Alert do
:timeframe,
:lifecycle,
:banner,
:image,
:image_alternative_text,
active_period: [],
informed_entity: []
]
Expand Down Expand Up @@ -232,6 +234,10 @@ defmodule Model.Alert do
[GTFS Realtime `FeedMessage` `FeedEntity` `Alert` `effect`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-alert)
* `:header` - This plain-text string will be highlighted, for example in boldface. See
[GTFS Realtime `FeedMessage` `FeedEntity` `Alert` `header_text`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-alert)
* `:image` - A link to an image to be displayed along with the alert text. See
[GTFS Realtime `FeedMessage` `FeedEntity` `Alert` `image`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-alert)
* `:image_alternative_text` - Text to be displayed along with the image. See
[GTFS Realtime `FeedMessage` `FeedEntity` `Alert` `image_alternative_text`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-alert)
* `:informed_entity` - Entities whose users we should notify of this alert. See
[GTFS Realtime `FeedMessage` `FeedEntity` `Alert` `informed_entity`](https://github.com/google/transit/blob/master/gtfs-realtime/spec/en/reference.md#message-alert)
* `:lifecycle` - Enumeration of where the alert is in its lifecycle. See `t:lifecycle/0`.
Expand All @@ -251,6 +257,8 @@ defmodule Model.Alert do
description: String.t(),
effect: effect,
header: String.t(),
image: String.t() | nil,
image_alternative_text: String.t() | nil,
informed_entity: [informed_entity],
lifecycle: lifecycle,
service_effect: String.t(),
Expand Down
38 changes: 37 additions & 1 deletion apps/parse/lib/parse/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ defmodule Parse.Alerts do
service_effect: alert |> Map.get("service_effect_text") |> translated_text,
timeframe: alert |> Map.get("timeframe_text") |> translated_text(default: nil),
lifecycle: alert |> Map.get("alert_lifecycle") |> lifecycle,
url: alert |> Map.get("url") |> translated_text(default: nil)
url: alert |> Map.get("url") |> translated_text(default: nil),
image: alert |> Map.get("image") |> translated_image(default: nil),
image_alternative_text:
alert |> Map.get("image_alternative_text") |> translated_text(default: nil)
}
end

Expand Down Expand Up @@ -112,6 +115,39 @@ defmodule Parse.Alerts do
do_translated_text(rest, opts)
end

defp translated_image(localizations, opts) do
opts =
opts
|> Map.new()
|> Map.put_new(:default, "")

do_localized_image(localizations, opts)
end

defp do_localized_image([], %{default: default}) do
default
end

defp do_localized_image(nil, %{default: default}) do
default
end

defp do_localized_image(%{"localized_image" => [%{"url" => url}]}, _) do
paulswartz marked this conversation as resolved.
Show resolved Hide resolved
copy(url)
end

defp do_localized_image(%{"localized_image" => [_ | _] = translations}, %{default: default}) do
translations =
translations
|> Enum.filter(&(&1["language"] == "en" or &1["language"] == nil))
|> Enum.sort(:desc)

case length(translations) >= 1 do
true -> hd(translations)["url"]
false -> default
end
end

defp active_period(%{"start" => start, "end" => stop}) do
{unix_timestamp(start), unix_timestamp(stop)}
end
Expand Down
Loading
Loading