Skip to content

Commit

Permalink
feat: Expose Captions on GraphQL (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Jun 21, 2024
1 parent ae43f73 commit c584efc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apps/cf/lib/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 26 additions & 1 deletion apps/cf_graphql/lib/resolvers/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
3 changes: 2 additions & 1 deletion apps/cf_graphql/lib/schema/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule CF.Graphql.Schema.Types do
Subscription,
UserAction,
User,
Video
Video,
VideoCaption
})
end
6 changes: 6 additions & 0 deletions apps/cf_graphql/lib/schema/types/video.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions apps/cf_graphql/lib/schema/types/video_caption.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c584efc

Please sign in to comment.