2012-08-06 03:01:00 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
|
2012-11-15 03:24:30 +00:00
|
|
|
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :city, :state, :country
|
2012-10-30 00:56:51 +00:00
|
|
|
attr_accessor :updating_password
|
2012-08-18 18:48:43 +00:00
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
self.primary_key = 'id'
|
2012-11-07 13:10:41 +00:00
|
|
|
|
2012-11-13 07:40:06 +00:00
|
|
|
# authorizations (for facebook, etc -- omniauth)
|
2012-11-13 21:21:04 +00:00
|
|
|
has_many :user_authorizations, :class_name => "JamRuby::UserAuthorization"
|
2012-11-13 02:52:05 +00:00
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# connections (websocket-gateway)
|
2012-10-02 05:02:02 +00:00
|
|
|
has_many :connections, :class_name => "JamRuby::Connection"
|
2012-10-01 21:27:32 +00:00
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# friend requests
|
2012-10-14 02:18:20 +00:00
|
|
|
has_many :friend_requests, :class_name => "JamRuby::FriendRequest"
|
|
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# instruments
|
2012-11-21 19:48:39 +00:00
|
|
|
has_many :musician_instruments, :class_name => "JamRuby::MusicianInstrument"
|
2012-10-30 05:42:16 +00:00
|
|
|
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
|
2012-11-07 13:10:41 +00:00
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# bands
|
2012-11-21 19:48:39 +00:00
|
|
|
has_many :band_musicians, :class_name => "JamRuby::BandMusician"
|
2012-10-30 05:42:16 +00:00
|
|
|
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
|
2012-10-14 02:18:20 +00:00
|
|
|
|
2012-11-16 02:08:37 +00:00
|
|
|
# recordings
|
2012-11-21 19:48:39 +00:00
|
|
|
has_many :musician_recordings, :class_name => "JamRuby::MusicianRecording", :foreign_key => "user_id"
|
|
|
|
|
has_many :recordings, :through => :musician_recordings, :class_name => "JamRuby::Recording"
|
2012-11-16 02:08:37 +00:00
|
|
|
|
2012-12-04 03:39:57 +00:00
|
|
|
# user likers (a musician has likers and may have likes too; fans do not have likers)
|
2012-12-17 06:01:48 +00:00
|
|
|
has_many :likers, :class_name => "JamRuby::UserLiker", :foreign_key => "user_id", :inverse_of => :user
|
2012-12-04 03:39:57 +00:00
|
|
|
has_many :inverse_likers, :through => :likers, :class_name => "JamRuby::User", :foreign_key => "liker_id"
|
2012-11-03 13:54:55 +00:00
|
|
|
|
2012-12-04 03:39:57 +00:00
|
|
|
# user likes (fans and musicians have likes)
|
2012-12-17 06:01:48 +00:00
|
|
|
has_many :likes, :class_name => "JamRuby::UserLike", :foreign_key => "liker_id", :inverse_of => :user
|
2012-12-04 03:39:57 +00:00
|
|
|
has_many :inverse_likes, :through => :followings, :class_name => "JamRuby::User", :foreign_key => "user_id"
|
2012-11-04 13:33:51 +00:00
|
|
|
|
2012-12-04 03:39:57 +00:00
|
|
|
# band likes
|
2012-12-17 06:01:48 +00:00
|
|
|
has_many :band_likes, :class_name => "JamRuby::BandLiker", :foreign_key => "liker_id", :inverse_of => :user
|
2012-12-04 03:39:57 +00:00
|
|
|
has_many :inverse_band_likes, :through => :band_likes, :class_name => "JamRuby::Band", :foreign_key => "band_id"
|
|
|
|
|
|
2012-12-17 06:01:48 +00:00
|
|
|
# followers
|
|
|
|
|
has_many :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id", :inverse_of => :user
|
|
|
|
|
has_many :inverse_followers, :through => :followers, :class_name => "JamRuby::User", :foreign_key => "follower_id"
|
|
|
|
|
|
|
|
|
|
# user followings
|
|
|
|
|
has_many :followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "follower_id", :inverse_of => :user
|
|
|
|
|
has_many :inverse_followings, :through => :followings, :class_name => "JamRuby::User", :foreign_key => "user_id"
|
|
|
|
|
|
|
|
|
|
# band followings
|
|
|
|
|
has_many :band_followings, :class_name => "JamRuby::BandFollower", :foreign_key => "follower_id", :inverse_of => :user
|
|
|
|
|
has_many :inverse_band_followings, :through => :band_followings, :class_name => "JamRuby::Band", :foreign_key => "band_id"
|
|
|
|
|
|
2012-11-16 02:08:37 +00:00
|
|
|
# favorites
|
2012-11-21 19:48:39 +00:00
|
|
|
has_many :favorites, :class_name => "JamRuby::UserFavorite", :foreign_key => "user_id"
|
2012-12-04 03:39:57 +00:00
|
|
|
has_many :inverse_favorites, :through => :favorites, :class_name => "JamRuby::User"
|
2012-11-03 13:54:55 +00:00
|
|
|
|
2012-10-29 10:45:47 +00:00
|
|
|
# friends
|
2012-10-26 10:33:39 +00:00
|
|
|
has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id"
|
|
|
|
|
has_many :friends, :through => :friendships, :class_name => "JamRuby::User"
|
2012-10-01 21:27:32 +00:00
|
|
|
has_many :inverse_friendships, :class_name => "JamRuby::Friendship", :foreign_key => "friend_id"
|
2012-10-26 10:33:39 +00:00
|
|
|
has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :class_name => "JamRuby::User"
|
2012-11-03 19:32:27 +00:00
|
|
|
|
|
|
|
|
# connections / music sessions
|
2012-10-03 03:50:23 +00:00
|
|
|
has_many :created_music_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::MusicSession" # sessions *created* by the user
|
2012-11-02 06:51:52 +00:00
|
|
|
has_many :music_sessions, :through => :connections, :class_name => "JamRuby::MusicSession"
|
2012-10-26 10:33:39 +00:00
|
|
|
|
2012-11-04 03:02:11 +00:00
|
|
|
# invitations
|
2012-10-26 10:33:39 +00:00
|
|
|
has_many :received_invitations, :foreign_key => "receiver_id", :inverse_of => :receiver, :class_name => "JamRuby::Invitation"
|
|
|
|
|
has_many :sent_invitations, :foreign_key => "sender_id", :inverse_of => :sender, :class_name => "JamRuby::Invitation"
|
2012-11-07 13:10:41 +00:00
|
|
|
|
2012-11-25 19:37:54 +00:00
|
|
|
# fan invitations
|
2012-11-16 02:50:03 +00:00
|
|
|
has_many :received_fan_invitations, :foreign_key => "receiver_id", :inverse_of => :receiver, :class_name => "JamRuby::FanInvitation"
|
|
|
|
|
has_many :sent_fan_invitations, :foreign_key => "sender_id", :inverse_of => :sender, :class_name => "JamRuby::FanInvitation"
|
|
|
|
|
|
2012-11-24 18:22:44 +00:00
|
|
|
# band invitations
|
|
|
|
|
has_many :received_band_invitations, :inverse_of => :receiver, :foreign_key => "user_id", :class_name => "JamRuby::BandInvitation"
|
|
|
|
|
has_many :sent_band_invitations, :inverse_of => :sender, :foreign_key => "creator_id", :class_name => "JamRuby::BandInvitation"
|
|
|
|
|
|
2013-01-06 20:46:48 +00:00
|
|
|
# session history
|
|
|
|
|
has_many :music_session_histories, :foreign_key => "user_id", :class_name => "JamRuby::MusicSessionHistory"
|
|
|
|
|
|
2013-01-15 02:13:45 +00:00
|
|
|
# saved tracks
|
|
|
|
|
has_many :saved_tracks, :foreign_key => "user_id", :class_name => "JamRuby::SavedTrack", :inverse_of => :user
|
|
|
|
|
|
2012-11-09 06:51:17 +00:00
|
|
|
# This causes the authenticate method to be generated (among other stuff)
|
2012-08-06 03:01:00 +00:00
|
|
|
has_secure_password
|
|
|
|
|
|
|
|
|
|
before_save { |user| user.email = email.downcase }
|
2012-12-09 04:05:54 +00:00
|
|
|
before_save :create_remember_token, :if => :should_validate_password?
|
2012-11-03 15:38:00 +00:00
|
|
|
|
2012-11-11 04:23:38 +00:00
|
|
|
validates :first_name, presence: true, length: {maximum: 50}
|
|
|
|
|
validates :last_name, presence: true, length: {maximum: 50}
|
2012-08-06 03:01:00 +00:00
|
|
|
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
|
|
|
|
|
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX},
|
|
|
|
|
uniqueness: {case_sensitive: false}
|
2012-08-29 13:21:30 +00:00
|
|
|
validates_length_of :password, minimum: 6, maximum: 100, :if => :should_validate_password?
|
2012-08-06 03:01:00 +00:00
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
validates_presence_of :password_confirmation, :if => :should_validate_password?
|
|
|
|
|
validates_confirmation_of :password, :if => :should_validate_password?
|
2012-08-29 13:21:30 +00:00
|
|
|
|
2012-10-14 02:18:20 +00:00
|
|
|
def online
|
2012-11-09 06:29:05 +00:00
|
|
|
@online ||= !self.connections.nil? && self.connections.size > 0
|
2012-10-14 02:18:20 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-18 02:59:59 +00:00
|
|
|
def name
|
|
|
|
|
return "#{first_name} #{last_name}"
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-07 13:10:41 +00:00
|
|
|
def location
|
2013-01-06 12:34:16 +00:00
|
|
|
loc = self.city.blank? ? '' : self.city
|
|
|
|
|
loc = loc.blank? ? self.state : "#{loc}, #{self.state}" unless self.state.blank?
|
|
|
|
|
loc = loc.blank? ? self.country : "#{loc}, #{self.country}" unless self.country.blank?
|
|
|
|
|
loc
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def location= location_hash
|
|
|
|
|
unless location_hash.blank?
|
|
|
|
|
self.city = location_hash[:city]
|
|
|
|
|
self.state = location_hash[:state]
|
|
|
|
|
self.country = location_hash[:country]
|
|
|
|
|
end if self.city.blank?
|
2012-11-07 13:10:41 +00:00
|
|
|
end
|
|
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
def should_validate_password?
|
|
|
|
|
updating_password || new_record?
|
|
|
|
|
end
|
2012-08-26 18:28:08 +00:00
|
|
|
|
2012-10-07 18:02:26 +00:00
|
|
|
def friends?(user)
|
|
|
|
|
return self.friends.exists?(user)
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-06 04:47:50 +00:00
|
|
|
def friend_count
|
|
|
|
|
return self.friends.size
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-04 03:39:57 +00:00
|
|
|
def liker_count
|
2012-12-17 07:02:20 +00:00
|
|
|
return self.likers.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def like_count
|
|
|
|
|
return self.likes.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def band_like_count
|
|
|
|
|
return self.band_likes.size
|
2012-12-04 03:39:57 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-06 04:47:50 +00:00
|
|
|
def follower_count
|
|
|
|
|
return self.followers.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def following_count
|
2012-12-17 07:02:20 +00:00
|
|
|
return self.followings.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def band_following_count
|
|
|
|
|
return self.band_followings.size
|
2012-11-06 04:47:50 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
def favorite_count
|
|
|
|
|
return self.favorites.size
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-04 03:39:57 +00:00
|
|
|
def recording_count
|
|
|
|
|
return self.recordings.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def session_count
|
|
|
|
|
return self.music_sessions.size
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def confirm_email!
|
2012-12-09 04:05:54 +00:00
|
|
|
self.email_confirmed = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def my_session_settings
|
|
|
|
|
unless self.session_settings.nil?
|
|
|
|
|
return JSON.parse(self.session_settings)
|
|
|
|
|
else
|
|
|
|
|
return ""
|
|
|
|
|
end
|
2012-12-04 03:39:57 +00:00
|
|
|
end
|
|
|
|
|
|
2013-01-06 20:46:48 +00:00
|
|
|
def session_history(user_id, band_id = nil, genre = nil)
|
|
|
|
|
return MusicSessionHistory.index(self, user_id, band_id, genre)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def session_user_history(user_id, session_id)
|
|
|
|
|
return MusicSessionUserHistory.where("music_session_id='#{session_id}'")
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
def to_s
|
|
|
|
|
return email unless email.nil?
|
2012-11-15 03:24:30 +00:00
|
|
|
|
|
|
|
|
if !first_name.nil? && !last_name.nil?
|
|
|
|
|
return first_name + ' ' + last_name
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
return id
|
2012-08-29 13:21:30 +00:00
|
|
|
end
|
|
|
|
|
|
2012-12-14 03:32:23 +00:00
|
|
|
def set_password(old_password, new_password, new_password_confirmation)
|
|
|
|
|
raise JamRuby::JamArgumentError unless authenticate old_password
|
2012-12-22 00:56:16 +00:00
|
|
|
change_password(new_password, new_password_confirmation)
|
|
|
|
|
save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.set_password_from_token(email, token, new_password, new_password_confirmation)
|
|
|
|
|
user = User.find_by_email(email)
|
|
|
|
|
if user.nil? || user.reset_password_token != token || Time.now - user.reset_password_token_created > 3.days
|
|
|
|
|
raise JamRuby::JamArgumentError
|
|
|
|
|
end
|
|
|
|
|
user.reset_password_token = nil
|
|
|
|
|
user.reset_password_token_created = nil
|
|
|
|
|
user.change_password(new_password, new_password_confirmation)
|
|
|
|
|
user.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def change_password(new_password, new_password_confirmation)
|
2012-12-14 03:32:23 +00:00
|
|
|
# FIXME: Should verify that the new password meets certain quality criteria. Really, maybe that should be a
|
|
|
|
|
# verification step.
|
2012-12-22 00:56:16 +00:00
|
|
|
self.updating_password = true
|
2012-12-14 03:32:23 +00:00
|
|
|
self.password = new_password
|
|
|
|
|
self.password_confirmation = new_password_confirmation
|
2012-12-14 09:16:54 +00:00
|
|
|
|
|
|
|
|
UserMailer.password_changed(self).deliver
|
2012-12-13 17:15:47 +00:00
|
|
|
end
|
|
|
|
|
|
2012-12-22 00:56:16 +00:00
|
|
|
def self.reset_password(email)
|
|
|
|
|
user = User.find_by_email(email)
|
|
|
|
|
raise JamRuby::JamArgumentError if user.nil?
|
|
|
|
|
|
|
|
|
|
user.reset_password_token = SecureRandom.urlsafe_base64
|
|
|
|
|
user.reset_password_token_created = Time.now
|
|
|
|
|
user.save
|
|
|
|
|
|
|
|
|
|
UserMailer.password_reset(user).deliver
|
2012-12-28 07:30:03 +00:00
|
|
|
|
|
|
|
|
user
|
2012-12-22 00:56:16 +00:00
|
|
|
end
|
|
|
|
|
|
2012-12-17 07:02:20 +00:00
|
|
|
def self.band_index(user_id)
|
|
|
|
|
bands = Band.joins(:band_musicians)
|
|
|
|
|
.where(:bands_musicians => {:user_id => "#{user_id}"})
|
|
|
|
|
|
|
|
|
|
return bands
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.recording_index(current_user, user_id)
|
|
|
|
|
hide_private = false
|
|
|
|
|
|
|
|
|
|
# hide private recordings from anyone but the current user
|
|
|
|
|
if current_user.id != user_id
|
|
|
|
|
hide_private = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if hide_private
|
|
|
|
|
recordings = Recording.joins(:musician_recordings)
|
|
|
|
|
.where(:musicians_recordings => {:user_id => "#{user_id}"}, :public => true)
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
recordings = Recording.joins(:musician_recordings)
|
|
|
|
|
.where(:musicians_recordings => {:user_id => "#{user_id}"})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return recordings
|
|
|
|
|
end
|
2012-12-13 17:15:47 +00:00
|
|
|
|
2012-11-03 13:54:55 +00:00
|
|
|
# helper method for creating / updating a User
|
2012-11-22 08:27:23 +00:00
|
|
|
def self.save(id, updater_id, first_name, last_name, email, password, password_confirmation, musician, gender,
|
2012-11-28 05:26:53 +00:00
|
|
|
birth_date, internet_service_provider, city, state, country, instruments, photo_url)
|
2012-11-21 19:48:39 +00:00
|
|
|
if id.nil?
|
2012-11-25 19:37:54 +00:00
|
|
|
validate_instruments(instruments, musician)
|
2012-10-29 10:45:47 +00:00
|
|
|
user = User.new()
|
|
|
|
|
else
|
2012-11-25 19:37:54 +00:00
|
|
|
validate_instruments(instruments, true)
|
2012-11-21 19:48:39 +00:00
|
|
|
user = User.find(id)
|
2012-11-13 02:52:05 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-22 08:27:23 +00:00
|
|
|
if user.id != updater_id
|
2012-11-25 19:37:54 +00:00
|
|
|
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
2012-11-22 08:27:23 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-11 04:23:38 +00:00
|
|
|
# first name
|
2012-11-21 19:48:39 +00:00
|
|
|
unless first_name.nil?
|
|
|
|
|
user.first_name = first_name
|
2012-11-11 04:23:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# last name
|
2012-11-21 19:48:39 +00:00
|
|
|
unless last_name.nil?
|
|
|
|
|
user.last_name = last_name
|
2012-11-11 04:23:38 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-03 13:54:55 +00:00
|
|
|
# email
|
2012-11-21 19:48:39 +00:00
|
|
|
unless email.nil?
|
|
|
|
|
user.email = email
|
2012-11-03 13:54:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# password
|
2012-11-21 19:48:39 +00:00
|
|
|
unless password.nil?
|
|
|
|
|
user.password = password
|
2012-10-29 10:45:47 +00:00
|
|
|
end
|
2012-11-03 13:54:55 +00:00
|
|
|
|
|
|
|
|
# password confirmation
|
2012-11-21 19:48:39 +00:00
|
|
|
unless password_confirmation.nil?
|
|
|
|
|
user.password_confirmation = password_confirmation
|
2012-11-03 13:54:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# musician flag
|
2012-11-21 19:48:39 +00:00
|
|
|
unless musician.nil?
|
|
|
|
|
user.musician = musician
|
2012-11-03 13:54:55 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-13 02:52:05 +00:00
|
|
|
# gender
|
2012-11-21 19:48:39 +00:00
|
|
|
unless gender.nil?
|
|
|
|
|
account.gender = gender
|
2012-11-13 02:52:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# birthdate
|
2012-11-21 19:48:39 +00:00
|
|
|
unless birth_date.nil?
|
|
|
|
|
account.birth_date = birth_date
|
2012-11-13 02:52:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# ISP
|
2012-11-21 19:48:39 +00:00
|
|
|
unless internet_service_provider.nil?
|
|
|
|
|
account.internet_service_provider = internet_service_provider
|
2012-11-13 02:52:05 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-06 02:55:08 +00:00
|
|
|
# city
|
2012-11-21 19:48:39 +00:00
|
|
|
unless city.nil?
|
|
|
|
|
user.city = city
|
2012-11-06 02:55:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# state
|
2012-11-21 19:48:39 +00:00
|
|
|
unless state.nil?
|
|
|
|
|
user.state = state
|
2012-11-06 02:55:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# country
|
2012-11-21 19:48:39 +00:00
|
|
|
unless country.nil?
|
|
|
|
|
user.country = country
|
2012-11-06 02:55:08 +00:00
|
|
|
end
|
|
|
|
|
|
2012-11-03 13:54:55 +00:00
|
|
|
# instruments
|
2012-11-21 19:48:39 +00:00
|
|
|
unless instruments.nil?
|
|
|
|
|
UserManager.active_record_transaction do |user_manager|
|
2012-11-03 13:54:55 +00:00
|
|
|
# delete all instruments for this user first
|
2012-11-03 19:32:27 +00:00
|
|
|
unless user.id.nil? || user.id.length == 0
|
|
|
|
|
MusicianInstrument.delete_all(["user_id = ?", user.id])
|
|
|
|
|
end
|
2012-11-03 13:54:55 +00:00
|
|
|
|
|
|
|
|
# loop through each instrument in the array and save to the db
|
2012-11-25 19:37:54 +00:00
|
|
|
instruments.each do |musician_instrument_param|
|
|
|
|
|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
|
|
|
|
|
musician_instrument = MusicianInstrument.new
|
|
|
|
|
musician_instrument.user = user
|
|
|
|
|
musician_instrument.instrument = instrument
|
|
|
|
|
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
|
|
|
|
|
musician_instrument.priority = musician_instrument_param[:priority]
|
|
|
|
|
musician_instrument.save
|
|
|
|
|
user.musician_instruments << musician_instrument
|
2012-11-03 13:54:55 +00:00
|
|
|
end
|
|
|
|
|
end
|
2012-10-29 10:45:47 +00:00
|
|
|
end
|
2012-11-03 13:54:55 +00:00
|
|
|
|
2012-11-28 05:26:53 +00:00
|
|
|
# photo url
|
|
|
|
|
unless photo_url.nil?
|
|
|
|
|
user.photo_url = photo_url
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-03 13:54:55 +00:00
|
|
|
user.updated_at = Time.now.getutc
|
|
|
|
|
user.save
|
2012-10-29 10:45:47 +00:00
|
|
|
return user
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-17 06:01:48 +00:00
|
|
|
def self.create_user_like(user_id, liker_id)
|
|
|
|
|
liker = UserLiker.new()
|
|
|
|
|
liker.user_id = user_id
|
|
|
|
|
liker.liker_id = liker_id
|
|
|
|
|
liker.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.delete_like(user_id, band_id, liker_id)
|
|
|
|
|
if !user_id.nil?
|
|
|
|
|
JamRuby::UserLiker.delete_all "(user_id = '#{user_id}' AND liker_id = '#{liker_id}')"
|
|
|
|
|
|
|
|
|
|
elsif !band_id.nil?
|
|
|
|
|
JamRuby::BandLiker.delete_all "(band_id = '#{band_id}' AND liker_id = '#{liker_id}')"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.create_band_like(band_id, liker_id)
|
|
|
|
|
liker = BandLiker.new()
|
|
|
|
|
liker.band_id = band_id
|
|
|
|
|
liker.liker_id = liker_id
|
|
|
|
|
liker.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.delete_band_like(band_id, liker_id)
|
|
|
|
|
JamRuby::BandLiker.delete_all "(band_id = '#{band_id}' AND liker_id = '#{liker_id}')"
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
def self.create_user_following(user_id, follower_id)
|
2012-11-22 08:27:23 +00:00
|
|
|
follower = UserFollower.new()
|
|
|
|
|
follower.user_id = user_id
|
|
|
|
|
follower.follower_id = follower_id
|
|
|
|
|
follower.save
|
2012-11-21 19:48:39 +00:00
|
|
|
end
|
|
|
|
|
|
2012-12-04 03:39:57 +00:00
|
|
|
def self.delete_following(user_id, band_id, follower_id)
|
|
|
|
|
if !user_id.nil?
|
|
|
|
|
JamRuby::UserFollower.delete_all "(user_id = '#{user_id}' AND follower_id = '#{follower_id}')"
|
|
|
|
|
|
|
|
|
|
elsif !band_id.nil?
|
|
|
|
|
JamRuby::BandFollower.delete_all "(band_id = '#{band_id}' AND follower_id = '#{follower_id}')"
|
|
|
|
|
end
|
2012-11-21 19:48:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.create_band_following(band_id, follower_id)
|
2012-11-22 08:27:23 +00:00
|
|
|
follower = BandFollower.new()
|
|
|
|
|
follower.band_id = band_id
|
|
|
|
|
follower.follower_id = follower_id
|
|
|
|
|
follower.save
|
2012-11-21 19:48:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.delete_band_following(band_id, follower_id)
|
|
|
|
|
JamRuby::BandFollower.delete_all "(band_id = '#{band_id}' AND follower_id = '#{follower_id}')"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.create_favorite(user_id, recording_id)
|
2012-11-22 08:27:23 +00:00
|
|
|
favorite = UserFavorite.new()
|
|
|
|
|
favorite.user_id = user_id
|
|
|
|
|
favorite.recording_id = recording_id
|
|
|
|
|
favorite.save
|
2012-11-21 19:48:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.delete_favorite(user_id, recording_id)
|
|
|
|
|
JamRuby::UserFavorite.delete_all "(user_id = '#{user_id}' AND recording_id = '#{recording_id}')"
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-09 04:05:54 +00:00
|
|
|
def self.save_session_settings(user, music_session)
|
|
|
|
|
unless user.nil?
|
|
|
|
|
|
2012-12-10 05:53:21 +00:00
|
|
|
# only save genre id and description
|
|
|
|
|
genres = []
|
|
|
|
|
unless music_session.genres.nil?
|
|
|
|
|
music_session.genres.each do |genre|
|
|
|
|
|
g = Hash.new
|
|
|
|
|
g["id"] = genre.id
|
|
|
|
|
g["description"] = genre.description
|
|
|
|
|
genres << g
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# only save invitation receiver id and name
|
|
|
|
|
invitees = []
|
|
|
|
|
unless music_session.invitations.nil?
|
|
|
|
|
music_session.invitations.each do |invitation|
|
|
|
|
|
i = Hash.new
|
|
|
|
|
i["id"] = invitation.receiver.id
|
|
|
|
|
i["name"] = invitation.receiver.name
|
|
|
|
|
invitees << i
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-09 04:05:54 +00:00
|
|
|
session_settings = { :band_id => music_session.band_id,
|
|
|
|
|
:musician_access => music_session.musician_access,
|
|
|
|
|
:approval_required => music_session.approval_required,
|
|
|
|
|
:fan_chat => music_session.fan_chat,
|
|
|
|
|
:fan_access => music_session.fan_access,
|
|
|
|
|
:description => music_session.description,
|
2012-12-10 05:53:21 +00:00
|
|
|
:genres => genres,
|
|
|
|
|
:invitees => invitees
|
2012-12-09 04:05:54 +00:00
|
|
|
}.to_json
|
|
|
|
|
|
|
|
|
|
user.session_settings = session_settings
|
|
|
|
|
user.save
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
# throws ActiveRecord::RecordNotFound if instrument is invalid
|
|
|
|
|
# throws an email delivery error if unable to connect out to SMTP
|
|
|
|
|
def self.signup(first_name, last_name, email, password, password_confirmation,
|
2012-11-28 05:26:53 +00:00
|
|
|
city, state, country, instruments, photo_url, signup_confirm_url)
|
2012-11-21 19:48:39 +00:00
|
|
|
user = User.new
|
2012-11-07 13:10:41 +00:00
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
UserManager.active_record_transaction do |user_manager|
|
|
|
|
|
user.first_name = first_name
|
|
|
|
|
user.last_name = last_name
|
|
|
|
|
user.email = email
|
|
|
|
|
|
|
|
|
|
#FIXME: Setting random password for social network logins. This
|
|
|
|
|
# is because we have validations all over the place on this.
|
|
|
|
|
# The right thing would be to have this null
|
|
|
|
|
if password.nil?
|
|
|
|
|
user.password = "blahblahblah"
|
|
|
|
|
user.password_confirmation = "blahblahblah"
|
|
|
|
|
else
|
|
|
|
|
user.password = password
|
|
|
|
|
user.password_confirmation = password_confirmation
|
|
|
|
|
end
|
2012-11-14 05:37:18 +00:00
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
user.admin = false
|
|
|
|
|
user.email_confirmed = false
|
2012-12-07 02:54:01 +00:00
|
|
|
user.musician = true
|
2012-11-21 19:48:39 +00:00
|
|
|
user.city = city
|
|
|
|
|
user.state = state
|
|
|
|
|
user.country = country
|
|
|
|
|
unless instruments.nil?
|
|
|
|
|
instruments.each do |musician_instrument_param|
|
|
|
|
|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
|
|
|
|
|
musician_instrument = MusicianInstrument.new
|
|
|
|
|
musician_instrument.user = user
|
|
|
|
|
musician_instrument.instrument = instrument
|
|
|
|
|
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
|
|
|
|
|
musician_instrument.priority = musician_instrument_param[:priority]
|
|
|
|
|
musician_instrument.save
|
|
|
|
|
user.musician_instruments << musician_instrument
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-11-28 05:26:53 +00:00
|
|
|
|
|
|
|
|
user.photo_url = photo_url
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
user.signup_token = SecureRandom.urlsafe_base64
|
2012-11-15 09:30:55 +00:00
|
|
|
|
2013-01-04 10:13:39 +00:00
|
|
|
user.can_invite = Limits::USERS_CAN_INVITE
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
user.save
|
2012-11-15 09:30:55 +00:00
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
if user.errors.any?
|
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
|
else
|
|
|
|
|
# FIXME:
|
|
|
|
|
# It's not standard to require a confirmation when a user signs up with Facebook.
|
|
|
|
|
# We should stop asking for it.
|
|
|
|
|
#
|
|
|
|
|
# any errors here should also rollback the transaction; that's OK. If emails aren't going to be delivered,
|
|
|
|
|
# it's already a really bad situation; make user signup again
|
|
|
|
|
UserMailer.welcome_message(user, signup_confirm_url.nil? ? nil : (signup_confirm_url + "/" + user.signup_token) ).deliver
|
2012-11-14 05:37:18 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
return user
|
|
|
|
|
end
|
2012-11-14 05:37:18 +00:00
|
|
|
|
2012-12-09 20:56:35 +00:00
|
|
|
# this is intended to be development-mode or test-mode only; VRFS-149
|
|
|
|
|
# it creates or updates one user per developer, so that we aren't in the business
|
|
|
|
|
# of constantly recreating users as we create new dev environments
|
|
|
|
|
|
|
|
|
|
# We guard against this code running in production mode,
|
|
|
|
|
# because otherwise it's a bit of uncomfortable code
|
|
|
|
|
# to have sitting around
|
|
|
|
|
def self.create_dev_user(first_name, last_name, email, password,
|
|
|
|
|
city, state, country, instruments, photo_url)
|
|
|
|
|
|
|
|
|
|
if Environment.mode == "production"
|
|
|
|
|
# short-circuit out
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user = User.find_or_create_by_email(email)
|
|
|
|
|
|
|
|
|
|
User.transaction do
|
|
|
|
|
user.first_name = first_name
|
|
|
|
|
user.last_name = last_name
|
|
|
|
|
user.email = email
|
|
|
|
|
user.password = password
|
|
|
|
|
user.password_confirmation = password
|
|
|
|
|
user.admin = true
|
|
|
|
|
user.email_confirmed = true
|
|
|
|
|
user.musician = true
|
|
|
|
|
user.city = city
|
|
|
|
|
user.state = state
|
|
|
|
|
user.country = country
|
|
|
|
|
|
|
|
|
|
if instruments.nil?
|
|
|
|
|
instruments = [{:instrument_id => "acoustic guitar", :proficiency_level => 3, :priority => 1}]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
unless user.new_record?
|
|
|
|
|
MusicianInstrument.delete_all(["user_id = ?", user.id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
instruments.each do |musician_instrument_param|
|
|
|
|
|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
|
|
|
|
|
musician_instrument = MusicianInstrument.new
|
|
|
|
|
musician_instrument.user = user
|
|
|
|
|
musician_instrument.instrument = instrument
|
|
|
|
|
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
|
|
|
|
|
musician_instrument.priority = musician_instrument_param[:priority]
|
|
|
|
|
musician_instrument.save
|
|
|
|
|
user.musician_instruments << musician_instrument
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user.photo_url = photo_url
|
|
|
|
|
user.signup_token = nil
|
|
|
|
|
user.save
|
|
|
|
|
|
|
|
|
|
if user.errors.any?
|
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
# throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user
|
|
|
|
|
def self.signup_confirm(signup_token)
|
|
|
|
|
if signup_token.nil? || signup_token.empty?
|
|
|
|
|
# there are plenty of confirmed users with nil signup_tokens, so we can't look on it
|
|
|
|
|
raise ActiveRecord::RecordNotFound
|
2012-11-14 05:37:18 +00:00
|
|
|
else
|
2012-11-21 19:48:39 +00:00
|
|
|
UserManager.active_record_transaction do |user_manager|
|
|
|
|
|
# throws ActiveRecord::RecordNotFound if invalid
|
|
|
|
|
user = User.find_by_signup_token!(signup_token)
|
|
|
|
|
user.signup_token = nil
|
2012-11-22 07:57:32 +00:00
|
|
|
user.confirm_email!
|
2012-11-21 19:48:39 +00:00
|
|
|
user.save
|
|
|
|
|
return user
|
|
|
|
|
end
|
2012-11-14 05:37:18 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
# if valid credentials are supplied for an 'active' user, returns the user
|
|
|
|
|
# if not authenticated, returns nil
|
|
|
|
|
def self.authenticate(email, password)
|
|
|
|
|
# we only allow users that have confirmed email to authenticate
|
|
|
|
|
user = User.where('email_confirmed=true').find_by_email(email)
|
2012-11-14 05:37:18 +00:00
|
|
|
|
2012-11-21 19:48:39 +00:00
|
|
|
if user && user.authenticate(password)
|
2012-11-14 05:37:18 +00:00
|
|
|
return user
|
2012-11-21 19:48:39 +00:00
|
|
|
else
|
|
|
|
|
return nil
|
2012-11-14 05:37:18 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-01-14 04:50:38 +00:00
|
|
|
def self.search(query, options = { :limit => 10 })
|
2012-11-14 05:37:18 +00:00
|
|
|
|
2013-01-14 04:50:38 +00:00
|
|
|
# only issue search if at least 2 characters are specified
|
|
|
|
|
if query.nil? || query.length < 2
|
|
|
|
|
return []
|
2012-11-07 13:10:41 +00:00
|
|
|
end
|
|
|
|
|
|
2013-01-14 04:50:38 +00:00
|
|
|
# create 'anded' statement
|
|
|
|
|
query = Search.create_tsquery(query)
|
2012-11-07 13:10:41 +00:00
|
|
|
|
2013-01-14 04:50:38 +00:00
|
|
|
if query.nil? || query.length == 0
|
|
|
|
|
return []
|
2012-11-07 13:10:41 +00:00
|
|
|
end
|
|
|
|
|
|
2013-01-14 04:50:38 +00:00
|
|
|
return User.where("email_confirmed = true and name_tsv @@ to_tsquery('jamenglish', ?)", query).limit(options[:limit])
|
|
|
|
|
end
|
2012-11-07 13:10:41 +00:00
|
|
|
|
2012-10-07 04:57:23 +00:00
|
|
|
private
|
2012-10-02 05:02:02 +00:00
|
|
|
def create_remember_token
|
|
|
|
|
self.remember_token = SecureRandom.urlsafe_base64
|
2012-11-07 13:10:41 +00:00
|
|
|
end
|
2012-11-25 19:37:54 +00:00
|
|
|
|
|
|
|
|
def self.validate_instruments(instruments, is_nil_ok)
|
|
|
|
|
if is_nil_ok && instruments.nil?
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if instruments.nil?
|
|
|
|
|
raise JamRuby::JamArgumentError, ValidationMessages::INSTRUMENT_MINIMUM_NOT_MET
|
|
|
|
|
else
|
|
|
|
|
if instruments.size < Limits::MIN_INSTRUMENTS_PER_MUSICIAN
|
|
|
|
|
raise JamRuby::JamArgumentError, ValidationMessages::INSTRUMENT_MINIMUM_NOT_MET
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if instruments.size > Limits::MAX_INSTRUMENTS_PER_MUSICIAN
|
|
|
|
|
raise JamRuby::JamArgumentError, ValidationMessages::INSTRUMENT_LIMIT_EXCEEDED
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-08-06 03:01:00 +00:00
|
|
|
end
|
2012-08-22 03:07:01 +00:00
|
|
|
end
|