Quick reply now takes you to your post rather than the first page of the topic.

Added the LastPage function.
This commit is contained in:
Azareal 2018-12-28 17:12:14 +10:00
parent 4813403fbb
commit f4337536dc
2 changed files with 59 additions and 6 deletions

View File

@ -868,7 +868,7 @@ func Paginate(count int, perPage int, maxPages int) []int {
// TODO: Write tests for this // TODO: Write tests for this
func PageOffset(count int, page int, perPage int) (int, int, int) { func PageOffset(count int, page int, perPage int) (int, int, int) {
var offset int var offset int
lastPage := (count / perPage) + 1 lastPage := LastPage(count, perPage)
if page > 1 { if page > 1 {
offset = (perPage * page) - perPage offset = (perPage * page) - perPage
} else if page == -1 { } else if page == -1 {
@ -884,3 +884,8 @@ func PageOffset(count int, page int, perPage int) (int, int, int) {
} }
return offset, page, lastPage return offset, page, lastPage
} }
// TODO: Write tests for this
func LastPage(count int, perPage int) int {
return (count / perPage) + 1
}

View File

@ -14,6 +14,7 @@ import (
type ReplyStmts struct { type ReplyStmts struct {
updateAttachs *sql.Stmt updateAttachs *sql.Stmt
createReplyPaging *sql.Stmt
} }
var replyStmts ReplyStmts var replyStmts ReplyStmts
@ -24,6 +25,7 @@ func init() {
replyStmts = ReplyStmts{ replyStmts = ReplyStmts{
// TODO: Less race-y attachment count updates // TODO: Less race-y attachment count updates
updateAttachs: acc.Update("replies").Set("attachCount = ?").Where("rid = ?").Prepare(), updateAttachs: acc.Update("replies").Set("attachCount = ?").Where("rid = ?").Prepare(),
createReplyPaging: acc.Select("replies").Cols("rid").Where("rid >= ? - 1 AND tid = ?").Orderby("rid ASC").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
@ -143,15 +145,61 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User)
return common.InternalErrorJSQ(err, w, r, js) return common.InternalErrorJSQ(err, w, r, js)
} }
if js { nTopic, err := common.Topics.Get(tid)
if err == sql.ErrNoRows {
return common.PreErrorJSQ("Couldn't find the parent topic", w, r, js)
} else if err != nil {
return common.InternalErrorJSQ(err, w, r, js)
}
page := common.LastPage(nTopic.PostCount, common.Config.ItemsPerPage)
rows, err := replyStmts.createReplyPaging.Query(reply.ID, topic.ID)
if err != nil && err != sql.ErrNoRows {
return common.InternalErrorJSQ(err, w, r, js)
}
defer rows.Close()
var rids []int
for rows.Next() {
var rid int
err := rows.Scan(&rid)
if err != nil {
return common.InternalErrorJSQ(err, w, r, js)
}
rids = append(rids, rid)
}
err = rows.Err()
if err != nil {
return common.InternalErrorJSQ(err, w, r, js)
}
if len(rids) == 0 {
return common.NotFoundJSQ(w, r, nil, js)
}
if page > 1 {
var offset int
if rids[0] == reply.ID {
offset = 1
} else if len(rids) == 2 && rids[1] == reply.ID {
offset = 2
}
page = common.LastPage(nTopic.PostCount-(len(rids)+offset), common.Config.ItemsPerPage)
}
prid, _ := strconv.Atoi(r.FormValue("prid"))
if js && (prid == 0 || rids[0] == prid) {
outBytes, err := json.Marshal(JsonReply{common.ParseMessage(reply.Content, topic.ParentID, "forums")}) outBytes, err := json.Marshal(JsonReply{common.ParseMessage(reply.Content, topic.ParentID, "forums")})
if err != nil { if err != nil {
return common.InternalErrorJSQ(err, w, r, js) return common.InternalErrorJSQ(err, w, r, js)
} }
w.Write(outBytes) w.Write(outBytes)
} else { } else {
// TODO: Send the user to the specific post on the page var spage string
http.Redirect(w, r, "/topic/"+strconv.Itoa(tid), http.StatusSeeOther) if page > 1 {
spage = "?page=" + strconv.Itoa(page)
}
http.Redirect(w, r, "/topic/"+strconv.Itoa(tid)+spage+"#post-"+strconv.Itoa(reply.ID), http.StatusSeeOther)
} }
counters.PostCounter.Bump() counters.PostCounter.Bump()