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.



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

def skip_pull_hotlinked_image
  @skip_pull_hotlinked_image
end

Class Method Details

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



106
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
# File 'app/models/user_profile.rb', line 106

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



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

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



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

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



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

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



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

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

#clear_card_backgroundObject



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

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

#clear_profile_backgroundObject



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

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

#rebake!Object



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

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

#recook_bioObject



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

def recook_bio
  self.bio_raw_will_change!
  cook
end

#upload_card_background(upload) ⇒ Object



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

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

#upload_profile_background(upload) ⇒ Object



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

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