REFACTOR: Remove `Discourse.SiteSettings` from uploads.js

REFACTOR: Remove Discourse.SiteSettings from uploads.js

This involves passing the siteSettings around, which is somewhat error prone so I tried to be careful.

diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js
index eb2205e..46dbadd 100644
--- a/app/assets/javascripts/discourse/app/components/composer-editor.js
+++ b/app/assets/javascripts/discourse/app/components/composer-editor.js
@@ -81,7 +81,10 @@ export default Component.extend({
     if (requiredCategoryMissing) {
       return "composer.reply_placeholder_choose_category";
     } else {
-      const key = authorizesOneOrMoreImageExtensions(this.currentUser.staff)
+      const key = authorizesOneOrMoreImageExtensions(
+        this.currentUser.staff,
+        this.siteSettings
+      )
         ? "reply_placeholder"
         : "reply_placeholder_no_images";
       return `composer.${key}`;
@@ -700,6 +703,7 @@ export default Component.extend({
 
       const opts = {
         user: this.currentUser,
+        siteSettings: this.siteSettings,
         isPrivateMessage,
         allowStaffToUploadAnyFileInPm: this.siteSettings
           .allow_staff_to_upload_any_file_in_pm
diff --git a/app/assets/javascripts/discourse/app/controllers/avatar-selector.js b/app/assets/javascripts/discourse/app/controllers/avatar-selector.js
index 38d5da9..e3b995f 100644
--- a/app/assets/javascripts/discourse/app/controllers/avatar-selector.js
+++ b/app/assets/javascripts/discourse/app/controllers/avatar-selector.js
@@ -49,7 +49,7 @@ export default Controller.extend(ModalFunctionality, {
   allowAvatarUpload() {
     return (
       this.siteSettings.allow_uploaded_avatars &&
-      allowsImages(this.currentUser.staff)
+      allowsImages(this.currentUser.staff, this.siteSettings)
     );
   },
 
diff --git a/app/assets/javascripts/discourse/app/controllers/composer.js b/app/assets/javascripts/discourse/app/controllers/composer.js
index 42242dc..62b7970 100644
--- a/app/assets/javascripts/discourse/app/controllers/composer.js
+++ b/app/assets/javascripts/discourse/app/controllers/composer.js
@@ -323,12 +323,15 @@ export default Controller.extend({
 
   @discourseComputed
   allowUpload() {
-    return authorizesOneOrMoreExtensions(this.currentUser.staff);
+    return authorizesOneOrMoreExtensions(
+      this.currentUser.staff,
+      this.siteSettings
+    );
   },
 
   @discourseComputed()
   uploadIcon() {
-    return uploadIcon(this.currentUser.staff);
+    return uploadIcon(this.currentUser.staff, this.siteSettings);
   },
 
   @action
diff --git a/app/assets/javascripts/discourse/app/controllers/upload-selector.js b/app/assets/javascripts/discourse/app/controllers/upload-selector.js
index 0c9b1d1..32e834a 100644
--- a/app/assets/javascripts/discourse/app/controllers/upload-selector.js
+++ b/app/assets/javascripts/discourse/app/controllers/upload-selector.js
@@ -10,37 +10,38 @@ import {
   uploadIcon
 } from "discourse/lib/uploads";
 
-function uploadTranslate(key, user) {
-  if (allowsAttachments(user.staff)) {
-    key += "_with_attachments";
-  }
-  return `upload_selector.${key}`;
-}
-
 export default Controller.extend(ModalFunctionality, {
   imageUrl: null,
   local: equal("selection", "local"),
   remote: equal("selection", "remote"),
   selection: "local",
 
+  uploadTranslate(key) {
+    if (allowsAttachments(this.currentUser.staff, this.siteSettings)) {
+      key += "_with_attachments";
+    }
+    return `upload_selector.${key}`;
+  },
+
   @discourseComputed()
   uploadIcon() {
-    return uploadIcon(this.currentUser.staff);
+    return uploadIcon(this.currentUser.staff, this.siteSettings);
   },
 
   @discourseComputed()
   title() {
-    return uploadTranslate("title", this.currentUser);
+    return this.uploadTranslate("title");
   },
 
   @discourseComputed("selection")
   tip(selection) {
     const authorized_extensions = authorizesAllExtensions(
-      this.currentUser.staff
+      this.currentUser.staff,
+      this.siteSettings
     )
       ? ""
-      : `(${authorizedExtensions(this.currentUser.staff)})`;
-    return I18n.t(uploadTranslate(`${selection}_tip`, this.currentUser), {
+      : `(${authorizedExtensions(this.currentUser.staff, this.siteSettings)})`;
+    return I18n.t(this.uploadTranslate(`${selection}_tip`), {
       authorized_extensions
     });
   },
diff --git a/app/assets/javascripts/discourse/app/lib/uploads.js b/app/assets/javascripts/discourse/app/lib/uploads.js
index 9ceafaf..8473892 100644
--- a/app/assets/javascripts/discourse/app/lib/uploads.js
+++ b/app/assets/javascripts/discourse/app/lib/uploads.js
@@ -53,7 +53,7 @@ function validateUploadedFile(file, opts) {
   let user = opts.user;
   let staff = user && user.staff;
 
-  if (!authorizesOneOrMoreExtensions(staff)) return false;
+  if (!authorizesOneOrMoreExtensions(staff, opts.siteSettings)) return false;
 
   const name = file && file.name;
 
@@ -69,10 +69,13 @@ function validateUploadedFile(file, opts) {
   }
 
   if (opts.imagesOnly) {
-    if (!isImage(name) && !isAuthorizedImage(name, staff)) {
+    if (!isImage(name) && !isAuthorizedImage(name, staff, opts.siteSettings)) {
       bootbox.alert(
         I18n.t("post.errors.upload_not_authorized", {
-          authorized_extensions: authorizedImagesExtensions(staff)
+          authorized_extensions: authorizedImagesExtensions(
+            staff,
+            opts.siteSettings
+          )
         })
       );
       return false;
@@ -83,10 +86,13 @@ function validateUploadedFile(file, opts) {
       return false;
     }
   } else {
-    if (!authorizesAllExtensions(staff) && !isAuthorizedFile(name, staff)) {
+    if (
+      !authorizesAllExtensions(staff, opts.siteSettings) &&
+      !isAuthorizedFile(name, staff, opts.siteSettings)
+    ) {
       bootbox.alert(
         I18n.t("post.errors.upload_not_authorized", {
-          authorized_extensions: authorizedExtensions(staff)
+          authorized_extensions: authorizedExtensions(staff, opts.siteSettings)
         })
       );
       return false;
@@ -117,20 +123,20 @@ function extensionsToArray(exts) {
     .filter(ext => ext.indexOf("*") === -1);
 }
 
-function extensions() {
-  return extensionsToArray(Discourse.SiteSettings.authorized_extensions);
+function extensions(siteSettings) {
+  return extensionsToArray(siteSettings.authorized_extensions);
 }
 
-function staffExtensions() {
-  return extensionsToArray(
-    Discourse.SiteSettings.authorized_extensions_for_staff
-  );
+function staffExtensions(siteSettings) {
+  return extensionsToArray(siteSettings.authorized_extensions_for_staff);
 }
 
-function imagesExtensions(staff) {
-  let exts = extensions().filter(ext => IMAGES_EXTENSIONS_REGEX.test(ext));
+function imagesExtensions(staff, siteSettings) {
+  let exts = extensions(siteSettings).filter(ext =>
+    IMAGES_EXTENSIONS_REGEX.test(ext)
+  );
   if (staff) {
-    const staffExts = staffExtensions().filter(ext =>
+    const staffExts = staffExtensions(siteSettings).filter(ext =>
       IMAGES_EXTENSIONS_REGEX.test(ext)
     );
     exts = _.union(exts, staffExts);
@@ -138,60 +144,61 @@ function imagesExtensions(staff) {
   return exts;
 }
 
-function extensionsRegex() {
-  return new RegExp("\\.(" + extensions().join("|") + ")$", "i");
-}
-
-function imagesExtensionsRegex(staff) {
-  return new RegExp("\\.(" + imagesExtensions(staff).join("|") + ")$", "i");
-}
-
-function staffExtensionsRegex() {
-  return new RegExp("\\.(" + staffExtensions().join("|") + ")$", "i");
-}
-
-function isAuthorizedFile(fileName, staff) {
-  if (staff && staffExtensionsRegex().test(fileName)) {
+function isAuthorizedFile(fileName, staff, siteSettings) {
+  if (
+    staff &&
+    new RegExp(
+      "\\.(" + staffExtensions(siteSettings).join("|") + ")$",
+      "i"
+    ).test(fileName)
+  ) {
     return true;
   }
-  return extensionsRegex().test(fileName);
+
+  return new RegExp(
+    "\\.(" + extensions(siteSettings).join("|") + ")$",
+    "i"
+  ).test(fileName);
 }
 
-function isAuthorizedImage(fileName, staff) {

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

GitHub sha: f8aa304c

This commit appears in #10298 which was merged by eviltrout.