Repository navigation
Add manual watched and unwatched toggles for movies, episodes, series, and seasons #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4e024e2
Add manual watched and unwatched toggles for movies and episodes
phantomic12 88536f2
Add watched toggles to library and history cards
phantomic12 833948b
Add series-level watched and unwatched actions
phantomic12 a7ff0a2
Add per-season watched and unwatched actions
phantomic12 913983d
Show watched checks on season tabs
phantomic12 c613150
Show partial watched counts on season tabs
phantomic12 a466c65
Add series watched progress to the header
phantomic12 476e3b1
Show watched progress on series library cards
phantomic12 3722d7e
Persist series episode totals on collection entries
phantomic12 b6a9a12
Add series totals backfill task
phantomic12 1ced841
Merge upstream main into watched-unplayed
phantomic12 fa9e26e
Keep stored totals on a failed catalogue call and stop counting undat…
phantomic12 7d41f0a
Treat an empty successful catalogue list as unknown, not as a zero total
phantomic12 1e862bc
Merge remote-tracking branch 'upstream/main' into watched-unplayed
phantomic12 72df706
Update continue-watching doubles to the merged episode helper
phantomic12 7828189
Merge upstream main into the watched toggles branch
phantomic12 608ddf7
Merge main into the watched toggles branch after the AIOStreams merge
phantomic12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class WatchedItemsController < ApplicationController | ||
| include ContentParamValidation | ||
|
|
||
| before_action :authenticate_user! | ||
|
|
||
| def update | ||
| imdb_id = params[:imdb_id] | ||
| type = params[:type] | ||
|
|
||
| return if reject_invalid_imdb_id!(imdb_id) || reject_invalid_content_type!(type) | ||
|
|
||
| unless parental_content_policy.allowed?(imdb_id) | ||
| redirect_back fallback_location: root_path, alert: "This content is not available for this account." | ||
| return | ||
| end | ||
|
|
||
| if params[:watched].nil? | ||
| redirect_back fallback_location: content_path(type: type, imdb_id: imdb_id), alert: "Watched state is required." | ||
| return | ||
| end | ||
| watched = ActiveModel::Type::Boolean.new.cast(params[:watched]) | ||
|
|
||
| result = Playback::WatchedToggle.new(current_user).call( | ||
| imdb_id: imdb_id, | ||
| type: type, | ||
| season: params[:season], | ||
| episode: params[:episode], | ||
| watched: watched, | ||
| title: params[:title], | ||
| poster_url: params[:poster_url], | ||
| duration_seconds: params[:duration_seconds] | ||
| ) | ||
|
|
||
| if result.failure? | ||
| redirect_back fallback_location: content_path(type: type, imdb_id: imdb_id), alert: result.error_message | ||
| return | ||
| end | ||
|
|
||
| redirect_back fallback_location: content_path(type: type, imdb_id: imdb_id), | ||
| notice: watched ? "Marked as watched." : "Marked as unwatched." | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Playback | ||
| # Counts watched progress across a series using only episodes that have | ||
| # already aired. Specials (season 0) and unreleased episodes are excluded, | ||
| # matching the series and season watched actions. | ||
| class SeriesProgress | ||
| def initialize(episodes, finished:) | ||
| @episodes = Array(episodes).select { |episode| released?(episode) } | ||
| @finished = finished | ||
| end | ||
|
|
||
| def seasons | ||
| @seasons ||= @episodes.group_by { |episode| episode[:season].to_i }.transform_values do |episodes| | ||
| watched = episodes.count { |episode| @finished[key(episode)] } | ||
| { watched: watched, total: episodes.size } | ||
| end | ||
| end | ||
|
|
||
| def watched_episodes | ||
| seasons.values.sum { |season| season[:watched] } | ||
| end | ||
|
|
||
| def total_episodes | ||
| seasons.values.sum { |season| season[:total] } | ||
| end | ||
|
|
||
| def percentage | ||
| return 0 unless total_episodes.positive? | ||
|
|
||
| ((watched_episodes.to_f / total_episodes) * 100).round | ||
| end | ||
|
|
||
| def complete? | ||
| total_episodes.positive? && watched_episodes == total_episodes | ||
| end | ||
|
|
||
| def summary | ||
| { | ||
| watched: watched_episodes, | ||
| total: total_episodes, | ||
| percentage: percentage, | ||
| complete: complete? | ||
| } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def key(episode) | ||
| [ episode[:season].to_i, episode[:episode].to_i ] | ||
| end | ||
|
|
||
| def released?(episode) | ||
| return false unless episode[:season].to_i.positive? | ||
| return false if episode[:released].blank? | ||
|
|
||
| Date.parse(episode[:released].to_s) <= Date.current | ||
| rescue ArgumentError | ||
| false | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Playback | ||
| # Fills persisted released-episode totals for library shows that predate the | ||
| # collection-entry totals. Failures leave the entry untouched so a later run | ||
| # can retry it. | ||
| class SeriesTotalBackfill | ||
| Result = Struct.new(:updated, :skipped, :failed, :failed_ids, keyword_init: true) | ||
|
|
||
| def initialize(catalog: Catalog::CinemetaClient.new, logger: Rails.logger) | ||
| @catalog = catalog | ||
| @logger = logger | ||
| end | ||
|
|
||
| def call(entries) | ||
| result = Result.new(updated: 0, skipped: 0, failed: 0, failed_ids: []) | ||
|
|
||
| entries.find_each do |entry| | ||
| if entry.show? | ||
| sync(entry, result) | ||
| else | ||
| result.skipped += 1 | ||
| end | ||
| end | ||
|
|
||
| result | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sync(entry, result) | ||
| metadata = @catalog.metadata(entry.imdb_id, "show", include_ratings: false) | ||
| unless metadata.success? | ||
| result.failed += 1 | ||
| result.failed_ids << entry.imdb_id | ||
| return | ||
| end | ||
|
|
||
| total = SeriesProgress.new(metadata.data[:episodes], finished: {}).total_episodes | ||
| entry.update_columns( | ||
| series_total_episodes: total, | ||
| series_total_episodes_synced_at: Time.current | ||
| ) | ||
| result.updated += 1 | ||
| rescue StandardError => e | ||
| @logger.warn("[Playback::SeriesTotalBackfill] #{entry.imdb_id}: #{e.class}: #{e.message}") | ||
| result.failed += 1 | ||
| result.failed_ids << entry.imdb_id | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.