1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/web/mutators.go

256 lines
6.7 KiB
Go
Raw Normal View History

package web
import (
"fmt"
"log"
"net/http"
"github.com/bouncepaw/mycomarkup"
"github.com/bouncepaw/mycomarkup/mycocontext"
"github.com/gorilla/mux"
2021-02-17 18:41:35 +00:00
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
2021-02-20 14:03:54 +00:00
"github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/user"
2020-08-31 17:52:26 +00:00
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/views"
)
func initMutators(r *mux.Router) {
// Those that do not actually mutate anything:
r.PathPrefix("/edit/").HandlerFunc(handlerEdit)
r.PathPrefix("/delete-ask/").HandlerFunc(handlerDeleteAsk)
r.PathPrefix("/rename-ask/").HandlerFunc(handlerRenameAsk)
r.PathPrefix("/unattach-ask/").HandlerFunc(handlerUnattachAsk)
// And those that do mutate something:
r.PathPrefix("/upload-binary/").HandlerFunc(handlerUploadBinary)
r.PathPrefix("/upload-text/").HandlerFunc(handlerUploadText)
r.PathPrefix("/delete-confirm/").HandlerFunc(handlerDeleteConfirm)
r.PathPrefix("/rename-confirm/").HandlerFunc(handlerRenameConfirm)
r.PathPrefix("/unattach-confirm/").HandlerFunc(handlerUnattachConfirm)
2021-01-19 18:08:59 +00:00
}
2021-02-17 18:41:35 +00:00
func factoryHandlerAsker(
actionPath string,
2021-02-20 14:03:54 +00:00
asker func(*user.User, *hyphae.Hypha) (error, string),
2021-02-17 18:41:35 +00:00
succTitleTemplate string,
succPageTemplate func(*http.Request, string, bool) string,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
2021-02-17 18:41:35 +00:00
var (
hyphaName = util.HyphaNameFromRq(rq, actionPath)
2021-02-17 18:41:35 +00:00
h = hyphae.ByName(hyphaName)
u = user.FromRequest(rq)
)
2021-02-20 14:03:54 +00:00
if err, errtitle := asker(u, h); err != nil {
httpErr(
2021-02-17 18:41:35 +00:00
w,
http.StatusInternalServerError,
hyphaName,
errtitle,
err.Error())
return
}
util.HTTP200Page(
w,
views.BaseHTML(
2021-08-12 12:12:53 +00:00
fmt.Sprintf(succTitleTemplate, util.BeautifulName(hyphaName)),
2021-02-17 18:41:35 +00:00
succPageTemplate(rq, hyphaName, h.Exists),
u))
2021-01-19 18:08:59 +00:00
}
}
2021-02-17 18:41:35 +00:00
var handlerUnattachAsk = factoryHandlerAsker(
"unattach-ask",
2021-02-20 14:03:54 +00:00
shroom.CanUnattach,
2021-02-17 18:41:35 +00:00
"Unattach %s?",
views.UnattachAskHTML,
2021-02-17 18:41:35 +00:00
)
2020-10-02 15:31:59 +00:00
2021-02-17 18:41:35 +00:00
var handlerDeleteAsk = factoryHandlerAsker(
"delete-ask",
2021-02-20 14:03:54 +00:00
shroom.CanDelete,
2021-02-17 18:41:35 +00:00
"Delete %s?",
views.DeleteAskHTML,
2021-02-17 18:41:35 +00:00
)
2020-10-02 15:31:59 +00:00
2021-02-17 18:41:35 +00:00
var handlerRenameAsk = factoryHandlerAsker(
"rename-ask",
2021-02-20 14:03:54 +00:00
shroom.CanRename,
2021-02-17 18:41:35 +00:00
"Rename %s?",
views.RenameAskHTML,
2021-02-17 18:41:35 +00:00
)
func factoryHandlerConfirmer(
actionPath string,
confirmer func(*hyphae.Hypha, *user.User, *http.Request) (*history.HistoryOp, string),
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
2021-02-17 18:41:35 +00:00
var (
hyphaName = util.HyphaNameFromRq(rq, actionPath)
2021-02-17 18:41:35 +00:00
h = hyphae.ByName(hyphaName)
u = user.FromRequest(rq)
)
if hop, errtitle := confirmer(h, u, rq); hop.HasErrors() {
httpErr(w, http.StatusInternalServerError, hyphaName,
2021-02-17 18:41:35 +00:00
errtitle,
hop.FirstErrorText())
return
2020-10-02 15:31:59 +00:00
}
2021-02-17 18:41:35 +00:00
http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther)
2020-10-02 15:31:59 +00:00
}
2020-09-29 15:04:22 +00:00
}
2021-02-17 18:41:35 +00:00
var handlerUnattachConfirm = factoryHandlerConfirmer(
"unattach-confirm",
func(h *hyphae.Hypha, u *user.User, _ *http.Request) (*history.HistoryOp, string) {
2021-02-20 14:03:54 +00:00
return shroom.UnattachHypha(u, h)
2021-02-17 18:41:35 +00:00
},
)
2020-09-29 15:04:22 +00:00
2021-02-17 18:41:35 +00:00
var handlerDeleteConfirm = factoryHandlerConfirmer(
"delete-confirm",
func(h *hyphae.Hypha, u *user.User, _ *http.Request) (*history.HistoryOp, string) {
2021-02-20 14:03:54 +00:00
return shroom.DeleteHypha(u, h)
2021-02-17 18:41:35 +00:00
},
)
var handlerRenameConfirm = factoryHandlerConfirmer(
"rename-confirm",
func(oldHypha *hyphae.Hypha, u *user.User, rq *http.Request) (*history.HistoryOp, string) {
var (
newName = util.CanonicalName(rq.PostFormValue("new-name"))
recursive = rq.PostFormValue("recursive") == "true"
newHypha = hyphae.ByName(newName)
)
2021-02-20 14:03:54 +00:00
return shroom.RenameHypha(oldHypha, newHypha, recursive, u)
2021-02-17 18:41:35 +00:00
},
)
// handlerEdit shows the edit form. It doesn't edit anything actually.
func handlerEdit(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
hyphaName = util.HyphaNameFromRq(rq, "edit")
2021-02-17 18:41:35 +00:00
h = hyphae.ByName(hyphaName)
warning string
textAreaFill string
err error
u = user.FromRequest(rq)
)
2021-02-20 14:03:54 +00:00
if err, errtitle := shroom.CanEdit(u, h); err != nil {
httpErr(w, http.StatusInternalServerError, hyphaName,
2021-02-17 18:41:35 +00:00
errtitle,
err.Error())
return
}
2021-02-17 18:41:35 +00:00
if h.Exists {
2021-02-20 14:03:54 +00:00
textAreaFill, err = shroom.FetchTextPart(h)
if err != nil {
log.Println(err)
httpErr(w, http.StatusInternalServerError, hyphaName,
2021-02-17 18:41:35 +00:00
"Error",
"Could not fetch text data")
return
}
} else {
2021-02-17 18:41:35 +00:00
warning = `<p class="warning warning_new-hypha">You are creating a new hypha.</p>`
}
util.HTTP200Page(
w,
views.BaseHTML(
2021-08-12 12:12:53 +00:00
fmt.Sprintf("Edit %s", util.BeautifulName(hyphaName)),
views.EditHTML(rq, hyphaName, textAreaFill, warning),
2021-02-17 18:41:35 +00:00
u))
}
// handlerUploadText uploads a new text part for the hypha.
func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
hyphaName = util.HyphaNameFromRq(rq, "upload-text")
2021-02-17 18:41:35 +00:00
h = hyphae.ByName(hyphaName)
textData = rq.PostFormValue("text")
2021-01-16 16:42:18 +00:00
action = rq.PostFormValue("action")
2021-06-14 00:38:43 +00:00
message = rq.PostFormValue("message")
u = user.FromRequest(rq)
2021-02-17 18:41:35 +00:00
hop *history.HistoryOp
errtitle string
)
2021-02-17 18:41:35 +00:00
if action != "Preview" {
2021-06-14 00:38:43 +00:00
hop, errtitle = shroom.UploadText(h, []byte(textData), message, u)
2021-02-20 16:21:10 +00:00
if hop.HasErrors() {
httpErr(w, http.StatusForbidden, hyphaName,
2021-02-20 16:21:10 +00:00
errtitle,
hop.FirstErrorText())
return
}
}
2021-02-17 18:41:35 +00:00
2021-01-16 16:42:18 +00:00
if action == "Preview" {
2021-05-25 07:11:16 +00:00
ctx, _ := mycocontext.ContextFromStringInput(hyphaName, textData)
2021-02-17 18:41:35 +00:00
util.HTTP200Page(
w,
views.BaseHTML(
2021-08-12 12:12:53 +00:00
fmt.Sprintf("Preview of %s", util.BeautifulName(hyphaName)),
views.PreviewHTML(
2021-02-17 18:41:35 +00:00
rq,
hyphaName,
textData,
2021-06-14 00:38:43 +00:00
message,
2021-02-17 18:41:35 +00:00
"",
2021-05-25 07:11:16 +00:00
mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx))),
2021-02-17 18:41:35 +00:00
u))
} else {
2021-02-17 18:41:35 +00:00
http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther)
}
}
// handlerUploadBinary uploads a new attachment for the hypha.
func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
2021-02-17 18:41:35 +00:00
rq.ParseMultipartForm(10 << 20) // Set upload limit
var (
hyphaName = util.HyphaNameFromRq(rq, "upload-binary")
2021-02-17 18:41:35 +00:00
h = hyphae.ByName(hyphaName)
u = user.FromRequest(rq)
file, handler, err = rq.FormFile("binary")
)
2021-02-20 14:03:54 +00:00
if err != nil {
httpErr(w, http.StatusInternalServerError, hyphaName,
2021-02-20 14:03:54 +00:00
"Error",
err.Error())
}
if err, errtitle := shroom.CanAttach(u, h); err != nil {
httpErr(w, http.StatusInternalServerError, hyphaName,
2021-02-17 18:41:35 +00:00
errtitle,
err.Error())
}
// If file is not passed:
if err != nil {
return
}
// If file is passed:
2021-02-17 18:41:35 +00:00
if file != nil {
defer file.Close()
}
var (
2021-02-17 18:41:35 +00:00
mime = handler.Header.Get("Content-Type")
2021-02-20 14:03:54 +00:00
hop, errtitle = shroom.UploadBinary(h, mime, file, u)
)
2021-02-17 18:41:35 +00:00
if hop.HasErrors() {
httpErr(w, http.StatusInternalServerError, hyphaName, errtitle, hop.FirstErrorText())
return
2020-08-19 18:54:23 +00:00
}
2021-02-17 18:41:35 +00:00
http.Redirect(w, rq, "/hypha/"+hyphaName, http.StatusSeeOther)
}