Skip to content

Commit

Permalink
impersonation, label checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed May 12, 2024
1 parent 43584ac commit c6579f3
Show file tree
Hide file tree
Showing 32 changed files with 543 additions and 129 deletions.
2 changes: 2 additions & 0 deletions app/controllers/account_connections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def impersonate
user = User.find_by(username: params[:username])
if current_user.child_accounts.find(user.id)
session[:parent_user] = current_user.id
Current.label_user = current_user
flash[:notice] = "signed as #{user.username}"
sign_in(:user, user)
redirect_to user_path(user.username)
Expand All @@ -75,6 +76,7 @@ def impersonate
if session[:parent_user].present?
user = User.find(session[:parent_user])
session[:parent_user] = nil
Current.label_user = nil
flash[:notice] = "signed as #{user.username}"
sign_in(:user, user)
redirect_to user_path(user.username)
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
before_action do
ActiveStorage::Current.url_options = {protocol: request.protocol, host: request.host, port: request.port}
# ActiveStorage::Current.url_options = { protocol: "http://", host: "localhost", port: "3000" }
Current.label_user = User.find(session[:parent_user]) if session[:parent_user].present?
end

before_action :set_locale
Expand All @@ -12,6 +13,11 @@ def flash_stream
turbo_stream.replace("flash", partial: "shared/flash", locals: { flash: flash })
end

helper_method :impersonating?
def impersonating?(user)
label_user.present? && current_user&.id == user&.id
end

def set_locale
if params[:locale].present?
cookies[:locale] = params[:locale]
Expand All @@ -23,6 +29,12 @@ def set_locale
end
end

helper_method :label_user
def label_user
return if session[:parent_user].blank?
@label_user ||= User.find session[:parent_user]
end

def become
if current_user.is_admin?
user = User.find_by(username: params[:id])
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/label_artists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class LabelArtistsController < ApplicationController


def index
@label = User.where(role: "artist", label: true).find_by(username: params[:user_id])
@label = User.where(role: ["artist", "admin"], label: true).find_by(username: params[:user_id])
end
end
6 changes: 6 additions & 0 deletions app/controllers/labels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class LabelsController < ApplicationController

before_action :find_user, except: [:index]
before_action :check_user_role, except: [:index]

end
18 changes: 13 additions & 5 deletions app/controllers/playlists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def show
def edit
@tab = params[:tab] || "basic-info-tab"
@playlist = current_user.playlists.friendly.find(params[:id])
@playlist.enable_label = @playlist.label_id.present?
end

def new
Expand All @@ -56,19 +57,25 @@ def new
def create
@tab = params[:tab] || "basic-info-tab"
@playlist = current_user.playlists.create(playlist_params)
if @playlist.errors.blank?
flash[:now] = "successfully created"
if @playlist
flash.now[:notice] = "successfully created"
else
flash.now[:error] = "error in creating"
end
end

def update
@tab = params[:tab] || "basic-info-tab"
@playlist = current_user.playlists.friendly.find(params[:id])

if !params[:nonpersist] && @playlist.update(playlist_params)
flash[:now] = "successfully updated"
end
@playlist.assign_attributes(playlist_params)

if !params[:nonpersist] && @playlist.save
flash.now[:notice] = "successfully updated"
else
flash.now[:error] = "error updating playlist"
end

if params[:nonpersist]
@playlist.assign_attributes(playlist_params)
end
Expand All @@ -83,6 +90,7 @@ def playlist_params
:title, :description, :private, :price,
:playlist_type, :release_date, :cover,
:record_label, :buy_link,
:enable_label,
:copyright,
:attribution, :noncommercial, :non_derivative_works, :copies,
track_playlists_attributes: [
Expand Down
16 changes: 12 additions & 4 deletions app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def create
audios = track_bulk_params["audio"].select { |o| o.is_a?(String) }.reject(&:empty?)
# @track = current_user.tracks.new(track_params)
@track_form.user = current_user
@track_form.private = track_bulk_params[:private]
@track_form.tracks_attributes = audios.map { |o| {audio: o} }
@track_form.step = "info"
else
Expand All @@ -44,12 +45,16 @@ def edit
def update
@track = current_user.tracks.friendly.find(params[:id])
@tab = params[:track][:tab] || "basic-info-tab"
@track.assign_attributes(track_params)
if params[:nonpersist]
@track.assign_attributes(track_params)
@track.valid?
else
flash.now[:notice] = "Track was successfully updated."
@track.update(track_params)
@track.label_id = label_user.id if !label_user.blank? && @track.enable_label
if @track.save
flash.now[:notice] = "Track was successfully updated."
else
flash.now[:error] = @track.errors.full_messages
end
end
# puts @track.errors.as_json
@track.tab = @tab
Expand Down Expand Up @@ -103,6 +108,7 @@ def get_meta_tags
def track_params
params.require(:track).permit(
:private,
:enable_label,
:audio, :title, :step, :description,
:tab, :genre, :contains_music, :artist, :publisher, :isrc,
:composer, :release_title, :buy_link, :album_title,
Expand All @@ -124,7 +130,9 @@ def check_activated_account

def track_bulk_params
params.require(:track_bulk_creator).permit(
:make_playlist, :private,
:make_playlist,
:private,
:enable_label,
:step,
audio: [], tracks_attributes: [
:audio, :cover, :title, :tags, :description
Expand Down
24 changes: 13 additions & 11 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ def playlists_filter

@kind = params[:kind].present? ? params[:kind].split(",") : Category.playlist_types

@playlists = @user.playlists
@playlists = Playlist
.where(playlist_type: @kind)
.where(user_id: @user.id)
.where(user_id: @user.id).or(Playlist.where(label_id: @user.id))
.where(private: false)
.with_attached_cover
.includes(user: {avatar_attachment: :blob})
.includes(tracks: {cover_attachment: :blob})
Expand Down Expand Up @@ -68,6 +69,8 @@ def playlists
@collection = @collection.where(private: false)
end

@collection.or(Playlist.where(label_id: @user.id)) if @user.label?

@collection = @collection.references(:tracks)
.page(params[:page])

Expand Down Expand Up @@ -152,19 +155,18 @@ def get_meta_tags
def get_tracks
# @collection = @user.tracks.page(params[:page]).per(2)
@collection = if current_user && @user.id == current_user&.id
User.track_preloaded_by_user(current_user&.id)
User.track_preloaded_by_user(current_user_id: current_user&.id, user: @user )
#.where(user_id: @user.id)
.with_attached_cover
.includes(user: {avatar_attachment: :blob})
.order("id desc")
.page(params[:page]).per(6)
else
User.track_preloaded_by_user_n(@user&.id)
User.track_preloaded_by_user_n(user: @user)
#.where(user_id: @user.id)
#@user.tracks.published
.with_attached_cover
.includes(user: {avatar_attachment: :blob})
.order("id desc").page(params[:page]).per(6)
end

@collection = @collection
.with_attached_cover
.includes(user: {avatar_attachment: :blob})
.order("id desc").page(params[:page]).per(6)
end

def find_user
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/labels_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LabelsHelper
end
1 change: 0 additions & 1 deletion app/javascript/controllers/audio_upload_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export default class extends Controller {
this.preventDefaults(e);
let files = e.dataTransfer.files;
this.inputTarget.files = files;
debugger
this.uploadFile();
}
}
12 changes: 12 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Current < ActiveSupport::CurrentAttributes
attribute :label_user, :user
attribute :request_id, :user_agent, :ip_address

resets { Time.zone = nil }

def user=(user)
super
#self.label_user = user.account
#Time.zone = user.time_zone
end
end
11 changes: 11 additions & 0 deletions app/models/playlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@ def self.plain
friendly_id :title, use: :slugged

belongs_to :user
belongs_to :label, class_name: "User", optional: true

has_many :track_playlists
has_many :tracks, through: :track_playlists
has_many :listening_events
has_many :comments, as: :commentable
has_many :likes, as: :likeable

has_one_attached :cover
has_one_attached :zip

acts_as_likeable

accepts_nested_attributes_for :track_playlists, allow_destroy: true

belongs_to :label, class_name: "User", optional: true
attr_accessor :enable_label
before_save :check_label

def check_label
self.label_id = Current.label_user.id if enable_label && Current.label_user
end

scope :latests, -> { order("id desc") }
scope :published, -> { where(private: false) }

Expand Down
7 changes: 7 additions & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class Track < ApplicationRecord
has_many :spotlights, as: :spotlightable
# has_many :spotlighted_tracks, through: :spotlight_tracks

belongs_to :label, class_name: "User", optional: true
attr_accessor :enable_label
before_save :check_label

def check_label
self.label_id = Current.label_user.id if enable_label && Current.label_user
end

has_one_attached :cover
has_one_attached :audio
Expand Down
2 changes: 1 addition & 1 deletion app/models/track_bulk_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TrackBulkCreator

# Initialize the tracks_attributes with an empty array
def initialize(attributes = {})
self.private = true
self.private = attributes[:private]
self.tracks_attributes ||= []
end

Expand Down
Loading

0 comments on commit c6579f3

Please sign in to comment.