From ff2183087d9e2f6bde0bcdd8588574718161df8f Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Tue, 9 Nov 2021 20:51:28 -0800 Subject: [PATCH] Preserve path when redirecting between official/personal projects --- src/hmnurl/hmnurl.go | 6 ++++++ src/website/requesthandling.go | 1 - src/website/routes.go | 18 ++++++++---------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/hmnurl/hmnurl.go b/src/hmnurl/hmnurl.go index be701d9..c5103b7 100644 --- a/src/hmnurl/hmnurl.go +++ b/src/hmnurl/hmnurl.go @@ -84,6 +84,12 @@ func UrlWithFragment(path string, query []Q, fragment string) string { return HMNProjectContext.UrlWithFragment(path, query, fragment) } +func (c *UrlContext) RewriteProjectUrl(u *url.URL) string { + // we need to strip anything matching the personal project regex to get the base path + match := RegexPersonalProject.FindString(u.Path) + return c.Url(u.Path[len(match):], QFromURL(u)) +} + func trim(path string) string { if len(path) > 0 && path[0] == '/' { return path[1:] diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index eec5729..0818a0c 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -93,7 +93,6 @@ nextroute: var params map[string]string for _, regex := range route.Regexes { - match := regex.FindStringSubmatch(currentPath) if len(match) == 0 { continue nextroute diff --git a/src/website/routes.go b/src/website/routes.go index d080d35..a16864f 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -8,6 +8,7 @@ import ( "math/rand" "net/http" "net/url" + "regexp" "strconv" "strings" "time" @@ -278,25 +279,23 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe } } + c.CurrentProject = &p.Project + c.UrlContext = UrlContextForProject(c.CurrentProject) + if !p.Project.Personal { - // TODO: Redirect to the same page on the other prefix - return c.Redirect(UrlContextForProject(&p.Project).BuildHomepage(), http.StatusSeeOther) + return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) } if c.PathParams["slug"] != models.GeneratePersonalProjectSlug(p.Project.Name) { - // TODO: Redirect to the same page on the other path - return c.Redirect(hmnurl.BuildPersonalProject(p.Project.ID, models.GeneratePersonalProjectSlug(p.Project.Name)), http.StatusSeeOther) + return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) } - c.CurrentProject = &p.Project - c.UrlContext = UrlContextForProject(c.CurrentProject) - return h(c) }) } attachProjectRoutes(rb) }) - anyProject.Group(hmnurl.RegexHomepage, func(rb *RouteBuilder) { + anyProject.Group(regexp.MustCompile("^"), func(rb *RouteBuilder) { rb.Middleware = func(h Handler) Handler { return anyProject.Middleware(func(c *RequestContext) ResponseData { // We could be on any project's subdomain. @@ -304,8 +303,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe // Check if the current project (matched by subdomain) is actually no longer official // and therefore needs to be redirected to the personal project version of the route. if c.CurrentProject.Personal { - // TODO: Redirect to the same page on the other prefix - return c.Redirect(hmnurl.BuildPersonalProject(c.CurrentProject.ID, c.CurrentProject.Slug), http.StatusSeeOther) + return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) } return h(c)