From b286c16ac2a4265c95d9cf5192d21691feb8ef3e Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Sat, 29 Oct 2022 16:01:06 +0300 Subject: [PATCH] Categories: Remove hypha from all cata after deletion --- categories/categories.go | 23 +++++++++++++++++++++++ shroom/delete.go | 2 ++ 2 files changed, 25 insertions(+) diff --git a/categories/categories.go b/categories/categories.go index d49ee3d..75d3482 100644 --- a/categories/categories.go +++ b/categories/categories.go @@ -95,6 +95,29 @@ func removeHyphaFromCategory(hyphaName, catName string) { go saveToDisk() } +// RemoveHyphaFromAllCategories removes the given hypha from all the categories. +func RemoveHyphaFromAllCategories(hyphaName string) { + cats := CategoriesWithHypha(hyphaName) + mutex.Lock() + defer mutex.Unlock() + for _, cat := range cats { + if node, ok := hyphaToCategories[hyphaName]; ok { + node.removeCategory(cat) + if len(node.categoryList) == 0 { + delete(hyphaToCategories, hyphaName) + } + } + + if node, ok := categoryToHyphae[cat]; ok { + node.removeHypha(hyphaName) + if len(node.hyphaList) == 0 { + delete(categoryToHyphae, cat) + } + } + } + go saveToDisk() +} + // RenameHyphaInAllCategories finds all mentions of oldName and replaces them with newName. Pass canonical names. Make sure newName is not taken. If oldName is not in any category, RenameHyphaInAllCategories is a no-op. func RenameHyphaInAllCategories(oldName, newName string) { mutex.Lock() diff --git a/shroom/delete.go b/shroom/delete.go index 38dbd54..d63703f 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -3,6 +3,7 @@ package shroom import ( "fmt" "github.com/bouncepaw/mycorrhiza/backlinks" + "github.com/bouncepaw/mycorrhiza/categories" "github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/user" @@ -30,6 +31,7 @@ func Delete(u *user.User, h hyphae.ExistingHypha) error { return hop.Errs[0] } backlinks.UpdateBacklinksAfterDelete(h, originalText) + categories.RemoveHyphaFromAllCategories(h.CanonicalName()) hyphae.DeleteHypha(h) return nil } -- 2.45.2