2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-27 12:54:04 -05:00
|
|
|
class UserProfile < ActiveRecord::Base
|
2019-04-28 22:58:52 -05:00
|
|
|
self.ignored_columns = %w{
|
|
|
|
card_background
|
|
|
|
profile_background
|
|
|
|
}
|
2018-04-24 08:46:57 -05:00
|
|
|
|
2014-06-10 00:19:08 -05:00
|
|
|
belongs_to :user, inverse_of: :user_profile
|
2019-04-28 22:58:52 -05:00
|
|
|
belongs_to :card_background_upload, class_name: "Upload"
|
|
|
|
belongs_to :profile_background_upload, class_name: "Upload"
|
2019-11-07 23:34:24 -06:00
|
|
|
belongs_to :granted_title_badge, class_name: "Badge"
|
2019-12-09 13:15:47 -06:00
|
|
|
belongs_to :featured_topic, class_name: 'Topic'
|
2014-06-10 00:19:08 -05:00
|
|
|
|
2014-09-08 14:17:31 -05:00
|
|
|
validates :bio_raw, length: { maximum: 3000 }
|
2017-12-20 22:27:17 -06:00
|
|
|
validates :website, url: true, allow_blank: true, if: Proc.new { |c| c.new_record? || c.website_changed? }
|
2014-06-10 00:19:08 -05:00
|
|
|
validates :user, presence: true
|
|
|
|
before_save :cook
|
2014-07-22 20:42:24 -05:00
|
|
|
after_save :trigger_badges
|
2014-06-10 00:19:08 -05:00
|
|
|
|
2016-12-26 09:24:54 -06:00
|
|
|
validate :website_domain_validator, if: Proc.new { |c| c.new_record? || c.website_changed? }
|
|
|
|
|
2015-09-14 02:51:17 -05:00
|
|
|
has_many :user_profile_views, dependent: :destroy
|
2014-10-20 12:15:58 -05:00
|
|
|
|
2014-08-05 01:37:56 -05:00
|
|
|
BAKED_VERSION = 1
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def bio_excerpt(length = 350, opts = {})
|
2019-05-29 10:05:52 -05:00
|
|
|
return nil if bio_cooked.blank?
|
2017-05-18 15:39:48 -05:00
|
|
|
excerpt = PrettyText.excerpt(bio_cooked, length, opts).sub(/<br>$/, '')
|
2014-11-25 15:14:21 -06:00
|
|
|
return excerpt if excerpt.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
|
2014-06-10 00:19:08 -05:00
|
|
|
PrettyText.strip_links(excerpt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_processed
|
2014-11-25 15:14:21 -06:00
|
|
|
return bio_cooked if bio_cooked.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
|
2014-06-10 00:19:08 -05:00
|
|
|
PrettyText.strip_links(bio_cooked)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_summary
|
2014-12-07 17:23:53 -06:00
|
|
|
bio_excerpt(500, strip_links: true, text_entities: true)
|
2014-06-10 00:19:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def recook_bio
|
|
|
|
self.bio_raw_will_change!
|
|
|
|
cook
|
|
|
|
end
|
|
|
|
|
2014-10-20 11:11:36 -05:00
|
|
|
def upload_card_background(upload)
|
2019-04-28 22:58:52 -05:00
|
|
|
self.update!(card_background_upload: upload)
|
2014-10-16 14:05:36 -05:00
|
|
|
end
|
|
|
|
|
2014-10-20 11:11:36 -05:00
|
|
|
def clear_card_background
|
2019-04-28 22:58:52 -05:00
|
|
|
self.update!(card_background_upload: nil)
|
2014-10-16 14:05:36 -05:00
|
|
|
end
|
|
|
|
|
2014-06-11 20:52:50 -05:00
|
|
|
def upload_profile_background(upload)
|
2019-04-28 22:58:52 -05:00
|
|
|
self.update!(profile_background_upload: upload)
|
2014-06-11 20:52:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_profile_background
|
2019-04-28 22:58:52 -05:00
|
|
|
self.update!(profile_background_upload: nil)
|
2014-06-11 20:52:50 -05:00
|
|
|
end
|
|
|
|
|
2014-08-05 01:37:56 -05:00
|
|
|
def self.rebake_old(limit)
|
|
|
|
problems = []
|
|
|
|
UserProfile.where('bio_cooked_version IS NULL OR bio_cooked_version < ?', BAKED_VERSION)
|
2017-07-27 20:20:09 -05:00
|
|
|
.limit(limit).each do |p|
|
2014-08-05 01:37:56 -05:00
|
|
|
begin
|
|
|
|
p.rebake!
|
|
|
|
rescue => e
|
2017-07-27 20:20:09 -05:00
|
|
|
problems << { profile: p, ex: e }
|
2014-08-05 01:37:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
problems
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebake!
|
|
|
|
update_columns(bio_cooked: cooked, bio_cooked_version: BAKED_VERSION)
|
|
|
|
end
|
|
|
|
|
2018-05-07 03:03:26 -05:00
|
|
|
def self.import_url_for_user(background_url, user, options = nil)
|
|
|
|
tempfile = FileHelper.download(
|
|
|
|
background_url,
|
|
|
|
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
|
|
|
tmp_file_name: "sso-profile-background",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
|
|
|
|
|
|
|
return unless tempfile
|
|
|
|
|
|
|
|
ext = FastImage.type(tempfile).to_s
|
|
|
|
tempfile.rewind
|
|
|
|
|
|
|
|
is_card_background = !options || options[:is_card_background]
|
|
|
|
type = is_card_background ? "card_background" : "profile_background"
|
|
|
|
|
|
|
|
upload = UploadCreator.new(tempfile, "external-profile-background." + ext, origin: background_url, type: type).create_for(user.id)
|
|
|
|
|
|
|
|
if (is_card_background)
|
|
|
|
user.user_profile.upload_card_background(upload)
|
|
|
|
else
|
|
|
|
user.user_profile.upload_profile_background(upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue Net::ReadTimeout, OpenURI::HTTPError
|
|
|
|
# skip saving, we are not connected to the net
|
|
|
|
ensure
|
|
|
|
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
|
|
|
|
end
|
|
|
|
|
2014-07-03 02:29:44 -05:00
|
|
|
protected
|
|
|
|
|
2014-07-22 20:42:24 -05:00
|
|
|
def trigger_badges
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::UserChange, user: self)
|
2014-07-03 02:29:44 -05:00
|
|
|
end
|
|
|
|
|
2014-06-10 00:19:08 -05:00
|
|
|
private
|
|
|
|
|
2014-08-05 01:37:56 -05:00
|
|
|
def cooked
|
|
|
|
if self.bio_raw.present?
|
2014-09-05 00:20:39 -05:00
|
|
|
PrettyText.cook(self.bio_raw, omit_nofollow: user.has_trust_level?(TrustLevel[3]) && !SiteSetting.tl3_links_no_follow)
|
2014-08-05 01:37:56 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-10 00:19:08 -05:00
|
|
|
def cook
|
|
|
|
if self.bio_raw.present?
|
2014-08-05 01:37:56 -05:00
|
|
|
if bio_raw_changed?
|
|
|
|
self.bio_cooked = cooked
|
|
|
|
self.bio_cooked_version = BAKED_VERSION
|
|
|
|
end
|
2014-06-10 00:19:08 -05:00
|
|
|
else
|
|
|
|
self.bio_cooked = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-26 09:24:54 -06:00
|
|
|
def website_domain_validator
|
2017-04-18 02:48:51 -05:00
|
|
|
allowed_domains = SiteSetting.user_website_domains_whitelist
|
|
|
|
return if (allowed_domains.blank? || self.website.blank?)
|
2016-12-26 09:24:54 -06:00
|
|
|
|
2018-05-17 02:51:24 -05:00
|
|
|
domain = begin
|
|
|
|
URI.parse(self.website).host
|
2018-08-14 05:23:32 -05:00
|
|
|
rescue URI::Error
|
2018-05-17 02:51:24 -05:00
|
|
|
end
|
2017-04-18 02:48:51 -05:00
|
|
|
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 09:24:54 -06:00
|
|
|
end
|
|
|
|
|
2019-12-09 13:15:47 -06:00
|
|
|
def self.remove_featured_topic_from_all_profiles(topic)
|
|
|
|
where(featured_topic_id: topic.id).update_all(featured_topic_id: nil)
|
|
|
|
end
|
2014-05-27 12:54:04 -05:00
|
|
|
end
|
2014-05-28 23:59:14 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_profiles
|
|
|
|
#
|
2019-04-28 22:58:52 -05:00
|
|
|
# user_id :integer not null, primary key
|
|
|
|
# location :string
|
|
|
|
# website :string
|
|
|
|
# bio_raw :text
|
|
|
|
# bio_cooked :text
|
|
|
|
# dismissed_banner_key :integer
|
|
|
|
# bio_cooked_version :integer
|
|
|
|
# badge_granted_title :boolean default(FALSE)
|
|
|
|
# views :integer default(0), not null
|
|
|
|
# profile_background_upload_id :integer
|
|
|
|
# card_background_upload_id :integer
|
2019-11-19 04:20:14 -06:00
|
|
|
# granted_title_badge_id :bigint
|
2014-08-06 22:33:11 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-11-19 04:20:14 -06:00
|
|
|
# index_user_profiles_on_bio_cooked_version (bio_cooked_version)
|
|
|
|
# index_user_profiles_on_card_background (card_background)
|
|
|
|
# index_user_profiles_on_granted_title_badge_id (granted_title_badge_id)
|
|
|
|
# index_user_profiles_on_profile_background (profile_background)
|
2014-05-28 23:59:14 -05:00
|
|
|
#
|
2019-05-13 09:53:42 -05:00
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (card_background_upload_id => uploads.id)
|
2019-11-07 23:34:24 -06:00
|
|
|
# fk_rails_... (granted_title_badge_id => badges.id)
|
2019-11-19 04:20:14 -06:00
|
|
|
# fk_rails_... (profile_background_upload_id => uploads.id)
|
2019-05-13 09:53:42 -05:00
|
|
|
#
|