The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0 KiB

package controllers
import (
"net/http"
"strings"
"time"
"git.aiterp.net/AiteRP/aitestory/model"
"git.aiterp.net/AiteRP/aitestory/viewmodel"
"git.aiterp.net/AiteRP/aitestory/view"
"git.aiterp.net/gisle/wrouter"
"git.aiterp.net/gisle/wrouter/auth"
)
// PageController serves page creation, viewing, editing and deleting through the website.
var PageController = wrouter.Router{}
func pageCreate(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
if (req.Method != "GET" && req.Method != "POST") || len(req.URL.Path) > len(path) {
return false
}
pc := viewmodel.PageForm{}
pc.Setup(user)
pc.Operation = "Create"
// Make sure the user is logged in
if user == nil {
// It's completely safe to eject logged-out users if they're not submitting
if req.Method == "GET" {
http.Redirect(w, req, "/user/login?form=/page/create", 302)
return true
}
// Logged in users would probably want a chance to log in on another form, though.
pc.Error = "You are not logged in."
}
// Respect the banhammer's authority
if user != nil && user.Level == "restricted" {
pc.Error = "Your user account is not permitted to do this"
}
// Handle submissions if nothing has complained yet
if req.Method == "POST" && pc.Error == "" {
// Validate form
req.ParseForm()
errs := pc.Page.ParseForm(req.Form)
if len(errs) > 0 {
pc.Error = "Validation failed: " + errs[0].Error()
}
// Parse the tags textbox
pc.TagInput = req.Form.Get("tags")
tagLines := strings.Split(pc.TagInput, "\n")
for _, line := range tagLines {
var tagType, tagName string
// Skip empty lines, and allow some accidental letters
if len(line) < 2 {
continue
}
// Parse tokens
tokens := strings.SplitN(line, ":", 2)
if len(tokens) == 2 {
tagType = strings.Trim(tokens[0], "  \t\r")
tagName = strings.Trim(tokens[1], "  \t\r")
} else {
tagType = "*" // Permit untyped tags if it exists.
tagName = strings.Trim(tokens[0], "  \t\r")
}
// Grab the tag
tag, err := model.EnsureTag(tagType, tagName)
if err != nil {
pc.Error = "Check your tags: " + err.Error()
break
}
// Take a copy of it
pc.Page.Tags = append(pc.Page.Tags, *tag)
}
// If everything worked out, fill in the last bits and send it off
if len(errs) == 0 && pc.Error == "" {
pc.Page.Author = user.FullID()
pc.Page.PublishDate = time.Now()
pc.Page.EditDate = pc.Page.PublishDate.Add(-time.Hour)
err := pc.Page.Insert()
if err != nil {
pc.Error = "Insert failed: " + err.Error()
} else {
// Let us see what you have wrought upon the world
http.Redirect(w, req, "/page/"+pc.Page.ID, 302)
return true
}
}
}
view.Render(w, "create", 200, pc)
return true
}
func pageView(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
if (req.Method != "GET" && req.Method != "POST") || strings.LastIndex(req.URL.Path, "/") > len(path) {
return false
}
return true
}
func init() {
PageController.Function("/create", pageCreate)
PageController.Function("/", pageView)
}