Skip to content

A platform for community discussion. Free, open, simple.

DEV: Refactor `/my/*` redirect handling (#24409)

David Taylor5e272906ae80
diff --git a/app/assets/javascripts/discourse/app/instance-initializers/url-redirects.js b/app/assets/javascripts/discourse/app/instance-initializers/url-redirects.js
index a90b9eb6c94..80bba7ca945 100644
--- a/app/assets/javascripts/discourse/app/instance-initializers/url-redirects.js
+++ b/app/assets/javascripts/discourse/app/instance-initializers/url-redirects.js
@@ -10,8 +10,16 @@ export default {
DiscourseURL.rewrite(/^\/groups$/, "/g");
DiscourseURL.rewrite(/^\/groups\//, "/g/");
- // Initialize default homepage
+ const currentUser = owner.lookup("service:current-user");
let siteSettings = owner.lookup("service:site-settings");
+
+ // Setup `/my` redirects
+ if (currentUser) {
+ DiscourseURL.rewrite(/^\/my\//, `/u/${currentUser.username_lower}/`);
+ } else {
+ DiscourseURL.rewrite(/^\/my\/.*/, "/login-preferences");
+ }
+
initializeDefaultHomepage(siteSettings);
},
};
diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js
index 34816737966..c9c21ebac68 100644
--- a/app/assets/javascripts/discourse/app/lib/url.js
+++ b/app/assets/javascripts/discourse/app/lib/url.js
@@ -8,7 +8,6 @@ import offsetCalculator from "discourse/lib/offset-calculator";
import { defaultHomepage } from "discourse/lib/utilities";
import Category from "discourse/models/category";
import Session from "discourse/models/session";
-import User from "discourse/models/user";
import { isTesting } from "discourse-common/config/environment";
import getURL, { withoutPrefix } from "discourse-common/lib/get-url";
@@ -227,21 +226,6 @@ const DiscourseURL = EmberObject.extend({
path = path.replace(/(https?\:)?\/\/[^\/]+/, "");
- // Rewrite /my/* urls
- let myPath = getURL("/my/");
- const fullPath = getURL(path);
- if (fullPath.startsWith(myPath)) {
- const currentUser = User.current();
- if (currentUser) {
- path = fullPath.replace(
- myPath,
- `${userPath(currentUser.get("username_lower"))}/`
- );
- } else {
- return this.redirectTo("/login-preferences");
- }
- }
-
// handle prefixes
if (path.startsWith("/")) {
path = withoutPrefix(path);
diff --git a/app/assets/javascripts/discourse/tests/acceptance/group-index-test.js b/app/assets/javascripts/discourse/tests/acceptance/group-index-test.js
index 389afa2c1a0..d89d1cc9eb2 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/group-index-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/group-index-test.js
@@ -1,4 +1,4 @@
-import { click, currentURL, visit } from "@ember/test-helpers";
+import { click, currentURL, settled, visit } from "@ember/test-helpers";
import { test } from "qunit";
import {
acceptance,
@@ -146,6 +146,9 @@ acceptance("Group Members", function (needs) {
});
});
+/**
+ * Workaround for https://github.com/tildeio/router.js/pull/335
+ */
async function visitWithRedirects(url) {
try {
await visit(url);
@@ -154,6 +157,7 @@ async function visitWithRedirects(url) {
if (message !== "TransitionAborted") {
throw error;
}
+ await settled();
}
}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-test.js
index 20f67acc58c..4d28c77012b 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/user-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/user-test.js
@@ -1,5 +1,11 @@
import EmberObject from "@ember/object";
-import { click, currentRouteName, visit } from "@ember/test-helpers";
+import {
+ click,
+ currentRouteName,
+ currentURL,
+ settled,
+ visit,
+} from "@ember/test-helpers";
import { test } from "qunit";
import userFixtures from "discourse/tests/fixtures/user-fixtures";
import {
@@ -14,6 +20,21 @@ import selectKit from "discourse/tests/helpers/select-kit-helper";
import { cloneJSON } from "discourse-common/lib/object";
import I18n from "discourse-i18n";
+/**
+ * Workaround for https://github.com/tildeio/router.js/pull/335
+ */
+async function visitWithRedirects(url) {
+ try {
+ await visit(url);
+ } catch (error) {
+ const { message } = error;
+ if (message !== "TransitionAborted") {
+ throw error;
+ }
+ await settled();
+ }
+}
+
acceptance("User Routes", function (needs) {
needs.user();
@@ -386,3 +407,25 @@ acceptance("User - Logout", function (needs) {
await click(".dialog-overlay");
});
});
+
+acceptance("User - /my/ shortcuts for logged-in users", function (needs) {
+ needs.user({ username: "eviltrout" });
+
+ test("Redirects correctly", async function (assert) {
+ await visitWithRedirects("/my/activity");
+ assert.strictEqual(currentURL(), "/u/eviltrout/activity");
+
+ await visitWithRedirects("/my/preferences/account");
+ assert.strictEqual(currentURL(), "/u/eviltrout/preferences/account");
+ });
+});
+
+acceptance("User - /my/ shortcuts for anon users", function () {
+ test("Redirects correctly", async function (assert) {
+ await visitWithRedirects("/my/activity");
+ assert.strictEqual(currentURL(), "/login-preferences");
+
+ await visitWithRedirects("/my/preferences/account");
+ assert.strictEqual(currentURL(), "/login-preferences");
+ });
+});
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js
index 81c6f570f21..387caf2b30d 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js
@@ -6,7 +6,6 @@ import DiscourseURL, {
prefixProtocol,
userPath,
} from "discourse/lib/url";
-import User from "discourse/models/user";
import { logIn } from "discourse/tests/helpers/qunit-helpers";
import { setPrefix } from "discourse-common/lib/get-url";
@@ -77,8 +76,6 @@ module("Unit | Utility | url", function (hooks) {
test("routeTo with prefix", async function (assert) {
setPrefix("/forum");
- logIn();
- const user = User.current();
sinon.stub(DiscourseURL, "router").get(() => {
return {
@@ -88,7 +85,7 @@ module("Unit | Utility | url", function (hooks) {
sinon.stub(DiscourseURL, "handleURL");
DiscourseURL.routeTo("/my/messages");
assert.ok(
- DiscourseURL.handleURL.calledWith(`/u/${user.username}/messages`),
+ DiscourseURL.handleURL.calledWith(`/my/messages`),
"it should navigate to the messages page"
);
});