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

40 lines
1.0 KiB
Go
Raw Normal View History

2021-02-20 14:03:54 +00:00
package shroom
import (
"fmt"
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
2021-02-20 14:03:54 +00:00
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/l18n"
2021-02-20 14:03:54 +00:00
"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
}
hop := history.
2022-02-19 08:26:38 +00:00
Operation(history.TypeDeleteHypha).
WithMsg(fmt.Sprintf("Delete %s", h.CanonicalName())).
WithUser(u)
2021-02-20 14:03:54 +00:00
2022-02-19 08:26:38 +00: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 14:03:54 +00:00
if !hop.HasErrors() {
backlinks.UpdateBacklinksAfterDelete(h, originalText)
2022-02-19 08:26:38 +00:00
hyphae.DeleteHypha(h.(hyphae.ExistingHypha)) // we panicked before, so it's safe
2021-02-20 14:03:54 +00:00
}
return nil
2021-02-20 14:03:54 +00:00
}