From 3d34306c7e3be431df79921f3fec7404001c09b0 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 15 Jun 2023 19:40:40 -0700 Subject: [PATCH] Better comment notification links --- pkg/controller/comment/post_comment.go | 3 ++- pkg/controller/forum/new_post.go | 19 ++++++++++++++++++- pkg/models/pagination.go | 2 +- pkg/models/thread.go | 24 ++++++++++++++++++++++++ web/templates/forum/thread.html | 2 +- web/templates/photo/permalink.html | 2 +- 6 files changed, 47 insertions(+), 5 deletions(-) diff --git a/pkg/controller/comment/post_comment.go b/pkg/controller/comment/post_comment.go index 36ead85..8088b92 100644 --- a/pkg/controller/comment/post_comment.go +++ b/pkg/controller/comment/post_comment.go @@ -1,6 +1,7 @@ package comment import ( + "fmt" "net/http" "net/url" "strconv" @@ -172,7 +173,7 @@ func PostComment() http.HandlerFunc { TableName: comment.TableName, TableID: comment.TableID, Message: message, - Link: fromURL, + Link: fmt.Sprintf("%s#p%d", fromURL, comment.ID), } if err := models.CreateNotification(notif); err != nil { log.Error("Couldn't create Comment notification: %s", err) diff --git a/pkg/controller/forum/new_post.go b/pkg/controller/forum/new_post.go index e4d6aa8..3329051 100644 --- a/pkg/controller/forum/new_post.go +++ b/pkg/controller/forum/new_post.go @@ -335,6 +335,16 @@ func NewPost() http.HandlerFunc { } } + // What page number of the thread will this comment appear on? + // Get the last page of the thread, and if not 1, append the + // query string - so the notification might go to e.g. + // "/forum/thread/:id?page=4#p50" to link directly to page 4 + // where comment 50 can be seen. + var queryString = "" + if lastPage := thread.Pages(); lastPage > 1 { + queryString = fmt.Sprintf("?page=%d", lastPage) + } + // Notify watchers about this new post. for _, userID := range models.GetSubscribers("threads", thread.ID) { if userID == currentUser.ID { @@ -348,7 +358,7 @@ func NewPost() http.HandlerFunc { TableName: "threads", TableID: thread.ID, Message: message, - Link: fmt.Sprintf("/forum/thread/%d", thread.ID), + Link: fmt.Sprintf("/forum/thread/%d%s#p%d", thread.ID, queryString, reply.ID), } if err := models.CreateNotification(notif); err != nil { log.Error("Couldn't create thread reply notification for subscriber %d: %s", userID, err) @@ -359,7 +369,14 @@ func NewPost() http.HandlerFunc { if _, err := models.SubscribeTo(currentUser, "threads", thread.ID); err != nil { log.Error("Couldn't subscribe user %d to forum thread %d: %s", currentUser.ID, thread.ID, err) } + + // Redirect the poster to the correct page number too. + templates.Redirect(w, fmt.Sprintf("/forum/thread/%d%s", thread.ID, queryString)) + return } + + // Called on the error case that the post couldn't be created - + // probably should not happen. templates.Redirect(w, fmt.Sprintf("/forum/thread/%d", thread.ID)) return } diff --git a/pkg/models/pagination.go b/pkg/models/pagination.go index 9b67adf..51aa30e 100644 --- a/pkg/models/pagination.go +++ b/pkg/models/pagination.go @@ -84,7 +84,7 @@ func (p *Pagination) Iter() []Page { return pages } -func (p *Pagination) Pages() int { +func (p Pagination) Pages() int { if p.PerPage == 0 { return 0 } diff --git a/pkg/models/thread.go b/pkg/models/thread.go index 2446b34..93d804f 100644 --- a/pkg/models/thread.go +++ b/pkg/models/thread.go @@ -87,6 +87,30 @@ func CreateThread(user *User, forumID uint64, title, message string, pinned, exp return thread, result.Error } +// Pages returns the number of pages in the thread - also useful to find out +// what is the final page number that has any posts. +func (t *Thread) Pages() int { + // How many posts total? + var postCount int64 + var query = DB.Table( + "comments", + ).Select( + "count(id) AS count", + ).Where( + "table_name = 'threads' AND table_id = ?", + t.ID, + ).Count(&postCount) + if query.Error != nil { + log.Error("SQL error getting post count for thread %d: %s", t.ID, query.Error) + } + + // Return what the Paginator would say is the inclusive page count. + return Pagination{ + PerPage: config.PageSizeThreadList, + Total: postCount, + }.Pages() +} + // Reply to a thread, adding an additional comment. func (t *Thread) Reply(user *User, message string) (*Comment, error) { // Save the thread on reply, updating its timestamp. diff --git a/web/templates/forum/thread.html b/web/templates/forum/thread.html index a3827c4..e847670 100644 --- a/web/templates/forum/thread.html +++ b/web/templates/forum/thread.html @@ -120,7 +120,7 @@ {{$Root := .}}
{{range $i, $c := .Comments}} -