Preserve path when redirecting between official/personal projects

This commit is contained in:
Ben Visness 2021-11-09 20:51:28 -08:00
parent dfbcfbeacc
commit ff2183087d
3 changed files with 14 additions and 11 deletions

View File

@ -84,6 +84,12 @@ func UrlWithFragment(path string, query []Q, fragment string) string {
return HMNProjectContext.UrlWithFragment(path, query, fragment) 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 { func trim(path string) string {
if len(path) > 0 && path[0] == '/' { if len(path) > 0 && path[0] == '/' {
return path[1:] return path[1:]

View File

@ -93,7 +93,6 @@ nextroute:
var params map[string]string var params map[string]string
for _, regex := range route.Regexes { for _, regex := range route.Regexes {
match := regex.FindStringSubmatch(currentPath) match := regex.FindStringSubmatch(currentPath)
if len(match) == 0 { if len(match) == 0 {
continue nextroute continue nextroute

View File

@ -8,6 +8,7 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "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 { if !p.Project.Personal {
// TODO: Redirect to the same page on the other prefix return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther)
return c.Redirect(UrlContextForProject(&p.Project).BuildHomepage(), http.StatusSeeOther)
} }
if c.PathParams["slug"] != models.GeneratePersonalProjectSlug(p.Project.Name) { if c.PathParams["slug"] != models.GeneratePersonalProjectSlug(p.Project.Name) {
// TODO: Redirect to the same page on the other path return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther)
return c.Redirect(hmnurl.BuildPersonalProject(p.Project.ID, models.GeneratePersonalProjectSlug(p.Project.Name)), http.StatusSeeOther)
} }
c.CurrentProject = &p.Project
c.UrlContext = UrlContextForProject(c.CurrentProject)
return h(c) return h(c)
}) })
} }
attachProjectRoutes(rb) attachProjectRoutes(rb)
}) })
anyProject.Group(hmnurl.RegexHomepage, func(rb *RouteBuilder) { anyProject.Group(regexp.MustCompile("^"), func(rb *RouteBuilder) {
rb.Middleware = func(h Handler) Handler { rb.Middleware = func(h Handler) Handler {
return anyProject.Middleware(func(c *RequestContext) ResponseData { return anyProject.Middleware(func(c *RequestContext) ResponseData {
// We could be on any project's subdomain. // 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 // 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. // and therefore needs to be redirected to the personal project version of the route.
if c.CurrentProject.Personal { if c.CurrentProject.Personal {
// TODO: Redirect to the same page on the other prefix return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther)
return c.Redirect(hmnurl.BuildPersonalProject(c.CurrentProject.ID, c.CurrentProject.Slug), http.StatusSeeOther)
} }
return h(c) return h(c)