2021-02-20 14:03:54 +00:00
|
|
|
|
package shroom
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/history"
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2021-10-10 03:51:34 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/l18n"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// UnattachHypha unattaches hypha and makes a history record about that.
|
2021-10-10 03:51:34 +00:00
|
|
|
|
func UnattachHypha(u *user.User, h *hyphae.Hypha, lc *l18n.Localizer) (hop *history.Op, errtitle string) {
|
2021-02-20 14:03:54 +00:00
|
|
|
|
hop = history.Operation(history.TypeUnattachHypha)
|
|
|
|
|
|
2021-10-10 03:51:34 +00:00
|
|
|
|
if errtitle, err := CanUnattach(u, h, lc); errtitle != "" {
|
2021-02-26 16:43:45 +00:00
|
|
|
|
hop.WithErrAbort(err)
|
2021-02-20 14:03:54 +00:00
|
|
|
|
return hop, errtitle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hop.
|
|
|
|
|
WithFilesRemoved(h.BinaryPath).
|
|
|
|
|
WithMsg(fmt.Sprintf("Unattach ‘%s’", h.Name)).
|
|
|
|
|
WithUser(u).
|
|
|
|
|
Apply()
|
|
|
|
|
|
|
|
|
|
if len(hop.Errs) > 0 {
|
|
|
|
|
rejectUnattachLog(h, u, "fail")
|
2021-02-26 16:43:45 +00:00
|
|
|
|
// FIXME: something may be wrong here
|
2021-10-01 17:12:16 +00:00
|
|
|
|
return hop.WithErrAbort(fmt.Errorf("Could not unattach this hypha due to internal server errors: <code>%v</code>", hop.Errs)), "Error"
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if h.BinaryPath != "" {
|
|
|
|
|
h.BinaryPath = ""
|
|
|
|
|
}
|
|
|
|
|
// If nothing is left of the hypha
|
|
|
|
|
if h.TextPath == "" {
|
2022-02-03 21:26:08 +00:00
|
|
|
|
hyphae.DeleteHypha(h)
|
2021-02-20 14:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
return hop, ""
|
|
|
|
|
}
|