2014-05-28 01:54:04 +08:00
|
|
|
class UserProfile < ActiveRecord::Base
|
2014-06-10 13:19:08 +08:00
|
|
|
belongs_to :user, inverse_of: :user_profile
|
|
|
|
|
2017-04-18 15:48:51 +08:00
|
|
|
WEBSITE_REGEXP = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,10}(([0-9]{1,5})?\/.*)?$)/ix
|
|
|
|
|
2014-09-09 03:17:31 +08:00
|
|
|
validates :bio_raw, length: { maximum: 3000 }
|
2017-04-18 15:48:51 +08:00
|
|
|
validates :website, format: { with: WEBSITE_REGEXP }, allow_blank: true, if: Proc.new { |c| c.new_record? || c.website_changed? }
|
2014-06-10 13:19:08 +08:00
|
|
|
validates :user, presence: true
|
|
|
|
before_save :cook
|
2014-07-23 09:42:24 +08:00
|
|
|
after_save :trigger_badges
|
2014-06-10 13:19:08 +08:00
|
|
|
|
2016-09-09 00:03:38 +08:00
|
|
|
validates :profile_background, upload_url: true, if: :profile_background_changed?
|
|
|
|
validates :card_background, upload_url: true, if: :card_background_changed?
|
2016-07-29 01:54:17 +08:00
|
|
|
|
2016-12-26 23:24:54 +08:00
|
|
|
validate :website_domain_validator, if: Proc.new { |c| c.new_record? || c.website_changed? }
|
|
|
|
|
2014-10-21 01:15:58 +08:00
|
|
|
belongs_to :card_image_badge, class_name: 'Badge'
|
2015-09-14 15:51:17 +08:00
|
|
|
has_many :user_profile_views, dependent: :destroy
|
2014-10-21 01:15:58 +08:00
|
|
|
|
2014-08-05 14:37:56 +08:00
|
|
|
BAKED_VERSION = 1
|
|
|
|
|
2014-12-08 07:23:53 +08:00
|
|
|
def bio_excerpt(length=350, opts={})
|
2017-05-19 04:39:48 +08:00
|
|
|
excerpt = PrettyText.excerpt(bio_cooked, length, opts).sub(/<br>$/, '')
|
2014-11-26 05:14:21 +08:00
|
|
|
return excerpt if excerpt.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
|
2014-06-10 13:19:08 +08:00
|
|
|
PrettyText.strip_links(excerpt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_processed
|
2014-11-26 05:14:21 +08:00
|
|
|
return bio_cooked if bio_cooked.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
|
2014-06-10 13:19:08 +08:00
|
|
|
PrettyText.strip_links(bio_cooked)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_summary
|
|
|
|
return nil unless bio_cooked.present?
|
2014-12-08 07:23:53 +08:00
|
|
|
bio_excerpt(500, strip_links: true, text_entities: true)
|
2014-06-10 13:19:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def recook_bio
|
|
|
|
self.bio_raw_will_change!
|
|
|
|
cook
|
|
|
|
end
|
|
|
|
|
2014-10-21 00:11:36 +08:00
|
|
|
def upload_card_background(upload)
|
|
|
|
self.card_background = upload.url
|
2014-10-17 03:05:36 +08:00
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2014-10-21 00:11:36 +08:00
|
|
|
def clear_card_background
|
|
|
|
self.card_background = ""
|
2014-10-17 03:05:36 +08:00
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2014-06-12 09:52:50 +08:00
|
|
|
def upload_profile_background(upload)
|
|
|
|
self.profile_background = upload.url
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_profile_background
|
|
|
|
self.profile_background = ""
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2014-08-05 14:37:56 +08:00
|
|
|
def self.rebake_old(limit)
|
|
|
|
problems = []
|
|
|
|
UserProfile.where('bio_cooked_version IS NULL OR bio_cooked_version < ?', BAKED_VERSION)
|
|
|
|
.limit(limit).each do |p|
|
|
|
|
begin
|
|
|
|
p.rebake!
|
|
|
|
rescue => e
|
|
|
|
problems << {profile: p, ex: e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
problems
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebake!
|
|
|
|
update_columns(bio_cooked: cooked, bio_cooked_version: BAKED_VERSION)
|
|
|
|
end
|
|
|
|
|
2014-07-03 15:29:44 +08:00
|
|
|
protected
|
|
|
|
|
2014-07-23 09:42:24 +08:00
|
|
|
def trigger_badges
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::UserChange, user: self)
|
2014-07-03 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2014-06-10 13:19:08 +08:00
|
|
|
private
|
|
|
|
|
2014-08-05 14:37:56 +08:00
|
|
|
def cooked
|
|
|
|
if self.bio_raw.present?
|
2014-09-05 13:20:39 +08:00
|
|
|
PrettyText.cook(self.bio_raw, omit_nofollow: user.has_trust_level?(TrustLevel[3]) && !SiteSetting.tl3_links_no_follow)
|
2014-08-05 14:37:56 +08:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-10 13:19:08 +08:00
|
|
|
def cook
|
|
|
|
if self.bio_raw.present?
|
2014-08-05 14:37:56 +08:00
|
|
|
if bio_raw_changed?
|
|
|
|
self.bio_cooked = cooked
|
|
|
|
self.bio_cooked_version = BAKED_VERSION
|
|
|
|
end
|
2014-06-10 13:19:08 +08:00
|
|
|
else
|
|
|
|
self.bio_cooked = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-26 23:24:54 +08:00
|
|
|
def website_domain_validator
|
2017-04-18 15:48:51 +08:00
|
|
|
allowed_domains = SiteSetting.user_website_domains_whitelist
|
|
|
|
return if (allowed_domains.blank? || self.website.blank?)
|
2016-12-26 23:24:54 +08:00
|
|
|
|
2017-04-18 15:48:51 +08:00
|
|
|
domain = URI.parse(self.website).host
|
|
|
|
self.errors.add :base, (I18n.t('user.website.domain_not_allowed', domains: allowed_domains.split('|').join(", "))) unless allowed_domains.split('|').include?(domain)
|
2016-12-26 23:24:54 +08:00
|
|
|
end
|
|
|
|
|
2014-05-28 01:54:04 +08:00
|
|
|
end
|
2014-05-29 12:59:14 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_profiles
|
|
|
|
#
|
2014-07-03 15:29:44 +08:00
|
|
|
# user_id :integer not null, primary key
|
2016-02-23 07:33:53 +08:00
|
|
|
# location :string
|
|
|
|
# website :string
|
2014-07-03 15:29:44 +08:00
|
|
|
# bio_raw :text
|
|
|
|
# bio_cooked :text
|
|
|
|
# profile_background :string(255)
|
2014-11-20 11:53:15 +08:00
|
|
|
# dismissed_banner_key :integer
|
2014-08-07 11:33:11 +08:00
|
|
|
# bio_cooked_version :integer
|
2014-11-20 11:53:15 +08:00
|
|
|
# badge_granted_title :boolean default(FALSE)
|
|
|
|
# card_background :string(255)
|
|
|
|
# card_image_badge_id :integer
|
2015-09-14 11:50:59 +08:00
|
|
|
# views :integer default(0), not null
|
2014-08-07 11:33:11 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_profiles_on_bio_cooked_version (bio_cooked_version)
|
2016-11-24 10:13:03 +08:00
|
|
|
# index_user_profiles_on_card_background (card_background)
|
|
|
|
# index_user_profiles_on_profile_background (profile_background)
|
2014-05-29 12:59:14 +08:00
|
|
|
#
|