Skip to content
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

Add liked playlist #365

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 5 additions & 9 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
after_action :verify_authorized, :verify_policy_scoped
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

before_action :set_playback_session_and_queue, :set_playlists
before_action :set_playback_session_and_queue
before_action :set_user_library

private
Expand All @@ -21,17 +21,13 @@ def set_playback_session_and_queue
@playback_queue = policy_scope(PlaybackQueue).find_or_create_by(user: current_user)
@playback_queue.ensure_default_items
@queue_items = @playback_queue.queue_items
.including_item_associations
.rank(:row_order)
.offset(1)
.including_item_associations
.rank(:row_order)
.offset(1)
end
end

def set_playlists
@playlists = policy_scope(Playlist).with_attached_image.limit(64) if Current&.user
end

def set_user_library
@user_library = policy_scope(UserLibrary).find_or_create_by!(user: Current.user) if Current&.user
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/library_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class LibraryItemsController < ApplicationController
def index
authorize @user_library, :show?

library_items = @user_library.library_items
library_items = @user_library.library_items.rank(row_order: :desc)

library_items = case params[:type]
when "playlists"
Expand Down
17 changes: 17 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class User < ApplicationRecord

has_one_attached :avatar

after_create_commit :setup_library_and_playlist

validates :username,
uniqueness: {case_sensitive: false},
length: {minimum: 3, maximum: 20},
Expand Down Expand Up @@ -55,6 +57,21 @@ def liked?(likeable)

private

def setup_library_and_playlist
User.transaction do
create_user_library! unless user_library
create_liked_playlist unless playlists.joins(:playlist_type).where("playlist_types.name = ?", "Liked").exists?
end
end

def create_liked_playlist
return unless user_library

playlist_type = PlaylistType.find_or_create_by!(name: "Liked")
liked_playlist = playlists.create!(title: "Liked", playlist_type:)
user_library.library_items.create!(item: liked_playlist, position: 0)
end

def search_data
{
username:,
Expand Down
Loading