You've already forked algora-tv
							
							
				mirror of
				https://github.com/algora-io/tv.git
				synced 2025-10-30 23:07:56 +02:00 
			
		
		
		
	soft delete videos until they are removed from storage (#77)
This commit is contained in:
		| @@ -192,6 +192,7 @@ defmodule Algora.Library do | ||||
|       order_by: [desc: v.inserted_at], | ||||
|       limit: 1 | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> Repo.one() | ||||
|   end | ||||
|  | ||||
| @@ -223,6 +224,7 @@ defmodule Algora.Library do | ||||
|         channel_avatar_url: u.avatar_url | ||||
|       } | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> Repo.one() | ||||
|   end | ||||
|  | ||||
| @@ -259,6 +261,7 @@ defmodule Algora.Library do | ||||
|       where: v.duration == 0 and v.is_live == false and v.format == :hls and v.corrupted == false, | ||||
|       select: v.id | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> Repo.all() | ||||
|     |> Enum.each(&terminate_stream/1) | ||||
|   end | ||||
| @@ -609,6 +612,7 @@ defmodule Algora.Library do | ||||
|         channel_avatar_url: u.avatar_url | ||||
|       } | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -624,6 +628,7 @@ defmodule Algora.Library do | ||||
|         channel_avatar_url: u.avatar_url | ||||
|       } | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -640,6 +645,7 @@ defmodule Algora.Library do | ||||
|         channel_avatar_url: u.avatar_url | ||||
|       } | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -656,6 +662,7 @@ defmodule Algora.Library do | ||||
|         }, | ||||
|         where: v.id in ^ids | ||||
|       ) | ||||
|       |> Video.not_deleted() | ||||
|       |> Repo.all() | ||||
|  | ||||
|     video_by_id = fn id -> | ||||
| @@ -680,6 +687,7 @@ defmodule Algora.Library do | ||||
|       }, | ||||
|       where: v.show_id in ^ids | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -699,6 +707,7 @@ defmodule Algora.Library do | ||||
|         channel_avatar_url: u.avatar_url | ||||
|       } | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -718,6 +727,7 @@ defmodule Algora.Library do | ||||
|           is_nil(v.transmuxed_from_id) and | ||||
|           v.user_id == ^channel.user_id | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -738,6 +748,7 @@ defmodule Algora.Library do | ||||
|         is_nil(v.transmuxed_from_id) and | ||||
|           v.user_id == ^channel.user_id | ||||
|     ) | ||||
|     |> Video.not_deleted() | ||||
|     |> order_by_inserted(:desc) | ||||
|     |> Repo.all() | ||||
|   end | ||||
| @@ -790,6 +801,7 @@ defmodule Algora.Library do | ||||
|           channel_avatar_url: u.avatar_url | ||||
|         } | ||||
|       ) | ||||
|       |> Video.not_deleted() | ||||
|       |> Repo.one!() | ||||
|  | ||||
|   def update_video(%Video{} = video, attrs) do | ||||
| @@ -799,7 +811,11 @@ defmodule Algora.Library do | ||||
|   end | ||||
|  | ||||
|   def delete_video(%Video{} = video) do | ||||
|     Repo.delete(video) | ||||
|     deleted_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) | ||||
|  | ||||
|     video | ||||
|     |> Ecto.Changeset.change(deleted_at: deleted_at) | ||||
|     |> Repo.update() | ||||
|   end | ||||
|  | ||||
|   defp order_by_inserted(%Ecto.Query{} = query, direction) when direction in [:asc, :desc] do | ||||
|   | ||||
| @@ -2,6 +2,7 @@ defmodule Algora.Library.Video do | ||||
|   require Logger | ||||
|   use Ecto.Schema | ||||
|   import Ecto.Changeset | ||||
|   import Ecto.Query | ||||
|  | ||||
|   alias Algora.Accounts.User | ||||
|   alias Algora.Library.Video | ||||
| @@ -33,6 +34,7 @@ defmodule Algora.Library.Video do | ||||
|     field :visibility, Ecto.Enum, values: [public: 1, unlisted: 2] | ||||
|     field :remote_path, :string | ||||
|     field :local_path, :string | ||||
|     field :deleted_at, :naive_datetime | ||||
|  | ||||
|     belongs_to :user, User | ||||
|     belongs_to :show, Show | ||||
| @@ -110,4 +112,8 @@ defmodule Algora.Library.Video do | ||||
|   def slug(%Video{} = video), do: Slug.slugify("#{video.id}-#{video.title}") | ||||
|  | ||||
|   def id_from_slug(slug), do: slug |> String.split("-") |> Enum.at(0) | ||||
|  | ||||
|   def not_deleted(query) do | ||||
|     from v in query, where: is_nil(v.deleted_at) | ||||
|   end | ||||
| end | ||||
|   | ||||
| @@ -0,0 +1,11 @@ | ||||
| defmodule Algora.Repo.Local.Migrations.AddDeletedAtToVideos do | ||||
|   use Ecto.Migration | ||||
|  | ||||
|   def change do | ||||
|     alter table(:videos) do | ||||
|       add :deleted_at, :naive_datetime | ||||
|     end | ||||
|  | ||||
|     create index(:videos, [:deleted_at]) | ||||
|   end | ||||
| end | ||||
		Reference in New Issue
	
	Block a user