2017-03-12 19:37:53 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
"go.rls.moe/nyx/http/errw"
|
|
|
|
"go.rls.moe/nyx/http/middle"
|
|
|
|
"go.rls.moe/nyx/resources"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleNewBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := middle.GetSession(r)
|
2017-03-15 08:13:15 +00:00
|
|
|
if !middle.IsAdminSession(sess) {
|
2017-03-12 19:37:53 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte("Unauthorized"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
2017-03-13 15:04:00 +00:00
|
|
|
return
|
2017-03-12 19:37:53 +00:00
|
|
|
}
|
|
|
|
db := middle.GetDB(r)
|
|
|
|
|
2017-03-15 13:14:35 +00:00
|
|
|
var board = &resources.Board{
|
|
|
|
Metadata: map[string]string{},
|
|
|
|
}
|
2017-03-12 19:37:53 +00:00
|
|
|
|
|
|
|
board.ShortName = r.FormValue("shortname")
|
|
|
|
board.LongName = r.FormValue("longname")
|
|
|
|
|
|
|
|
if board.ShortName == "" {
|
|
|
|
errw.ErrorWriter(errors.New("Need shortname"), w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-15 13:14:35 +00:00
|
|
|
if board.ShortName == "admin" || board.ShortName == "@" || board.ShortName == "mod" {
|
2017-03-12 19:37:53 +00:00
|
|
|
errw.ErrorWriter(errors.New("No"), w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if board.LongName == "" && len(board.LongName) < 5 {
|
|
|
|
errw.ErrorWriter(errors.New("Need 5 characters for long name"), w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
return resources.NewBoard(tx, r.Host, board)
|
|
|
|
}); err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/admin/panel.html", http.StatusSeeOther)
|
|
|
|
}
|