From fe2909fbb22ee75d035f532db0b28b4415cf1638 Mon Sep 17 00:00:00 2001 From: Cora Grant Date: Mon, 19 Aug 2024 14:38:49 -0400 Subject: [PATCH] refactor: remove unused audio component/endpoint Nothing seems to be calling this endpoint in production, and the only reference to it in the code is this `Audio` component, which itself is not imported anywhere. Since this was the only case where we sent Polly requests using plain text instead of SSML, the parameter for text type can also be removed. --- assets/src/components/v2/audio.tsx | 40 ------------------- lib/screens/audio.ex | 6 +-- .../controllers/audio_controller.ex | 2 +- .../controllers/v2/audio_controller.ex | 12 +----- lib/screens_web/router.ex | 4 -- 5 files changed, 5 insertions(+), 59 deletions(-) delete mode 100644 assets/src/components/v2/audio.tsx diff --git a/assets/src/components/v2/audio.tsx b/assets/src/components/v2/audio.tsx deleted file mode 100644 index 0e30a9253..000000000 --- a/assets/src/components/v2/audio.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import React, { useEffect } from "react"; -import { ComponentType, useRef } from "react"; - -interface Props { - text: string; - volume: number; -} - -const Audio: ComponentType = ({ text, volume = 1 }) => { - const ref = useRef(null as HTMLAudioElement | null); - - useEffect(() => { - fetchAudio("test"); - }, [text]); - - const fetchAudio = (text) => { - try { - fetch("/v2/audio/text_to_speech/" + text) - .then((response) => response.blob()) - .then((blob) => { - const url = URL.createObjectURL(blob); - if (ref?.current instanceof HTMLAudioElement) { - ref.current.src = url; - ref.current.volume = volume; - ref.current.play(); - } - }); - } catch (err) { - console.log(err); - } - }; - - return ( -
-
- ); -}; - -export default Audio; diff --git a/lib/screens/audio.ex b/lib/screens/audio.ex index 8c1599a1a..456c1445b 100644 --- a/lib/screens/audio.ex +++ b/lib/screens/audio.ex @@ -20,11 +20,11 @@ defmodule Screens.Audio do @type departure_group :: {departure_group_key(), [map()]} - @spec synthesize(String.t(), String.t(), keyword()) :: {:ok, binary()} | :error - def synthesize(ssml_string, text_type, log_meta) do + @spec synthesize(String.t(), keyword()) :: {:ok, binary()} | :error + def synthesize(ssml_string, log_meta) do result = ssml_string - |> ExAws.Polly.synthesize_speech(lexicon_names: @lexicon_names, text_type: text_type) + |> ExAws.Polly.synthesize_speech(lexicon_names: @lexicon_names, text_type: "ssml") |> ExAws.request() case result do diff --git a/lib/screens_web/controllers/audio_controller.ex b/lib/screens_web/controllers/audio_controller.ex index 7060fdaae..4f693655d 100644 --- a/lib/screens_web/controllers/audio_controller.ex +++ b/lib/screens_web/controllers/audio_controller.ex @@ -38,7 +38,7 @@ defmodule ScreensWeb.AudioController do template_assigns <- Screens.Audio.from_api_data(data, screen_id), ssml <- render_ssml(template_assigns), {:ok, audio_data} <- - Screens.Audio.synthesize(ssml, "ssml", screen_id: screen_id, is_screen: is_screen) do + Screens.Audio.synthesize(ssml, screen_id: screen_id, is_screen: is_screen) do send_audio(conn, {:binary, audio_data}, disposition) else _ -> send_fallback_audio(conn, is_screen, screen_id, disposition) diff --git a/lib/screens_web/controllers/v2/audio_controller.ex b/lib/screens_web/controllers/v2/audio_controller.ex index c58ef3a94..5a09299d9 100644 --- a/lib/screens_web/controllers/v2/audio_controller.ex +++ b/lib/screens_web/controllers/v2/audio_controller.ex @@ -47,20 +47,10 @@ defmodule ScreensWeb.V2.AudioController do end end - def text_to_speech(conn, %{"text" => text}) do - case Screens.Audio.synthesize(text, "text", is_screen: false) do - {:ok, audio_data} -> - send_download(conn, {:binary, audio_data}, filename: "readout.mp3", disposition: :inline) - - :error -> - not_found(conn) - end - end - defp readout(conn, screen_id, real_screen?, disposition) do screen_id |> fetch_ssml() - |> Screens.Audio.synthesize("ssml", screen_id: screen_id, is_screen: real_screen?) + |> Screens.Audio.synthesize(screen_id: screen_id, is_screen: real_screen?) |> case do {:ok, audio_data} -> send_download(conn, {:binary, audio_data}, diff --git a/lib/screens_web/router.ex b/lib/screens_web/router.ex index 20cbae059..f6ba6ab9b 100644 --- a/lib/screens_web/router.ex +++ b/lib/screens_web/router.ex @@ -123,12 +123,8 @@ defmodule ScreensWeb.Router do pipe_through [:redirect_prod_http, :browser, :api] get "/:id/readout.mp3", AudioController, :show - get "/:id/volume", AudioController, :show_volume - get "/:id/debug", AudioController, :debug - - get "/text_to_speech/:text", AudioController, :text_to_speech end end