2012-10-03 03:50:23 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class MusicSession < ActiveRecord::Base
|
|
|
|
|
self.primary_key = 'id'
|
2012-11-02 06:51:52 +00:00
|
|
|
|
2013-06-11 01:25:42 +00:00
|
|
|
attr_accessor :legal_terms, :skip_genre_validation
|
2013-06-11 01:37:10 +00:00
|
|
|
attr_accessible :creator, :description, :musician_access, :approval_required, :fan_chat, :fan_access, :genres
|
2012-11-16 02:50:03 +00:00
|
|
|
|
2012-10-03 03:50:23 +00:00
|
|
|
belongs_to :creator, :inverse_of => :music_sessions, :class_name => "JamRuby::User", :foreign_key => "user_id"
|
|
|
|
|
|
2012-10-21 01:55:49 +00:00
|
|
|
has_many :connections, :class_name => "JamRuby::Connection"
|
|
|
|
|
has_many :users, :through => :connections, :class_name => "JamRuby::User"
|
2012-10-25 12:11:10 +00:00
|
|
|
has_and_belongs_to_many :genres, :class_name => "::JamRuby::Genre", :join_table => "genres_music_sessions"
|
2012-11-30 15:23:43 +00:00
|
|
|
has_many :join_requests, :foreign_key => "music_session_id", :inverse_of => :music_session, :class_name => "JamRuby::JoinRequest"
|
2012-10-26 10:33:39 +00:00
|
|
|
has_many :invitations, :foreign_key => "music_session_id", :inverse_of => :music_session, :class_name => "JamRuby::Invitation"
|
2012-11-16 02:50:03 +00:00
|
|
|
has_many :invited_musicians, :through => :invitations, :class_name => "JamRuby::User", :foreign_key => "receiver_id", :source => :receiver
|
|
|
|
|
|
|
|
|
|
has_many :fan_invitations, :foreign_key => "music_session_id", :inverse_of => :music_session, :class_name => "JamRuby::FanInvitation"
|
|
|
|
|
has_many :invited_fans, :through => :fan_invitations, :class_name => "JamRuby::User", :foreign_key => "receiver_id", :source => :receiver
|
2013-11-03 20:55:55 +00:00
|
|
|
has_many :recordings, :class_name => "JamRuby::Recording", :inverse_of => :music_session
|
2012-11-30 15:23:43 +00:00
|
|
|
belongs_to :band, :inverse_of => :music_sessions, :class_name => "JamRuby::Band", :foreign_key => "band_id"
|
|
|
|
|
|
2013-06-11 01:37:10 +00:00
|
|
|
after_save :require_at_least_one_genre, :limit_max_genres
|
2012-10-25 12:11:10 +00:00
|
|
|
|
2013-07-23 20:20:39 +00:00
|
|
|
after_destroy do |obj|
|
2013-09-30 02:37:22 +00:00
|
|
|
JamRuby::MusicSessionHistory.removed_music_session(obj.id)
|
2013-07-23 20:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
2013-07-26 08:07:24 +00:00
|
|
|
validates :description, :presence => true, :no_profanity => true
|
2012-11-16 02:50:03 +00:00
|
|
|
validates :fan_chat, :inclusion => {:in => [true, false]}
|
|
|
|
|
validates :fan_access, :inclusion => {:in => [true, false]}
|
|
|
|
|
validates :approval_required, :inclusion => {:in => [true, false]}
|
|
|
|
|
validates :musician_access, :inclusion => {:in => [true, false]}
|
2013-06-11 01:25:42 +00:00
|
|
|
validates :legal_terms, :inclusion => {:in => [true]}, :on => :create
|
2012-11-30 15:23:43 +00:00
|
|
|
validates :creator, :presence => true
|
|
|
|
|
validate :creator_is_musician
|
|
|
|
|
|
|
|
|
|
def creator_is_musician
|
|
|
|
|
unless creator.musician?
|
2013-11-03 20:55:55 +00:00
|
|
|
errors.add(:creator, "must be a musician")
|
2012-11-30 15:23:43 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-11-16 02:50:03 +00:00
|
|
|
|
2012-12-06 01:56:12 +00:00
|
|
|
# This is a little confusing. You can specify *BOTH* friends_only and my_bands_only to be true
|
|
|
|
|
# If so, then it's an OR condition. If both are false, you can get sessions with anyone.
|
2013-02-02 23:55:26 +00:00
|
|
|
def self.index(current_user, participants = nil, genres = nil, friends_only = false, my_bands_only = false, keyword = nil)
|
2012-12-06 01:56:12 +00:00
|
|
|
|
|
|
|
|
query = MusicSession
|
2012-12-05 19:10:58 +00:00
|
|
|
.joins(
|
|
|
|
|
%Q{
|
2013-09-22 18:56:51 +00:00
|
|
|
INNER JOIN
|
2012-12-05 19:10:58 +00:00
|
|
|
connections
|
|
|
|
|
ON
|
|
|
|
|
music_sessions.id = connections.music_session_id
|
|
|
|
|
}
|
|
|
|
|
)
|
2012-12-04 06:37:36 +00:00
|
|
|
.joins(
|
|
|
|
|
%Q{
|
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
|
friendships
|
|
|
|
|
ON
|
2012-12-05 19:10:58 +00:00
|
|
|
connections.user_id = friendships.user_id
|
2012-12-04 06:37:36 +00:00
|
|
|
AND
|
|
|
|
|
friendships.friend_id = '#{current_user.id}'
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.joins(
|
|
|
|
|
%Q{
|
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
|
invitations
|
|
|
|
|
ON
|
2012-12-04 07:00:32 +00:00
|
|
|
invitations.music_session_id = music_sessions.id
|
|
|
|
|
AND
|
2012-12-04 06:37:36 +00:00
|
|
|
invitations.receiver_id = '#{current_user.id}'
|
|
|
|
|
}
|
|
|
|
|
)
|
2012-12-05 19:10:58 +00:00
|
|
|
.group(
|
|
|
|
|
%Q{
|
|
|
|
|
music_sessions.id
|
|
|
|
|
}
|
|
|
|
|
)
|
2012-12-04 06:37:36 +00:00
|
|
|
.order(
|
|
|
|
|
%Q{
|
2012-12-05 19:10:58 +00:00
|
|
|
SUM(CASE WHEN invitations.id IS NULL THEN 0 ELSE 1 END) DESC,
|
|
|
|
|
SUM(CASE WHEN friendships.user_id IS NULL THEN 0 ELSE 1 END) DESC,
|
2012-12-04 06:37:36 +00:00
|
|
|
music_sessions.created_at DESC
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.where(
|
|
|
|
|
%Q{
|
2012-12-05 19:10:58 +00:00
|
|
|
musician_access = true
|
|
|
|
|
OR
|
|
|
|
|
invitations.id IS NOT NULL
|
2012-12-04 06:37:36 +00:00
|
|
|
}
|
|
|
|
|
)
|
2013-01-30 15:43:01 +00:00
|
|
|
|
2013-02-02 23:55:26 +00:00
|
|
|
query = query.where("music_sessions.description like '%#{keyword}%'") unless keyword.nil?
|
2013-01-30 15:43:01 +00:00
|
|
|
query = query.where("connections.user_id" => participants.split(',')) unless participants.nil?
|
|
|
|
|
query = query.joins(:genres).where("genres.id" => genres.split(',')) unless genres.nil?
|
2012-12-06 01:56:12 +00:00
|
|
|
|
|
|
|
|
if my_bands_only
|
|
|
|
|
query = query.joins(
|
|
|
|
|
%Q{
|
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
|
bands_musicians
|
|
|
|
|
ON
|
|
|
|
|
bands_musicians.user_id = '#{current_user.id}'
|
|
|
|
|
}
|
|
|
|
|
)
|
2012-12-05 19:10:58 +00:00
|
|
|
end
|
2012-12-06 01:56:12 +00:00
|
|
|
|
|
|
|
|
if my_bands_only || friends_only
|
|
|
|
|
query = query.where(
|
|
|
|
|
%Q{
|
|
|
|
|
#{friends_only ? "friendships.user_id IS NOT NULL" : "false"}
|
|
|
|
|
OR
|
|
|
|
|
#{my_bands_only ? "bands_musicians.band_id = music_sessions.band_id" : "false"}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return query
|
2012-11-30 15:23:43 +00:00
|
|
|
end
|
2012-11-02 06:51:52 +00:00
|
|
|
|
|
|
|
|
# Verifies that the specified user can join this music session
|
2012-11-18 03:41:12 +00:00
|
|
|
def can_join? user, as_musician
|
|
|
|
|
if as_musician
|
2012-11-30 15:23:43 +00:00
|
|
|
if !user.musician
|
|
|
|
|
return false # "a fan can not join a music session as a musician"
|
|
|
|
|
raise PermissionError, "a fan can not join a music session as a musician"
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-18 03:41:12 +00:00
|
|
|
if self.musician_access
|
2012-11-30 15:23:43 +00:00
|
|
|
if self.approval_required
|
|
|
|
|
return self.invited_musicians.exists?(user)
|
|
|
|
|
else
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-18 03:41:12 +00:00
|
|
|
else
|
|
|
|
|
# the creator can always join, and the invited users can join
|
|
|
|
|
return self.creator == user || self.invited_musicians.exists?(user)
|
|
|
|
|
end
|
2012-11-02 06:51:52 +00:00
|
|
|
else
|
2012-11-18 03:41:12 +00:00
|
|
|
# it's a fan, and the only way a fan can join is if fan_access is true
|
|
|
|
|
return self.fan_access
|
2012-11-02 06:51:52 +00:00
|
|
|
end
|
2012-11-18 03:41:12 +00:00
|
|
|
|
2012-11-02 06:51:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Verifies that the specified user can see this music session
|
|
|
|
|
def can_see? user
|
|
|
|
|
if self.musician_access
|
|
|
|
|
return true
|
|
|
|
|
else
|
|
|
|
|
# the creator can always see, and the invited users can see it too
|
2012-11-16 02:50:03 +00:00
|
|
|
return self.creator == user || self.invited_musicians.exists?(user)
|
2012-11-02 06:51:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Verifies that the specified user can delete this music session
|
|
|
|
|
def can_delete? user
|
|
|
|
|
# the creator can delete
|
|
|
|
|
return self.creator == user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def access? user
|
|
|
|
|
return self.users.exists? user
|
2012-10-03 03:50:23 +00:00
|
|
|
end
|
2013-11-03 20:55:55 +00:00
|
|
|
|
|
|
|
|
# is this music session currently recording?
|
|
|
|
|
def is_recording?
|
|
|
|
|
recordings.where(:duration => nil).count > 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def recording
|
|
|
|
|
recordings.where(:duration => nil).first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# stops any active recording
|
|
|
|
|
def stop_recording
|
|
|
|
|
current_recording = self.recording
|
|
|
|
|
current_recording.stop unless current_recording.nil?
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-03 03:50:23 +00:00
|
|
|
def to_s
|
2012-10-25 12:11:10 +00:00
|
|
|
return description
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def require_at_least_one_genre
|
2013-06-11 01:25:42 +00:00
|
|
|
unless skip_genre_validation
|
|
|
|
|
if self.genres.count < Limits::MIN_GENRES_PER_RECORDING
|
|
|
|
|
errors.add(:genres, ValidationMessages::GENRE_MINIMUM_NOT_MET)
|
|
|
|
|
end
|
2012-10-25 12:11:10 +00:00
|
|
|
end
|
2012-10-03 03:50:23 +00:00
|
|
|
end
|
|
|
|
|
|
2013-06-11 01:37:10 +00:00
|
|
|
def limit_max_genres
|
2013-06-11 01:25:42 +00:00
|
|
|
unless skip_genre_validation
|
|
|
|
|
if self.genres.count > Limits::MAX_GENRES_PER_RECORDING
|
|
|
|
|
errors.add(:genres, ValidationMessages::GENRE_LIMIT_EXCEEDED)
|
|
|
|
|
end
|
2012-10-25 12:11:10 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-10-03 03:50:23 +00:00
|
|
|
end
|
|
|
|
|
end
|