From 606398e8c569a5afdeba7a0e0e6b15f405689bf6 Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Fri, 4 Jan 2019 23:45:04 +0100 Subject: [PATCH] feat(Videos): Add function + graphql for videos added by user --- apps/cf/lib/videos/videos.ex | 20 +++++++++++++++++-- apps/cf_graphql/lib/resolvers/users.ex | 7 +++++++ apps/cf_graphql/lib/schema/types/user.ex | 8 ++++++++ .../lib/controllers/video_controller.ex | 2 +- apps/cf_rest_api/lib/views/video_view.ex | 3 ++- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/apps/cf/lib/videos/videos.ex b/apps/cf/lib/videos/videos.ex index e3129af1..049514ba 100644 --- a/apps/cf/lib/videos/videos.ex +++ b/apps/cf/lib/videos/videos.ex @@ -35,6 +35,19 @@ defmodule CF.Videos do def videos_list(filters, false), do: Repo.all(Video.query_list(Video, filters)) + @doc """ + Get videos added by given user. This will return all videos, included the ones + marked as `unlisted`. + """ + def added_by_user(user, paginate_options \\ []) do + Video + |> join(:inner, [v], a in DB.Schema.UserAction, a.video_id == v.id) + |> where([_, a], a.user_id == ^user.id) + |> where([_, a], a.type == ^:add and a.entity == ^:video) + |> DB.Query.order_by_last_inserted_desc() + |> Repo.paginate(paginate_options) + end + @doc """ Return the corresponding video if it has already been added, `nil` otherwise """ @@ -61,14 +74,17 @@ defmodule CF.Videos do Returns video if success or {:error, reason} if something bad append. Can also throw if bad permissions. """ - def create!(user, video_url, is_partner \\ nil) do + def create!(user, video_url, params) do UserPermissions.check!(user, :add, :video) with metadata_fetcher when not is_nil(metadata_fetcher) <- get_metadata_fetcher(video_url), {:ok, metadata} <- metadata_fetcher.(video_url) do # Videos posted by publishers are recorded as partner unless explicitely # specified otherwise (false) - base_video = %Video{is_partner: user.is_publisher && is_partner != false} + base_video = %Video{ + is_partner: user.is_publisher && Keyword.get(params, :is_partner) != false, + unlisted: Keyword.get(params, :unlisted, false) + } Multi.new() |> Multi.insert(:video_without_hash_id, Video.changeset(base_video, metadata)) diff --git a/apps/cf_graphql/lib/resolvers/users.ex b/apps/cf_graphql/lib/resolvers/users.ex index f990f182..01de544e 100644 --- a/apps/cf_graphql/lib/resolvers/users.ex +++ b/apps/cf_graphql/lib/resolvers/users.ex @@ -52,4 +52,11 @@ defmodule CF.GraphQL.Resolvers.Users do |> Repo.paginate(page: offset, page_size: limit) |> Result.ok() end + + @doc """ + Get videos added by this user + """ + def videos_added(user, %{offset: offset, limit: limit}, _) do + {:ok, CF.Videos.added_by_user(user, page: offset, page_size: limit)} + end end diff --git a/apps/cf_graphql/lib/schema/types/user.ex b/apps/cf_graphql/lib/schema/types/user.ex index 629b65ed..22ae7702 100644 --- a/apps/cf_graphql/lib/schema/types/user.ex +++ b/apps/cf_graphql/lib/schema/types/user.ex @@ -36,6 +36,14 @@ defmodule CF.GraphQL.Schema.Types.User do arg(:limit, :integer, default_value: 10) resolve(&Resolvers.Users.activity_log/3) end + + @desc "A paginated list of videos added by this user" + field :videos_added, :paginated_videos do + complexity(join_complexity()) + arg(:offset, :integer, default_value: 1) + arg(:limit, :integer, default_value: 10) + resolve(&Resolvers.Users.videos_added/3) + end end @desc "A paginated list of user actions" diff --git a/apps/cf_rest_api/lib/controllers/video_controller.ex b/apps/cf_rest_api/lib/controllers/video_controller.ex index 21895037..dbf4e979 100644 --- a/apps/cf_rest_api/lib/controllers/video_controller.ex +++ b/apps/cf_rest_api/lib/controllers/video_controller.ex @@ -33,7 +33,7 @@ defmodule CF.RestApi.VideoController do nil -> conn |> Guardian.Plug.current_resource() - |> create!(url, params["is_partner"]) + |> create!(url, is_partner: params["is_partner"], unlisted: params["unlisted"]) |> case do {:error, error} when is_binary(error) -> conn diff --git a/apps/cf_rest_api/lib/views/video_view.ex b/apps/cf_rest_api/lib/views/video_view.ex index 89bb5c66..e923af48 100644 --- a/apps/cf_rest_api/lib/views/video_view.ex +++ b/apps/cf_rest_api/lib/views/video_view.ex @@ -22,7 +22,8 @@ defmodule CF.RestApi.VideoView do posted_at: video.inserted_at, speakers: render_many(video.speakers, CF.RestApi.SpeakerView, "speaker.json"), language: video.language, - is_partner: video.is_partner + is_partner: video.is_partner, + unlisted: video.unlisted } end end