Add redirect for old wiki URLs

This commit is contained in:
Ben Visness 2021-09-08 19:43:24 -05:00
parent 36bb2ce2d5
commit fd7754a7b6
4 changed files with 51 additions and 1 deletions

View File

@ -433,6 +433,8 @@ func BuildForumPostReply(projectSlug string, subforums []string, threadId int, p
return ProjectUrl(builder.String(), nil, projectSlug) return ProjectUrl(builder.String(), nil, projectSlug)
} }
var RegexWikiArticle = regexp.MustCompile(`^/wiki/(?P<threadid>\d+)(-([^/]+))?$`)
/* /*
* Blog * Blog
*/ */

View File

@ -2,6 +2,7 @@ package website
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
@ -851,6 +852,41 @@ func ForumPostDeleteSubmit(c *RequestContext) ResponseData {
} }
} }
func WikiArticleRedirect(c *RequestContext) ResponseData {
threadIdStr := c.PathParams["threadid"]
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
panic(err)
}
ithread, err := db.QueryOne(c.Context(), c.Conn, models.Thread{},
`
SELECT $columns
FROM handmade_thread
WHERE
id = $1
AND project_id = $2
AND NOT deleted
`,
threadId,
c.CurrentProject.ID,
)
if errors.Is(err, db.ErrNoMatchingRows) {
return FourOhFour(c)
} else if err != nil {
return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to look up wiki thread"))
}
thread := ithread.(*models.Thread)
c.Perf.StartBlock("SQL", "Fetch subforum tree")
subforumTree := models.GetFullSubforumTree(c.Context(), c.Conn)
lineageBuilder := models.MakeSubforumLineageBuilder(subforumTree)
c.Perf.EndBlock()
dest := UrlForGenericThread(thread, lineageBuilder, c.CurrentProject.Slug)
return c.Redirect(dest, http.StatusFound)
}
type commonForumData struct { type commonForumData struct {
c *RequestContext c *RequestContext

View File

@ -6,7 +6,18 @@ import (
"git.handmade.network/hmn/hmn/src/templates" "git.handmade.network/hmn/hmn/src/templates"
) )
// NOTE(asaf): Please don't use this if you already know the kind of the post beforehand. Just call the appropriate build function. // NOTE(asaf): Please don't use these if you already know the kind of the thread beforehand. Just call the appropriate build function.
func UrlForGenericThread(thread *models.Thread, lineageBuilder *models.SubforumLineageBuilder, projectSlug string) string {
switch thread.Type {
case models.ThreadTypeProjectBlogPost:
return hmnurl.BuildBlogThread(projectSlug, thread.ID, thread.Title)
case models.ThreadTypeForumPost:
return hmnurl.BuildForumThread(projectSlug, lineageBuilder.GetSubforumLineageSlugs(*thread.SubforumID), thread.ID, thread.Title, 1)
}
return hmnurl.BuildProjectHomepage(projectSlug)
}
func UrlForGenericPost(thread *models.Thread, post *models.Post, lineageBuilder *models.SubforumLineageBuilder, projectSlug string) string { func UrlForGenericPost(thread *models.Thread, post *models.Post, lineageBuilder *models.SubforumLineageBuilder, projectSlug string) string {
switch post.ThreadType { switch post.ThreadType {
case models.ThreadTypeProjectBlogPost: case models.ThreadTypeProjectBlogPost:

View File

@ -211,6 +211,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe
mainRoutes.POST(hmnurl.RegexForumPostEdit, authMiddleware(csrfMiddleware(ForumPostEditSubmit))) mainRoutes.POST(hmnurl.RegexForumPostEdit, authMiddleware(csrfMiddleware(ForumPostEditSubmit)))
mainRoutes.GET(hmnurl.RegexForumPostDelete, authMiddleware(ForumPostDelete)) mainRoutes.GET(hmnurl.RegexForumPostDelete, authMiddleware(ForumPostDelete))
mainRoutes.POST(hmnurl.RegexForumPostDelete, authMiddleware(csrfMiddleware(ForumPostDeleteSubmit))) mainRoutes.POST(hmnurl.RegexForumPostDelete, authMiddleware(csrfMiddleware(ForumPostDeleteSubmit)))
mainRoutes.GET(hmnurl.RegexWikiArticle, WikiArticleRedirect)
mainRoutes.GET(hmnurl.RegexBlog, BlogIndex) mainRoutes.GET(hmnurl.RegexBlog, BlogIndex)
mainRoutes.GET(hmnurl.RegexBlogNewThread, authMiddleware(BlogNewThread)) mainRoutes.GET(hmnurl.RegexBlogNewThread, authMiddleware(BlogNewThread))