2021-02-20 19:03:54 +05:00
|
|
|
|
package shroom
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2022-04-02 19:58:57 +03:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/backlinks"
|
2021-02-20 19:03:54 +05:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/history"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-20 12:27:30 +03:00
|
|
|
|
// Delete deletes the hypha and makes a history record about that.
|
|
|
|
|
func Delete(u *user.User, h hyphae.ExistingHypha) error {
|
2022-02-19 19:42:32 +03:00
|
|
|
|
hop := history.
|
2022-02-19 11:26:38 +03:00
|
|
|
|
Operation(history.TypeDeleteHypha).
|
|
|
|
|
WithMsg(fmt.Sprintf("Delete ‘%s’", h.CanonicalName())).
|
|
|
|
|
WithUser(u)
|
2021-02-20 19:03:54 +05:00
|
|
|
|
|
2022-06-06 17:41:33 +03:00
|
|
|
|
originalText, _ := hyphae.FetchMycomarkupFile(h)
|
2022-02-19 11:26:38 +03:00
|
|
|
|
switch h := h.(type) {
|
|
|
|
|
case *hyphae.MediaHypha:
|
2022-05-07 01:03:20 +05:00
|
|
|
|
if h.HasTextFile() {
|
|
|
|
|
hop.WithFilesRemoved(h.MediaFilePath(), h.TextFilePath())
|
|
|
|
|
} else {
|
|
|
|
|
hop.WithFilesRemoved(h.MediaFilePath())
|
|
|
|
|
}
|
2022-02-19 11:26:38 +03:00
|
|
|
|
case *hyphae.TextualHypha:
|
|
|
|
|
hop.WithFilesRemoved(h.TextFilePath())
|
|
|
|
|
}
|
2022-02-20 12:27:30 +03:00
|
|
|
|
if hop.Apply().HasErrors() {
|
|
|
|
|
return hop.Errs[0]
|
2021-02-20 19:03:54 +05:00
|
|
|
|
}
|
2022-02-20 12:27:30 +03:00
|
|
|
|
backlinks.UpdateBacklinksAfterDelete(h, originalText)
|
|
|
|
|
hyphae.DeleteHypha(h)
|
2022-02-19 19:42:32 +03:00
|
|
|
|
return nil
|
2021-02-20 19:03:54 +05:00
|
|
|
|
}
|