DEV: Make site setting type uploaded_image_list use upload IDs (#10401)

DEV: Make site setting type uploaded_image_list use upload IDs (#10401)

It used to be a list of concatenated upload URLs which was prone to break.

diff --git a/app/assets/javascripts/admin/addon/controllers/modals/admin-uploaded-image-list.js b/app/assets/javascripts/admin/addon/controllers/modals/admin-uploaded-image-list.js
index 2185b7b..0aeffaa 100644
--- a/app/assets/javascripts/admin/addon/controllers/modals/admin-uploaded-image-list.js
+++ b/app/assets/javascripts/admin/addon/controllers/modals/admin-uploaded-image-list.js
@@ -7,7 +7,7 @@ export default Controller.extend(ModalFunctionality, {
   @observes("model.value")
   _setup() {
     const value = this.get("model.value");
-    this.set("images", value && value.length ? value.split("\n") : []);
+    this.set("images", value && value.length ? value.split("|") : []);
   },
 
   actions: {
@@ -20,7 +20,7 @@ export default Controller.extend(ModalFunctionality, {
     },
 
     close() {
-      this.save(this.images.join("\n"));
+      this.save(this.images.join("|"));
       this.send("closeModal");
     },
   },
diff --git a/app/assets/javascripts/discourse/app/controllers/avatar-selector.js b/app/assets/javascripts/discourse/app/controllers/avatar-selector.js
index 843a2bb..099919c 100644
--- a/app/assets/javascripts/discourse/app/controllers/avatar-selector.js
+++ b/app/assets/javascripts/discourse/app/controllers/avatar-selector.js
@@ -12,6 +12,31 @@ export default Controller.extend(ModalFunctionality, {
   gravatarLoginUrl: setting("gravatar_login_url"),
 
   @discourseComputed(
+    "siteSettings.selectable_avatars_enabled",
+    "siteSettings.selectable_avatars"
+  )
+  selectableAvatars(enabled, list) {
+    if (enabled) {
+      return list ? list.split("|") : [];
+    }
+  },
+
+  @discourseComputed(
+    "user.avatar_template",
+    "user.system_avatar_template",
+    "user.gravatar_avatar_template"
+  )
+  selected(avatarTemplate, systemAvatarTemplate, gravatarAvatarTemplate) {
+    if (avatarTemplate === systemAvatarTemplate) {
+      return "system";
+    } else if (avatarTemplate === gravatarAvatarTemplate) {
+      return "gravatar";
+    } else {
+      return "custom";
+    }
+  },
+
+  @discourseComputed(
     "selected",
     "user.system_avatar_upload_id",
     "user.gravatar_avatar_upload_id",
@@ -55,7 +80,7 @@ export default Controller.extend(ModalFunctionality, {
 
   actions: {
     uploadComplete() {
-      this.set("selected", "uploaded");
+      this.set("selected", "custom");
     },
 
     refreshGravatar() {
diff --git a/app/assets/javascripts/discourse/app/initializers/avatar-select.js b/app/assets/javascripts/discourse/app/initializers/avatar-select.js
deleted file mode 100644
index 2c3d48f..0000000
--- a/app/assets/javascripts/discourse/app/initializers/avatar-select.js
+++ /dev/null
@@ -1,36 +0,0 @@
-import showModal from "discourse/lib/show-modal";
-import { ajax } from "discourse/lib/ajax";
-
-export default {
-  name: "avatar-select",
-
-  initialize(container) {
-    this.selectableAvatarsEnabled = container.lookup(
-      "site-settings:main"
-    ).selectable_avatars_enabled;
-
-    container
-      .lookup("service:app-events")
-      .on("show-avatar-select", this, "_showAvatarSelect");
-  },
-
-  _showAvatarSelect(user) {
-    const avatarTemplate = user.avatar_template;
-    let selected = "uploaded";
-
-    if (avatarTemplate === user.system_avatar_template) {
-      selected = "system";
-    } else if (avatarTemplate === user.gravatar_avatar_template) {
-      selected = "gravatar";
-    }
-
-    const modal = showModal("avatar-selector");
-    modal.setProperties({ user, selected });
-
-    if (this.selectableAvatarsEnabled) {
-      ajax("/site/selectable-avatars.json").then((avatars) =>
-        modal.set("selectableAvatars", avatars)
-      );
-    }
-  },
-};
diff --git a/app/assets/javascripts/discourse/app/routes/preferences-account.js b/app/assets/javascripts/discourse/app/routes/preferences-account.js
index bfd5874..7e2fee5 100644
--- a/app/assets/javascripts/discourse/app/routes/preferences-account.js
+++ b/app/assets/javascripts/discourse/app/routes/preferences-account.js
@@ -1,3 +1,4 @@
+import showModal from "discourse/lib/show-modal";
 import UserBadge from "discourse/models/user-badge";
 import RestrictedUserRoute from "discourse/routes/restricted-user";
 
@@ -33,7 +34,7 @@ export default RestrictedUserRoute.extend({
 
   actions: {
     showAvatarSelector(user) {
-      this.appEvents.trigger("show-avatar-select", user);
+      showModal("avatar-selector").setProperties({ user });
     },
   },
 });
diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/avatar-selector-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/avatar-selector-test.js
index 5988091..aff8c19 100644
--- a/app/assets/javascripts/discourse/tests/unit/controllers/avatar-selector-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/controllers/avatar-selector-test.js
@@ -1,6 +1,7 @@
+import EmberObject from "@ember/object";
+import { mapRoutes } from "discourse/mapping-router";
 import { moduleFor } from "ember-qunit";
 import { test } from "qunit";
-import { mapRoutes } from "discourse/mapping-router";
 
 moduleFor("controller:avatar-selector", "controller:avatar-selector", {
   beforeEach() {
@@ -12,29 +13,33 @@ moduleFor("controller:avatar-selector", "controller:avatar-selector", {
 test("avatarTemplate", function (assert) {
   const avatarSelectorController = this.subject();
 
-  avatarSelectorController.setProperties({
-    selected: "system",
-    user: {
-      system_avatar_upload_id: 1,
-      gravatar_avatar_upload_id: 2,
-      custom_avatar_upload_id: 3,
-    },
+  const user = EmberObject.create({
+    avatar_template: "avatar",
+    system_avatar_template: "system",
+    gravatar_avatar_template: "gravatar",
+
+    system_avatar_upload_id: 1,
+    gravatar_avatar_upload_id: 2,
+    custom_avatar_upload_id: 3,
   });
 
+  avatarSelectorController.setProperties({ user });
+
+  user.set("avatar_template", "system");
   assert.equal(
     avatarSelectorController.get("selectedUploadId"),
     1,
     "we are using system by default"
   );
 
-  avatarSelectorController.set("selected", "gravatar");
+  user.set("avatar_template", "gravatar");
   assert.equal(
     avatarSelectorController.get("selectedUploadId"),
     2,
     "we are using gravatar when set"
   );
 
-  avatarSelectorController.set("selected", "custom");
+  user.set("avatar_template", "avatar");
   assert.equal(
     avatarSelectorController.get("selectedUploadId"),
     3,
diff --git a/app/controllers/admin/site_settings_controller.rb b/app/controllers/admin/site_settings_controller.rb
index f8695f2..2455a5b 100644
--- a/app/controllers/admin/site_settings_controller.rb
+++ b/app/controllers/admin/site_settings_controller.rb
@@ -16,6 +16,10 @@ class Admin::SiteSettingsController < Admin::AdminController
     value.strip! if value.is_a?(String)
     raise_access_hidden_setting(id)
 
+    if SiteSetting.type_supervisor.get_type(id) == :uploaded_image_list
+      value = Upload.get_from_urls(value.split("|")).to_a
+    end
+
     if SiteSetting.type_supervisor.get_type(id) == :upload
       value = Upload.get_from_url(value) || ""
     end
diff --git a/app/controllers/site_controller.rb b/app/controllers/site_controller.rb
index f1ea98d..c2df175 100644
--- a/app/controllers/site_controller.rb
+++ b/app/controllers/site_controller.rb
@@ -25,16 +25,6 @@ class SiteController < ApplicationController
     render json: custom_emoji
   end
 
-  def selectable_avatars
-    avatars = if SiteSetting.selectable_avatars_enabled?
-      (SiteSetting.selectable_avatars.presence || "").split("\n")
-    else
-      []
-    end
-
-    render json: avatars, root: false
-  end
-
   def basic_info
     results = {
       logo_url: UrlHelper.absolute(SiteSetting.site_logo_url),
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index aa82acb..b3fdf66 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb

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

GitHub sha: 25b8ed74

This commit appears in #10401 which was approved by danielwaterworth. It was merged by nbianca.