mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-03 09:26:23 -07:00
Make board creation work with new API
This commit is contained in:
parent
538050068f
commit
dec2609591
5 changed files with 83 additions and 94 deletions
|
@ -189,7 +189,7 @@ func NewBoardSimple(dir string, title string, subtitle string, description strin
|
|||
// It sets board.ID and board.CreatedAt if it is successfull
|
||||
func CreateBoard(board *Board, appendToAllBoards bool) error {
|
||||
const sqlINSERT = `INSERT INTO DBPREFIXboards
|
||||
(section_id, uri, dir, navbar_position, title, suttitle,
|
||||
(section_id, uri, dir, navbar_position, title, subtitle,
|
||||
description, max_file_size, max_threads, default_style, locked,
|
||||
anonymous_name, force_anonymous, autosage_after, no_images_after, max_message_length,
|
||||
min_message_length, allow_embeds, redirect_to_thread, require_file, enable_catalog)
|
||||
|
@ -197,6 +197,18 @@ func CreateBoard(board *Board, appendToAllBoards bool) error {
|
|||
if board == nil {
|
||||
return ErrNilBoard
|
||||
}
|
||||
if DoesBoardExistByDir(board.Dir) {
|
||||
return ErrBoardExists
|
||||
}
|
||||
if board.Dir == "" {
|
||||
return errors.New("board dir string must not be empty")
|
||||
}
|
||||
if board.URI == "" {
|
||||
board.URI = board.Dir
|
||||
}
|
||||
if board.Title == "" {
|
||||
return errors.New("board title string must not be empty")
|
||||
}
|
||||
id, err := getNextFreeID("DBPREFIXboards")
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -741,45 +741,70 @@ var actions = []Action{
|
|||
Permissions: AdminPerms,
|
||||
JSONoutput: NoJSON,
|
||||
Callback: func(writer http.ResponseWriter, request *http.Request, staff *gcsql.Staff, wantsJSON bool, infoEv *zerolog.Event, errEv *zerolog.Event) (output interface{}, err error) {
|
||||
board := new(gcsql.Board)
|
||||
|
||||
board := &gcsql.Board{
|
||||
MaxFilesize: 1000 * 1000 * 15,
|
||||
AnonymousName: "Anonymous",
|
||||
EnableCatalog: true,
|
||||
MaxMessageLength: 1500,
|
||||
AutosageAfter: 200,
|
||||
NoImagesAfter: 0,
|
||||
}
|
||||
requestType, _, _ := boardsRequestType(request)
|
||||
switch requestType {
|
||||
case "create":
|
||||
// create button clicked, create the board with the request fields
|
||||
if err = getBoardDataFromForm(board, request); err != nil {
|
||||
serveError(writer, err.Error(), "boards", err.Error(), wantsJSON)
|
||||
errEv.Err(err).Caller().Send()
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = gcsql.CreateBoard(board, true)
|
||||
if err = gcsql.CreateBoard(board, true); err != nil {
|
||||
errEv.Err(err).Caller().Send()
|
||||
return "", err
|
||||
}
|
||||
infoEv.
|
||||
Str("createBoard", board.Dir).
|
||||
Int("boardID", board.ID).
|
||||
Msg("New board created")
|
||||
case "delete":
|
||||
// delete button clicked, delete the board
|
||||
boardID, err := getIntField("boardid", staff.Username, request, 0)
|
||||
if err != nil {
|
||||
serveError(writer, "boardid", "boards", err.Error(), wantsJSON)
|
||||
return "", err
|
||||
}
|
||||
if board, err = gcsql.GetBoardFromID(boardID); err != nil {
|
||||
errEv.Err(err).Int("deleteBoardID", boardID).Caller().Send()
|
||||
return "", err
|
||||
}
|
||||
err = board.Delete()
|
||||
if err != nil {
|
||||
if err = board.Delete(); err != nil {
|
||||
errEv.Err(err).Str("deleteBoard", board.Dir).Caller().Send()
|
||||
return "", err
|
||||
}
|
||||
gcutil.LogInfo().
|
||||
Str("staff", staff.Username).
|
||||
Str("action", "boards").
|
||||
infoEv.
|
||||
Str("deleteBoard", board.Dir).Send()
|
||||
err = os.RemoveAll(board.AbsolutePath())
|
||||
if err = os.RemoveAll(board.AbsolutePath()); err != nil {
|
||||
errEv.Err(err).Caller().Send()
|
||||
return "", err
|
||||
}
|
||||
if err = building.BuildBoards(false); err != nil {
|
||||
return "", err
|
||||
}
|
||||
case "edit":
|
||||
// edit button clicked, fill the input fields with board data to be edited
|
||||
err = getBoardDataFromForm(board, request)
|
||||
boardID, err := getIntField("boardid", staff.Username, request, 0)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if board, err = gcsql.GetBoardFromID(boardID); err != nil {
|
||||
errEv.Err(err).
|
||||
Int("boardID", boardID).
|
||||
Caller().Msg("Unable to get board info")
|
||||
return "", err
|
||||
}
|
||||
case "modify":
|
||||
// save changes button clicked, apply changes to the board based on the request fields
|
||||
err = getBoardDataFromForm(board, request)
|
||||
if err = getBoardDataFromForm(board, request); err != nil {
|
||||
return "", err
|
||||
}
|
||||
case "cancel":
|
||||
// cancel button was clicked
|
||||
fallthrough
|
||||
|
@ -788,9 +813,6 @@ var actions = []Action{
|
|||
default:
|
||||
// board.SetDefaults("", "", "")
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if requestType == "create" || requestType == "modify" && err != nil {
|
||||
if err = building.BuildBoardListJSON(); err != nil {
|
||||
return "", err
|
||||
|
@ -982,26 +1004,6 @@ var actions = []Action{
|
|||
}
|
||||
return buildStr, nil
|
||||
}},
|
||||
// {
|
||||
// ID: "rebuildboard",
|
||||
// Title: "Rebuild board",
|
||||
// Permissions: AdminPerms,
|
||||
// Callback: func(writer http.ResponseWriter, request *http.Request, wantsJSON bool) (output interface{}, err error) {
|
||||
// if err = gctemplates.InitTemplates(); err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
// for b, board := range request.Form {
|
||||
// if b == "board" {
|
||||
// return board[0], nil
|
||||
// }
|
||||
// }
|
||||
// return "", &ErrStaffAction{
|
||||
// ErrorField: "staffaction",
|
||||
// Action: "rebuildboard",
|
||||
// Message: fmt.Sprintf("/%s/ is not a board"),
|
||||
// }
|
||||
// }},
|
||||
{
|
||||
ID: "rebuildboards",
|
||||
Title: "Rebuild boards",
|
||||
|
|
|
@ -30,28 +30,6 @@ func getStringField(field string, staff string, request *http.Request, logCaller
|
|||
return request.FormValue(field), nil
|
||||
}
|
||||
|
||||
func getBooleanField(field string, staff string, request *http.Request, logCallerOffset ...int) (bool, error) {
|
||||
action := request.FormValue("action")
|
||||
callerOffset := 1
|
||||
if len(logCallerOffset) > 0 {
|
||||
callerOffset += logCallerOffset[0]
|
||||
}
|
||||
if len(request.Form[field]) == 0 {
|
||||
gcutil.LogError(nil).
|
||||
Str("IP", gcutil.GetRealIP(request)).
|
||||
Str("staff", staff).
|
||||
Str("action", action).
|
||||
Str("field", field).
|
||||
Caller(callerOffset).Msg("Missing required field")
|
||||
return false, &ErrStaffAction{
|
||||
ErrorField: field,
|
||||
Action: action,
|
||||
Message: fmt.Sprintf("Missing required field %q", field),
|
||||
}
|
||||
}
|
||||
return request.FormValue(field) == "on", nil
|
||||
}
|
||||
|
||||
// getIntField gets the requested value from the form and tries to convert it to int. If it fails, it logs the error
|
||||
// and wraps it in ErrStaffAction
|
||||
func getIntField(field string, staff string, request *http.Request, logCallerOffset ...int) (int, error) {
|
||||
|
@ -60,14 +38,15 @@ func getIntField(field string, staff string, request *http.Request, logCallerOff
|
|||
if len(logCallerOffset) > 0 {
|
||||
callerOffset += logCallerOffset[0]
|
||||
}
|
||||
errEv := gcutil.LogError(nil).
|
||||
Str("IP", gcutil.GetRealIP(request)).
|
||||
Str("staff", staff).
|
||||
Str("action", action).
|
||||
Str("field", field)
|
||||
defer errEv.Discard()
|
||||
|
||||
if len(request.Form[field]) == 0 {
|
||||
gcutil.LogError(nil).
|
||||
Str("IP", gcutil.GetRealIP(request)).
|
||||
Str("staff", staff).
|
||||
Str("action", action).
|
||||
Str("field", field).
|
||||
Caller(callerOffset).Msg("Missing required field")
|
||||
errEv.Caller(callerOffset).Msg("Missing required field")
|
||||
return 0, &ErrStaffAction{
|
||||
ErrorField: field,
|
||||
Action: action,
|
||||
|
@ -78,16 +57,11 @@ func getIntField(field string, staff string, request *http.Request, logCallerOff
|
|||
|
||||
intVal, err := strconv.Atoi(strVal)
|
||||
if err != nil {
|
||||
gcutil.LogError(err).
|
||||
Str("IP", gcutil.GetRealIP(request)).
|
||||
Str("staff", staff).
|
||||
Str("action", action).
|
||||
Str("field", field).
|
||||
Caller(callerOffset).Msg("Unable to convert field to int")
|
||||
errEv.Err(err).Caller(callerOffset).Msg("Unable to convert field to int")
|
||||
return 0, &ErrStaffAction{
|
||||
ErrorField: field,
|
||||
Action: action,
|
||||
Message: fmt.Sprintf("Unable to convert form field %q to int"),
|
||||
Message: fmt.Sprintf("Unable to convert form field %q to int", field),
|
||||
}
|
||||
}
|
||||
return intVal, nil
|
||||
|
|
|
@ -279,39 +279,36 @@ func getBoardDataFromForm(board *gcsql.Board, request *http.Request) error {
|
|||
if board.DefaultStyle, err = getStringField("defaultstyle", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.Locked, err = getBooleanField("locked", staff, request, 1); err != nil {
|
||||
board.Locked = request.FormValue("locked") == "on"
|
||||
if board.AnonymousName, err = getStringField("anonname", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.AnonymousName, err = getStringField("anonymousname", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.ForceAnonymous, err = getBooleanField("forcedanonymous", staff, request, 1); err != nil {
|
||||
return err
|
||||
if board.AnonymousName == "" {
|
||||
board.AnonymousName = "Anonymous"
|
||||
}
|
||||
board.ForceAnonymous = request.FormValue("forcedanonymous") == "on"
|
||||
if board.AutosageAfter, err = getIntField("autosageafter", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.NoImagesAfter, err = getIntField("noimagesafter", staff, request, 1); err != nil {
|
||||
if board.AutosageAfter < 1 {
|
||||
board.AutosageAfter = 200
|
||||
}
|
||||
if board.NoImagesAfter, err = getIntField("nouploadsafter", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.MaxMessageLength, err = getIntField("maxmessagelength", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.MaxMessageLength < 1 {
|
||||
board.MaxMessageLength = 1024
|
||||
}
|
||||
if board.MinMessageLength, err = getIntField("minmessagelength", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.AllowEmbeds, err = getBooleanField("allowembeds", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.RedirectToThread, err = getBooleanField("redirecttothread", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.RequireFile, err = getBooleanField("requirefile", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if board.EnableCatalog, err = getBooleanField("enablecatalog", staff, request, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
board.AllowEmbeds = request.FormValue("allowembeds") == "on"
|
||||
board.RedirectToThread = request.FormValue("redirecttothread") == "on"
|
||||
board.RequireFile = request.FormValue("requirefile") == "on"
|
||||
board.EnableCatalog = request.FormValue("enablecatalog") == "on"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
{{else}}
|
||||
<h2>Create new board</h2>
|
||||
{{end}}
|
||||
<form action="{{$.webroot}}manage?action=boards" method="GET">
|
||||
<form action="{{$.webroot}}manage?action=boards" method="POST">
|
||||
<input type="hidden" name="action" value="boards">
|
||||
<input type="hidden" name="board" value="{{$.board.ID}}"/>
|
||||
<table>
|
||||
|
@ -54,7 +54,7 @@
|
|||
</tr>
|
||||
<tr>
|
||||
<td>Navbar position</td>
|
||||
<td><input type="number" min="0" name="listorder" value="{{$.board.NavbarPosition}}"></td>
|
||||
<td><input type="number" min="0" name="navbarposition" value="{{$.board.NavbarPosition}}"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Max filesize</td>
|
||||
|
@ -98,6 +98,10 @@
|
|||
<td>Max message length (in characters)</td>
|
||||
<td><input type="number" min="0" name="maxmessagelength" value="{{$.board.MaxMessageLength}}"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Min message length (in characters)</td>
|
||||
<td><input type="number" min="0" name="minmessagelength" value="{{$.board.MinMessageLength}}"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Allow embeds</td>
|
||||
<td><input type="checkbox" name="embedsallowed" {{if $.board.AllowEmbeds}}checked="checked"{{end}}/>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue