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

This change means that the `/my` redirects will be handled by the ember 'unknown' route, and will therefore function correctly when using pure-ember transition methods like `router.transitionTo`
This commit is contained in:
David Taylor 2023-11-16 12:16:06 +00:00 committed by GitHub
parent e9288896e7
commit 5e272906ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 23 deletions

View File

@ -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);
},
};

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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");
});
});

View File

@ -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"
);
});