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"
|
2022-10-29 13:01:06 +00:00
|
|
|
|
"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 {
|
2022-02-19 16:42:32 +00:00
|
|
|
|
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:
|
2022-05-06 20:03:20 +00:00
|
|
|
|
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)
|
2022-10-29 13:01:06 +00:00
|
|
|
|
categories.RemoveHyphaFromAllCategories(h.CanonicalName())
|
2022-02-20 09:27:30 +00:00
|
|
|
|
hyphae.DeleteHypha(h)
|
2022-02-19 16:42:32 +00:00
|
|
|
|
return nil
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|