2021-04-29 04:52:27 +00:00
|
|
|
package website
|
|
|
|
|
|
|
|
import (
|
2021-05-04 01:59:45 +00:00
|
|
|
"errors"
|
2021-04-29 04:52:27 +00:00
|
|
|
"math"
|
2021-07-04 22:48:08 +00:00
|
|
|
"net"
|
2021-04-29 04:52:27 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.handmade.network/hmn/hmn/src/db"
|
2021-05-03 23:59:43 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/hmnurl"
|
2021-04-29 04:52:27 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/models"
|
|
|
|
"git.handmade.network/hmn/hmn/src/oops"
|
2021-07-04 20:25:28 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/parsing"
|
2021-05-03 23:59:43 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/templates"
|
2021-05-06 05:57:14 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/utils"
|
2021-04-29 04:52:27 +00:00
|
|
|
)
|
|
|
|
|
2021-05-03 14:51:07 +00:00
|
|
|
type forumCategoryData struct {
|
|
|
|
templates.BaseData
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
NewThreadUrl string
|
|
|
|
MarkReadUrl string
|
2021-05-03 23:59:43 +00:00
|
|
|
Threads []templates.ThreadListItem
|
|
|
|
Pagination templates.Pagination
|
|
|
|
Subcategories []forumSubcategoryData
|
|
|
|
}
|
|
|
|
|
|
|
|
type forumSubcategoryData struct {
|
|
|
|
Name string
|
|
|
|
Url string
|
|
|
|
Threads []templates.ThreadListItem
|
|
|
|
TotalThreads int
|
2021-05-03 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 04:52:27 +00:00
|
|
|
func ForumCategory(c *RequestContext) ResponseData {
|
|
|
|
const threadsPerPage = 25
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch category tree")
|
|
|
|
categoryTree := models.GetFullCategoryTree(c.Context(), c.Conn)
|
|
|
|
lineageBuilder := models.MakeCategoryLineageBuilder(categoryTree)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
currentCatId, valid := validateSubforums(lineageBuilder, c.CurrentProject, c.PathParams["cats"])
|
|
|
|
if !valid {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSubforumSlugs := lineageBuilder.GetSubforumLineageSlugs(currentCatId)
|
2021-04-29 04:52:27 +00:00
|
|
|
|
2021-05-03 23:59:43 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch count of page threads")
|
2021-04-29 04:52:27 +00:00
|
|
|
numThreads, err := db.QueryInt(c.Context(), c.Conn,
|
|
|
|
`
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM handmade_thread AS thread
|
|
|
|
WHERE
|
|
|
|
thread.category_id = $1
|
|
|
|
AND NOT thread.deleted
|
|
|
|
`,
|
|
|
|
currentCatId,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to get count of threads"))
|
|
|
|
}
|
2021-05-03 23:59:43 +00:00
|
|
|
c.Perf.EndBlock()
|
2021-04-29 04:52:27 +00:00
|
|
|
|
|
|
|
numPages := int(math.Ceil(float64(numThreads) / threadsPerPage))
|
|
|
|
|
|
|
|
page := 1
|
|
|
|
pageString, hasPage := c.PathParams["page"]
|
|
|
|
if hasPage && pageString != "" {
|
|
|
|
if pageParsed, err := strconv.Atoi(pageString); err == nil {
|
|
|
|
page = pageParsed
|
|
|
|
} else {
|
2021-05-11 22:53:23 +00:00
|
|
|
return c.Redirect(hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, 1), http.StatusSeeOther)
|
2021-04-29 04:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if page < 1 || numPages < page {
|
2021-05-11 22:53:23 +00:00
|
|
|
return c.Redirect(hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, utils.IntClamp(1, page, numPages)), http.StatusSeeOther)
|
2021-04-29 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
howManyThreadsToSkip := (page - 1) * threadsPerPage
|
|
|
|
|
|
|
|
var currentUserId *int
|
|
|
|
if c.CurrentUser != nil {
|
|
|
|
currentUserId = &c.CurrentUser.ID
|
|
|
|
}
|
|
|
|
|
2021-05-03 23:59:43 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch page threads")
|
|
|
|
type threadQueryResult struct {
|
2021-04-29 04:52:27 +00:00
|
|
|
Thread models.Thread `db:"thread"`
|
|
|
|
FirstPost models.Post `db:"firstpost"`
|
|
|
|
LastPost models.Post `db:"lastpost"`
|
2021-05-03 14:51:07 +00:00
|
|
|
FirstUser *models.User `db:"firstuser"`
|
|
|
|
LastUser *models.User `db:"lastuser"`
|
2021-04-29 04:52:27 +00:00
|
|
|
ThreadLastReadTime *time.Time `db:"tlri.lastread"`
|
|
|
|
CatLastReadTime *time.Time `db:"clri.lastread"`
|
|
|
|
}
|
2021-05-03 23:59:43 +00:00
|
|
|
itMainThreads, err := db.Query(c.Context(), c.Conn, threadQueryResult{},
|
2021-04-29 04:52:27 +00:00
|
|
|
`
|
|
|
|
SELECT $columns
|
|
|
|
FROM
|
|
|
|
handmade_thread AS thread
|
|
|
|
JOIN handmade_post AS firstpost ON thread.first_id = firstpost.id
|
|
|
|
JOIN handmade_post AS lastpost ON thread.last_id = lastpost.id
|
2021-05-03 14:51:07 +00:00
|
|
|
LEFT JOIN auth_user AS firstuser ON firstpost.author_id = firstuser.id
|
|
|
|
LEFT JOIN auth_user AS lastuser ON lastpost.author_id = lastuser.id
|
|
|
|
LEFT JOIN handmade_threadlastreadinfo AS tlri ON (
|
2021-04-29 04:52:27 +00:00
|
|
|
tlri.thread_id = thread.id
|
|
|
|
AND tlri.user_id = $2
|
|
|
|
)
|
2021-05-03 14:51:07 +00:00
|
|
|
LEFT JOIN handmade_categorylastreadinfo AS clri ON (
|
2021-04-29 04:52:27 +00:00
|
|
|
clri.category_id = $1
|
|
|
|
AND clri.user_id = $2
|
|
|
|
)
|
|
|
|
WHERE
|
|
|
|
thread.category_id = $1
|
|
|
|
AND NOT thread.deleted
|
|
|
|
ORDER BY lastpost.postdate DESC
|
|
|
|
LIMIT $3 OFFSET $4
|
|
|
|
`,
|
|
|
|
currentCatId,
|
|
|
|
currentUserId,
|
|
|
|
threadsPerPage,
|
|
|
|
howManyThreadsToSkip,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to fetch threads"))
|
|
|
|
}
|
2021-05-03 23:59:43 +00:00
|
|
|
c.Perf.EndBlock()
|
2021-05-03 14:51:07 +00:00
|
|
|
defer itMainThreads.Close()
|
2021-04-29 04:52:27 +00:00
|
|
|
|
2021-05-03 23:59:43 +00:00
|
|
|
makeThreadListItem := func(row *threadQueryResult) templates.ThreadListItem {
|
2021-05-03 22:53:28 +00:00
|
|
|
hasRead := false
|
|
|
|
if row.ThreadLastReadTime != nil && row.ThreadLastReadTime.After(row.LastPost.PostDate) {
|
|
|
|
hasRead = true
|
|
|
|
} else if row.CatLastReadTime != nil && row.CatLastReadTime.After(row.LastPost.PostDate) {
|
|
|
|
hasRead = true
|
|
|
|
}
|
|
|
|
|
2021-05-03 23:59:43 +00:00
|
|
|
return templates.ThreadListItem{
|
2021-05-11 22:53:23 +00:00
|
|
|
Title: row.Thread.Title,
|
|
|
|
Url: hmnurl.BuildForumThread(c.CurrentProject.Slug, lineageBuilder.GetSubforumLineageSlugs(row.Thread.CategoryID), row.Thread.ID, row.Thread.Title, 1),
|
2021-05-25 13:12:20 +00:00
|
|
|
FirstUser: templates.UserToTemplate(row.FirstUser, c.Theme),
|
2021-05-03 14:51:07 +00:00
|
|
|
FirstDate: row.FirstPost.PostDate,
|
2021-05-25 13:12:20 +00:00
|
|
|
LastUser: templates.UserToTemplate(row.LastUser, c.Theme),
|
2021-05-03 14:51:07 +00:00
|
|
|
LastDate: row.LastPost.PostDate,
|
2021-05-03 22:53:28 +00:00
|
|
|
|
|
|
|
Unread: !hasRead,
|
2021-05-03 23:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var threads []templates.ThreadListItem
|
|
|
|
for _, irow := range itMainThreads.ToSlice() {
|
|
|
|
row := irow.(*threadQueryResult)
|
|
|
|
threads = append(threads, makeThreadListItem(row))
|
2021-04-29 04:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// Subcategory things
|
|
|
|
// ---------------------
|
|
|
|
|
2021-05-03 23:59:43 +00:00
|
|
|
var subcats []forumSubcategoryData
|
|
|
|
if page == 1 {
|
2021-05-11 22:53:23 +00:00
|
|
|
subcatNodes := categoryTree[currentCatId].Children
|
2021-05-03 23:59:43 +00:00
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
for _, catNode := range subcatNodes {
|
2021-05-03 23:59:43 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch count of subcategory threads")
|
2021-05-04 12:02:33 +00:00
|
|
|
// TODO(asaf): [PERF] [MINOR] Consider replacing querying count per subcat with a single query for all cats with GROUP BY.
|
2021-05-03 23:59:43 +00:00
|
|
|
numThreads, err := db.QueryInt(c.Context(), c.Conn,
|
|
|
|
`
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM handmade_thread AS thread
|
|
|
|
WHERE
|
|
|
|
thread.category_id = $1
|
|
|
|
AND NOT thread.deleted
|
|
|
|
`,
|
2021-05-11 22:53:23 +00:00
|
|
|
catNode.ID,
|
2021-05-03 23:59:43 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to get count of threads"))
|
|
|
|
}
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
c.Perf.StartBlock("SQL", "Fetch subcategory threads")
|
2021-05-04 12:02:33 +00:00
|
|
|
// TODO(asaf): [PERF] [MINOR] Consider batching these.
|
2021-05-03 23:59:43 +00:00
|
|
|
itThreads, err := db.Query(c.Context(), c.Conn, threadQueryResult{},
|
|
|
|
`
|
|
|
|
SELECT $columns
|
|
|
|
FROM
|
|
|
|
handmade_thread AS thread
|
|
|
|
JOIN handmade_post AS firstpost ON thread.first_id = firstpost.id
|
|
|
|
JOIN handmade_post AS lastpost ON thread.last_id = lastpost.id
|
|
|
|
LEFT JOIN auth_user AS firstuser ON firstpost.author_id = firstuser.id
|
|
|
|
LEFT JOIN auth_user AS lastuser ON lastpost.author_id = lastuser.id
|
|
|
|
LEFT JOIN handmade_threadlastreadinfo AS tlri ON (
|
|
|
|
tlri.thread_id = thread.id
|
|
|
|
AND tlri.user_id = $2
|
|
|
|
)
|
|
|
|
LEFT JOIN handmade_categorylastreadinfo AS clri ON (
|
|
|
|
clri.category_id = $1
|
|
|
|
AND clri.user_id = $2
|
|
|
|
)
|
|
|
|
WHERE
|
|
|
|
thread.category_id = $1
|
|
|
|
AND NOT thread.deleted
|
|
|
|
ORDER BY lastpost.postdate DESC
|
|
|
|
LIMIT 3
|
|
|
|
`,
|
2021-05-11 22:53:23 +00:00
|
|
|
catNode.ID,
|
2021-05-03 23:59:43 +00:00
|
|
|
currentUserId,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer itThreads.Close()
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
var threads []templates.ThreadListItem
|
|
|
|
for _, irow := range itThreads.ToSlice() {
|
|
|
|
threadRow := irow.(*threadQueryResult)
|
|
|
|
threads = append(threads, makeThreadListItem(threadRow))
|
|
|
|
}
|
|
|
|
|
|
|
|
subcats = append(subcats, forumSubcategoryData{
|
2021-05-11 22:53:23 +00:00
|
|
|
Name: *catNode.Name,
|
|
|
|
Url: hmnurl.BuildForumCategory(c.CurrentProject.Slug, lineageBuilder.GetSubforumLineageSlugs(catNode.ID), 1),
|
2021-05-03 23:59:43 +00:00
|
|
|
Threads: threads,
|
|
|
|
TotalThreads: numThreads,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// Template assembly
|
|
|
|
// ---------------------
|
2021-05-03 14:51:07 +00:00
|
|
|
|
|
|
|
baseData := getBaseData(c)
|
2021-05-06 04:04:58 +00:00
|
|
|
baseData.Title = c.CurrentProject.Name + " Forums"
|
2021-05-06 05:57:14 +00:00
|
|
|
baseData.Breadcrumbs = []templates.Breadcrumb{ // TODO(ben): This is wrong; it needs to account for subcategories.
|
2021-05-03 22:45:17 +00:00
|
|
|
{
|
2021-05-06 04:04:58 +00:00
|
|
|
Name: c.CurrentProject.Name,
|
2021-05-11 22:53:23 +00:00
|
|
|
Url: hmnurl.BuildProjectHomepage(c.CurrentProject.Slug),
|
2021-05-03 22:45:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Forums",
|
2021-05-11 22:53:23 +00:00
|
|
|
Url: hmnurl.BuildForumCategory(c.CurrentProject.Slug, nil, 1),
|
2021-05-03 22:45:17 +00:00
|
|
|
Current: true,
|
|
|
|
},
|
|
|
|
}
|
2021-05-03 14:51:07 +00:00
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
currentSubforums := lineageBuilder.GetSubforumLineage(currentCatId)
|
|
|
|
for i, subforum := range currentSubforums {
|
|
|
|
baseData.Breadcrumbs = append(baseData.Breadcrumbs, templates.Breadcrumb{
|
|
|
|
Name: *subforum.Name, // NOTE(asaf): All subforum categories must have names.
|
|
|
|
Url: hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs[0:i+1], 1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:51:07 +00:00
|
|
|
var res ResponseData
|
2021-05-03 22:45:17 +00:00
|
|
|
err = res.WriteTemplate("forum_category.html", forumCategoryData{
|
2021-05-11 22:53:23 +00:00
|
|
|
BaseData: baseData,
|
2021-06-12 03:51:07 +00:00
|
|
|
NewThreadUrl: hmnurl.BuildForumNewThread(c.CurrentProject.Slug, currentSubforumSlugs, false),
|
2021-05-11 22:53:23 +00:00
|
|
|
MarkReadUrl: hmnurl.BuildMarkRead(currentCatId),
|
|
|
|
Threads: threads,
|
2021-05-03 22:45:17 +00:00
|
|
|
Pagination: templates.Pagination{
|
|
|
|
Current: page,
|
|
|
|
Total: numPages,
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
FirstUrl: hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, 1),
|
|
|
|
LastUrl: hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, numPages),
|
|
|
|
NextUrl: hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, utils.IntClamp(1, page+1, numPages)),
|
|
|
|
PreviousUrl: hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, utils.IntClamp(1, page-1, numPages)),
|
2021-05-03 22:45:17 +00:00
|
|
|
},
|
2021-05-03 23:59:43 +00:00
|
|
|
Subcategories: subcats,
|
2021-05-03 14:51:07 +00:00
|
|
|
}, c.Perf)
|
2021-05-03 22:45:17 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-03 14:51:07 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-05-04 01:59:45 +00:00
|
|
|
type forumThreadData struct {
|
|
|
|
templates.BaseData
|
2021-05-06 05:57:14 +00:00
|
|
|
|
2021-05-04 01:59:45 +00:00
|
|
|
Thread templates.Thread
|
|
|
|
Posts []templates.Post
|
2021-05-06 05:57:14 +00:00
|
|
|
|
|
|
|
CategoryUrl string
|
|
|
|
ReplyUrl string
|
|
|
|
Pagination templates.Pagination
|
2021-05-04 01:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 13:52:43 +00:00
|
|
|
var threadViewPostsPerPage = 15
|
2021-05-04 01:59:45 +00:00
|
|
|
|
2021-06-25 13:52:43 +00:00
|
|
|
func ForumThread(c *RequestContext) ResponseData {
|
2021-05-11 22:53:23 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch category tree")
|
|
|
|
categoryTree := models.GetFullCategoryTree(c.Context(), c.Conn)
|
|
|
|
lineageBuilder := models.MakeCategoryLineageBuilder(categoryTree)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
currentCatId, valid := validateSubforums(lineageBuilder, c.CurrentProject, c.PathParams["cats"])
|
|
|
|
if !valid {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
2021-05-04 01:59:45 +00:00
|
|
|
threadId, err := strconv.Atoi(c.PathParams["threadid"])
|
|
|
|
if err != nil {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
currentSubforumSlugs := lineageBuilder.GetSubforumLineageSlugs(currentCatId)
|
|
|
|
|
2021-05-04 01:59:45 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch current thread")
|
|
|
|
type threadQueryResult struct {
|
|
|
|
Thread models.Thread `db:"thread"`
|
|
|
|
}
|
|
|
|
irow, err := db.QueryOne(c.Context(), c.Conn, threadQueryResult{},
|
|
|
|
`
|
|
|
|
SELECT $columns
|
2021-05-06 05:57:14 +00:00
|
|
|
FROM
|
|
|
|
handmade_thread AS thread
|
|
|
|
JOIN handmade_category AS cat ON cat.id = thread.category_id
|
|
|
|
WHERE
|
|
|
|
thread.id = $1
|
|
|
|
AND NOT thread.deleted
|
2021-05-11 22:53:23 +00:00
|
|
|
AND cat.id = $2
|
2021-05-04 01:59:45 +00:00
|
|
|
`,
|
|
|
|
threadId,
|
2021-05-11 22:53:23 +00:00
|
|
|
currentCatId, // NOTE(asaf): This verifies that the requested thread is under the requested subforum.
|
2021-05-04 01:59:45 +00:00
|
|
|
)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, db.ErrNoMatchingRows) {
|
|
|
|
return FourOhFour(c)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread := irow.(*threadQueryResult).Thread
|
|
|
|
|
2021-05-06 05:57:14 +00:00
|
|
|
numPosts, err := db.QueryInt(c.Context(), c.Conn,
|
|
|
|
`
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM handmade_post
|
|
|
|
WHERE
|
|
|
|
thread_id = $1
|
|
|
|
AND NOT deleted
|
|
|
|
`,
|
|
|
|
thread.ID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to get count of posts for thread"))
|
|
|
|
}
|
2021-06-25 13:52:43 +00:00
|
|
|
page, numPages, ok := getPageInfo(c.PathParams["page"], numPosts, threadViewPostsPerPage)
|
2021-05-04 01:59:45 +00:00
|
|
|
if !ok {
|
2021-05-11 22:53:23 +00:00
|
|
|
urlNoPage := hmnurl.BuildForumThread(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, thread.Title, 1)
|
2021-05-04 01:59:45 +00:00
|
|
|
return c.Redirect(urlNoPage, http.StatusSeeOther)
|
|
|
|
}
|
2021-05-06 05:57:14 +00:00
|
|
|
pagination := templates.Pagination{
|
|
|
|
Current: page,
|
|
|
|
Total: numPages,
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
FirstUrl: hmnurl.BuildForumThread(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, thread.Title, 1),
|
|
|
|
LastUrl: hmnurl.BuildForumThread(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, thread.Title, numPages),
|
|
|
|
NextUrl: hmnurl.BuildForumThread(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, thread.Title, utils.IntClamp(1, page+1, numPages)),
|
|
|
|
PreviousUrl: hmnurl.BuildForumThread(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, thread.Title, utils.IntClamp(1, page-1, numPages)),
|
2021-05-06 05:57:14 +00:00
|
|
|
}
|
2021-05-04 01:59:45 +00:00
|
|
|
|
|
|
|
c.Perf.StartBlock("SQL", "Fetch posts")
|
|
|
|
type postsQueryResult struct {
|
2021-05-06 05:57:14 +00:00
|
|
|
Post models.Post `db:"post"`
|
|
|
|
Ver models.PostVersion `db:"ver"`
|
|
|
|
Author *models.User `db:"author"`
|
|
|
|
Editor *models.User `db:"editor"`
|
2021-05-04 01:59:45 +00:00
|
|
|
}
|
|
|
|
itPosts, err := db.Query(c.Context(), c.Conn, postsQueryResult{},
|
|
|
|
`
|
|
|
|
SELECT $columns
|
|
|
|
FROM
|
|
|
|
handmade_post AS post
|
|
|
|
JOIN handmade_postversion AS ver ON post.current_id = ver.id
|
|
|
|
LEFT JOIN auth_user AS author ON post.author_id = author.id
|
2021-05-06 05:57:14 +00:00
|
|
|
LEFT JOIN auth_user AS editor ON ver.editor_id = editor.id
|
2021-05-04 01:59:45 +00:00
|
|
|
WHERE
|
|
|
|
post.thread_id = $1
|
2021-05-06 05:57:14 +00:00
|
|
|
AND NOT post.deleted
|
2021-05-04 01:59:45 +00:00
|
|
|
ORDER BY postdate
|
|
|
|
LIMIT $2 OFFSET $3
|
|
|
|
`,
|
|
|
|
thread.ID,
|
2021-06-25 13:52:43 +00:00
|
|
|
threadViewPostsPerPage,
|
|
|
|
(page-1)*threadViewPostsPerPage,
|
2021-05-04 01:59:45 +00:00
|
|
|
)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer itPosts.Close()
|
|
|
|
|
|
|
|
var posts []templates.Post
|
|
|
|
for _, irow := range itPosts.ToSlice() {
|
|
|
|
row := irow.(*postsQueryResult)
|
2021-05-06 05:57:14 +00:00
|
|
|
|
2021-05-25 13:12:20 +00:00
|
|
|
post := templates.PostToTemplate(&row.Post, row.Author, c.Theme)
|
2021-07-04 21:24:48 +00:00
|
|
|
post.AddContentVersion(row.Ver, row.Editor)
|
2021-05-11 22:53:23 +00:00
|
|
|
post.AddUrls(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, post.ID)
|
2021-05-06 05:57:14 +00:00
|
|
|
|
|
|
|
posts = append(posts, post)
|
2021-05-04 01:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
baseData := getBaseData(c)
|
2021-05-06 05:57:14 +00:00
|
|
|
baseData.Title = thread.Title
|
2021-05-04 12:02:33 +00:00
|
|
|
// TODO(asaf): Set breadcrumbs
|
2021-05-04 01:59:45 +00:00
|
|
|
|
|
|
|
var res ResponseData
|
|
|
|
err = res.WriteTemplate("forum_thread.html", forumThreadData{
|
2021-05-06 05:57:14 +00:00
|
|
|
BaseData: baseData,
|
|
|
|
Thread: templates.ThreadToTemplate(&thread),
|
|
|
|
Posts: posts,
|
2021-05-11 22:53:23 +00:00
|
|
|
CategoryUrl: hmnurl.BuildForumCategory(c.CurrentProject.Slug, currentSubforumSlugs, 1),
|
|
|
|
ReplyUrl: hmnurl.BuildForumPostReply(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, *thread.FirstID),
|
2021-05-06 05:57:14 +00:00
|
|
|
Pagination: pagination,
|
2021-05-04 01:59:45 +00:00
|
|
|
}, c.Perf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:52:43 +00:00
|
|
|
func ForumPostRedirect(c *RequestContext) ResponseData {
|
|
|
|
c.Perf.StartBlock("SQL", "Fetch category tree")
|
|
|
|
categoryTree := models.GetFullCategoryTree(c.Context(), c.Conn)
|
|
|
|
lineageBuilder := models.MakeCategoryLineageBuilder(categoryTree)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
currentCatId, valid := validateSubforums(lineageBuilder, c.CurrentProject, c.PathParams["cats"])
|
|
|
|
if !valid {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedThreadId, err := strconv.Atoi(c.PathParams["threadid"])
|
|
|
|
if err != nil {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedPostId, err := strconv.Atoi(c.PathParams["postid"])
|
|
|
|
if err != nil {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Perf.StartBlock("SQL", "Fetch post ids for thread")
|
|
|
|
type postQuery struct {
|
|
|
|
PostID int `db:"post.id"`
|
|
|
|
}
|
|
|
|
postQueryResult, err := db.Query(c.Context(), c.Conn, postQuery{},
|
|
|
|
`
|
|
|
|
SELECT $columns
|
|
|
|
FROM
|
|
|
|
handmade_post AS post
|
|
|
|
WHERE
|
|
|
|
post.category_id = $1
|
|
|
|
AND post.thread_id = $2
|
|
|
|
AND NOT post.deleted
|
|
|
|
ORDER BY postdate
|
|
|
|
`,
|
|
|
|
currentCatId,
|
|
|
|
requestedThreadId,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch post ids"))
|
|
|
|
}
|
|
|
|
postQuerySlice := postQueryResult.ToSlice()
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
postIdx := -1
|
|
|
|
for i, id := range postQuerySlice {
|
|
|
|
if id.(*postQuery).PostID == requestedPostId {
|
|
|
|
postIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if postIdx == -1 {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Perf.StartBlock("SQL", "Fetch thread title")
|
|
|
|
type threadTitleQuery struct {
|
|
|
|
ThreadTitle string `db:"thread.title"`
|
|
|
|
}
|
|
|
|
threadTitleQueryResult, err := db.QueryOne(c.Context(), c.Conn, threadTitleQuery{},
|
|
|
|
`
|
|
|
|
SELECT $columns
|
|
|
|
FROM handmade_thread AS thread
|
|
|
|
WHERE thread.id = $1
|
|
|
|
`,
|
|
|
|
requestedThreadId,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch thread title"))
|
|
|
|
}
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
threadTitle := threadTitleQueryResult.(*threadTitleQuery).ThreadTitle
|
|
|
|
|
|
|
|
page := (postIdx / threadViewPostsPerPage) + 1
|
|
|
|
|
|
|
|
return c.Redirect(hmnurl.BuildForumThreadWithPostHash(
|
|
|
|
c.CurrentProject.Slug,
|
|
|
|
lineageBuilder.GetSubforumLineageSlugs(currentCatId),
|
|
|
|
requestedThreadId,
|
|
|
|
threadTitle,
|
|
|
|
page,
|
|
|
|
requestedPostId,
|
|
|
|
), http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2021-06-12 03:51:07 +00:00
|
|
|
type editorData struct {
|
|
|
|
templates.BaseData
|
2021-07-04 22:48:08 +00:00
|
|
|
SubmitUrl string
|
|
|
|
PostTitle string
|
|
|
|
PostBody string
|
|
|
|
SubmitLabel string
|
|
|
|
IsEditing bool // false if new post, true if updating existing one
|
2021-06-12 03:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ForumNewThread(c *RequestContext) ResponseData {
|
|
|
|
baseData := getBaseData(c)
|
|
|
|
baseData.Title = "Create New Thread"
|
2021-06-20 19:06:22 +00:00
|
|
|
baseData.MathjaxEnabled = true
|
2021-06-12 03:51:07 +00:00
|
|
|
// TODO(ben): Set breadcrumbs
|
|
|
|
|
2021-07-02 05:11:58 +00:00
|
|
|
c.Perf.StartBlock("SQL", "Fetch category tree")
|
|
|
|
categoryTree := models.GetFullCategoryTree(c.Context(), c.Conn)
|
|
|
|
lineageBuilder := models.MakeCategoryLineageBuilder(categoryTree)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
currentCatId, valid := validateSubforums(lineageBuilder, c.CurrentProject, c.PathParams["cats"])
|
|
|
|
if !valid {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
2021-06-12 03:51:07 +00:00
|
|
|
var res ResponseData
|
|
|
|
err := res.WriteTemplate("editor.html", editorData{
|
2021-07-04 22:48:08 +00:00
|
|
|
BaseData: baseData,
|
|
|
|
SubmitUrl: hmnurl.BuildForumNewThread(c.CurrentProject.Slug, lineageBuilder.GetSubforumLineageSlugs(currentCatId), true),
|
|
|
|
SubmitLabel: "Post New Thread",
|
2021-06-12 03:51:07 +00:00
|
|
|
}, c.Perf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func ForumNewThreadSubmit(c *RequestContext) ResponseData {
|
2021-07-04 20:25:28 +00:00
|
|
|
tx, err := c.Conn.Begin(c.Context())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-04 22:48:08 +00:00
|
|
|
defer tx.Rollback(c.Context())
|
2021-07-04 20:25:28 +00:00
|
|
|
|
|
|
|
c.Perf.StartBlock("SQL", "Fetch category tree")
|
|
|
|
categoryTree := models.GetFullCategoryTree(c.Context(), c.Conn)
|
|
|
|
lineageBuilder := models.MakeCategoryLineageBuilder(categoryTree)
|
|
|
|
c.Perf.EndBlock()
|
|
|
|
|
|
|
|
currentCatId, valid := validateSubforums(lineageBuilder, c.CurrentProject, c.PathParams["cats"])
|
|
|
|
if !valid {
|
|
|
|
return FourOhFour(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Req.ParseForm()
|
|
|
|
|
|
|
|
title := c.Req.Form.Get("title")
|
|
|
|
unparsed := c.Req.Form.Get("body")
|
|
|
|
sticky := false
|
|
|
|
if c.CurrentUser.IsStaff && c.Req.Form.Get("sticky") != "" {
|
|
|
|
sticky = true
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := parsing.ParsePostInput(unparsed, false)
|
2021-07-04 22:48:08 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
ip := net.ParseIP(c.Req.RemoteAddr)
|
2021-07-04 20:25:28 +00:00
|
|
|
|
|
|
|
// Create thread
|
|
|
|
var threadId int
|
|
|
|
err = tx.QueryRow(c.Context(),
|
|
|
|
`
|
2021-07-04 22:48:08 +00:00
|
|
|
INSERT INTO handmade_thread (title, sticky, category_id, first_id, last_id)
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
2021-07-04 20:25:28 +00:00
|
|
|
RETURNING id
|
|
|
|
`,
|
|
|
|
title,
|
|
|
|
sticky,
|
|
|
|
currentCatId,
|
2021-07-04 22:48:08 +00:00
|
|
|
-1,
|
|
|
|
-1,
|
2021-07-04 20:25:28 +00:00
|
|
|
).Scan(&threadId)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to create thread"))
|
|
|
|
}
|
|
|
|
|
2021-07-04 22:48:08 +00:00
|
|
|
// Create post
|
|
|
|
var postId int
|
|
|
|
err = tx.QueryRow(c.Context(),
|
|
|
|
`
|
|
|
|
INSERT INTO handmade_post (postdate, category_id, thread_id, preview, current_id, author_id, category_kind, project_id)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
|
|
RETURNING id
|
|
|
|
`,
|
|
|
|
now,
|
|
|
|
currentCatId,
|
|
|
|
threadId,
|
|
|
|
"lol", // TODO: Actual previews
|
|
|
|
-1,
|
|
|
|
c.CurrentUser.ID,
|
|
|
|
models.CatKindForum,
|
|
|
|
c.CurrentProject.ID,
|
|
|
|
).Scan(&postId)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to create post"))
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:25:28 +00:00
|
|
|
// Create post version
|
2021-07-04 22:48:08 +00:00
|
|
|
var versionId int
|
|
|
|
err = tx.QueryRow(c.Context(),
|
2021-07-04 20:25:28 +00:00
|
|
|
`
|
2021-07-04 22:48:08 +00:00
|
|
|
INSERT INTO handmade_postversion (post_id, text_raw, text_parsed, ip, date)
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
|
|
RETURNING id
|
2021-07-04 20:25:28 +00:00
|
|
|
`,
|
2021-07-04 22:48:08 +00:00
|
|
|
postId,
|
2021-07-04 20:25:28 +00:00
|
|
|
unparsed,
|
|
|
|
parsed,
|
2021-07-04 22:48:08 +00:00
|
|
|
ip,
|
|
|
|
now,
|
|
|
|
).Scan(&versionId)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to create post version"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update post with version id
|
|
|
|
_, err = tx.Exec(c.Context(),
|
|
|
|
`
|
|
|
|
UPDATE handmade_post
|
|
|
|
SET current_id = $1
|
|
|
|
WHERE id = $2
|
|
|
|
`,
|
|
|
|
versionId,
|
|
|
|
postId,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to set current post version"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update thread with post id
|
|
|
|
_, err = tx.Exec(c.Context(),
|
|
|
|
`
|
|
|
|
UPDATE handmade_thread
|
|
|
|
SET
|
|
|
|
first_id = $1,
|
|
|
|
last_id = $1
|
|
|
|
WHERE id = $2
|
|
|
|
`,
|
|
|
|
postId,
|
|
|
|
threadId,
|
2021-07-04 20:25:28 +00:00
|
|
|
)
|
2021-07-04 22:48:08 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(oops.New(err, "failed to set thread post ids"))
|
|
|
|
}
|
2021-07-04 20:25:28 +00:00
|
|
|
|
|
|
|
err = tx.Commit(c.Context())
|
|
|
|
if err != nil {
|
|
|
|
return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to create new forum thread"))
|
|
|
|
}
|
|
|
|
|
2021-07-04 22:48:08 +00:00
|
|
|
newThreadUrl := hmnurl.BuildForumThread(c.CurrentProject.Slug, lineageBuilder.GetSubforumLineageSlugs(currentCatId), threadId, title, 1)
|
|
|
|
return c.Redirect(newThreadUrl, http.StatusSeeOther)
|
2021-06-12 03:51:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
func validateSubforums(lineageBuilder *models.CategoryLineageBuilder, project *models.Project, catPath string) (int, bool) {
|
|
|
|
if project.ForumID == nil {
|
|
|
|
return -1, false
|
|
|
|
}
|
2021-05-03 14:51:07 +00:00
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
subforumCatId := *project.ForumID
|
|
|
|
if len(catPath) == 0 {
|
|
|
|
return subforumCatId, true
|
|
|
|
}
|
2021-04-29 04:52:27 +00:00
|
|
|
|
2021-05-11 22:53:23 +00:00
|
|
|
catPath = strings.ToLower(catPath)
|
|
|
|
valid := false
|
|
|
|
catSlugs := strings.Split(catPath, "/")
|
|
|
|
lastSlug := catSlugs[len(catSlugs)-1]
|
|
|
|
if len(lastSlug) > 0 {
|
|
|
|
lastSlugCatId := lineageBuilder.FindIdBySlug(project.ID, lastSlug)
|
|
|
|
if lastSlugCatId != -1 {
|
|
|
|
subforumSlugs := lineageBuilder.GetSubforumLineageSlugs(lastSlugCatId)
|
|
|
|
allMatch := true
|
|
|
|
for i, subforum := range subforumSlugs {
|
|
|
|
if subforum != catSlugs[i] {
|
|
|
|
allMatch = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valid = allMatch
|
|
|
|
}
|
|
|
|
if valid {
|
|
|
|
subforumCatId = lastSlugCatId
|
|
|
|
}
|
2021-05-03 14:51:07 +00:00
|
|
|
}
|
2021-05-11 22:53:23 +00:00
|
|
|
return subforumCatId, valid
|
2021-04-29 04:52:27 +00:00
|
|
|
}
|