Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unlisted videos limitations #308

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 25 additions & 5 deletions apps/cf/lib/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,26 @@ defmodule CF.Videos do
Can also throw if bad permissions.
"""
def create!(user, video_url, params \\ []) do
UserPermissions.check!(user, :add, :video)
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 @@ -99,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 @@ -122,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
2 changes: 2 additions & 0 deletions apps/cf/priv/limitations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ 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:
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
3 changes: 2 additions & 1 deletion apps/db/lib/db_type/entity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ defenum(
comment: 4,
fact: 5,
user_action: 6,
user: 7
user: 7,
unlisted_video: 8
)