jam-cloud/lib/jam_ruby/models/user.rb

391 lines
12 KiB
Ruby

module JamRuby
class User < ActiveRecord::Base
include Tire::Model::Search
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :city, :state, :country
attr_accessor :updating_password
self.primary_key = 'id'
# authorizations (for facebook, etc -- omniauth)
has_many :user_authorizations, :class_name => "JamRuby::UserAuthorization"
# account
belongs_to :account, :class_name => "JamRuby::Account"
# connections (websocket-gateway)
has_many :connections, :class_name => "JamRuby::Connection"
# friend requests
has_many :friend_requests, :class_name => "JamRuby::FriendRequest"
# instruments
has_many :musician_instruments
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
# bands
has_many :band_musicians
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
# followers
has_many :followers, :class_name => "JamRuby::UserFollower", :foreign_key => "user_id"
has_many :inverse_followers, :through => :followers, :source => :user, :class_name => "JamRuby::User", :foreign_key => "follower_id"
# user followings
has_many :followings, :class_name => "JamRuby::UserFollowing", :foreign_key => "follower_id"
has_many :inverse_followings, :through => :followings, :source => :user, :class_name => "JamRuby::User", :foreign_key => "user_id"
# band followings
has_many :band_followings, :class_name => "JamRuby::BandFollower", :foreign_key => "follower_id"
has_many :inverse_band_followings, :through => :band_followings, :class_name => "JamRuby::Band", :foreign_key => "band_id"
# favorites (needs Recording model)
# friends
has_many :friendships, :class_name => "JamRuby::Friendship", :foreign_key => "user_id"
has_many :friends, :through => :friendships, :class_name => "JamRuby::User"
has_many :inverse_friendships, :class_name => "JamRuby::Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user, :class_name => "JamRuby::User"
# connections / music sessions
has_many :created_music_sessions, :foreign_key => "user_id", :inverse_of => :user, :class_name => "JamRuby::MusicSession" # sessions *created* by the user
has_many :music_sessions, :through => :connections, :class_name => "JamRuby::MusicSession"
# invitations
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"
# fan_invitations
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"
# This causes the authenticate method to be generated (among other stuff)
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
after_save :limit_to_five_instruments
validates :first_name, presence: true, length: {maximum: 50}
validates :last_name, presence: true, length: {maximum: 50}
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}
validates_length_of :password, minimum: 6, maximum: 100, :if => :should_validate_password?
validates_presence_of :password_confirmation, :if => :should_validate_password?
#validates :password_confirmation, presence: true
validates_confirmation_of :password, :if => :should_validate_password?
def online
@online ||= !self.connections.nil? && self.connections.size > 0
end
def photo_url
# TODO: move image path to config
@photo_url = "http://www.jamkazam.com/images/users/photos/#{self.id}.gif";
end
def name
return "#{first_name} #{last_name}"
end
def location
# TODO: implement a single string version of location;
# this will be indexed into elasticsearch and returned in search
return "Austin, TX"
end
def should_validate_password?
updating_password || new_record?
end
def friends?(user)
return self.friends.exists?(user)
end
def friend_count
return self.friends.size
end
def follower_count
return self.followers.size
end
def following_count
return self.followings.size + self.band_followings.size
end
def to_s
return email unless email.nil?
if !first_name.nil? && !last_name.nil?
return first_name + ' ' + last_name
end
return id
end
# helper method for creating / updating a User
def self.save(params)
if params[:id].nil?
user = User.new()
else
user = User.find(params[:id])
end
# account id
unless params[:account_id].nil?
user.account_id = params[:account_id]
end
# first name
unless params[:first_name].nil?
user.first_name = params[:first_name]
end
# last name
unless params[:last_name].nil?
user.last_name = params[:last_name]
end
# email
unless params[:email].nil?
user.email = params[:email]
end
# password
unless params[:password].nil?
user.password = params[:password]
end
# password confirmation
unless params[:password_confirmation].nil?
user.password_confirmation = params[:password_confirmation]
end
# musician flag
unless params[:musician].nil?
user.musician = params[:musician]
end
# gender
unless params[:gender].nil?
account.gender = params[:gender]
end
# birthdate
unless params[:birth_date].nil?
account.birth_date = params[:birth_date]
end
# ISP
unless params[:internet_service_provider].nil?
account.internet_service_provider = params[:internet_service_provider]
end
# city
unless params[:city].nil?
user.city = params[:city]
end
# state
unless params[:state].nil?
user.state = params[:state]
end
# country
unless params[:country].nil?
user.country = params[:country]
end
# instruments
unless params[:instruments].nil?
ActiveRecord::Base.transaction do
# delete all instruments for this user first
unless user.id.nil? || user.id.length == 0
MusicianInstrument.delete_all(["user_id = ?", user.id])
end
# loop through each instrument in the array and save to the db
params[:instruments].each do |instrument|
mu = MusicianInstrument.new()
mu.user_id = user.id
mu.instrument_id = instrument[:id]
mu.priority = instrument[:priority]
mu.proficiency_level = instrument[:proficiency_level]
user.musician_instruments << mu
end
end
end
user.updated_at = Time.now.getutc
user.save
return user
end
def limit_to_five_instruments
if instruments.count > 5
errors.add(:instruments, "No more than 5 instruments are allowed.")
end
end
# 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,
city, state, country, instruments, signup_confirm_url)
user = User.new
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
user.admin = false
user.email_confirmed = false
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
user.signup_token = SecureRandom.urlsafe_base64
user.save
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
end
end
return user
end
# 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
else
UserManager.active_record_transaction do |user_manager|
# throws ActiveRecord::RecordNotFound if invalid
user = User.find_by_signup_token!(signup_token)
user.signup_token = nil
user.email_confirmed = true
user.save
return user
end
end
end
# 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)
if user && user.authenticate(password)
return user
else
return nil
end
end
### Elasticsearch/Tire integration ###
#
# Define the name based on the environment
# We wouldn't like to erase dev data during
# test runs!
#
index_name("#{Environment.mode}-#{Environment.application}-users")
def to_indexed_json
{
:first_name => first_name,
:last_name => last_name,
:photo_url => photo_url,
:location => location,
:musician => musician
}.to_json
end
# only put users into the search index if their eail is confirmed
after_save do
update_index if email_confirmed
end
after_destroy do
update_index
end
class << self
def create_search_index
Tire.index(User.index_name) do
create(
:settings => Search.index_settings,
:mappings => {
"jam_ruby/user" => {
:properties => {
:photo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false},
:location => { :type => :string },
:first_name => { :type => :string, :boost => 100 },
:last_name => { :type => :string, :boost => 100 },
:musician => { :type => :boolean, :index => :not_analyzed, :include_in_all => false}
}
}
}
)
end
end
def delete_search_index
search_index.delete
end
def search_index
Tire.index(User.index_name)
end
end
### Elasticsearch/Tire integration
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
end