Skip to content

Commit

Permalink
chore(Videos): Provider-specific statements shifting
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Dec 19, 2018
1 parent 8a719c5 commit 76623fb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion apps/cf/lib/actions/action_creator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ defmodule CF.Actions.ActionCreator do
user_id,
:video,
:update,
video_id: video.video_id,
video_id: video.id,
changes: changes
)
end
Expand Down
30 changes: 9 additions & 21 deletions apps/cf/lib/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule CF.Videos do
import Ecto.Query, warn: false
import CF.Videos.MetadataFetcher
import CF.Videos.CaptionsFetcher
import CF.Actions.ActionCreator, only: [action_update: 2]

alias Ecto.Multi
alias DB.Repo
Expand Down Expand Up @@ -97,33 +98,20 @@ defmodule CF.Videos do
Returns {:ok, statements} if success, {:error, reason} otherwise.
Returned statements contains only an id and a key
"""
def shift_statements(user, video_id, offset) when is_integer(offset) do
def shift_statements(user, video_id, offsets) do
UserPermissions.check!(user, :update, :video)
statements_query = where(Statement, [s], s.video_id == ^video_id)
video = Repo.get!(Video, video_id)
changeset = Video.changeset_shift_offsets(video, offsets)

Multi.new()
|> Multi.update_all(
:statements_update,
statements_query,
[inc: [time: offset]],
returning: [:id, :time]
)
|> Multi.insert(
:action,
ActionCreator.action(
user.id,
:video,
:update,
video_id: video_id,
changes: %{"statements_time" => offset}
)
)
|> Multi.update(:video, changeset)
|> Multi.insert(:action_update, action_update(user.id, changeset))
|> Repo.transaction()
|> case do
{:ok, %{statements_update: {_, statements}}} ->
{:ok, Enum.map(statements, &%{id: &1.id, time: &1.time})}
{:ok, %{video: video}} ->
{:ok, video}

{:error, _, reason, _} ->
{:error, _operation, reason, _changes} ->
{:error, reason}
end
end
Expand Down
20 changes: 0 additions & 20 deletions apps/cf_rest_api/lib/channels/statements_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ defmodule CF.RestApi.StatementsChannel do
handle_in_authenticated(command, params, socket, &handle_in_authenticated!/3)
end

@doc """
Shift all video's statements
"""
def handle_in_authenticated!("shift_all", offset, socket) do
user = Repo.get(DB.Schema.User, socket.assigns.user_id)

case Videos.shift_statements(
user,
socket.assigns.video_id,
String.to_integer(offset)
) do
{:ok, statements} ->
broadcast!(socket, "statements_updated", %{statements: statements})
{:reply, :ok, socket}

{:error, _} ->
{:reply, :error, socket}
end
end

@doc """
Add a new statement
"""
Expand Down
22 changes: 22 additions & 0 deletions apps/cf_rest_api/lib/channels/video_debate_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule CF.RestApi.VideoDebateChannel do
alias DB.Schema.Speaker
alias DB.Schema.VideoSpeaker

alias CF.Videos
alias CF.Accounts.UserPermissions
alias CF.RestApi.{VideoView, SpeakerView, ChangesetView}

Expand Down Expand Up @@ -58,6 +59,27 @@ defmodule CF.RestApi.VideoDebateChannel do
handle_in_authenticated(command, params, socket, &handle_in_authenticated!/3)
end

@doc """
Shift all video's statements
"""
def handle_in_authenticated!("shift_statements", offsets, socket) do
user = Repo.get(DB.Schema.User, socket.assigns.user_id)

case Videos.shift_statements(user, socket.assigns.video_id, offsets) do
{:ok, video} ->
rendered_video =
video
|> DB.Repo.preload(:speakers)
|> View.render_one(VideoView, "video.json")

broadcast!(socket, "video_updated", %{video: rendered_video})
{:reply, :ok, socket}

{:error, _} ->
{:reply, :error, socket}
end
end

@doc """
Add an existing speaker to the video
"""
Expand Down
11 changes: 11 additions & 0 deletions apps/db/lib/db_schema/video.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ defmodule DB.Schema.Video do
change(video, hash_id: VideoHashId.encode(id))
end

@doc """
Builds a changeset that allows shifting statements for all providers
## Examples
iex> DB.Schema.Video.changeset_shift_offsets(youtube_offset: 42)
"""
def changeset_shift_offsets(struct, params \\ %{}) do
cast(struct, params, [:youtube_offset])
end

@doc """
Given a changeset, fill the `{provider}_id` fields or add an error if URL is not valid.
"""
Expand Down

0 comments on commit 76623fb

Please sign in to comment.