Added incrementing index for upload dates
This commit is contained in:
parent
0c71501e51
commit
1a2ba15a9d
3 changed files with 98 additions and 0 deletions
|
|
@ -11,6 +11,7 @@ defmodule Pinchflat.Media.MediaItem do
|
||||||
alias Pinchflat.Repo
|
alias Pinchflat.Repo
|
||||||
alias Pinchflat.Tasks.Task
|
alias Pinchflat.Tasks.Task
|
||||||
alias Pinchflat.Sources.Source
|
alias Pinchflat.Sources.Source
|
||||||
|
alias Pinchflat.Media.MediaQuery
|
||||||
alias Pinchflat.Metadata.MediaMetadata
|
alias Pinchflat.Metadata.MediaMetadata
|
||||||
alias Pinchflat.Media.MediaItemsSearchIndex
|
alias Pinchflat.Media.MediaItemsSearchIndex
|
||||||
|
|
||||||
|
|
@ -102,6 +103,7 @@ defmodule Pinchflat.Media.MediaItem do
|
||||||
|> cast(attrs, @allowed_fields)
|
|> cast(attrs, @allowed_fields)
|
||||||
|> cast_assoc(:metadata, with: &MediaMetadata.changeset/2, required: false)
|
|> cast_assoc(:metadata, with: &MediaMetadata.changeset/2, required: false)
|
||||||
|> dynamic_default(:uuid, fn _ -> Ecto.UUID.generate() end)
|
|> dynamic_default(:uuid, fn _ -> Ecto.UUID.generate() end)
|
||||||
|
|> update_upload_date_index()
|
||||||
|> validate_required(@required_fields)
|
|> validate_required(@required_fields)
|
||||||
|> unique_constraint([:media_id, :source_id])
|
|> unique_constraint([:media_id, :source_id])
|
||||||
end
|
end
|
||||||
|
|
@ -126,6 +128,23 @@ defmodule Pinchflat.Media.MediaItem do
|
||||||
~w(__meta__ __struct__ metadata tasks media_items_search_index)a
|
~w(__meta__ __struct__ metadata tasks media_items_search_index)a
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp update_upload_date_index(%{changes: changes} = changeset) when is_map_key(changes, :upload_date) do
|
||||||
|
source_id = get_field(changeset, :source_id)
|
||||||
|
|
||||||
|
current_max =
|
||||||
|
MediaQuery.new()
|
||||||
|
|> MediaQuery.for_source(source_id)
|
||||||
|
|> MediaQuery.where_uploaded_on_date(changes.upload_date)
|
||||||
|
|> Repo.aggregate(:max, :upload_date_index)
|
||||||
|
|
||||||
|
case current_max do
|
||||||
|
nil -> put_change(changeset, :upload_date_index, 0)
|
||||||
|
max -> put_change(changeset, :upload_date_index, max + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp update_upload_date_index(changeset), do: changeset
|
||||||
|
|
||||||
defimpl Jason.Encoder, for: MediaItem do
|
defimpl Jason.Encoder, for: MediaItem do
|
||||||
def encode(value, opts) do
|
def encode(value, opts) do
|
||||||
value
|
value
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,10 @@ defmodule Pinchflat.Media.MediaQuery do
|
||||||
MediaItem
|
MediaItem
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def for_source(query, source_id) when is_integer(source_id) do
|
||||||
|
where(query, [mi], mi.source_id == ^source_id)
|
||||||
|
end
|
||||||
|
|
||||||
def for_source(query, source) do
|
def for_source(query, source) do
|
||||||
where(query, [mi], mi.source_id == ^source.id)
|
where(query, [mi], mi.source_id == ^source.id)
|
||||||
end
|
end
|
||||||
|
|
@ -99,6 +103,10 @@ defmodule Pinchflat.Media.MediaQuery do
|
||||||
|> where([mi, source], is_nil(source.download_cutoff_date) or mi.upload_date >= source.download_cutoff_date)
|
|> where([mi, source], is_nil(source.download_cutoff_date) or mi.upload_date >= source.download_cutoff_date)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def where_uploaded_on_date(query, date) do
|
||||||
|
where(query, [mi], mi.upload_date == ^date)
|
||||||
|
end
|
||||||
|
|
||||||
def where_download_not_prevented(query) do
|
def where_download_not_prevented(query) do
|
||||||
where(query, [mi], mi.prevent_download == false)
|
where(query, [mi], mi.prevent_download == false)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,77 @@ defmodule Pinchflat.MediaTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "schema when testing upload_date_index" do
|
||||||
|
test "upload_date_index is set to 0 if it's the only video uploaded that day" do
|
||||||
|
upload_date = Date.utc_today()
|
||||||
|
media_item = media_item_fixture(%{upload_date: upload_date})
|
||||||
|
|
||||||
|
assert media_item.upload_date_index == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
test "upload_date_index is set to 1 if it's the second video uploaded that day" do
|
||||||
|
upload_date = Date.utc_today()
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
media_item_one = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||||
|
media_item_two = media_item_fixture(%{source_id: source.id, upload_date: upload_date})
|
||||||
|
|
||||||
|
assert media_item_one.upload_date_index == 0
|
||||||
|
assert media_item_two.upload_date_index == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "upload_date_index doesn't increment if the video is uploaded on a different day" do
|
||||||
|
today = Date.utc_today()
|
||||||
|
one_day_ago = Date.add(today, -1)
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
media_item_new = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||||
|
media_item_old = media_item_fixture(%{source_id: source.id, upload_date: one_day_ago})
|
||||||
|
|
||||||
|
assert media_item_new.upload_date_index == 0
|
||||||
|
assert media_item_old.upload_date_index == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
test "recomputes upload_date_index if an upload_date is changed...somehow" do
|
||||||
|
today = Date.utc_today()
|
||||||
|
one_day_ago = Date.add(today, -1)
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
media_item_new = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||||
|
media_item_old = media_item_fixture(%{source_id: source.id, upload_date: one_day_ago})
|
||||||
|
|
||||||
|
{:ok, updated_media_item} = Media.update_media_item(media_item_old, %{upload_date: today})
|
||||||
|
|
||||||
|
assert media_item_new.upload_date_index == 0
|
||||||
|
assert updated_media_item.upload_date_index == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "upload_date_index doesn't increment if the video is for a different source" do
|
||||||
|
today = Date.utc_today()
|
||||||
|
|
||||||
|
source_one = source_fixture()
|
||||||
|
source_two = source_fixture()
|
||||||
|
|
||||||
|
media_item_one = media_item_fixture(%{source_id: source_one.id, upload_date: today})
|
||||||
|
media_item_two = media_item_fixture(%{source_id: source_two.id, upload_date: today})
|
||||||
|
|
||||||
|
assert media_item_one.upload_date_index == 0
|
||||||
|
assert media_item_two.upload_date_index == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
test "upload_date_index doesn't increment if the a video's upload_date is updated but doesn't change" do
|
||||||
|
today = Date.utc_today()
|
||||||
|
source = source_fixture()
|
||||||
|
|
||||||
|
media_item_one = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||||
|
_media_item_two = media_item_fixture(%{source_id: source.id, upload_date: today})
|
||||||
|
|
||||||
|
{:ok, updated_media_item} = Media.update_media_item(media_item_one, %{upload_date: today, title: "New title"})
|
||||||
|
|
||||||
|
assert updated_media_item.upload_date_index == 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "list_media_items/0" do
|
describe "list_media_items/0" do
|
||||||
test "it returns all media_items" do
|
test "it returns all media_items" do
|
||||||
media_item = media_item_fixture()
|
media_item = media_item_fixture()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue