Better comment notification links

face-detect
Noah Petherbridge 2023-06-15 19:40:40 -07:00
parent 70d252dd75
commit 3d34306c7e
6 changed files with 47 additions and 5 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -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.

View File

@ -120,7 +120,7 @@
{{$Root := .}}
<div class="block p-2">
{{range $i, $c := .Comments}}
<div class="box has-background-link-light">
<div class="box has-background-link-light" id="p{{.ID}}">
<div class="columns">
<div class="column is-2 has-text-centered">
<div>

View File

@ -198,7 +198,7 @@
</p>
{{else}}
{{range .Comments}}
<div class="box has-background-link-light">
<div class="box has-background-link-light" id="p{{.ID}}">
<div class="columns">
<div class="column is-2 has-text-centered">
<div>