1622 lines
59 KiB
Ruby
1622 lines
59 KiB
Ruby
include Devise::Models
|
|
|
|
module JamRuby
|
|
class User < ActiveRecord::Base
|
|
|
|
include Geokit::ActsAsMappable::Glue unless defined?(acts_as_mappable)
|
|
include HtmlSanitize
|
|
html_sanitize strict: [:first_name, :last_name, :city, :state, :country, :biography]
|
|
|
|
#devise: for later: :trackable
|
|
|
|
@@log = Logging.logger[User]
|
|
|
|
VALID_EMAIL_REGEX = /\A[\w+\-.]+@([a-z\d\-]+\.)+[a-z]+\z/i
|
|
JAM_REASON_REGISTRATION = 'r'
|
|
JAM_REASON_NETWORK_TEST = 'n'
|
|
JAM_REASON_FTUE = 'g'
|
|
JAM_REASON_JOIN = 'j'
|
|
JAM_REASON_IMPORT = 'i'
|
|
JAM_REASON_LOGIN = 'l'
|
|
|
|
# MOD KEYS
|
|
MOD_GEAR = "gear"
|
|
MOD_GEAR_FRAME_OPTIONS = "show_frame_options"
|
|
|
|
MOD_NO_SHOW = "no_show"
|
|
|
|
# MIN/MAX AUDIO LATENCY
|
|
MINIMUM_AUDIO_LATENCY = 2
|
|
MAXIMUM_AUDIO_LATENCY = 10000
|
|
|
|
devise :database_authenticatable, :recoverable, :rememberable
|
|
|
|
acts_as_mappable
|
|
|
|
# after_save :check_lat_lng
|
|
|
|
attr_accessible :first_name, :last_name, :email, :city, :password, :password_confirmation, :state, :country, :birth_date, :subscribe_email, :terms_of_service, :original_fpfile, :cropped_fpfile, :cropped_large_fpfile, :cropped_s3_path, :cropped_large_s3_path, :photo_url, :large_photo_url, :crop_selection
|
|
|
|
# updating_password corresponds to a lost_password
|
|
attr_accessor :updating_password, :updating_email, :updated_email, :update_email_confirmation_url, :administratively_created, :current_password, :setting_password, :confirm_current_password, :updating_avatar, :updating_progression_field, :mods_json
|
|
|
|
belongs_to :icecast_server_group, class_name: "JamRuby::IcecastServerGroup", inverse_of: :users, foreign_key: 'icecast_server_group_id'
|
|
|
|
# authorizations (for facebook, etc -- omniauth)
|
|
has_many :user_authorizations, :class_name => "JamRuby::UserAuthorization"
|
|
|
|
# connections (websocket-gateway)
|
|
has_many :connections, :class_name => "JamRuby::Connection"
|
|
|
|
# friend requests
|
|
has_many :sent_friend_requests, :class_name => "JamRuby::FriendRequest", :foreign_key => 'user_id'
|
|
has_many :received_friend_requests, :class_name => "JamRuby::FriendRequest", :foreign_key => 'friend_id'
|
|
|
|
# instruments
|
|
has_many :musician_instruments, :class_name => "JamRuby::MusicianInstrument"
|
|
has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument"
|
|
|
|
# bands
|
|
has_many :band_musicians, :class_name => "JamRuby::BandMusician"
|
|
has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band"
|
|
|
|
# genres
|
|
has_many :genre_players, as: :player, class_name: "JamRuby::GenrePlayer", dependent: :destroy
|
|
has_many :genres, through: :genre_players, class_name: "JamRuby::Genre"
|
|
|
|
# recordings
|
|
has_many :owned_recordings, :class_name => "JamRuby::Recording", :foreign_key => "owner_id"
|
|
has_many :recordings, :through => :claimed_recordings, :class_name => "JamRuby::Recording"
|
|
has_many :claimed_recordings, :class_name => "JamRuby::ClaimedRecording", :inverse_of => :user
|
|
has_many :playing_claimed_recordings, :class_name => "JamRuby::ActiveMusicSession", :inverse_of => :claimed_recording_initiator
|
|
has_many :playing_jam_tracks, :class_name => "JamRuby::ActiveMusicSession", :inverse_of => :jam_track_initiator
|
|
|
|
# VRFS-2916 jam_tracks.id is varchar: REMOVE
|
|
# has_many :jam_tracks_played, :class_name => "JamRuby::PlayablePlay", :foreign_key => 'player_id', :conditions => "jam_track_id IS NOT NULL"
|
|
# VRFS-2916 jam_tracks.id is varchar: ADD
|
|
has_many :jam_tracks_played, :class_name => "JamRuby::PlayablePlay", :foreign_key => 'player_id', :conditions => ["playable_type = 'JamRuby::JamTrack'"]
|
|
|
|
# self.id = user_id in likes table
|
|
has_many :likings, :class_name => "JamRuby::Like", :inverse_of => :user, :dependent => :destroy
|
|
|
|
# self.id = likable_id in likes table
|
|
has_many :likers, :as => :likable, :class_name => "JamRuby::Like", :dependent => :destroy
|
|
|
|
# self.id = user_id in follows table
|
|
has_many :followings, :class_name => "JamRuby::Follow", :inverse_of => :user, :dependent => :destroy
|
|
|
|
# self.id = followable_id in follows table
|
|
has_many :followers, :as => :followable, :class_name => "JamRuby::Follow", :dependent => :destroy
|
|
|
|
# text messages
|
|
has_many :text_messages, :class_name => "JamRuby:TextMessage", :foreign_key => "target_user_id"
|
|
|
|
# notifications
|
|
has_many :notifications, :class_name => "JamRuby::Notification", :foreign_key => "target_user_id"
|
|
has_many :inverse_notifications, :through => :notifications, :class_name => "JamRuby::User"
|
|
|
|
# chats
|
|
has_many :chats, :class_name => "JamRuby::ChatMessage", :foreign_key => "user_id"
|
|
|
|
# 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::ActiveMusicSession" # sessions *created* by the user
|
|
has_many :music_sessions, :through => :connections, :class_name => "JamRuby::ActiveMusicSession"
|
|
|
|
# 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"
|
|
|
|
# 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"
|
|
|
|
# session history
|
|
has_many :music_session_histories, :foreign_key => "user_id", :class_name => "JamRuby::MusicSession", :inverse_of => :user
|
|
has_many :music_session_user_histories, :foreign_key => "user_id", :class_name => "JamRuby::MusicSessionUserHistory", :inverse_of => :user
|
|
|
|
# saved tracks
|
|
has_many :recorded_tracks, :foreign_key => "user_id", :class_name => "JamRuby::RecordedTrack", :inverse_of => :user
|
|
has_many :recorded_videos, :foreign_key => "user_id", :class_name => "JamRuby::RecordedVideo", :inverse_of => :user
|
|
has_many :recorded_backing_tracks, :foreign_key => "user_id", :class_name => "JamRuby::RecordedBackingTrack", :inverse_of => :user
|
|
has_many :quick_mixes, :foreign_key => "user_id", :class_name => "JamRuby::QuickMix", :inverse_of => :user
|
|
has_many :recorded_jam_track_tracks, :foreign_key => "user_id", :class_name => "JamRuby::RecordedJamTrackTrack", :inverse_of => :user
|
|
|
|
# jam track recordings started
|
|
has_many :initiated_jam_track_recordings, :foreign_key => 'jam_track_initiator_id', :class_name => "JamRuby::Recording", :inverse_of => :jam_track_initiator
|
|
|
|
# invited users
|
|
has_many :invited_users, :foreign_key => "sender_id", :class_name => "JamRuby::InvitedUser"
|
|
|
|
# crash dumps
|
|
has_many :crash_dumps, :foreign_key => "user_id", :class_name => "JamRuby::CrashDump"
|
|
|
|
# events
|
|
has_many :event_sessions, :class_name => "JamRuby::EventSession"
|
|
|
|
# affiliate_partner
|
|
has_one :affiliate_partner, :class_name => "JamRuby::AffiliatePartner", :foreign_key => :partner_user_id
|
|
belongs_to :affiliate_referral, :class_name => "JamRuby::AffiliatePartner", :foreign_key => :affiliate_referral_id, :counter_cache => :referral_user_count
|
|
# diagnostics
|
|
has_many :diagnostics, :class_name => "JamRuby::Diagnostic"
|
|
|
|
# jam_tracks
|
|
has_many :jam_track_rights, :class_name => "JamRuby::JamTrackRight", :foreign_key => "user_id"
|
|
has_many :purchased_jam_tracks, :through => :jam_track_rights, :class_name => "JamRuby::JamTrack", :source => :jam_track, :order => :created_at
|
|
|
|
# Shopping carts
|
|
has_many :shopping_carts, :class_name => "JamRuby::ShoppingCart"
|
|
|
|
# score history
|
|
has_many :from_score_histories, :class_name => "JamRuby::ScoreHistory", foreign_key: 'from_user_id'
|
|
has_many :to_score_histories, :class_name => "JamRuby::ScoreHistory", foreign_key: 'to_user_id'
|
|
has_many :sales, :class_name => 'JamRuby::Sale', dependent: :destroy
|
|
has_many :recurly_transaction_web_hooks, :class_name => 'JamRuby::RecurlyTransactionWebHook', dependent: :destroy
|
|
|
|
# This causes the authenticate method to be generated (among other stuff)
|
|
#has_secure_password
|
|
|
|
before_save :create_remember_token, :if => :should_validate_password?
|
|
before_save :stringify_avatar_info , :if => :updating_avatar
|
|
|
|
validates :first_name, presence: true, length: {maximum: 50}, no_profanity: true
|
|
validates :last_name, presence: true, length: {maximum: 50}, no_profanity: true
|
|
validates :biography, length: {maximum: 4000}, no_profanity: true
|
|
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}
|
|
validates :update_email, presence: true, format: {with: VALID_EMAIL_REGEX}, :if => :updating_email
|
|
|
|
validates_length_of :password, minimum: 6, maximum: 100, :if => :should_validate_password?
|
|
validates_presence_of :password_confirmation, :if => :should_validate_password?
|
|
validates_confirmation_of :password, :if => :should_validate_password?
|
|
|
|
validates :terms_of_service, :acceptance => {:accept => true, :on => :create, :allow_nil => false }
|
|
validates :reuse_card, :inclusion => {:in => [true, false]}
|
|
validates :has_redeemable_jamtrack, :inclusion => {:in => [true, false]}
|
|
validates :subscribe_email, :inclusion => {:in => [nil, true, false]}
|
|
validates :musician, :inclusion => {:in => [true, false]}
|
|
validates :show_whats_next, :inclusion => {:in => [nil, true, false]}
|
|
validates :mods, json: true
|
|
validates_numericality_of :last_jam_audio_latency, greater_than:MINIMUM_AUDIO_LATENCY, less_than:MAXIMUM_AUDIO_LATENCY, :allow_nil => true
|
|
validates :last_jam_updated_reason, :inclusion => {:in => [nil, JAM_REASON_REGISTRATION, JAM_REASON_NETWORK_TEST, JAM_REASON_FTUE, JAM_REASON_JOIN, JAM_REASON_IMPORT, JAM_REASON_LOGIN] }
|
|
|
|
# custom validators
|
|
validate :validate_musician_instruments
|
|
validate :validate_current_password
|
|
validate :validate_update_email
|
|
validate :validate_avatar_info
|
|
validate :email_case_insensitive_uniqueness
|
|
validate :update_email_case_insensitive_uniqueness, :if => :updating_email
|
|
validate :validate_mods
|
|
|
|
scope :musicians, where(:musician => true)
|
|
scope :fans, where(:musician => false)
|
|
scope :geocoded_users, where(User.arel_table[:last_jam_locidispid].not_eq(nil))
|
|
scope :musicians_geocoded, musicians.geocoded_users
|
|
scope :email_opt_in, where(:subscribe_email => true)
|
|
|
|
def user_progression_fields
|
|
@user_progression_fields ||= Set.new ["first_downloaded_client_at", "first_ran_client_at", "first_music_session_at", "first_real_music_session_at", "first_good_music_session_at", "first_certified_gear_at", "first_invited_at", "first_friended_at", "first_recording_at", "first_social_promoted_at", "first_played_jamtrack_at" ]
|
|
end
|
|
|
|
def update_progression_field(field_name, time = DateTime.now)
|
|
@updating_progression_field = true
|
|
if self[field_name].nil?
|
|
self[field_name] = time
|
|
self.save
|
|
end
|
|
end
|
|
|
|
def failed_qualification(reason)
|
|
self.last_failed_certified_gear_at = DateTime.now
|
|
self.last_failed_certified_gear_reason = reason
|
|
self.save
|
|
end
|
|
|
|
def validate_musician_instruments
|
|
errors.add(:musician_instruments, ValidationMessages::INSTRUMENT_MINIMUM_NOT_MET) if !administratively_created && musician && musician_instruments.length == 0
|
|
errors.add(:musician_instruments, ValidationMessages::INSTRUMENT_LIMIT_EXCEEDED) if !administratively_created && musician && musician_instruments.length > 5
|
|
end
|
|
|
|
# let's work to stop junk from getting into the mods array; this is essentially the schema
|
|
def validate_mods
|
|
mods_json.each do |key, value|
|
|
if key == MOD_NO_SHOW || key == MOD_GEAR
|
|
errors.add(:mods, ValidationMessages::MODS_MUST_BE_HASH) unless value.is_a?(Hash)
|
|
else
|
|
errors.add(:mods, ValidationMessages::MODS_UNKNOWN_KEY)
|
|
end
|
|
end
|
|
end
|
|
|
|
def validate_current_password
|
|
# checks if the user put in their current password (used when changing your email, for instance)
|
|
errors.add(:current_password, ValidationMessages::NOT_YOUR_PASSWORD) if should_confirm_existing_password? && !valid_password?(self.current_password)
|
|
end
|
|
|
|
def validate_update_email
|
|
if updating_email && self.update_email == self.email
|
|
errors.add(:update_email, ValidationMessages::EMAIL_MATCHES_CURRENT)
|
|
elsif updating_email && User.where("email ILIKE ?", self.update_email).first != nil
|
|
errors.add(:update_email, ValidationMessages::EMAIL_ALREADY_TAKEN)
|
|
end
|
|
end
|
|
|
|
def validate_avatar_info
|
|
if updating_avatar
|
|
# we want to mak sure that original_fpfile and cropped_fpfile seems like real fpfile info objects (i.e, json objects from filepicker.io)
|
|
errors.add(:original_fpfile, ValidationMessages::INVALID_FPFILE) if self.original_fpfile.nil? || self.original_fpfile["key"].nil? || self.original_fpfile["url"].nil?
|
|
errors.add(:cropped_fpfile, ValidationMessages::INVALID_FPFILE) if self.cropped_fpfile.nil? || self.cropped_fpfile["key"].nil? || self.cropped_fpfile["url"].nil?
|
|
errors.add(:cropped_large_fpfile, ValidationMessages::INVALID_FPFILE) if self.cropped_large_fpfile.nil? || self.cropped_large_fpfile["key"].nil? || self.cropped_large_fpfile["url"].nil?
|
|
end
|
|
end
|
|
|
|
def email_case_insensitive_uniqueness
|
|
# using the case insensitive unique check of active record will downcase the field, which is not what we want--we want to preserve original casing
|
|
search = User.where("email ILIKE ?", self.email).first
|
|
if search != nil && search != self
|
|
errors.add(:email, ValidationMessages::EMAIL_ALREADY_TAKEN)
|
|
end
|
|
end
|
|
|
|
def update_email_case_insensitive_uniqueness
|
|
# using the case insensitive unique check of active record will downcase the field, which is not what we want--we want to preserve original casing
|
|
search = User.where("update_email ILIKE ?", self.update_email).first
|
|
if search != nil && search != self
|
|
errors.add(:update_email, ValidationMessages::EMAIL_ALREADY_TAKEN)
|
|
end
|
|
end
|
|
|
|
def online
|
|
online?
|
|
end
|
|
|
|
def name
|
|
"#{first_name} #{last_name}"
|
|
end
|
|
|
|
def location
|
|
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?
|
|
# XXX WHY IS COUNTRY COMMENTED OUT?
|
|
loc
|
|
end
|
|
|
|
def location= location_hash
|
|
unless location_hash.nil?
|
|
self.city = location_hash[:city]
|
|
self.state = location_hash[:state]
|
|
self.country = location_hash[:country]
|
|
end
|
|
end
|
|
|
|
def musician?
|
|
return musician
|
|
end
|
|
|
|
def should_validate_password?
|
|
(updating_password || new_record?)
|
|
end
|
|
|
|
def should_confirm_existing_password?
|
|
confirm_current_password
|
|
end
|
|
|
|
def end_user_created?
|
|
return !administratively_created
|
|
end
|
|
|
|
def pending_friend_request?(user)
|
|
FriendRequest.where("((user_id='#{self.id}' AND friend_id='#{user.id}') OR (user_id='#{user.id}' AND friend_id='#{self.id}')) AND status is null").size > 0
|
|
end
|
|
|
|
def friends?(user)
|
|
self.friends.exists?(user)
|
|
end
|
|
|
|
def friend_count
|
|
self.friends.size
|
|
end
|
|
|
|
# check if "this user" likes entity
|
|
def likes?(entity)
|
|
self.likings.where(:likable_id => entity.id).size > 0
|
|
end
|
|
|
|
def liking_count
|
|
self.likings.size
|
|
end
|
|
|
|
def liker_count
|
|
self.likers.size
|
|
end
|
|
|
|
# check if "this user" follows entity
|
|
def following?(entity)
|
|
self.followings.where(:followable_id => entity.id).size > 0
|
|
end
|
|
|
|
def following_count
|
|
self.followings.size
|
|
end
|
|
|
|
def follower_count
|
|
self.followers.size
|
|
end
|
|
|
|
def recording_count
|
|
self.recordings.size
|
|
end
|
|
|
|
def age
|
|
now = Time.now.utc.to_date
|
|
self.birth_date.nil? ? "unspecified" : now.year - self.birth_date.year - (self.birth_date.to_date.change(:year => now.year) > now ? 1 : 0)
|
|
end
|
|
|
|
def session_count
|
|
MusicSession.where("user_id = ? AND started_at IS NOT NULL", self.id).size
|
|
end
|
|
|
|
# count up any session you are RSVP'ed to
|
|
def upcoming_session_count
|
|
MusicSession.scheduled_rsvp(self, true).length
|
|
end
|
|
|
|
def purchased_jamtracks_count
|
|
self.purchased_jam_tracks.count
|
|
end
|
|
|
|
def sales_count
|
|
self.sales.count
|
|
end
|
|
|
|
def joined_score
|
|
return nil unless has_attribute?(:score)
|
|
a = read_attribute(:score)
|
|
a.nil? ? nil : a.to_i
|
|
end
|
|
|
|
def music_session_id
|
|
return nil unless has_attribute?(:music_session_id)
|
|
read_attribute(:music_session_id)
|
|
end
|
|
|
|
# ===== ARTIFICIAL ATTRIBUTES CREATED BY ActiveMusicSession.ams_users, MusicSession.sms_users
|
|
def full_score
|
|
return nil unless has_attribute?(:full_score)
|
|
a = read_attribute(:full_score)
|
|
a.nil? ? nil : a.to_i
|
|
end
|
|
|
|
def internet_score
|
|
return nil unless has_attribute?(:internet_score)
|
|
a = read_attribute(:internet_score)
|
|
a.nil? ? nil : a.to_i
|
|
end
|
|
|
|
def audio_latency
|
|
return nil unless has_attribute?(:audio_latency)
|
|
a = read_attribute(:audio_latency)
|
|
a.nil? ? nil : a.to_i
|
|
end
|
|
# ====== END ARTIFICAL ATTRIBUTES
|
|
|
|
def score_info(destination_user)
|
|
if self.last_jam_locidispid && destination_user.last_jam_locidispid
|
|
self.connection.execute("select score from current_network_scores where alocidispid = #{self.last_jam_locidispid} and blocidispid = #{destination_user.last_jam_locidispid}").check
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
|
|
# mods comes back as text; so give ourselves a parsed version
|
|
def mods_json
|
|
@mods_json ||= mods ? JSON.parse(mods) : {}
|
|
end
|
|
|
|
# new_modes should be a regular hash with non-symbolized keys (vs symbolized keys)
|
|
def mod_merge(new_mods)
|
|
self.mods = (mods_json.merge(new_mods) do |key, old_val, new_val|
|
|
if key == MOD_NO_SHOW || key == MOD_GEAR
|
|
# we take the values from previous hash, and merge it with the new hash
|
|
old_val.merge(new_val)
|
|
else
|
|
raise "unknown in mode_merge key: #{key}"
|
|
end
|
|
end).to_json
|
|
@mods_json = nil # invalidate this since we've updated self.mods
|
|
|
|
end
|
|
|
|
# any mod with the value 'null' will be deleted
|
|
def delete_mod(root_key, sub_key)
|
|
mod = mods_json
|
|
root = mod[root_key]
|
|
if root
|
|
root.delete(sub_key)
|
|
# check if root key is completely empty
|
|
mod.delete(root_key) if root.length == 0
|
|
# check if mod key is empty
|
|
mod = nil if mod.length == 0
|
|
end
|
|
|
|
self.mods = mod.nil? ? nil : mod.to_json
|
|
@mods_json = nil # invalidate this since we've updated self.mods
|
|
end
|
|
|
|
def get_mod(root_key, sub_key)
|
|
mod = mods_json
|
|
root = mod[root_key]
|
|
root[sub_key] if root
|
|
end
|
|
|
|
def get_gear_mod(sub_key)
|
|
get_mod(MOD_GEAR, sub_key)
|
|
end
|
|
|
|
def get_no_show_mod(sub_key)
|
|
get_mod(MOD_NO_SHOW, sub_key)
|
|
end
|
|
|
|
def heartbeat_interval_client
|
|
mods_json[:heartbeat_interval_client]
|
|
end
|
|
|
|
def connection_expire_time_client
|
|
mods_json[:connection_expire_time_client]
|
|
end
|
|
|
|
def recent_history(session_id, claimed_recording_id)
|
|
# used to exclude currently viewed recording
|
|
recording_exclusion = "claimed_recordings.id != '#{claimed_recording_id}'" if claimed_recording_id
|
|
recordings = Recording
|
|
.joins(:claimed_recordings)
|
|
.where(:owner_id => self.id)
|
|
.where("claimed_recordings.user_id = '#{self.id}'")
|
|
.where('claimed_recordings.is_public=true')
|
|
.where(recording_exclusion)
|
|
.order('created_at DESC')
|
|
.limit(10)
|
|
|
|
# used to exclude currently viewed session
|
|
session_exclusion = "music_sessions.id != '#{session_id}'" if session_id
|
|
msh = MusicSession
|
|
.where(:user_id => self.id)
|
|
.where(:fan_access => true)
|
|
.where(session_exclusion)
|
|
.order('created_at DESC')
|
|
.limit(10)
|
|
|
|
recordings.concat(msh)
|
|
recordings.sort! {|a,b| b.created_at <=> a.created_at}.first(5)
|
|
end
|
|
|
|
# returns the # of new notifications
|
|
def new_notifications
|
|
search = Notification.select('id').where(target_user_id: self.id)
|
|
search = search.where('created_at > ?', self.notification_seen_at) if self.notification_seen_at
|
|
search.count
|
|
end
|
|
|
|
# the user can pass in a timestamp string, or the keyword 'LATEST'
|
|
# if LATEST is specified, we'll use the latest_notification as the timestamp
|
|
# if not, just use seen as-is
|
|
def update_notification_seen_at seen
|
|
new_latest_seen = nil
|
|
if seen == 'LATEST'
|
|
latest = self.latest_notification
|
|
new_latest_seen = latest.created_at if latest
|
|
else
|
|
new_latest_seen = seen
|
|
end
|
|
|
|
self.notification_seen_at = new_latest_seen
|
|
end
|
|
|
|
def latest_notification
|
|
Notification.select('created_at').where(target_user_id: id).limit(1).order('created_at DESC').first
|
|
end
|
|
|
|
def confirm_email!
|
|
self.email_confirmed = true
|
|
end
|
|
|
|
def my_session_settings
|
|
unless self.session_settings.nil?
|
|
return JSON.parse(self.session_settings)
|
|
else
|
|
return ""
|
|
end
|
|
end
|
|
|
|
def session_history(user_id, band_id = nil, genre = nil)
|
|
return MusicSession.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
|
|
|
|
# always returns a non-null value for photo-url,
|
|
# using the generic avatar if no user photo available
|
|
def resolved_photo_url
|
|
if self.photo_url == nil || self.photo_url == ''
|
|
"#{APP_CONFIG.external_root_url}/assets/shared/avatar_generic.png"
|
|
else
|
|
return self.photo_url
|
|
end
|
|
end
|
|
|
|
def to_s
|
|
return email unless email.nil?
|
|
|
|
if !first_name.nil? && !last_name.nil?
|
|
return first_name + ' ' + last_name
|
|
end
|
|
|
|
id
|
|
end
|
|
|
|
def set_password(old_password, new_password, new_password_confirmation)
|
|
|
|
# so that UserObserver knows to send a confirmation email on success
|
|
self.setting_password = true
|
|
# so that should_validate_password? fires
|
|
self.updating_password = true
|
|
|
|
attributes = { :password => new_password, :password_confirmation => new_password_confirmation }
|
|
|
|
# taken liberally from Devise::DatabaseAuthenticatable.update_with_password
|
|
|
|
if valid_password?(old_password)
|
|
update_attributes(attributes)
|
|
else
|
|
self.assign_attributes(attributes)
|
|
self.valid?
|
|
self.errors.add(:current_password, old_password.blank? ? :blank : :invalid)
|
|
end
|
|
|
|
#clean_up_passwords
|
|
|
|
end
|
|
|
|
def self.set_password_from_token(email, token, new_password, new_password_confirmation)
|
|
user = User.where("email ILIKE ?", email).first
|
|
if user.nil? || user.reset_password_token != token || Time.now - user.reset_password_token_created > 3.days || new_password.length < 6 || new_password != new_password_confirmation
|
|
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)
|
|
# FIXME: Should verify that the new password meets certain quality criteria. Really, maybe that should be a
|
|
# verification step.
|
|
self.updating_password = true
|
|
self.password = new_password
|
|
self.password_confirmation = new_password_confirmation
|
|
|
|
UserMailer.password_changed(self).deliver
|
|
end
|
|
|
|
def self.reset_password(email, base_uri)
|
|
user = User.where("email ILIKE ?", email).first
|
|
raise JamRuby::JamArgumentError.new('unknown email', :email) if user.nil?
|
|
|
|
user.reset_password_token = SecureRandom.urlsafe_base64
|
|
user.reset_password_token_created = Time.now
|
|
user.save
|
|
|
|
reset_url = "#{base_uri}/reset_password_token?token=#{user.reset_password_token}&email=#{CGI.escape(email)}"
|
|
UserMailer.password_reset(user, reset_url).deliver
|
|
|
|
user
|
|
end
|
|
|
|
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
|
|
|
|
def update_genres(gids)
|
|
unless self.new_record?
|
|
GenrePlayer.delete_all(["player_id = ? AND player_type = ?",
|
|
self.id, self.class.name])
|
|
end
|
|
gids.each do |gid|
|
|
self.genres << Genre.find_by_id(gid)
|
|
end
|
|
end
|
|
|
|
# given an array of instruments, update a user's instruments
|
|
def update_instruments(instruments)
|
|
# delete all instruments for this user first
|
|
unless self.new_record?
|
|
MusicianInstrument.delete_all(["user_id = ?", self.id])
|
|
end
|
|
|
|
# loop through each instrument in the array and save to the db
|
|
instruments.each do |musician_instrument_param|
|
|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
|
|
musician_instrument = MusicianInstrument.new
|
|
musician_instrument.user = self
|
|
musician_instrument.instrument = instrument
|
|
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
|
|
musician_instrument.priority = musician_instrument_param[:priority]
|
|
musician_instrument.save
|
|
self.musician_instruments << musician_instrument
|
|
end
|
|
end
|
|
|
|
# this easy_save routine guards against nil sets, but many of these fields can be set to null.
|
|
# I've started to use it less as I go forward
|
|
def easy_save(first_name, last_name, email, password, password_confirmation, musician, gender,
|
|
birth_date, internet_service_provider, city, state, country, instruments, photo_url, biography = nil)
|
|
|
|
# first name
|
|
unless first_name.nil?
|
|
self.first_name = first_name
|
|
end
|
|
|
|
# last name
|
|
unless last_name.nil?
|
|
self.last_name = last_name
|
|
end
|
|
|
|
# email
|
|
# !! Email is changed in a dedicated method, 'update_email'
|
|
#unless email.nil?
|
|
# self.email = email
|
|
#end
|
|
|
|
# password
|
|
unless password.nil?
|
|
self.password = password
|
|
end
|
|
|
|
# password confirmation
|
|
unless password_confirmation.nil?
|
|
self.password_confirmation = password_confirmation
|
|
end
|
|
|
|
# musician flag
|
|
unless musician.nil?
|
|
self.musician = musician
|
|
end
|
|
|
|
# gender
|
|
unless gender.nil?
|
|
self.gender = gender
|
|
end
|
|
|
|
# birthdate
|
|
unless birth_date.nil?
|
|
self.birth_date = birth_date
|
|
end
|
|
|
|
# ISP
|
|
unless internet_service_provider.nil?
|
|
self.internet_service_provider = internet_service_provider
|
|
end
|
|
|
|
# city
|
|
unless city.nil?
|
|
self.city = city
|
|
end
|
|
|
|
# state
|
|
unless state.nil?
|
|
self.state = state
|
|
end
|
|
|
|
# country
|
|
unless country.nil?
|
|
self.country = country
|
|
end
|
|
|
|
# instruments
|
|
unless instruments.nil?
|
|
update_instruments(instruments)
|
|
end
|
|
|
|
# photo url
|
|
unless photo_url.nil?
|
|
self.photo_url = photo_url
|
|
end
|
|
|
|
unless biography.nil?
|
|
self.biography = biography
|
|
end
|
|
|
|
self.updated_at = Time.now
|
|
self.save
|
|
end
|
|
|
|
# helper method for creating / updating a User
|
|
def self.save(id, updater_id, first_name, last_name, email, password, password_confirmation, musician, gender,
|
|
birth_date, internet_service_provider, city, state, country, instruments, photo_url, biography)
|
|
if id.nil?
|
|
user = User.new()
|
|
else
|
|
user = User.find(id)
|
|
end
|
|
|
|
if user.id != updater_id
|
|
raise JamPermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
|
end
|
|
|
|
user.easy_save(first_name, last_name, email, password, password_confirmation, musician, gender,
|
|
birth_date, internet_service_provider, city, state, country, instruments, photo_url, biography)
|
|
return user
|
|
end
|
|
|
|
def begin_update_email(email, current_password, confirmation_url)
|
|
# sets the user model in a state such that it's expecting to have it's email updated
|
|
# two columns matter for this; 'update_email_token' and 'update_email'
|
|
# confirmation_link is odd in the sense that it can likely only come from www.jamkazam.com (jam-web)
|
|
|
|
# an observer should be set up to send an email based on this activity
|
|
self.updating_email = self.confirm_current_password = true
|
|
self.current_password = current_password
|
|
self.update_email = email
|
|
self.update_email_token = SecureRandom.urlsafe_base64
|
|
self.update_email_confirmation_url = "#{confirmation_url}#{self.update_email_token}"
|
|
|
|
self.save
|
|
end
|
|
|
|
def create_user_following(targetUserId)
|
|
targetUser = User.find(targetUserId)
|
|
|
|
follow = Follow.new
|
|
follow.followable = targetUser
|
|
follow.user = self
|
|
follow.save
|
|
|
|
# TODO: make this async
|
|
Notification.send_new_user_follower(self, targetUser)
|
|
end
|
|
|
|
def create_band_following(targetBandId)
|
|
|
|
targetBand= Band.find(targetBandId)
|
|
|
|
follow = Follow.new
|
|
follow.followable = targetBand
|
|
follow.user = self
|
|
follow.save
|
|
|
|
# TODO: make this async
|
|
Notification.send_new_band_follower(self, targetBand)
|
|
end
|
|
|
|
def self.delete_following(followerId, targetEntityId)
|
|
Follow.delete_all "(user_id = '#{followerId}' AND followable_id = '#{targetEntityId}')"
|
|
end
|
|
|
|
def create_user_liking(targetUserId)
|
|
targetUser = User.find(targetUserId)
|
|
|
|
like = Like.new
|
|
like.likable = targetUser
|
|
like.user = self
|
|
like.save
|
|
end
|
|
|
|
def create_band_liking(targetBandId)
|
|
targetBand = Band.find(targetBandId)
|
|
|
|
like = Like.new
|
|
like.likable = targetBand
|
|
like.user = self
|
|
like.save
|
|
end
|
|
|
|
def self.delete_liking(likerId, targetEntityId)
|
|
Like.delete_all "(user_id = '#{likerId}' AND likable_id = '#{targetEntityId}')"
|
|
end
|
|
|
|
# def create_session_like(targetSessionId)
|
|
# targetSession = MusicSession.find(targetSessionId)
|
|
|
|
# like = Like.new
|
|
# like.likable = targetSession
|
|
# like.user = self
|
|
# like.save
|
|
# end
|
|
|
|
# def create_recording_like(targetRecordingId)
|
|
# targetRecording = Recording.find(targetRecordingId)
|
|
|
|
# like = Like.new
|
|
# like.likable = targetRecording
|
|
# like.user = self
|
|
# like.save
|
|
# end
|
|
|
|
def self.finalize_update_email(update_email_token)
|
|
# updates the user model to have a new email address
|
|
user = User.find_by_update_email_token!(update_email_token)
|
|
|
|
user.updated_email = true
|
|
user.email = user.update_email
|
|
user.update_email_token = nil
|
|
user.save
|
|
begin
|
|
RecurlyClient.new.update_account(user)
|
|
rescue Recurly::Error
|
|
@@log.debug("No recurly account found; continuing")
|
|
end
|
|
|
|
return user
|
|
end
|
|
|
|
def self.create_favorite(user_id, recording_id)
|
|
favorite = UserFavorite.new
|
|
favorite.user_id = user_id
|
|
favorite.recording_id = recording_id
|
|
favorite.save
|
|
end
|
|
|
|
def favorite_count
|
|
0 # FIXME: update this with recording likes count when implemented
|
|
end
|
|
|
|
def self.delete_favorite(user_id, recording_id)
|
|
JamRuby::UserFavorite.delete_all "(user_id = '#{user_id}' AND recording_id = '#{recording_id}')"
|
|
end
|
|
|
|
def self.save_session_settings(user, music_session)
|
|
unless user.nil?
|
|
|
|
# only save genre id and description
|
|
genres = [{id: music_session.genre.id, description: music_session.genre.description}]
|
|
|
|
# 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
|
|
|
|
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,
|
|
:genres => genres,
|
|
:invitees => invitees
|
|
}.to_json
|
|
|
|
user.session_settings = session_settings
|
|
user.save
|
|
end
|
|
end
|
|
|
|
# throws ActiveRecord::RecordNotFound if instrument is invalid
|
|
# throws an email delivery error if unable to connect out to SMTP
|
|
def self.signup(options)
|
|
|
|
first_name = options[:first_name]
|
|
last_name = options[:last_name]
|
|
email = options[:email]
|
|
password = options[:password]
|
|
password_confirmation = options[:password_confirmation]
|
|
terms_of_service = options[:terms_of_service]
|
|
location = options[:location]
|
|
instruments = options[:instruments]
|
|
birth_date = options[:birth_date]
|
|
musician = options[:musician]
|
|
photo_url = options[:photo_url]
|
|
invited_user = options[:invited_user]
|
|
fb_signup = options[:fb_signup]
|
|
signup_confirm_url = options[:signup_confirm_url]
|
|
affiliate_referral_id = options[:affiliate_referral_id]
|
|
recaptcha_failed = options[:recaptcha_failed]
|
|
any_user = options[:any_user]
|
|
reuse_card = options[:reuse_card]
|
|
signup_hint = options[:signup_hint]
|
|
|
|
user = User.new
|
|
|
|
UserManager.active_record_transaction do |user_manager|
|
|
user.first_name = first_name
|
|
user.last_name = last_name
|
|
user.email = email
|
|
user.subscribe_email = true
|
|
user.terms_of_service = terms_of_service
|
|
user.musician = musician
|
|
user.reuse_card unless reuse_card.nil?
|
|
|
|
# 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
|
|
|
|
# Seth: I think we need a flag in the signature of signup to say 'social_signup=true'. If that flag is set,
|
|
# then you can do use.updating_password = false and instead set a null password
|
|
if password.nil?
|
|
user.password = user.password_confirmation = SecureRandom.urlsafe_base64
|
|
else
|
|
user.password = password
|
|
user.password_confirmation = password_confirmation
|
|
end
|
|
|
|
user.admin = false
|
|
user.location = location
|
|
# user.city = location[:city]
|
|
# user.state = location[:state]
|
|
# user.country = location[:country]
|
|
user.birth_date = birth_date
|
|
|
|
if musician
|
|
user.last_jam_addr = location[:addr]
|
|
user.last_jam_locidispid = location[:locidispid]
|
|
user.last_jam_updated_reason = JAM_REASON_REGISTRATION
|
|
user.last_jam_updated_at = Time.now
|
|
end
|
|
|
|
if musician # only update instruments if the user is a musician
|
|
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]
|
|
user.musician_instruments << musician_instrument
|
|
end
|
|
end
|
|
end
|
|
|
|
user.photo_url = photo_url
|
|
|
|
# copy over the shopping cart to the new user, if a shopping cart is provided
|
|
if any_user
|
|
user.shopping_carts = any_user.shopping_carts
|
|
if user.shopping_carts
|
|
user.shopping_carts.each do |shopping_cart|
|
|
shopping_cart.anonymous_user_id = nil # nil out the anonymous user ID; required for uniqeness constraint on ShoppingCart
|
|
end
|
|
end
|
|
end
|
|
|
|
unless fb_signup.nil?
|
|
user.update_fb_authorization(fb_signup)
|
|
|
|
if fb_signup.email.casecmp(user.email).zero?
|
|
user.email_confirmed = true
|
|
user.signup_token = nil
|
|
else
|
|
user.email_confirmed = false
|
|
user.signup_token = SecureRandom.urlsafe_base64
|
|
end
|
|
end
|
|
|
|
if invited_user.nil?
|
|
user.can_invite = Limits::USERS_CAN_INVITE
|
|
|
|
unless user.email_confirmed # important that the only time this goes true is if some other mechanism, like fb_signup, set this high
|
|
user.email_confirmed = false
|
|
user.signup_token = SecureRandom.urlsafe_base64
|
|
end
|
|
else
|
|
# if you are invited by an admin, we'll say you can invite too.
|
|
# but if not, then you can not invite
|
|
user.can_invite = Limits::USERS_CAN_INVITE #invited_user.invited_by_administrator?
|
|
|
|
# if you came in from an invite and used the same email to signup,
|
|
# then we know you are a real human and that your email is valid.
|
|
# lucky! we'll log you in immediately
|
|
if invited_user.email && invited_user.email.casecmp(user.email).zero?
|
|
user.email_confirmed = true
|
|
user.signup_token = nil
|
|
else
|
|
user.email_confirmed = false
|
|
user.signup_token = SecureRandom.urlsafe_base64
|
|
end
|
|
|
|
|
|
# now that the user is saved, let's
|
|
if invited_user.autofriend && !invited_user.sender.nil?
|
|
# hookup this user with the sender
|
|
Friendship.save_using_models(user, invited_user.sender)
|
|
end
|
|
|
|
invited_user.accept!
|
|
invited_user.save
|
|
|
|
if invited_user.errors.any?
|
|
raise ActiveRecord::Rollback
|
|
end
|
|
end
|
|
|
|
user.save
|
|
|
|
# if the user has just one, free jamtrack in their shopping cart, and it matches the signup hint, then auto-buy it
|
|
# only_freebie_in_cart =
|
|
# signup_hint &&
|
|
# signup_hint.jam_track &&
|
|
# user.shopping_carts.length == 1 &&
|
|
# user.shopping_carts[0].cart_product == signup_hint.jam_track &&
|
|
# user.shopping_carts[0].product_info[:free]
|
|
#
|
|
# if only_freebie_in_cart
|
|
# Sale.place_order(user, user.shopping_carts)
|
|
# end
|
|
|
|
user.errors.add("recaptcha", "verification failed") if recaptcha_failed
|
|
|
|
if user.errors.any?
|
|
raise ActiveRecord::Rollback
|
|
else
|
|
if user.affiliate_referral = AffiliatePartner.find_by_id(affiliate_referral_id)
|
|
user.save
|
|
end if affiliate_referral_id.present?
|
|
|
|
# don't send an signup email if email is already confirmed
|
|
if user.email_confirmed
|
|
UserMailer.welcome_message(user).deliver
|
|
else
|
|
# 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.confirm_email(user, signup_confirm_url.nil? ? nil : (signup_confirm_url + "/" + user.signup_token) ).deliver
|
|
end
|
|
end
|
|
end
|
|
user
|
|
end # def signup
|
|
|
|
# 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
|
|
user.terms_of_service = true
|
|
|
|
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]
|
|
user.musician_instruments << musician_instrument
|
|
end
|
|
|
|
if photo_url.nil?
|
|
user.photo_url = photo_url
|
|
end
|
|
|
|
user.signup_token = nil
|
|
user.save
|
|
|
|
if user.errors.any?
|
|
raise ActiveRecord::Rollback
|
|
end
|
|
end
|
|
|
|
return user
|
|
end
|
|
|
|
def signup_confirm
|
|
self.signup_token = nil
|
|
self.confirm_email!
|
|
self.save
|
|
end
|
|
|
|
# gets the GeoIpLocation for the user's last_jam_locidispid (where are they REALLY, vs profile info)
|
|
def geoiplocation
|
|
GeoIpLocations.find_by_locid(last_jam_locidispid / 1000000) if last_jam_locidispid
|
|
end
|
|
|
|
def update_last_jam(remote_ip, reason)
|
|
location = GeoIpLocations.lookup(remote_ip)
|
|
self.last_jam_addr = location[:addr]
|
|
self.last_jam_locidispid = location[:locidispid]
|
|
self.last_jam_updated_reason = reason
|
|
self.last_jam_updated_at = Time.now
|
|
save!
|
|
end
|
|
|
|
def update_addr_loc(connection, reason)
|
|
unless connection
|
|
@@log.warn("no connection specified in update_addr_loc with reason #{reason}")
|
|
return
|
|
end
|
|
|
|
if connection.locidispid.nil?
|
|
@@log.warn("no locidispid for connection's ip_address: #{connection.ip_address}")
|
|
return
|
|
end
|
|
|
|
# we don't use a websocket login to update the user's record unless there is no addr
|
|
if reason == JAM_REASON_LOGIN && last_jam_addr
|
|
return
|
|
end
|
|
|
|
self.last_jam_addr = connection.addr
|
|
self.last_jam_locidispid = connection.locidispid
|
|
self.last_jam_updated_reason = reason
|
|
self.last_jam_updated_at = Time.now
|
|
unless self.save
|
|
@@log.warn("unable to update user #{self} with last_jam_reason #{reason}. errors: #{self.errors.inspect}")
|
|
end
|
|
end
|
|
|
|
def escape_filename(path)
|
|
dir = File.dirname(path)
|
|
file = File.basename(path)
|
|
"#{dir}/#{ERB::Util.url_encode(file)}"
|
|
end
|
|
|
|
def update_avatar(original_fpfile, cropped_fpfile, cropped_large_fpfile, crop_selection, aws_bucket)
|
|
self.updating_avatar = true
|
|
|
|
cropped_s3_path = cropped_fpfile["key"]
|
|
cropped_large_s3_path = cropped_large_fpfile["key"]
|
|
|
|
self.update_attributes(
|
|
:original_fpfile => original_fpfile,
|
|
:cropped_fpfile => cropped_fpfile,
|
|
:cropped_large_fpfile => cropped_large_fpfile,
|
|
:cropped_s3_path => cropped_s3_path,
|
|
:cropped_large_s3_path => cropped_large_s3_path,
|
|
:crop_selection => crop_selection,
|
|
:photo_url => S3Util.url(aws_bucket, escape_filename(cropped_s3_path), :secure => true),
|
|
:large_photo_url => S3Util.url(aws_bucket, escape_filename(cropped_large_s3_path), :secure => true)
|
|
)
|
|
end
|
|
|
|
def delete_avatar(aws_bucket)
|
|
|
|
User.transaction do
|
|
|
|
unless self.cropped_s3_path.nil?
|
|
S3Util.delete(aws_bucket, File.dirname(self.cropped_s3_path) + '/cropped.jpg')
|
|
S3Util.delete(aws_bucket, self.cropped_s3_path)
|
|
S3Util.delete(aws_bucket, self.cropped_large_s3_path)
|
|
end
|
|
|
|
return self.update_attributes(
|
|
:original_fpfile => nil,
|
|
:cropped_fpfile => nil,
|
|
:cropped_large_fpfile => nil,
|
|
:cropped_s3_path => nil,
|
|
:cropped_large_s3_path => nil,
|
|
:photo_url => nil,
|
|
:crop_selection => nil,
|
|
:large_photo_url => nil
|
|
)
|
|
end
|
|
|
|
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_confirm
|
|
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)
|
|
# remove email_confirmed restriction due to VRFS-378
|
|
|
|
# we only allow users that have confirmed email to authenticate
|
|
# user = User.where('email_confirmed=true').find_by_email(email)
|
|
|
|
# do a case insensitive search for email, because we store it case sensitive
|
|
user = User.where("email ILIKE ?", email).first
|
|
|
|
if user && user.valid_password?(password)
|
|
return user
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
|
|
def invalidate_user_authorization(provider)
|
|
auth = user_authorization(provider)
|
|
auth.destroy if auth
|
|
end
|
|
|
|
def user_authorization(provider)
|
|
user_authorizations.where(provider: provider).first
|
|
end
|
|
|
|
def auth_twitter
|
|
!user_authorization('twitter').nil?
|
|
end
|
|
|
|
def build_twitter_authorization(auth_hash)
|
|
|
|
twitter_uid = auth_hash[:uid]
|
|
credentials = auth_hash[:credentials]
|
|
secret = credentials[:secret] if credentials
|
|
token = credentials[:token] if credentials
|
|
|
|
if twitter_uid && secret && token
|
|
user_authorization = nil
|
|
|
|
unless self.new_record?
|
|
# see if this user has an existing user_authorization for this provider
|
|
user_authorization = UserAuthorization.find_by_user_id_and_provider(self.id, 'twitter')
|
|
end
|
|
end
|
|
|
|
if user_authorization.nil?
|
|
user_authorization = UserAuthorization.new(provider: 'twitter',
|
|
uid: twitter_uid,
|
|
token: token,
|
|
secret: secret,
|
|
user: self)
|
|
else
|
|
user_authorization.uid = twitter_uid
|
|
user_authorization.token = token
|
|
user_authorization.secret = secret
|
|
end
|
|
|
|
user_authorization
|
|
end
|
|
|
|
# updates an existing user_authorization for facebook, or creates a new one if none exist
|
|
def update_fb_authorization(fb_signup)
|
|
if fb_signup.uid && fb_signup.token && fb_signup.token_expires_at
|
|
|
|
user_authorization = nil
|
|
|
|
unless self.new_record?
|
|
# see if this user has an existing user_authorization for this provider
|
|
user_authorization = UserAuthorization.find_by_user_id_and_provider(self.id, 'facebook')
|
|
end
|
|
|
|
if user_authorization.nil?
|
|
self.user_authorizations.build provider: 'facebook',
|
|
uid: fb_signup.uid,
|
|
token: fb_signup.token,
|
|
token_expiration: fb_signup.token_expires_at,
|
|
user: self
|
|
else
|
|
user_authorization.uid = fb_signup.uid
|
|
user_authorization.token = fb_signup.token
|
|
user_authorization.token_expiration = fb_signup.token_expires_at
|
|
user_authorization.save
|
|
end
|
|
end
|
|
end
|
|
|
|
def provides_location?
|
|
!self.city.blank? && (!self.state.blank? || !self.country.blank?)
|
|
end
|
|
|
|
def self.update_locidispids(use_copied=true)
|
|
# using last_jam_addr, we can rebuild
|
|
# * last_jam_locidispid
|
|
# * last_jam_updated_reason
|
|
# * last_jam_updated_at
|
|
|
|
# this will set a user's last_jam_locidispid = NULL if there are no geoiplocations/blocks that match their IP address, or if there are no JamIsps that match the IP address
|
|
# otherwise, last_jam_locidispid will be updated to the correct new value.
|
|
# updates all user's locidispids
|
|
|
|
table_suffix = use_copied ? '_copied' : ''
|
|
|
|
User.connection.execute("UPDATE users SET last_jam_locidispid = (SELECT geolocs.locid as geolocid FROM geoipblocks#{table_suffix} as geoblocks INNER JOIN geoiplocations#{table_suffix} AS geolocs ON geoblocks.locid = geolocs.locid WHERE geoblocks.geom && ST_MakePoint(users.last_jam_addr, 0) AND users.last_jam_addr BETWEEN geoblocks.beginip AND geoblocks.endip LIMIT 1) * 1000000::bigint +(SELECT coid FROM jamisp#{table_suffix} as jisp WHERE geom && ST_MakePoint(users.last_jam_addr, 0) AND users.last_jam_addr BETWEEN beginip AND endip LIMIT 1), last_jam_updated_at = NOW(), last_jam_updated_reason='i' ").check
|
|
end
|
|
|
|
def self.after_maxmind_import
|
|
update_locidispids
|
|
end
|
|
# def check_lat_lng
|
|
# if (city_changed? || state_changed? || country_changed?) && !lat_changed? && !lng_changed?
|
|
# update_lat_lng
|
|
# end
|
|
# end
|
|
|
|
# def update_lat_lng(ip_addy=nil)
|
|
# if provides_location? # ip_addy argument ignored in this case
|
|
# return false unless ip_addy.nil? # do nothing if attempting to set latlng from an ip address
|
|
# query = { :city => self.city }
|
|
# query[:region] = self.state unless self.state.blank?
|
|
# query[:country] = self.country unless self.country.blank?
|
|
# if geo = MaxMindGeo.where(query).limit(1).first
|
|
# geo.lat = nil if geo.lat = 0
|
|
# geo.lng = nil if geo.lng = 0
|
|
# if geo.lat && geo.lng && (self.lat != geo.lat || self.lng != geo.lng)
|
|
# self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
|
|
# return true
|
|
# end
|
|
# end
|
|
# elsif ip_addy
|
|
# if geo = MaxMindGeo.ip_lookup(ip_addy)
|
|
# geo.lat = nil if geo.lat = 0
|
|
# geo.lng = nil if geo.lng = 0
|
|
# if self.lat != geo.lat || self.lng != geo.lng
|
|
# self.update_attributes({ :lat => geo.lat, :lng => geo.lng })
|
|
# return true
|
|
# end
|
|
# end
|
|
# else
|
|
# if self.lat || self.lng
|
|
# self.update_attributes({ :lat => nil, :lng => nil })
|
|
# return true
|
|
# end
|
|
# end
|
|
# false
|
|
# end
|
|
|
|
def current_city(ip_addy=nil)
|
|
# unless self.city
|
|
# if self.lat && self.lng
|
|
# # todo this is really dumb, you can't compare lat lng for equality
|
|
# return MaxMindGeo.where(['lat = ? AND lng = ?',self.lat,self.lng]).limit(1).first.try(:city)
|
|
# elsif ip_addy
|
|
# return MaxMindGeo.ip_lookup(ip_addy).try(:city)
|
|
# end
|
|
# else
|
|
# return self.city
|
|
# end
|
|
self.city
|
|
end
|
|
|
|
def update_audio_latency(connection, audio_latency)
|
|
# the backend sometimes gives tiny numbers, and sometimes very large numbers
|
|
if audio_latency > MINIMUM_AUDIO_LATENCY && audio_latency < MAXIMUM_AUDIO_LATENCY
|
|
# updating the connection is best effort; if it's not there that's OK
|
|
if connection
|
|
Connection.where(:id => connection.id).update_all(:last_jam_audio_latency => audio_latency)
|
|
end
|
|
|
|
self.last_jam_audio_latency = audio_latency
|
|
self.save
|
|
end
|
|
end
|
|
|
|
def top_followings
|
|
@topf ||= User.joins("INNER JOIN follows ON follows.followable_id = users.id AND follows.followable_type = '#{self.class.to_s}'")
|
|
.where(['follows.user_id = ?', self.id])
|
|
.order('follows.created_at DESC')
|
|
.limit(3)
|
|
end
|
|
|
|
def nearest_musicians
|
|
# FIXME: replace with Scotts scoring query
|
|
Search.new_musicians(self, Time.now - 1.week)
|
|
end
|
|
|
|
def self.deliver_new_musician_notifications(since_date=nil)
|
|
since_date ||= Time.now-1.week
|
|
# return musicians with locidispid not null
|
|
self.musicians_geocoded.find_each do |usr|
|
|
Search.new_musicians(usr, since_date) do |new_nearby|
|
|
UserMailer.new_musicians(usr, new_nearby).deliver
|
|
end
|
|
end
|
|
end
|
|
|
|
def facebook_invite!
|
|
unless iu = InvitedUser.facebook_invite(self)
|
|
iu = InvitedUser.new
|
|
iu.sender = self
|
|
iu.autofriend = true
|
|
iu.invite_medium = InvitedUser::FB_MEDIUM
|
|
iu.save
|
|
end
|
|
iu
|
|
end
|
|
|
|
# both email and name helps someone understand/recall/verify who they are looking at
|
|
def autocomplete_display_name
|
|
"#{email} (#{name})"
|
|
end
|
|
|
|
# used by formtastic for display
|
|
def to_label
|
|
autocomplete_display_name
|
|
end
|
|
|
|
# devise compatibility
|
|
|
|
#def encrypted_password
|
|
# logger.debug("password digest returned #{self.password_digest}")
|
|
# self.password_digest
|
|
#end
|
|
|
|
#def encrypted_password=(encrypted_password)
|
|
# self.password_digest = encrypted_password
|
|
#end
|
|
|
|
def self.id_for_email(email)
|
|
User.where(:email => email).limit(1).pluck(:id).first
|
|
end
|
|
|
|
# checks if user has submitted RSVP to a session
|
|
def has_rsvp(session)
|
|
slots = RsvpSlot.find_by_sql(%Q{select rs.*
|
|
from rsvp_slots rs
|
|
inner join rsvp_requests_rsvp_slots rrrs on rrrs.rsvp_slot_id = rs.id
|
|
inner join rsvp_requests rr on rr.id = rrrs.rsvp_request_id
|
|
where rs.music_session_id = '#{session.id}'
|
|
and rr.user_id = '#{self.id}'
|
|
})
|
|
|
|
!slots.blank?
|
|
end
|
|
|
|
def has_approved_rsvp(session)
|
|
approved_slots = RsvpSlot.find_by_sql(%Q{select rs.*
|
|
from rsvp_slots rs
|
|
inner join rsvp_requests_rsvp_slots rrrs on rrrs.rsvp_slot_id = rs.id
|
|
inner join rsvp_requests rr on rr.id = rrrs.rsvp_request_id
|
|
where rs.music_session_id = '#{session.id}'
|
|
and rr.user_id = '#{self.id}'
|
|
and rrrs.chosen = true
|
|
})
|
|
|
|
!approved_slots.blank?
|
|
end
|
|
# end devise compatibility
|
|
|
|
def self.stats
|
|
stats = {}
|
|
result = User.select('count(CASE WHEN musician THEN 1 ELSE null END) as musician_count, count(CASE WHEN musician = FALSE THEN 1 ELSE null END) as fan_count, count(first_downloaded_client_at) first_downloaded_client_at_count, count(first_ran_client_at) first_ran_client_at_count, count(first_certified_gear_at) first_certified_gear_at_count, count(first_music_session_at) as first_music_session_at_count, count(first_invited_at) first_invited_at_count, count(first_friended_at) as first_friended_at_count, count(first_social_promoted_at) first_social_promoted_at_count, avg(last_jam_audio_latency) last_jam_audio_latency_avg').first
|
|
stats['musicians'] = result['musician_count'].to_i
|
|
stats['fans'] = result['fan_count'].to_i
|
|
stats['downloaded_client'] = result['first_downloaded_client_at_count'].to_i
|
|
stats['ran_client'] = result['first_ran_client_at_count'].to_i
|
|
stats['certified_gear'] = result['first_certified_gear_at_count'].to_i
|
|
stats['jammed'] = result['first_music_session_at_count'].to_i
|
|
stats['invited'] = result['first_invited_at_count'].to_i
|
|
stats['friended'] = result['first_friended_at_count'].to_i
|
|
stats['social_promoted'] = result['first_social_promoted_at_count'].to_i
|
|
stats['audio_latency_avg'] = result['last_jam_audio_latency_avg'].to_f
|
|
stats
|
|
end
|
|
|
|
def destroy_all_shopping_carts
|
|
ShoppingCart.where("user_id=?", self).destroy_all
|
|
end
|
|
|
|
def unsubscribe_token
|
|
self.class.create_access_token(self)
|
|
end
|
|
|
|
# Verifier based on our application secret
|
|
def self.verifier
|
|
ActiveSupport::MessageVerifier.new(APP_CONFIG.secret_token)
|
|
end
|
|
|
|
# Get a user from a token
|
|
def self.read_access_token(signature)
|
|
uid = self.verifier.verify(signature)
|
|
User.find_by_id uid
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
|
nil
|
|
end
|
|
|
|
# Class method for token generation
|
|
def self.create_access_token(user)
|
|
verifier.generate(user.id)
|
|
end
|
|
|
|
# URL to jam-admin
|
|
def admin_url
|
|
APP_CONFIG.admin_root_url + "/admin/users/" + id
|
|
end
|
|
|
|
def jam_track_rights_admin_url
|
|
APP_CONFIG.admin_root_url + "/admin/jam_track_rights?q[user_id_equals]=#{id}&commit=Filter&order=created_at DESC"
|
|
end
|
|
|
|
# these are signup attributes that we default to when not presenting the typical form @ /signup
|
|
def self.musician_defaults(remote_ip, confirmation_url, any_user, options)
|
|
options = options || {}
|
|
options[:remote_ip] = remote_ip
|
|
options[:birth_date] = nil
|
|
options[:instruments] = [{:instrument_id => 'other', :proficiency_level => 1, :priority => 1}]
|
|
options[:musician] = true
|
|
options[:skip_recaptcha] = true
|
|
options[:invited_user] = nil
|
|
options[:fb_signup] = nil
|
|
options[:signup_confirm_url] = confirmation_url
|
|
options[:any_user] = any_user
|
|
options
|
|
end
|
|
|
|
private
|
|
def create_remember_token
|
|
self.remember_token = SecureRandom.urlsafe_base64
|
|
end
|
|
|
|
def stringify_avatar_info
|
|
# fpfile comes in as a hash, which is a easy-to-use and validate form. However, we store it as a VARCHAR,
|
|
# so we need t oconvert it to JSON before storing it (otherwise it gets serialized as a ruby object)
|
|
# later, when serving this data out to the REST API, we currently just leave it as a string and make a JSON capable
|
|
# client parse it, because it's very rare when it's needed at all
|
|
self.original_fpfile = original_fpfile.to_json if !original_fpfile.nil?
|
|
self.cropped_fpfile = cropped_fpfile.to_json if !cropped_fpfile.nil?
|
|
self.crop_selection = crop_selection.to_json if !crop_selection.nil?
|
|
end
|
|
end
|
|
end
|