commit 60d1642a004fcd6a9815f72bfc6379751c855c6f
parent 7c17609c3b51c1b38bef6e7a0f0b34deafc98b74
Author: cblgh <cblgh@cblgh.org>
Date: Mon, 31 Jan 2022 10:56:53 +0100
add sort by recent posts functionality
Diffstat:
3 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/database/database.go b/database/database.go
@@ -233,14 +233,21 @@ type Thread struct {
}
// get a list of threads
-func (d DB) ListThreads() []Thread {
+func (d DB) ListThreads(sortByPost bool) []Thread {
query := `
SELECT count(t.id), t.title, t.id, u.name FROM threads t
INNER JOIN users u on u.id = t.authorid
INNER JOIN posts p ON t.id = p.threadid
GROUP BY t.id
- ORDER BY t.publishtime DESC
+ %s
`
+ orderBy := `ORDER BY t.publishtime DESC`
+ // get a list of threads by ordering them based on most recent post
+ if sortByPost {
+ orderBy = `ORDER BY max(p.id) DESC`
+ }
+ query = fmt.Sprintf(query, orderBy)
+
stmt, err := d.db.Prepare(query)
util.Check(err, "list threads: prepare query")
defer stmt.Close()
diff --git a/html/head.html b/html/head.html
@@ -21,6 +21,7 @@
display: flex;
justify-content: space-between;
align-items: center;
+ margin-bottom: 2rem;
}
label {
display: block;
@@ -127,6 +128,16 @@
display: none;
}
+ header details, header details summary {
+ margin-bottom: unset;
+ }
+ header details ul {
+ position: absolute;
+ }
+ header details ul > li {
+ display: block;
+ }
+
@supports (display: flex) {
header > a {
background-image: none;
@@ -175,6 +186,18 @@
</a>
<nav>
<ul type="menu">
+
+ {{ if eq .Title "threads" }}
+ <li>
+ <details>
+ <summary>sort</summary>
+ <ul>
+ <li> <a href="/?sort=posts">recent posts</a></li>
+ <li> <a href="/">most recent thread</a></li>
+ </ul>
+ </details>
+ </li>
+ {{ end }}
{{ if .QuickNav }}
<li><a href="#bottom">bottom</a></li>
{{end}}
diff --git a/server/server.go b/server/server.go
@@ -222,8 +222,15 @@ func (h RequestHandler) IndexRoute(res http.ResponseWriter, req *http.Request) {
return
}
loggedIn, _ := h.IsLoggedIn(req)
+ var mostRecentPost bool
+
+ params := req.URL.Query()
+ if q, exists := params["sort"]; exists {
+ sortby := q[0]
+ mostRecentPost = sortby == "posts"
+ }
// show index listing
- threads := h.db.ListThreads()
+ threads := h.db.ListThreads(mostRecentPost)
view := TemplateData{Data: IndexData{threads}, LoggedIn: loggedIn, Title: "threads"}
h.renderView(res, "index", view)
}