DEV: Move `Discourse.getURL` and related functions to a module (#9966)

DEV: Move Discourse.getURL and related functions to a module (#9966)

  • DEV: Move Discourse.getURL and related functions to a module

  • DEV: Remove Discourse.getURL and Discourse.getURLWithCDN

  • FIX: get-url is required for server side code

  • DEV: Deprecate BaseUri too.

diff --git a/app/assets/javascripts/admin/components/resumable-upload.js b/app/assets/javascripts/admin/components/resumable-upload.js
index ed95ea6..97b6f19 100644
--- a/app/assets/javascripts/admin/components/resumable-upload.js
+++ b/app/assets/javascripts/admin/components/resumable-upload.js
@@ -1,3 +1,4 @@
+import getURL from "discourse-common/lib/get-url";
 import I18n from "I18n";
 import { later, schedule } from "@ember/runloop";
 import Component from "@ember/component";
@@ -31,7 +32,7 @@ export default Component.extend({
   @on("init")
   _initialize() {
     this.resumable = new Resumable({
-      target: Discourse.getURL(this.target),
+      target: getURL(this.target),
       maxFiles: 1, // only 1 file at a time
       headers: {
         "X-CSRF-Token": document.querySelector("meta[name='csrf-token']")
diff --git a/app/assets/javascripts/admin/controllers/admin-dashboard-general.js b/app/assets/javascripts/admin/controllers/admin-dashboard-general.js
index 07a195b..2030c1e 100644
--- a/app/assets/javascripts/admin/controllers/admin-dashboard-general.js
+++ b/app/assets/javascripts/admin/controllers/admin-dashboard-general.js
@@ -8,6 +8,7 @@ import AdminDashboard from "admin/models/admin-dashboard";
 import Report from "admin/models/report";
 import PeriodComputationMixin from "admin/mixins/period-computation";
 import { computed } from "@ember/object";
+import getURL from "discourse-common/lib/get-url";
 
 function staticReport(reportType) {
   return computed("reports.[]", function() {
@@ -20,7 +21,6 @@ export default Controller.extend(PeriodComputationMixin, {
   dashboardFetchedAt: null,
   exceptionController: inject("exception"),
   logSearchQueriesEnabled: setting("log_search_queries"),
-  basePath: Discourse.BaseUri,
 
   @discourseComputed("siteSettings.dashboard_general_tab_activity_metrics")
   activityMetrics(metrics) {
@@ -107,7 +107,7 @@ export default Controller.extend(PeriodComputationMixin, {
   @discourseComputed
   trendingSearchDisabledLabel() {
     return I18n.t("admin.dashboard.reports.trending_search.disabled", {
-      basePath: Discourse.BaseUri
+      basePath: getURL("/")
     });
   },
 
@@ -150,6 +150,6 @@ export default Controller.extend(PeriodComputationMixin, {
   },
 
   _reportsForPeriodURL(period) {
-    return Discourse.getURL(`/admin?period=${period}`);
+    return getURL(`/admin?period=${period}`);
   }
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-dashboard-moderation.js b/app/assets/javascripts/admin/controllers/admin-dashboard-moderation.js
index 95f820b..f37d860 100644
--- a/app/assets/javascripts/admin/controllers/admin-dashboard-moderation.js
+++ b/app/assets/javascripts/admin/controllers/admin-dashboard-moderation.js
@@ -1,3 +1,4 @@
+import getURL from "discourse-common/lib/get-url";
 import discourseComputed from "discourse-common/utils/decorators";
 import Controller from "@ember/controller";
 import PeriodComputationMixin from "admin/mixins/period-computation";
@@ -45,6 +46,6 @@ export default Controller.extend(PeriodComputationMixin, {
   },
 
   _reportsForPeriodURL(period) {
-    return Discourse.getURL(`/admin/dashboard/moderation?period=${period}`);
+    return getURL(`/admin/dashboard/moderation?period=${period}`);
   }
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js b/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js
index 031ad8e..e6f20ee 100644
--- a/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js
+++ b/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js
@@ -6,6 +6,7 @@ import Controller, { inject as controller } from "@ember/controller";
 import { popupAjaxError } from "discourse/lib/ajax-error";
 import { extractDomainFromUrl } from "discourse/lib/utilities";
 import EmberObject from "@ember/object";
+import { isAbsoluteURL } from "discourse-common/lib/get-url";
 
 export default Controller.extend({
   adminWebHooks: controller(),
@@ -109,7 +110,7 @@ export default Controller.extend({
         domain === "localhost" ||
         domain.match(/192\.168\.\d+\.\d+/) ||
         domain.match(/127\.\d+\.\d+\.\d+/) ||
-        url.startsWith(Discourse.BaseUrl)
+        isAbsoluteURL(url)
       ) {
         return bootbox.confirm(
           I18n.t("admin.web_hooks.warn_local_payload_url"),
diff --git a/app/assets/javascripts/admin/models/admin-user.js b/app/assets/javascripts/admin/models/admin-user.js
index 9977fa8..8dc7685 100644
--- a/app/assets/javascripts/admin/models/admin-user.js
+++ b/app/assets/javascripts/admin/models/admin-user.js
@@ -1,3 +1,4 @@
+import getURL from "discourse-common/lib/get-url";
 import I18n from "I18n";
 import discourseComputed from "discourse-common/utils/decorators";
 import { filter, or, gt, lt, not } from "@ember/object/computed";
@@ -43,7 +44,7 @@ const AdminUser = User.extend({
 
   @discourseComputed
   bounceLink() {
-    return Discourse.getURL("/admin/email/bounced");
+    return getURL("/admin/email/bounced");
   },
 
   canResetBounceScore: gt("bounce_score", 0),
@@ -306,7 +307,7 @@ const AdminUser = User.extend({
       type: "POST",
       data: { username_or_email: this.username }
     })
-      .then(() => (document.location = Discourse.getURL("/")))
+      .then(() => (document.location = getURL("/")))
       .catch(e => {
         if (e.status === 404) {
           bootbox.alert(I18n.t("admin.impersonate.not_found"));
@@ -395,11 +396,11 @@ const AdminUser = User.extend({
         .then(function(data) {
           if (data.success) {
             if (data.username) {
-              document.location = Discourse.getURL(
+              document.location = getURL(
                 `/admin/users/${user.get("id")}/${data.username}`
               );
             } else {
-              document.location = Discourse.getURL("/admin/users/list/active");
+              document.location = getURL("/admin/users/list/active");
             }
           } else {
             bootbox.alert(I18n.t("admin.user.anonymize_failed"));
@@ -456,7 +457,7 @@ const AdminUser = User.extend({
             if (/^\/admin\/users\/list\//.test(location)) {
               document.location = location;
             } else {
-              document.location = Discourse.getURL("/admin/users/list/active");
+              document.location = getURL("/admin/users/list/active");
             }
           } else {
             bootbox.alert(I18n.t("admin.user.delete_failed"));
diff --git a/app/assets/javascripts/admin/models/backup.js b/app/assets/javascripts/admin/models/backup.js
index 45727a2..0b6f712 100644
--- a/app/assets/javascripts/admin/models/backup.js
+++ b/app/assets/javascripts/admin/models/backup.js
@@ -1,3 +1,4 @@
+import getURL from "discourse-common/lib/get-url";
 import I18n from "I18n";
 import { ajax } from "discourse/lib/ajax";
 import { extractError } from "discourse/lib/ajax-error";
@@ -66,7 +67,7 @@ Backup.reopenClass({
         bootbox.alert(result.message);
       } else {
         // redirect to homepage (session might be lost)
-        window.location = Discourse.getURL("/");
+        window.location = getURL("/");
       }
     });
   }
diff --git a/app/assets/javascripts/admin/models/email-log.js b/app/assets/javascripts/admin/models/email-log.js
index c2eaaa2..1317df2 100644
--- a/app/assets/javascripts/admin/models/email-log.js
+++ b/app/assets/javascripts/admin/models/email-log.js
@@ -1,3 +1,4 @@
+import getURL from "discourse-common/lib/get-url";
 import { ajax } from "discourse/lib/ajax";
 import AdminUser from "admin/models/admin-user";
 import EmberObject from "@ember/object";
@@ -13,7 +14,7 @@ EmailLog.reopenClass({
     }
 
     if (attrs.post_url) {
-      attrs.post_url = Discourse.getURL(attrs.post_url);
+      attrs.post_url = getURL(attrs.post_url);
     }
 
     return this._super(attrs);
diff --git a/app/assets/javascripts/admin/models/report.js b/app/assets/javascripts/admin/models/report.js
index 2e0b448..9d2f25f 100644

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

GitHub sha: ba00cc8e

1 Like

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