2017-03-12 19:37:53 +00:00
|
|
|
package board
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/pressly/chi"
|
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
"go.rls.moe/nyx/http/errw"
|
|
|
|
"go.rls.moe/nyx/http/middle"
|
|
|
|
"go.rls.moe/nyx/resources"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serveThread(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dat := bytes.NewBuffer([]byte{})
|
|
|
|
db := middle.GetDB(r)
|
|
|
|
ctx := middle.GetBaseCtx(r)
|
|
|
|
err := db.View(func(tx *buntdb.Tx) error {
|
|
|
|
bName := chi.URLParam(r, "board")
|
|
|
|
b, err := resources.GetBoard(tx, r.Host, bName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx["Board"] = b
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(chi.URLParam(r, "thread"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
thread, err := resources.GetThread(tx, r.Host, bName, id)
|
2017-03-12 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resources.FillReplies(tx, r.Host, thread)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx["Thread"] = thread
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
err = tmpls.ExecuteTemplate(dat, "board/thread", ctx)
|
2017-03-12 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "board.html", time.Now(), bytes.NewReader(dat.Bytes()))
|
|
|
|
}
|