mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-01-21 07:46:52 +00:00
Play around with package shroom
This commit is contained in:
parent
41651f9e9b
commit
4cb963d47d
@ -45,32 +45,6 @@ func canFactory(
|
||||
|
||||
// CanDelete and etc are hyphae operation checkers based on user rights and hyphae existence.
|
||||
var (
|
||||
CanDelete = canFactory(
|
||||
rejectDeleteLog,
|
||||
"delete-confirm",
|
||||
nil,
|
||||
"ui.act_norights_delete",
|
||||
"ui.act_notexist_delete",
|
||||
true,
|
||||
)
|
||||
|
||||
CanUnattach = canFactory(
|
||||
rejectUnattachLog,
|
||||
"unattach-confirm",
|
||||
func(h hyphae.Hypha, u *user.User, lc *l18n.Localizer) (errmsg, errtitle string) {
|
||||
switch h := h.(type) {
|
||||
case *hyphae.EmptyHypha, *hyphae.TextualHypha:
|
||||
rejectUnattachLog(h, u, "no amnt")
|
||||
return lc.Get("ui.act_noattachment_tip"), lc.Get("ui.act_noattachment")
|
||||
}
|
||||
|
||||
return "", ""
|
||||
},
|
||||
"ui.act_norights_unattach",
|
||||
"ui.act_notexist_unattach",
|
||||
true,
|
||||
)
|
||||
|
||||
CanEdit = canFactory(
|
||||
rejectEditLog,
|
||||
"upload-text",
|
||||
|
@ -6,34 +6,27 @@ import (
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
||||
"github.com/bouncepaw/mycorrhiza/user"
|
||||
)
|
||||
|
||||
// DeleteHypha deletes hypha and makes a history record about that.
|
||||
func DeleteHypha(u *user.User, h hyphae.Hypha, lc *l18n.Localizer) error {
|
||||
if err := CanDelete(u, h, lc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete deletes the hypha and makes a history record about that.
|
||||
func Delete(u *user.User, h hyphae.ExistingHypha) error {
|
||||
hop := history.
|
||||
Operation(history.TypeDeleteHypha).
|
||||
WithMsg(fmt.Sprintf("Delete ‘%s’", h.CanonicalName())).
|
||||
WithUser(u)
|
||||
|
||||
originalText, _ := FetchTextFile(h)
|
||||
switch h := h.(type) {
|
||||
case *hyphae.MediaHypha:
|
||||
hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath())
|
||||
case *hyphae.TextualHypha:
|
||||
hop.WithFilesRemoved(h.TextFilePath())
|
||||
default:
|
||||
panic("impossible")
|
||||
}
|
||||
originalText, _ := FetchTextFile(h)
|
||||
hop.Apply()
|
||||
if !hop.HasErrors() {
|
||||
backlinks.UpdateBacklinksAfterDelete(h, originalText)
|
||||
hyphae.DeleteHypha(h.(hyphae.ExistingHypha)) // we panicked before, so it's safe
|
||||
if hop.Apply().HasErrors() {
|
||||
return hop.Errs[0]
|
||||
}
|
||||
backlinks.UpdateBacklinksAfterDelete(h, originalText)
|
||||
hyphae.DeleteHypha(h)
|
||||
return nil
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
)
|
||||
|
||||
// Rename renames the old hypha to the new name. Call if and only if the user has the permission to rename.
|
||||
// Rename renames the old hypha to the new name and makes a history record about that. Call if and only if the user has the permission to rename.
|
||||
func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *user.User) error {
|
||||
if newName == "" {
|
||||
rejectRenameLog(oldHypha, u, "no new name given")
|
||||
|
@ -1,3 +1,6 @@
|
||||
// Package shroom provides utilities for hypha manipulation.
|
||||
//
|
||||
// Some of them are wrappers around functions provided by package hyphae. They manage history for you.
|
||||
package shroom
|
||||
|
||||
import (
|
@ -5,12 +5,11 @@ import (
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
||||
"github.com/bouncepaw/mycorrhiza/user"
|
||||
)
|
||||
|
||||
// RemoveMedia unattaches hypha and makes a history record about that.
|
||||
func RemoveMedia(u *user.User, h *hyphae.MediaHypha, lc *l18n.Localizer) error {
|
||||
// RemoveMedia removes media from the media hypha and makes a history record about that. If it only had media, the hypha will be deleted. If it also had text, the hypha will become textual.
|
||||
func RemoveMedia(u *user.User, h *hyphae.MediaHypha) error {
|
||||
hop := history.
|
||||
Operation(history.TypeUnattachHypha).
|
||||
WithFilesRemoved(h.MediaFilePath()).
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
|
||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
||||
"github.com/bouncepaw/mycorrhiza/mimetype"
|
||||
"github.com/bouncepaw/mycorrhiza/user"
|
||||
"io"
|
||||
@ -47,7 +46,7 @@ func writeTextToDisk(h hyphae.ExistingHypha, data []byte, hop *history.Op) error
|
||||
}
|
||||
|
||||
// UploadText edits the hypha's text part and makes a history record about that.
|
||||
func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User, lc *l18n.Localizer) error {
|
||||
func UploadText(h hyphae.Hypha, data []byte, userMessage string, u *user.User) error {
|
||||
hop := history.
|
||||
Operation(history.TypeEditText).
|
||||
WithMsg(historyMessageForTextUpload(h, userMessage)).
|
||||
@ -164,7 +163,7 @@ func writeMediaToDisk(h hyphae.Hypha, mime string, data []byte) (string, error)
|
||||
}
|
||||
|
||||
// UploadBinary edits the hypha's media part and makes a history record about that.
|
||||
func UploadBinary(h hyphae.Hypha, mime string, file multipart.File, u *user.User, lc *l18n.Localizer) error {
|
||||
func UploadBinary(h hyphae.Hypha, mime string, file multipart.File, u *user.User) error {
|
||||
|
||||
// Privilege check
|
||||
if !u.CanProceed("upload-binary") {
|
||||
|
@ -55,7 +55,7 @@ func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) {
|
||||
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "no media to remove")
|
||||
return
|
||||
case *hyphae.MediaHypha:
|
||||
if err := shroom.RemoveMedia(u, h, lc); err != nil {
|
||||
if err := shroom.RemoveMedia(u, h); err != nil {
|
||||
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
|
||||
return
|
||||
}
|
||||
@ -70,20 +70,20 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
|
||||
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
|
||||
)
|
||||
|
||||
switch h.(type) {
|
||||
case *hyphae.EmptyHypha:
|
||||
log.Printf("%s tries to delete empty hypha ‘%s’", u.Name, h.CanonicalName())
|
||||
// TODO: localize
|
||||
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "Cannot delete an empty hypha")
|
||||
return
|
||||
}
|
||||
|
||||
if !u.CanProceed("delete") {
|
||||
log.Printf("%s has no rights to delete ‘%s’\n", u.Name, h.CanonicalName())
|
||||
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "No rights")
|
||||
return
|
||||
}
|
||||
|
||||
switch h.(type) {
|
||||
case *hyphae.EmptyHypha:
|
||||
log.Printf("%s tries to delete empty hypha ‘%s’\n", u.Name, h.CanonicalName())
|
||||
// TODO: localize
|
||||
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "Cannot delete an empty hypha")
|
||||
return
|
||||
}
|
||||
|
||||
if rq.Method == "GET" {
|
||||
util.HTTP200Page(
|
||||
w,
|
||||
@ -95,9 +95,10 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := shroom.DeleteHypha(u, h, lc); err != nil {
|
||||
if err := shroom.Delete(u, h.(hyphae.ExistingHypha)); err != nil {
|
||||
log.Println(err)
|
||||
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
|
||||
return
|
||||
}
|
||||
http.Redirect(w, rq, "/hypha/"+h.CanonicalName(), http.StatusSeeOther)
|
||||
}
|
||||
@ -200,7 +201,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
|
||||
)
|
||||
|
||||
if action != "Preview" {
|
||||
if err := shroom.UploadText(h, []byte(textData), message, u, lc); err != nil {
|
||||
if err := shroom.UploadText(h, []byte(textData), message, u); err != nil {
|
||||
httpErr(w, lc, http.StatusForbidden, hyphaName, err.Error())
|
||||
return
|
||||
}
|
||||
@ -261,7 +262,7 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
|
||||
mime = handler.Header.Get("Content-Type")
|
||||
)
|
||||
|
||||
if err := shroom.UploadBinary(h, mime, file, u, lc); err != nil {
|
||||
if err := shroom.UploadBinary(h, mime, file, u); err != nil {
|
||||
httpErr(w, lc, http.StatusInternalServerError, hyphaName, err.Error())
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user