Class: UserProfile

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user_profile.rb

Constant Summary collapse

BAKED_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skip_pull_hotlinked_imageObject

Returns the value of attribute skip_pull_hotlinked_image.



47
48
49
# File 'app/models/user_profile.rb', line 47

def skip_pull_hotlinked_image
  @skip_pull_hotlinked_image
end

Class Method Details

.import_url_for_user(background_url, user, options = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/user_profile.rb', line 107

def self.import_url_for_user(background_url, user, options = nil)
  if SiteSetting.verbose_upload_logging
    Rails.logger.warn(
      "Verbose Upload Logging: Downloading profile background from #{background_url}",
    )
  end

  tempfile =
    FileHelper.download(
      background_url,
      max_file_size: SiteSetting.max_image_size_kb.kilobytes,
      tmp_file_name: "sso-profile-background",
      follow_redirect: true,
      skip_rate_limit: 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..upload_card_background(upload)
  else
    user..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

.rebake_old(limit) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/user_profile.rb', line 88

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

Instance Method Details

#bio_excerpt(length = 350, opts = {}) ⇒ Object



49
50
51
52
53
54
# File 'app/models/user_profile.rb', line 49

def bio_excerpt(length = 350, opts = {})
  return nil if bio_cooked.blank?
  excerpt = PrettyText.excerpt(bio_cooked, length, opts).sub(/<br>\z/, "")
  return excerpt if excerpt.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
  PrettyText.strip_links(excerpt)
end

#bio_processedObject



56
57
58
59
60
61
# File 'app/models/user_profile.rb', line 56

def bio_processed
  if bio_cooked.blank? || (user.has_trust_level?(TrustLevel[1]) && !user.suspended?)
    return bio_cooked
  end
  PrettyText.strip_links(bio_cooked)
end

#bio_summaryObject



63
64
65
# File 'app/models/user_profile.rb', line 63

def bio_summary
  bio_excerpt(500, strip_links: true, text_entities: true)
end

#clear_card_backgroundObject



76
77
78
# File 'app/models/user_profile.rb', line 76

def clear_card_background
  self.update!(card_background_upload: nil)
end

#clear_profile_backgroundObject



84
85
86
# File 'app/models/user_profile.rb', line 84

def clear_profile_background
  self.update!(profile_background_upload: nil)
end

#rebake!Object



103
104
105
# File 'app/models/user_profile.rb', line 103

def rebake!
  update_columns(bio_cooked: cooked, bio_cooked_version: BAKED_VERSION)
end

#recook_bioObject



67
68
69
70
# File 'app/models/user_profile.rb', line 67

def recook_bio
  self.bio_raw_will_change!
  cook
end

#upload_card_background(upload) ⇒ Object



72
73
74
# File 'app/models/user_profile.rb', line 72

def upload_card_background(upload)
  self.update!(card_background_upload: upload)
end

#upload_profile_background(upload) ⇒ Object



80
81
82
# File 'app/models/user_profile.rb', line 80

def upload_profile_background(upload)
  self.update!(profile_background_upload: upload)
end