Repository navigation
Add playlists and a one-feature-per-PR Jellyfin parity backlog #38
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ba5eb54
Add playlists and the Jellyfin parity backlog
phantomic12 c1e5e69
Merge upstream main into jellyfin-parity
phantomic12 6ab5689
Fix the series year add, hidden-item counts and move handling in play…
phantomic12 3bfc659
Build playlist counts on the create failure path and only normalise r…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PlaylistItemsController < ApplicationController | ||
| before_action :authenticate_user! | ||
| before_action :set_playlist | ||
| before_action :set_item, only: %i[destroy move] | ||
|
|
||
| def create | ||
| attributes = item_params | ||
| unless PlaylistItem.content_types.key?(attributes[:content_type].to_s) | ||
| redirect_to playlist_path(@playlist), alert: "Invalid content type." | ||
| return | ||
| end | ||
| unless parental_content_policy.allowed?(attributes[:imdb_id]) | ||
| redirect_to playlist_path(@playlist), alert: "That title is not available for this account." | ||
| return | ||
| end | ||
|
|
||
| item = @playlist.playlist_items.find_or_initialize_by( | ||
| imdb_id: attributes[:imdb_id], | ||
| content_type: attributes[:content_type] | ||
| ) | ||
| if item.persisted? | ||
| redirect_to playlist_path(@playlist), notice: "Already in this playlist." | ||
| return | ||
| end | ||
|
|
||
| item.assign_attributes(attributes.except(:imdb_id, :content_type)) | ||
| if item.save | ||
| redirect_to playlist_path(@playlist), notice: "Added to #{@playlist.name}." | ||
| else | ||
| redirect_to playlist_path(@playlist), alert: item.errors.full_messages.to_sentence | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| @item.destroy! | ||
| compact_positions | ||
| redirect_to playlist_path(@playlist), notice: "Removed from playlist." | ||
| end | ||
|
|
||
| def move | ||
| direction = params[:direction].to_s | ||
| unless %w[up down].include?(direction) | ||
| redirect_to playlist_path(@playlist), alert: "Invalid move direction." | ||
| return | ||
| end | ||
|
|
||
| unless @item.move!(direction) | ||
| redirect_to playlist_path(@playlist), alert: "That item is already at the edge of the playlist." | ||
| return | ||
| end | ||
|
|
||
| redirect_to playlist_path(@playlist), notice: "Playlist order updated." | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_playlist | ||
| @playlist = current_user.playlists.find(params[:playlist_id]) | ||
| end | ||
|
|
||
| def set_item | ||
| @item = @playlist.playlist_items.find(params[:id]) | ||
| end | ||
|
|
||
| def item_params | ||
| params.require(:playlist_item).permit(:imdb_id, :content_type, :title, :poster_url, :year) | ||
| end | ||
|
|
||
| def compact_positions | ||
| @playlist.playlist_items.ordered.each_with_index do |item, index| | ||
| item.update_column(:position, index) unless item.position == index | ||
| 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,91 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PlaylistsController < ApplicationController | ||
| before_action :authenticate_user! | ||
| before_action :set_playlist, only: %i[show update destroy] | ||
|
|
||
| def index | ||
| @playlists = playlists_for_index | ||
| @playlist = current_user.playlists.new | ||
| build_item_counts | ||
| end | ||
|
|
||
| def show | ||
| @items = parental_content_policy.filter(@playlist.playlist_items.ordered) | ||
| @ordered_ids = @playlist.playlist_items.ordered.pluck(:id) | ||
| end | ||
|
|
||
| def create | ||
| @playlist = current_user.playlists.new(playlist_params) | ||
| item_attributes = nested_item_params | ||
| if item_attributes.present? && !PlaylistItem.content_types.key?(item_attributes[:content_type].to_s) | ||
| redirect_to playlists_path, alert: "Invalid content type." | ||
| return | ||
| end | ||
| if item_attributes.present? && !parental_content_policy.allowed?(item_attributes[:imdb_id]) | ||
| redirect_to playlists_path, alert: content_restricted_message | ||
| return | ||
| end | ||
|
|
||
| Playlist.transaction do | ||
| @playlist.save! | ||
| @playlist.playlist_items.create!(item_attributes) if item_attributes.present? | ||
| end | ||
| redirect_to playlist_path(@playlist), notice: "Playlist created." | ||
| rescue ActiveRecord::RecordInvalid => e | ||
| @playlists = playlists_for_index | ||
| build_item_counts | ||
| flash.now[:alert] = e.record.errors.full_messages.to_sentence | ||
| render :index, status: :unprocessable_entity | ||
| end | ||
|
|
||
| def update | ||
| if @playlist.update(playlist_params) | ||
| redirect_to playlist_path(@playlist), notice: "Playlist updated." | ||
| else | ||
| @items = parental_content_policy.filter(@playlist.playlist_items.ordered) | ||
| @ordered_ids = @playlist.playlist_items.ordered.pluck(:id) | ||
| render :show, status: :unprocessable_entity | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| @playlist.destroy! | ||
| redirect_to playlists_path, notice: "Playlist deleted." | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_playlist | ||
| @playlist = current_user.playlists.find(params[:id]) | ||
| end | ||
|
|
||
| def playlists_for_index | ||
| current_user.playlists.includes(:playlist_items).order(updated_at: :desc) | ||
| end | ||
|
|
||
| # :index is rendered from both index and the create failure path, which | ||
| # runs with playlists already on the account, so the counts have to be | ||
| # built there too or the view fetches from nil. | ||
| def build_item_counts | ||
| @item_counts = @playlists.index_with do |playlist| | ||
| items = playlist.playlist_items.to_a | ||
| visible = parental_content_policy.filter(items).size | ||
| { visible: visible, hidden: items.size - visible } | ||
| end | ||
| end | ||
|
|
||
| def playlist_params | ||
| params.require(:playlist).permit(:name, :description) | ||
| end | ||
|
|
||
| def nested_item_params | ||
| return if params[:playlist_item].blank? | ||
|
|
||
| params.require(:playlist_item).permit(:imdb_id, :content_type, :title, :poster_url, :year).to_h | ||
| end | ||
|
|
||
| def content_restricted_message | ||
| "That title is not available for this account." | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Playlist < ApplicationRecord | ||
| belongs_to :user | ||
|
|
||
| has_many :playlist_items, | ||
| -> { order(position: :asc, id: :asc) }, | ||
| dependent: :destroy, | ||
| inverse_of: :playlist | ||
|
|
||
| validates :name, presence: true, length: { maximum: 80 } | ||
| validates :name, uniqueness: { scope: :user_id, case_sensitive: false } | ||
|
|
||
| before_validation :normalize_name | ||
|
|
||
| def item_count | ||
| playlist_items.size | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def normalize_name | ||
| self.name = name.to_s.squish.presence | ||
| 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,58 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class PlaylistItem < ApplicationRecord | ||
| belongs_to :playlist, touch: true | ||
|
|
||
| enum :content_type, { movie: 0, show: 1 }, validate: true | ||
|
|
||
| validates :imdb_id, :title, presence: true | ||
| validates :imdb_id, uniqueness: { | ||
| scope: %i[playlist_id content_type], | ||
| message: "is already in this playlist" | ||
| } | ||
| validates :year, numericality: { greater_than: 1800, less_than: 2100 }, allow_nil: true | ||
|
|
||
| before_validation :normalize_year | ||
| before_create :assign_position | ||
|
|
||
| scope :ordered, -> { order(position: :asc, id: :asc) } | ||
|
|
||
| # Swaps positions with the adjacent item. Returns false at either end so a | ||
| # controller can leave the playlist untouched. | ||
| def move!(direction) | ||
| siblings = playlist.playlist_items.ordered.to_a | ||
| index = siblings.index { |item| item.id == id } | ||
| return false unless index | ||
|
|
||
| target = direction.to_s == "up" ? index - 1 : index + 1 | ||
| return false unless target.between?(0, siblings.length - 1) | ||
|
|
||
| other = siblings[target] | ||
| self.class.transaction do | ||
| own_position = position | ||
| update_columns(position: other.position, updated_at: Time.current) | ||
| other.update_columns(position: own_position, updated_at: Time.current) | ||
| end | ||
| true | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Cinemeta reports a series year as a range ("2008–2013"), and numericality | ||
| # validates the value before it is cast to an integer, so the range rejects | ||
| # the whole row. Accept a single four-digit year or a range of two and keep | ||
| # the first; anything else is left alone for numericality to reject. | ||
| YEAR_PATTERN = /\A(\d{4})(?:\s*[–—-]\s*\d{4})?\z/ | ||
|
|
||
| def normalize_year | ||
| raw = read_attribute_before_type_cast(:year) | ||
| return if raw.nil? || raw.is_a?(Integer) | ||
|
|
||
| match = raw.to_s.strip.match(YEAR_PATTERN) | ||
| self.year = match[1] if match | ||
| end | ||
|
|
||
| def assign_position | ||
| self.position = (playlist.playlist_items.maximum(:position) || -1) + 1 | ||
| 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
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.