diff --git a/apps/cf/lib/actions/action_creator.ex b/apps/cf/lib/actions/action_creator.ex index 182c2be3..0c5ee518 100644 --- a/apps/cf/lib/actions/action_creator.ex +++ b/apps/cf/lib/actions/action_creator.ex @@ -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 diff --git a/apps/cf/lib/videos/videos.ex b/apps/cf/lib/videos/videos.ex index d01d087e..be147a24 100644 --- a/apps/cf/lib/videos/videos.ex +++ b/apps/cf/lib/videos/videos.ex @@ -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 @@ -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 diff --git a/apps/cf_rest_api/lib/channels/statements_channel.ex b/apps/cf_rest_api/lib/channels/statements_channel.ex index cb7dd1e7..004a5bec 100644 --- a/apps/cf_rest_api/lib/channels/statements_channel.ex +++ b/apps/cf_rest_api/lib/channels/statements_channel.ex @@ -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 """ diff --git a/apps/cf_rest_api/lib/channels/video_debate_channel.ex b/apps/cf_rest_api/lib/channels/video_debate_channel.ex index ba47e116..fd63a65c 100644 --- a/apps/cf_rest_api/lib/channels/video_debate_channel.ex +++ b/apps/cf_rest_api/lib/channels/video_debate_channel.ex @@ -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} @@ -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 """ diff --git a/apps/db/lib/db_schema/video.ex b/apps/db/lib/db_schema/video.ex index 00848e01..79f52a46 100644 --- a/apps/db/lib/db_schema/video.ex +++ b/apps/db/lib/db_schema/video.ex @@ -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. """