Skip to content

Commit

Permalink
Updated video action creator and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wdestin committed Apr 1, 2021
1 parent 78bb551 commit 3bb3b42
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
8 changes: 4 additions & 4 deletions apps/cf/lib/actions/action_creator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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: %{
Expand All @@ -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
Expand Down
33 changes: 25 additions & 8 deletions apps/cf/lib/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,26 @@ 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
# Videos posted by publishers are recorded as partner unless explicitely
# 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()
Expand All @@ -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
Expand All @@ -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}} ->
Expand Down
1 change: 1 addition & 0 deletions apps/cf/priv/limitations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 50 additions & 3 deletions apps/cf/test/videos/videos_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ 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 ->
Videos.create!(user, test_url())
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
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 3bb3b42

Please sign in to comment.