1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-14 06:10:26 +00:00
mycorrhiza/shroom/delete.go

33 lines
835 B
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/user"
)
2022-02-20 09:27:30 +00:00
// Delete deletes the hypha and makes a history record about that.
func Delete(u *user.User, h hyphae.ExistingHypha) error {
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-20 09:27:30 +00:00
originalText, _ := FetchTextFile(h)
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())
}
2022-02-20 09:27:30 +00:00
if hop.Apply().HasErrors() {
return hop.Errs[0]
2021-02-20 14:03:54 +00:00
}
2022-02-20 09:27:30 +00:00
backlinks.UpdateBacklinksAfterDelete(h, originalText)
hyphae.DeleteHypha(h)
return nil
2021-02-20 14:03:54 +00:00
}