mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-04-27 04:53:10 +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.
|
// CanDelete and etc are hyphae operation checkers based on user rights and hyphae existence.
|
||||||
var (
|
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(
|
CanEdit = canFactory(
|
||||||
rejectEditLog,
|
rejectEditLog,
|
||||||
"upload-text",
|
"upload-text",
|
||||||
|
@ -6,34 +6,27 @@ import (
|
|||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/history"
|
"github.com/bouncepaw/mycorrhiza/history"
|
||||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/user"
|
"github.com/bouncepaw/mycorrhiza/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteHypha deletes hypha and makes a history record about that.
|
// Delete deletes the hypha and makes a history record about that.
|
||||||
func DeleteHypha(u *user.User, h hyphae.Hypha, lc *l18n.Localizer) error {
|
func Delete(u *user.User, h hyphae.ExistingHypha) error {
|
||||||
if err := CanDelete(u, h, lc); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
hop := history.
|
hop := history.
|
||||||
Operation(history.TypeDeleteHypha).
|
Operation(history.TypeDeleteHypha).
|
||||||
WithMsg(fmt.Sprintf("Delete ‘%s’", h.CanonicalName())).
|
WithMsg(fmt.Sprintf("Delete ‘%s’", h.CanonicalName())).
|
||||||
WithUser(u)
|
WithUser(u)
|
||||||
|
|
||||||
|
originalText, _ := FetchTextFile(h)
|
||||||
switch h := h.(type) {
|
switch h := h.(type) {
|
||||||
case *hyphae.MediaHypha:
|
case *hyphae.MediaHypha:
|
||||||
hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath())
|
hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath())
|
||||||
case *hyphae.TextualHypha:
|
case *hyphae.TextualHypha:
|
||||||
hop.WithFilesRemoved(h.TextFilePath())
|
hop.WithFilesRemoved(h.TextFilePath())
|
||||||
default:
|
|
||||||
panic("impossible")
|
|
||||||
}
|
}
|
||||||
originalText, _ := FetchTextFile(h)
|
if hop.Apply().HasErrors() {
|
||||||
hop.Apply()
|
return hop.Errs[0]
|
||||||
if !hop.HasErrors() {
|
|
||||||
backlinks.UpdateBacklinksAfterDelete(h, originalText)
|
|
||||||
hyphae.DeleteHypha(h.(hyphae.ExistingHypha)) // we panicked before, so it's safe
|
|
||||||
}
|
}
|
||||||
|
backlinks.UpdateBacklinksAfterDelete(h, originalText)
|
||||||
|
hyphae.DeleteHypha(h)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"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 {
|
func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *user.User) error {
|
||||||
if newName == "" {
|
if newName == "" {
|
||||||
rejectRenameLog(oldHypha, u, "no new name given")
|
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
|
package shroom
|
||||||
|
|
||||||
import (
|
import (
|
@ -5,12 +5,11 @@ import (
|
|||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/history"
|
"github.com/bouncepaw/mycorrhiza/history"
|
||||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/user"
|
"github.com/bouncepaw/mycorrhiza/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RemoveMedia unattaches hypha and makes a history record about that.
|
// 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, lc *l18n.Localizer) error {
|
func RemoveMedia(u *user.User, h *hyphae.MediaHypha) error {
|
||||||
hop := history.
|
hop := history.
|
||||||
Operation(history.TypeUnattachHypha).
|
Operation(history.TypeUnattachHypha).
|
||||||
WithFilesRemoved(h.MediaFilePath()).
|
WithFilesRemoved(h.MediaFilePath()).
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/bouncepaw/mycorrhiza/history"
|
"github.com/bouncepaw/mycorrhiza/history"
|
||||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||||
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
|
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
|
||||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/mimetype"
|
"github.com/bouncepaw/mycorrhiza/mimetype"
|
||||||
"github.com/bouncepaw/mycorrhiza/user"
|
"github.com/bouncepaw/mycorrhiza/user"
|
||||||
"io"
|
"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.
|
// 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.
|
hop := history.
|
||||||
Operation(history.TypeEditText).
|
Operation(history.TypeEditText).
|
||||||
WithMsg(historyMessageForTextUpload(h, userMessage)).
|
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.
|
// 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
|
// Privilege check
|
||||||
if !u.CanProceed("upload-binary") {
|
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")
|
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "no media to remove")
|
||||||
return
|
return
|
||||||
case *hyphae.MediaHypha:
|
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())
|
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -70,20 +70,20 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
|
|||||||
h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete"))
|
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") {
|
if !u.CanProceed("delete") {
|
||||||
log.Printf("%s has no rights to delete ‘%s’\n", u.Name, h.CanonicalName())
|
log.Printf("%s has no rights to delete ‘%s’\n", u.Name, h.CanonicalName())
|
||||||
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "No rights")
|
httpErr(w, lc, http.StatusForbidden, h.CanonicalName(), "No rights")
|
||||||
return
|
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" {
|
if rq.Method == "GET" {
|
||||||
util.HTTP200Page(
|
util.HTTP200Page(
|
||||||
w,
|
w,
|
||||||
@ -95,9 +95,10 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := shroom.DeleteHypha(u, h, lc); err != nil {
|
if err := shroom.Delete(u, h.(hyphae.ExistingHypha)); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
|
httpErr(w, lc, http.StatusInternalServerError, h.CanonicalName(), err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
http.Redirect(w, rq, "/hypha/"+h.CanonicalName(), http.StatusSeeOther)
|
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 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())
|
httpErr(w, lc, http.StatusForbidden, hyphaName, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -261,7 +262,7 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
|
|||||||
mime = handler.Header.Get("Content-Type")
|
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())
|
httpErr(w, lc, http.StatusInternalServerError, hyphaName, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user