diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 10faeaf09..b751d1b9a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 @@ -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 \ No newline at end of file +end diff --git a/app/controllers/library_items_controller.rb b/app/controllers/library_items_controller.rb index 7711de085..73d316c68 100644 --- a/app/controllers/library_items_controller.rb +++ b/app/controllers/library_items_controller.rb @@ -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" diff --git a/app/models/user.rb b/app/models/user.rb index e3523e69f..78cefe18c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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}, @@ -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:,