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

38 lines
1.0 KiB
Go
Raw Normal View History

2021-02-20 14:03:54 +00:00
package shroom
import (
"fmt"
2022-04-02 16:58:57 +00:00
"github.com/bouncepaw/mycorrhiza/backlinks"
"github.com/bouncepaw/mycorrhiza/categories"
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-06-06 14:41:33 +00:00
originalText, _ := hyphae.FetchMycomarkupFile(h)
2022-02-19 08:26:38 +00:00
switch h := h.(type) {
case *hyphae.MediaHypha:
if h.HasTextFile() {
hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath())
} else {
hop.WithFilesRemoved(h.MediaFilePath())
}
2022-02-19 08:26:38 +00:00
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)
categories.RemoveHyphaFromAllCategories(h.CanonicalName())
2022-02-20 09:27:30 +00:00
hyphae.DeleteHypha(h)
return nil
2021-02-20 14:03:54 +00:00
}