Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Different tools for different jobs. If you want a permanent offline library, a d
- **Popular and trending** rows for films and TV, refreshed from live metadata.
- **Recommendations** based on what you have watched, using TMDB's collaborative filtering.
- **Rich title pages** with backdrop, overview, cast, ratings, seasons, and every available source.
- **Playlists** queue films and shows in your own order: add a title from its detail page, then reorder or remove it later.

### Play it properly

Expand Down Expand Up @@ -255,6 +256,8 @@ More at [vitobotta.com](https://vitobotta.com).

Contributions are welcome. Development setup, the test suites, architecture notes, CI, and the release process are in [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md).

The Jellyfin parity backlog, one feature per pull request, is tracked in [docs/jellyfin-parity.md](docs/jellyfin-parity.md).

For a bug report, include your deployment type, the relevant log lines, and what you were playing. For a playback problem, the server log and the browser console together are worth more than either alone.

## Disclaimer and responsible use
Expand Down
1 change: 1 addition & 0 deletions app/controllers/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def show
@collection_entry = current_user.collection_entries.find_by(imdb_id: @imdb_id)
@in_library = @collection_entry&.library? || false
@in_wishlist = @collection_entry&.wishlist? || false
@playlists = current_user.playlists.order(:name)

if @type == "show"
progress_rows = current_user.playback_progresses.for_show(@imdb_id)
Expand Down
76 changes: 76 additions & 0 deletions app/controllers/playlist_items_controller.rb
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
91 changes: 91 additions & 0 deletions app/controllers/playlists_controller.rb
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
1 change: 1 addition & 0 deletions app/helpers/icon_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module IconHelper
library: '<rect x="3.5" y="4.5" width="5" height="15" rx="1.2"/><rect x="10.5" y="4.5" width="5" height="15" rx="1.2"/><path d="m18.4 5.6 2.3 13.2"/>',
wishlist: '<path d="M12 20s-7.5-4.6-7.5-9.6A4.4 4.4 0 0 1 12 7.4a4.4 4.4 0 0 1 7.5 3C19.5 15.4 12 20 12 20Z"/>',
history: '<path d="M3.5 12a8.5 8.5 0 1 0 2.6-6.1"/><path d="M3.5 5.5V10H8"/><path d="M12 8.5V12l2.6 1.6"/>',
playlist: '<path d="M4 6h10M4 12h10M4 18h7"/><path d="M17 9v8"/><path d="m14 15 3 3 3-3"/>',
settings: '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .34 1.87l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.7 1.7 0 0 0-1.87-.34 1.7 1.7 0 0 0-1.03 1.56V21a2 2 0 1 1-4 0v-.06a1.7 1.7 0 0 0-1.1-1.56 1.7 1.7 0 0 0-1.88.34l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06A1.7 1.7 0 0 0 4.6 15a1.7 1.7 0 0 0-1.56-1.03H3a2 2 0 1 1 0-4h.06A1.7 1.7 0 0 0 4.6 8.87a1.7 1.7 0 0 0-.34-1.87l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.7 1.7 0 0 0 1.87.34H9a1.7 1.7 0 0 0 1.03-1.56V3a2 2 0 1 1 4 0v.06a1.7 1.7 0 0 0 1.03 1.56 1.7 1.7 0 0 0 1.87-.34l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.7 1.7 0 0 0-.34 1.87V9a1.7 1.7 0 0 0 1.56 1.03H21a2 2 0 1 1 0 4h-.06A1.7 1.7 0 0 0 19.4 15Z"/>',
play: '<path d="M8 5.5v13l10-6.5z" fill="currentColor" stroke="none"/>',
plus: '<path d="M12 5v14M5 12h14"/>',
Expand Down
25 changes: 25 additions & 0 deletions app/models/playlist.rb
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
58 changes: 58 additions & 0 deletions app/models/playlist_item.rb
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
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class User < ApplicationRecord
has_many :collection_entries, dependent: :destroy
has_many :playback_progresses, dependent: :destroy
has_many :hls_sessions, dependent: :destroy
has_many :playlists, dependent: :destroy

# Language preferences
serialize :preferred_languages, coder: JSON
Expand Down
24 changes: 24 additions & 0 deletions app/views/content/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@
<span data-toggle-label><%= @in_wishlist ? "On wishlist" : "Wishlist" %></span>
</button>
</div>

<details class="mt-4">
<summary class="cursor-pointer select-none text-xs text-ink-300 transition-colors hover:text-ink-50">Add to playlist</summary>
<div class="mt-3 flex flex-col gap-2">
<% if @playlists.any? %>
<div class="flex flex-wrap gap-1.5">
<% @playlists.each do |playlist| %>
<%= button_to playlist.name, playlist_playlist_items_path(playlist),
params: { playlist_item: { imdb_id: @imdb_id, content_type: @type, title: @metadata[:title], poster_url: @metadata[:poster_url], year: @metadata[:year] } },
Comment thread
vitobotta marked this conversation as resolved.
Resolved
class: "chip transition-colors hover:border-ink-400 hover:text-ink-50" %>
<% end %>
</div>
<% end %>
<%= form_with url: playlists_path, method: :post, class: "flex flex-col sm:flex-row gap-2" do %>
<input type="hidden" name="playlist_item[imdb_id]" value="<%= @imdb_id %>">
<input type="hidden" name="playlist_item[content_type]" value="<%= @type %>">
<input type="hidden" name="playlist_item[title]" value="<%= @metadata[:title] %>">
<input type="hidden" name="playlist_item[poster_url]" value="<%= @metadata[:poster_url] %>">
<input type="hidden" name="playlist_item[year]" value="<%= @metadata[:year] %>">
<input type="text" name="playlist[name]" class="input-field" placeholder="New playlist name" maxlength="80" required>
<button type="submit" class="btn-secondary whitespace-nowrap text-sm">Create &amp; add</button>
<% end %>
</div>
</details>
</div>
</div>
</section>
Expand Down
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<%= link_to wishlist_index_path, class: "nav-link #{'active' if controller_name == 'wishlist'}", "aria-current": (controller_name == "wishlist" ? "page" : nil) do %>
<%= icon :wishlist %><span>Wishlist</span>
<% end %>
<%= link_to playlists_path, class: "nav-link #{'active' if controller_name == 'playlists'}", "aria-current": (controller_name == "playlists" ? "page" : nil) do %>
<%= icon :playlist %><span>Playlists</span>
<% end %>
<%= link_to watch_history_index_path, class: "nav-link #{'active' if controller_name == 'watch_history'}", "aria-current": (controller_name == "watch_history" ? "page" : nil) do %>
<%= icon :history %><span>History</span>
<% end %>
Expand Down Expand Up @@ -146,6 +149,9 @@
<%= link_to wishlist_index_path, class: "mobile-nav__link #{'active' if controller_name == 'wishlist'}" do %>
<%= icon :wishlist %><span>Wishlist</span>
<% end %>
<%= link_to playlists_path, class: "mobile-nav__link #{'active' if controller_name == 'playlists'}" do %>
<%= icon :playlist %><span>Playlists</span>
<% end %>
<%= link_to watch_history_index_path, class: "mobile-nav__link #{'active' if controller_name == 'watch_history'}" do %>
<%= icon :history %><span>History</span>
<% end %>
Expand Down
Loading
Loading