Skip to content

Commit

Permalink
[Enhancement] Add media count to sources page (#251)
Browse files Browse the repository at this point in the history
* [WIP] Got query kinda working, now to refactor other queries

* Refactored all query methods to use dynamic snippets

* Refactored tab layout to grab tabs by name

* Removed standalone show buttons from in-app tables

* Removed unneeded comment
  • Loading branch information
kieraneglin authored May 21, 2024
1 parent 94c0cf9 commit a2a0f48
Show file tree
Hide file tree
Showing 28 changed files with 210 additions and 233 deletions.
14 changes: 7 additions & 7 deletions assets/js/tabs.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
window.setTabIndex = (index) => {
window.location.hash = `tab-${index}`
window.setTabByName = (tabName) => {
window.location.hash = `tab-${tabName}`

return index
return tabName
}

// The conditionals and currIndex stuff ensures that
// the tab index is always set to 0 if the hash is empty
// AND other hash values are ignored
window.getTabIndex = (currIndex) => {
window.getTabFromHash = (currentTabName, defaultTabName) => {
if (window.location.hash === '' || window.location.hash === '#') {
return 0
return defaultTabName
}

if (window.location.hash.startsWith('#tab-')) {
return parseInt(window.location.hash.replace('#tab-', ''))
return window.location.hash.replace('#tab-', '')
}

return currIndex
return currentTabName
}
14 changes: 10 additions & 4 deletions lib/pinchflat/downloading/downloading_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ defmodule Pinchflat.Downloading.DownloadingHelpers do
"""
def kickoff_redownload_for_existing_media(%Source{} = source) do
MediaQuery.new()
|> MediaQuery.for_source(source)
|> MediaQuery.with_media_downloaded_at()
|> MediaQuery.where_download_not_prevented()
|> MediaQuery.where_not_culled()
|> MediaQuery.require_assoc(:media_profile)
|> where(
^dynamic(
[m, s, mp],
^MediaQuery.for_source(source) and
^MediaQuery.downloaded() and
not (^MediaQuery.download_prevented()) and
not (^MediaQuery.culled())
)
)
|> Repo.all()
|> Enum.map(&MediaDownloadWorker.kickoff_with_task/1)
end
Expand Down
3 changes: 1 addition & 2 deletions lib/pinchflat/fast_indexing/fast_indexing_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpers do

defp list_media_items_by_media_id_for(source, media_ids) do
MediaQuery.new()
|> MediaQuery.for_source(source)
|> MediaQuery.with_media_ids(media_ids)
|> where(^dynamic([mi], ^MediaQuery.for_source(source) and mi.media_id in ^media_ids))
|> Repo.all()
end

Expand Down
7 changes: 3 additions & 4 deletions lib/pinchflat/lifecycle/notifications/source_notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ defmodule Pinchflat.Lifecycle.Notifications.SourceNotifications do

defp pending_media_item_count(source) do
MediaQuery.new()
|> MediaQuery.for_source(source)
|> MediaQuery.where_pending_download()
|> MediaQuery.require_assoc(:media_profile)
|> where(^dynamic(^MediaQuery.for_source(source) and ^MediaQuery.pending()))
|> Repo.aggregate(:count)
end

defp downloaded_media_item_count(source) do
MediaQuery.new()
|> MediaQuery.for_source(source)
|> MediaQuery.with_media_filepath()
|> where(^dynamic(^MediaQuery.for_source(source) and ^MediaQuery.downloaded()))
|> Repo.aggregate(:count)
end

Expand Down
26 changes: 10 additions & 16 deletions lib/pinchflat/media/media.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ defmodule Pinchflat.Media do
"""
def list_cullable_media_items do
MediaQuery.new()
|> MediaQuery.with_media_filepath()
|> MediaQuery.where_past_retention_period()
|> MediaQuery.where_culling_not_prevented()
|> MediaQuery.require_assoc(:source)
|> where(^MediaQuery.cullable())
|> Repo.all()
end

Expand All @@ -54,34 +53,29 @@ defmodule Pinchflat.Media do
"""
def list_redownloadable_media_items do
MediaQuery.new()
|> MediaQuery.with_media_downloaded_at()
|> MediaQuery.where_download_not_prevented()
|> MediaQuery.where_not_culled()
|> MediaQuery.where_media_not_redownloaded()
|> MediaQuery.where_past_redownload_delay()
|> MediaQuery.require_assoc(:media_profile)
|> where(^MediaQuery.redownloadable())
|> Repo.all()
end

@doc """
Returns a list of pending media_items for a given source, where
pending means the `media_filepath` is `nil` AND the media_item
matches satisfies `MediaQuery.where_pending_download`. You
pending means the media_item satisfies `MediaQuery.pending`. You
should really check out that function if you need to know more
because it has a lot going on.
Returns [%MediaItem{}, ...].
"""
def list_pending_media_items_for(%Source{} = source) do
MediaQuery.new()
|> MediaQuery.for_source(source)
|> MediaQuery.where_pending_download()
|> MediaQuery.require_assoc(:media_profile)
|> where(^dynamic(^MediaQuery.for_source(source) and ^MediaQuery.pending()))
|> Repo.all()
end

@doc """
For a given media_item, tells you if it is pending download. This is defined as
the media_item having a `media_filepath` of `nil` and matching the format selection
rules of the parent media_profile.
the media_item satisfying `MediaQuery.pending` which you should really check out.
Intentionally does not take the `download_media` setting of the source into account.
Expand All @@ -91,8 +85,8 @@ defmodule Pinchflat.Media do
media_item = Repo.preload(media_item, source: :media_profile)

MediaQuery.new()
|> MediaQuery.with_id(media_item.id)
|> MediaQuery.where_pending_download()
|> MediaQuery.require_assoc(:media_profile)
|> where(^dynamic([m, s, mp], m.id == ^media_item.id and ^MediaQuery.pending()))
|> Repo.exists?()
end

Expand Down
3 changes: 1 addition & 2 deletions lib/pinchflat/media/media_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ defmodule Pinchflat.Media.MediaItem do

current_max =
MediaQuery.new()
|> MediaQuery.for_source(source_id)
|> MediaQuery.where_uploaded_on_date(changes.upload_date)
|> where(^dynamic([mi], mi.upload_date == ^changes.upload_date and ^MediaQuery.for_source(source)))
|> Repo.aggregate(aggregator, :upload_date_index)

case current_max do
Expand Down
Loading

0 comments on commit a2a0f48

Please sign in to comment.