From 78bb5517fc2a2066148e44a795d3e38cc8b70956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wid-Ga=C3=ABl=20Destin?= Date: Mon, 15 Mar 2021 18:52:56 +0100 Subject: [PATCH 1/2] Added unlisted videos limitations --- apps/cf/lib/videos/videos.ex | 5 ++++- apps/cf/priv/limitations.yaml | 1 + apps/db/lib/db_type/entity.ex | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/cf/lib/videos/videos.ex b/apps/cf/lib/videos/videos.ex index 81ec17e2..33dcf174 100644 --- a/apps/cf/lib/videos/videos.ex +++ b/apps/cf/lib/videos/videos.ex @@ -80,7 +80,10 @@ defmodule CF.Videos do Can also throw if bad permissions. """ def create!(user, video_url, params \\ []) do - UserPermissions.check!(user, :add, :video) + case Keyword.get(params, :unlisted, false) do + false -> UserPermissions.check!(user, :add, :video) + true -> UserPermissions.check!(user, :add, :unlisted_video) + end with metadata_fetcher when not is_nil(metadata_fetcher) <- get_metadata_fetcher(video_url), {:ok, metadata} <- metadata_fetcher.(video_url) do diff --git a/apps/cf/priv/limitations.yaml b/apps/cf/priv/limitations.yaml index f104f761..44306846 100644 --- a/apps/cf/priv/limitations.yaml +++ b/apps/cf/priv/limitations.yaml @@ -7,6 +7,7 @@ create: statement: [ 0 , 3 , 6 , 12 , 15 , 25 , 30 , 30 , 30 ] speaker: [ 0 , 0 , 0 , 3 , 6 , 15 , 30 , 40 , 50 ] add: + unlisted_video: [ 0 , 0 , 1 , 1 , 1 , 2 , 3 , 5 , 10 ] video: [ 0 , 0 , 0 , 0 , 1 , 2 , 3 , 5 , 10 ] speaker: [ 0 , 0 , 0 , 3 , 6 , 15 , 30 , 40 , 50 ] update: diff --git a/apps/db/lib/db_type/entity.ex b/apps/db/lib/db_type/entity.ex index 9baffe67..db12a903 100644 --- a/apps/db/lib/db_type/entity.ex +++ b/apps/db/lib/db_type/entity.ex @@ -8,5 +8,6 @@ defenum( comment: 4, fact: 5, user_action: 6, - user: 7 + user: 7, + unlisted_video: 8 ) From 3bb3b42624518a390bf76ac69d89d6ebe69a43cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wid-Ga=C3=ABl=20Destin?= Date: Fri, 2 Apr 2021 01:57:00 +0200 Subject: [PATCH 2/2] Updated video action creator and added tests --- apps/cf/lib/actions/action_creator.ex | 8 ++-- apps/cf/lib/videos/videos.ex | 33 +++++++++++++---- apps/cf/priv/limitations.yaml | 1 + apps/cf/test/videos/videos_test.exs | 53 +++++++++++++++++++++++++-- 4 files changed, 80 insertions(+), 15 deletions(-) diff --git a/apps/cf/lib/actions/action_creator.ex b/apps/cf/lib/actions/action_creator.ex index 59b11742..37c73c62 100644 --- a/apps/cf/lib/actions/action_creator.ex +++ b/apps/cf/lib/actions/action_creator.ex @@ -70,10 +70,10 @@ defmodule CF.Actions.ActionCreator do ) end - def action_add(user_id, video = %Video{}) do + def action_add(user_id, video_entity, video = %Video{}) do action( user_id, - :video, + video_entity, :add, video_id: video.id, changes: %{ @@ -95,10 +95,10 @@ defmodule CF.Actions.ActionCreator do ) end - def action_update(user_id, %{data: video = %Video{}, changes: changes}) do + def action_update(user_id, video_entity, %{data: video = %Video{}, changes: changes}) do action( user_id, - :video, + video_entity, :update, video_id: video.id, changes: changes diff --git a/apps/cf/lib/videos/videos.ex b/apps/cf/lib/videos/videos.ex index 33dcf174..5735c108 100644 --- a/apps/cf/lib/videos/videos.ex +++ b/apps/cf/lib/videos/videos.ex @@ -80,10 +80,18 @@ defmodule CF.Videos do Can also throw if bad permissions. """ def create!(user, video_url, params \\ []) do - case Keyword.get(params, :unlisted, false) do - false -> UserPermissions.check!(user, :add, :video) - true -> UserPermissions.check!(user, :add, :unlisted_video) - end + is_unlisted = + case Keyword.get(params, :unlisted, false) do + v when v in [nil, false] -> + UserPermissions.check!(user, :add, :video) + false + + true -> + UserPermissions.check!(user, :add, :unlisted_video) + true + end + + video_entity = if is_unlisted, do: :unlisted_video, else: :video with metadata_fetcher when not is_nil(metadata_fetcher) <- get_metadata_fetcher(video_url), {:ok, metadata} <- metadata_fetcher.(video_url) do @@ -91,7 +99,7 @@ defmodule CF.Videos do # specified otherwise (false) base_video = %Video{ is_partner: user.is_publisher && Keyword.get(params, :is_partner) != false, - unlisted: Keyword.get(params, :unlisted, false) + unlisted: is_unlisted } Multi.new() @@ -102,7 +110,7 @@ defmodule CF.Videos do |> Repo.update() end) |> Multi.run(:action, fn _repo, %{video: video} -> - Repo.insert(ActionCreator.action_add(user.id, video)) + Repo.insert(ActionCreator.action_add(user.id, video_entity, video)) end) |> Repo.transaction() |> case do @@ -125,13 +133,22 @@ defmodule CF.Videos do Returned statements contains only an id and a key """ def shift_statements(user, video_id, offsets) do - UserPermissions.check!(user, :update, :video) video = Repo.get!(Video, video_id) + + case video.unlisted do + false -> + UserPermissions.check!(user, :update, :video) + + true -> + UserPermissions.check!(user, :update, :unlisted_video) + end + + video_entity = if video.unlisted, do: :unlisted_video, else: :video changeset = Video.changeset_shift_offsets(video, offsets) Multi.new() |> Multi.update(:video, changeset) - |> Multi.insert(:action_update, action_update(user.id, changeset)) + |> Multi.insert(:action_update, ActionCreator.action_update(user.id, video_entity, changeset)) |> Repo.transaction() |> case do {:ok, %{video: video}} -> diff --git a/apps/cf/priv/limitations.yaml b/apps/cf/priv/limitations.yaml index 44306846..a38875b2 100644 --- a/apps/cf/priv/limitations.yaml +++ b/apps/cf/priv/limitations.yaml @@ -13,6 +13,7 @@ add: update: statement: [ 0 , 0 , 5 , 15 , 30 , 75 , 125 , 150 , 200 ] speaker: [ 0 , 0 , 0 , 0 , 5 , 20 , 30 , 50 , 100 ] + unlisted_video: [ 0 , 0 , 1 , 1 , 3 , 6 , 12 , 25 , 40 ] video: [ 0 , 0 , 0 , 0 , 3 , 6 , 12 , 25 , 40 ] user: [ 1 , 10 , 15 , 20 , 20 , 30 , 30 , 50 , 50 ] delete: diff --git a/apps/cf/test/videos/videos_test.exs b/apps/cf/test/videos/videos_test.exs index 111196bf..3a4b57ab 100644 --- a/apps/cf/test/videos/videos_test.exs +++ b/apps/cf/test/videos/videos_test.exs @@ -7,8 +7,21 @@ defmodule CF.VideosTest do defp test_url, do: "https://www.youtube.com/watch?v=#{DB.Utils.TokenGenerator.generate(11)}" - describe "Add video" do - test "without enough reputation" do + describe "Add" do + test "video as unlisted without enough reputation" do + user = insert(:user, reputation: 0, is_publisher: false) + + assert_raise PermissionsError, fn -> + Videos.create!(user, test_url(), unlisted: true) + end + end + + test "video as unlisted with enough reputation" do + user = insert(:user, reputation: 15) + {:ok, _video} = Videos.create!(user, test_url(), unlisted: true) + end + + test "video as listed without enough reputation" do user = insert(:user, reputation: 0, is_publisher: false) assert_raise PermissionsError, fn -> @@ -16,7 +29,7 @@ defmodule CF.VideosTest do end end - test "with enough reputation" do + test "video as listed with enough reputation" do user = insert(:user, reputation: 50_000) {:ok, _video} = Videos.create!(user, test_url()) end @@ -57,6 +70,40 @@ defmodule CF.VideosTest do end end + describe "Update" do + test "an unlisted video without enough reputation" do + user = insert(:user, reputation: 50_000) + user2 = insert(:user, reputation: 0) + {:ok, video} = Videos.create!(user, test_url(), unlisted: true) + + assert_raise PermissionsError, fn -> + Videos.shift_statements(user2, video.id, %{youtube_offset: 42}) + end + end + + test "an unlisted video with enough reputation" do + user = insert(:user, reputation: 15) + {:ok, video} = Videos.create!(user, test_url(), unlisted: true) + {:ok, _video} = Videos.shift_statements(user, video.id, %{youtube_offset: 42}) + end + + test "a listed video without enough reputation" do + user = insert(:user, reputation: 50_000) + user2 = insert(:user, reputation: 0) + {:ok, video} = Videos.create!(user, test_url()) + + assert_raise PermissionsError, fn -> + Videos.shift_statements(user2, video.id, %{youtube_offset: 42}) + end + end + + test "a listed video with enough reputation" do + user = insert(:user, reputation: 50_000) + {:ok, video} = Videos.create!(user, test_url()) + {:ok, _video} = Videos.shift_statements(user, video.id, %{youtube_offset: 42}) + end + end + describe "Fetch captions" do test "fetch captions" do video =