FIX: Properly associate user_profiles background urls via upload id.

FIX: Properly associate user_profiles background urls via upload id.

Upload#url is more likely and can change from time to time. When it does changes, we don’t want to have to look through multiple tables to ensure that the URLs are all up to date. Instead, we simply associate uploads properly to UserProfile so that it does not have to replicate the URLs in the table.

diff --git a/app/assets/javascripts/discourse/components/user-card-contents.js.es6 b/app/assets/javascripts/discourse/components/user-card-contents.js.es6
index 83e83ac..d78d1d6 100644
--- a/app/assets/javascripts/discourse/components/user-card-contents.js.es6
+++ b/app/assets/javascripts/discourse/components/user-card-contents.js.es6
@@ -131,7 +131,7 @@ export default Ember.Component.extend(
       }
     },
 
-    @observes("user.card_background")
+    @observes("user.card_background_upload_url")
     addBackground() {
       if (!this.get("allowBackgrounds")) {
         return;
@@ -142,7 +142,7 @@ export default Ember.Component.extend(
         return;
       }
 
-      const url = this.get("user.card_background");
+      const url = this.get("user.card_background_upload_url");
       const bg = Ember.isEmpty(url)
         ? ""
         : `url(${Discourse.getURLWithCDN(url)})`;
diff --git a/app/assets/javascripts/discourse/controllers/preferences/profile.js.es6 b/app/assets/javascripts/discourse/controllers/preferences/profile.js.es6
index 87fe1fa..803eea2 100644
--- a/app/assets/javascripts/discourse/controllers/preferences/profile.js.es6
+++ b/app/assets/javascripts/discourse/controllers/preferences/profile.js.es6
@@ -10,8 +10,8 @@ export default Ember.Controller.extend(PreferencesTabController, {
     "location",
     "custom_fields",
     "user_fields",
-    "profile_background",
-    "card_background",
+    "profile_background_upload_url",
+    "card_background_upload_url",
     "date_of_birth"
   ],
 
diff --git a/app/assets/javascripts/discourse/controllers/user.js.es6 b/app/assets/javascripts/discourse/controllers/user.js.es6
index 038dfcc..85e6c3f 100644
--- a/app/assets/javascripts/discourse/controllers/user.js.es6
+++ b/app/assets/javascripts/discourse/controllers/user.js.es6
@@ -21,8 +21,8 @@ export default Ember.Controller.extend(CanCheckEmails, {
     return !profileHidden && viewingSelf;
   },
 
-  @computed("model.profileBackground")
-  hasProfileBackground(background) {
+  @computed("model.profileBackgroundUrl")
+  hasProfileBackgroundUrl(background) {
     return !Ember.isEmpty(background.toString());
   },
 
diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6
index e80e060..4af96fe 100644
--- a/app/assets/javascripts/discourse/models/user.js.es6
+++ b/app/assets/javascripts/discourse/models/user.js.es6
@@ -77,8 +77,8 @@ const User = RestModel.extend({
     return username;
   },
 
-  @computed("profile_background")
-  profileBackground(bgUrl) {
+  @computed("profile_background_upload_url")
+  profileBackgroundUrl(bgUrl) {
     if (
       Ember.isEmpty(bgUrl) ||
       !Discourse.SiteSettings.allow_profile_backgrounds
@@ -250,8 +250,8 @@ const User = RestModel.extend({
       "user_fields",
       "muted_usernames",
       "ignored_usernames",
-      "profile_background",
-      "card_background",
+      "profile_background_upload_url",
+      "card_background_upload_url",
       "muted_tags",
       "tracked_tags",
       "watched_tags",
diff --git a/app/assets/javascripts/discourse/templates/preferences/profile.hbs b/app/assets/javascripts/discourse/templates/preferences/profile.hbs
index b906cfe..3d5a2bc 100644
--- a/app/assets/javascripts/discourse/templates/preferences/profile.hbs
+++ b/app/assets/javascripts/discourse/templates/preferences/profile.hbs
@@ -32,7 +32,8 @@
   <div class="control-group pref-profile-bg">
     <label class="control-label">{{i18n 'user.change_profile_background.title'}}</label>
     <div class="controls">
-      {{image-uploader imageUrl=model.profile_background type="profile_background"}}
+      {{image-uploader imageUrl=model.profile_background_upload_url
+                       type="profile_background"}}
     </div>
     <div class='instructions'>
       {{i18n 'user.change_profile_background.instructions'}}
@@ -42,7 +43,8 @@
   <div class="control-group pref-profile-bg">
     <label class="control-label">{{i18n 'user.change_card_background.title'}}</label>
     <div class="controls">
-      {{image-uploader imageUrl=model.card_background type="card_background"}}
+      {{image-uploader imageUrl=model.card_background_upload_url
+                       type="card_background"}}
     </div>
     <div class='instructions'>
       {{i18n 'user.change_card_background.instructions'}}
diff --git a/app/assets/javascripts/discourse/templates/user.hbs b/app/assets/javascripts/discourse/templates/user.hbs
index afee93b..04a4ab8 100644
--- a/app/assets/javascripts/discourse/templates/user.hbs
+++ b/app/assets/javascripts/discourse/templates/user.hbs
@@ -1,7 +1,7 @@
 <div class="container {{if viewingSelf 'viewing-self'}}">
   {{#d-section class="user-main"}}
 
-    <section class="{{if collapsedInfo 'collapsed-info'}} about {{if hasProfileBackground 'has-background' 'no-background'}}" >
+    <section class="{{if collapsedInfo 'collapsed-info'}} about {{if hasProfileBackgroundUrl 'has-background' 'no-background'}}" >
     {{#unless collapsedInfo}}
     {{#if showStaffCounters}}
       <div class='staff-counters'>
@@ -34,7 +34,7 @@
         {{/if}}
       </div>
     {{/if}}
-    <div class="user-profile-image" style={{model.profileBackground}}></div>
+    <div class="user-profile-image" style={{model.profileBackgroundUrl}}></div>
     {{/unless}}
 
       <div class='details'>
diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb
index 47d6c5c..52594e2 100644
--- a/app/controllers/session_controller.rb
+++ b/app/controllers/session_controller.rb
@@ -69,12 +69,16 @@ class SessionController < ApplicationController
           sso.avatar_url = UrlHelper.absolute Discourse.store.cdn_url(avatar_url)
         end
 
-        if current_user.user_profile.profile_background.present?
-          sso.profile_background_url = UrlHelper.absolute upload_cdn_path(current_user.user_profile.profile_background)
+        if current_user.user_profile.profile_background_upload.present?
+          sso.profile_background_url = UrlHelper.absolute(upload_cdn_path(
+            current_user.user_profile.profile_background_upload.url
+          ))
         end
 
-        if current_user.user_profile.card_background.present?
-          sso.card_background_url = UrlHelper.absolute upload_cdn_path(current_user.user_profile.card_background)
+        if current_user.user_profile.card_background_upload.present?
+          sso.card_background_url = UrlHelper.absolute(upload_cdn_path(
+            current_user.user_profile.card_background_upload.url
+          ))
         end
 
         if request.xhr?
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 972c1b6..bd04b32 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1241,8 +1241,8 @@ class UsersController < ApplicationController
       :location,
       :website,
       :dismissed_banner_key,
-      :profile_background,
-      :card_background
+      :profile_background_upload_url,
+      :card_background_upload_url
     ]
 
     permitted << { custom_fields: User.editable_user_custom_fields } unless User.editable_user_custom_fields.blank?
diff --git a/app/jobs/onceoff/recover_user_profile_backgrounds.rb b/app/jobs/onceoff/recover_user_profile_backgrounds.rb
deleted file mode 100644
index b96523b..0000000
--- a/app/jobs/onceoff/recover_user_profile_backgrounds.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Jobs
-  class RecoverUserProfileBackgrounds < Jobs::Onceoff
-    def execute_onceoff(_)
-      base_url = Discourse.store.absolute_base_url
-      return if !base_url.match?(/s3\.dualstack/)
-
-      old = base_url.sub('s3.dualstack.', 's3-')
-      old_like = %"#{old}%"
-
-      DB.exec(<<~SQL, from: old, to: base_url, old_like: old_like)
-        UPDATE user_profiles
-        SET profile_background = replace(profile_background, :from, :to)
-        WHERE profile_background ilike :old_like
-      SQL
-
-      DB.exec(<<~SQL, from: old, to: base_url, old_like: old_like)

[... diff too long, it was truncated ...]

GitHub sha: 24347ace

1 Like

FIX: Can't remove user profile uploads.

DEV: Add missing indexes to user_profiles (#8691)