2021-02-20 19:03:54 +05:00
|
|
|
|
package shroom
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-12-21 00:08:21 +03:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
|
2021-02-20 19:03:54 +05:00
|
|
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/history"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2021-10-10 11:51:34 +08:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/l18n"
|
2021-02-20 19:03:54 +05:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DeleteHypha deletes hypha and makes a history record about that.
|
2022-02-19 11:26:38 +03:00
|
|
|
|
func DeleteHypha(u *user.User, h hyphae.Hypha, lc *l18n.Localizer) (hop *history.Op, errtitle string) {
|
|
|
|
|
hop = history.
|
|
|
|
|
Operation(history.TypeDeleteHypha).
|
|
|
|
|
WithMsg(fmt.Sprintf("Delete ‘%s’", h.CanonicalName())).
|
|
|
|
|
WithUser(u)
|
2021-02-20 19:03:54 +05:00
|
|
|
|
|
2021-10-10 11:51:34 +08:00
|
|
|
|
if errtitle, err := CanDelete(u, h, lc); errtitle != "" {
|
2021-02-26 21:43:45 +05:00
|
|
|
|
hop.WithErrAbort(err)
|
2021-02-20 19:03:54 +05:00
|
|
|
|
return hop, errtitle
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 11:26:38 +03:00
|
|
|
|
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()
|
2021-02-20 19:03:54 +05:00
|
|
|
|
if !hop.HasErrors() {
|
2021-12-31 00:59:28 +05:00
|
|
|
|
backlinks.UpdateBacklinksAfterDelete(h, originalText)
|
2022-02-19 11:26:38 +03:00
|
|
|
|
hyphae.DeleteHypha(h.(hyphae.ExistingHypha)) // we panicked before, so it's safe
|
2021-02-20 19:03:54 +05:00
|
|
|
|
}
|
|
|
|
|
return hop, ""
|
|
|
|
|
}
|