commit 7c17609c3b51c1b38bef6e7a0f0b34deafc98b74
parent 68576596f839f039c7b1b53817ce84e927659d90
Author: cblgh <cblgh@cblgh.org>
Date: Sun, 30 Jan 2022 23:11:03 +0100
fix visited state when making new post, unify slug creation
Diffstat:
4 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/database/database.go b/database/database.go
@@ -256,7 +256,7 @@ func (d DB) ListThreads() []Thread {
if err := rows.Scan(&postCount, &data.Title, &data.ID, &data.Author); err != nil {
log.Fatalln(util.Eout(err, "list threads: read in data via scan"))
}
- data.Slug = fmt.Sprintf("%d/%s-%d/", data.ID, util.SanitizeURL(data.Title), postCount)
+ data.Slug = util.GetThreadSlug(data.ID, data.Title, postCount)
threads = append(threads, data)
}
return threads
diff --git a/html/index.html b/html/index.html
@@ -1,7 +1,7 @@
{{ template "head" . }}
<main>
{{ range $index, $thread := .Data.Threads }}
- <h2><a href="/thread/{{$thread.Slug}}">{{ $thread.Title }}</a></h2>
+ <h2><a href="{{$thread.Slug}}">{{ $thread.Title }}</a></h2>
{{ end }}
</main>
{{ if .LoggedIn }}
diff --git a/server/server.go b/server/server.go
@@ -180,8 +180,14 @@ func (h RequestHandler) ThreadRoute(res http.ResponseWriter, req *http.Request)
// TODO (2022-01-09): make sure rendered content won't be empty after sanitizing:
// * run sanitize step && strings.TrimSpace and check length **before** doing AddPost
// TODO(2022-01-09): send errors back to thread's posting view
- postID := h.db.AddPost(content, threadid, userid)
- http.Redirect(res, req, fmt.Sprintf("%s#%d", req.URL.Path, postID), http.StatusFound)
+ _ = h.db.AddPost(content, threadid, userid)
+ // we want to effectively redirect to <#posts+1> to mark the thread as read in the thread index
+ // TODO(2022-01-30): find a solution for either:
+ // * scrolling to thread bottom (and maintaining the same slug, important for visited state in browser)
+ // * passing data to signal "your post was successfully added" (w/o impacting visited state / url)
+ posts := h.db.GetThread(threadid)
+ newSlug := util.GetThreadSlug(threadid, posts[0].ThreadTitle, len(posts))
+ http.Redirect(res, req, newSlug, http.StatusFound)
return
}
// TODO (2022-01-07):
diff --git a/util/util.go b/util/util.go
@@ -93,6 +93,10 @@ func SanitizeStringStrict(s string) string {
return strictContentGuardian.Sanitize(s)
}
+func GetThreadSlug(threadid int, title string, threadLen int) string {
+ return fmt.Sprintf("/thread/%d/%s-%d/", threadid, SanitizeURL(title), threadLen)
+}
+
// make a string be suitable for use as part of a url
func SanitizeURL(input string) string {
input = strings.ReplaceAll(input, " ", "-")