From c584efc3858933eedc2781a2b55079887e09c38e Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Fri, 21 Jun 2024 20:22:33 +0200 Subject: [PATCH] feat: Expose Captions on GraphQL (#460) --- apps/cf/lib/videos/videos.ex | 3 +++ apps/cf_graphql/lib/resolvers/videos.ex | 27 ++++++++++++++++++- apps/cf_graphql/lib/schema/types.ex | 3 ++- apps/cf_graphql/lib/schema/types/video.ex | 6 +++++ .../lib/schema/types/video_caption.ex | 18 +++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 apps/cf_graphql/lib/schema/types/video_caption.ex diff --git a/apps/cf/lib/videos/videos.ex b/apps/cf/lib/videos/videos.ex index f9bee4e9..05b26302 100644 --- a/apps/cf/lib/videos/videos.ex +++ b/apps/cf/lib/videos/videos.ex @@ -76,6 +76,9 @@ defmodule CF.Videos do def get_video_by_id(id), do: Repo.get(Video, id) + def get_video_by_hash_id(hash_id), + do: Repo.get_by(Video, hash_id: hash_id) + @doc """ Add a new video. Returns video if success or {:error, reason} if something bad append. diff --git a/apps/cf_graphql/lib/resolvers/videos.ex b/apps/cf_graphql/lib/resolvers/videos.ex index f3f8fce1..376c0701 100644 --- a/apps/cf_graphql/lib/resolvers/videos.ex +++ b/apps/cf_graphql/lib/resolvers/videos.ex @@ -10,6 +10,7 @@ defmodule CF.Graphql.Resolvers.Videos do alias DB.Repo alias DB.Schema.Video + alias DB.Schema.VideoCaption alias DB.Schema.Statement # Queries @@ -22,7 +23,7 @@ defmodule CF.Graphql.Resolvers.Videos do end def get(_root, %{hash_id: id}, _info) do - case CF.Videos.get_video_by_id(id) do + case CF.Videos.get_video_by_hash_id(id) do nil -> {:error, "Video #{id} doesn't exist"} video -> {:ok, video} end @@ -66,6 +67,22 @@ defmodule CF.Graphql.Resolvers.Videos do end) end + def captions(video, _, _) do + batch({__MODULE__, :fetch_captions_by_video_ids}, video.id, fn results -> + {:ok, + case Map.get(results, video.id) do + captions when is_list(captions) -> + captions + |> List.first(captions) + |> Map.get(:parsed) + |> Enum.map(&CF.Utils.map_string_keys_to_atom_keys/1) + + nil -> + nil + end} + end) + end + def fetch_statements_by_videos_ids(_, videos_ids) do Statement |> where([s], s.video_id in ^videos_ids) @@ -74,4 +91,12 @@ defmodule CF.Graphql.Resolvers.Videos do |> Repo.all() |> Enum.group_by(& &1.video_id) end + + def fetch_captions_by_video_ids(_, video_ids) do + VideoCaption + |> where([c], c.video_id in ^video_ids) + |> order_by(desc: :updated_at) + |> Repo.all() + |> Enum.group_by(& &1.video_id) + end end diff --git a/apps/cf_graphql/lib/schema/types.ex b/apps/cf_graphql/lib/schema/types.ex index 19bc1c0c..dcc47593 100644 --- a/apps/cf_graphql/lib/schema/types.ex +++ b/apps/cf_graphql/lib/schema/types.ex @@ -13,6 +13,7 @@ defmodule CF.Graphql.Schema.Types do Subscription, UserAction, User, - Video + Video, + VideoCaption }) end diff --git a/apps/cf_graphql/lib/schema/types/video.ex b/apps/cf_graphql/lib/schema/types/video.ex index dcfc8a19..f61b5e4a 100644 --- a/apps/cf_graphql/lib/schema/types/video.ex +++ b/apps/cf_graphql/lib/schema/types/video.ex @@ -50,6 +50,12 @@ defmodule CF.Graphql.Schema.Types.Video do complexity(join_complexity()) end + @desc "Video captions" + field(:captions, list_of(:video_caption)) do + resolve(&Resolvers.Videos.captions/3) + complexity(join_complexity()) + end + # Video providers # YouTube diff --git a/apps/cf_graphql/lib/schema/types/video_caption.ex b/apps/cf_graphql/lib/schema/types/video_caption.ex new file mode 100644 index 00000000..29b5eba6 --- /dev/null +++ b/apps/cf_graphql/lib/schema/types/video_caption.ex @@ -0,0 +1,18 @@ +defmodule CF.Graphql.Schema.Types.VideoCaption do + @moduledoc """ + A single caption for a video + """ + + use Absinthe.Schema.Notation + use Absinthe.Ecto, repo: DB.Repo + + @desc "Information about the application" + object :video_caption do + @desc "Caption text" + field(:text, non_null(:string)) + @desc "Caption start time (in seconds)" + field(:start, non_null(:float)) + @desc "Caption duration (in seconds)" + field(:duration, non_null(:float)) + end +end