From 1d5e2e515ca70a749b1985389c9960f43ffb02c9 Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Sun, 3 Jan 2021 02:25:04 +0500 Subject: [PATCH] Fix anon hypha deletion bug and start using hypha.Count() --- history/operations.go | 1 + http_mutators.go | 2 +- hypha.go | 6 +++++- hypha/count.go | 33 ------------------------------ hyphae/count.go | 31 ++++++++++++++++++++++++++++ {hypha => hyphae}/hypha.go | 41 +++++++++++++++++--------------------- main.go | 9 +++++---- 7 files changed, 61 insertions(+), 62 deletions(-) delete mode 100644 hypha/count.go create mode 100644 hyphae/count.go rename {hypha => hyphae}/hypha.go (53%) diff --git a/history/operations.go b/history/operations.go index e16c367..fdc64af 100644 --- a/history/operations.go +++ b/history/operations.go @@ -121,6 +121,7 @@ func (hop *HistoryOp) WithMsg(userMsg string) *HistoryOp { // WithUser sets a user for the commit. func (hop *HistoryOp) WithUser(u *user.User) *HistoryOp { + u = u.OrAnon() if u.Group != user.UserAnon { hop.name = u.Name hop.email = u.Name + "@mycorrhiza" diff --git a/http_mutators.go b/http_mutators.go index 1283f59..f1cd5da 100644 --- a/http_mutators.go +++ b/http_mutators.go @@ -96,7 +96,7 @@ func handlerDeleteConfirm(w http.ResponseWriter, rq *http.Request) { hyphaData, isOld = HyphaStorage[hyphaName] u = user.FromRequest(rq) ) - if !u.CanProceed("delete-confirm") { + if !user.CanProceed(rq, "delete-confirm") { HttpErr(w, http.StatusForbidden, hyphaName, "Not enough rights", "You must be a moderator to delete pages.") log.Println("Rejected", rq.URL) return diff --git a/hypha.go b/hypha.go index 5280639..c9f2612 100644 --- a/hypha.go +++ b/hypha.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/bouncepaw/mycorrhiza/history" + "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/markup" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" @@ -84,6 +85,7 @@ func uploadHelp(hop *history.HistoryOp, hyphaName, ext string, data []byte, u *u // New hyphae must be added to the hypha storage if !isOld { HyphaStorage[hyphaName] = hyphaData + hyphae.IncrementCount() } *originalFullPath = fullPath return hop.WithFiles(fullPath). @@ -121,6 +123,7 @@ func (hd *HyphaData) DeleteHypha(hyphaName string, u *user.User) *history.Histor Apply() if len(hop.Errs) == 0 { delete(HyphaStorage, hyphaName) + hyphae.DecrementCount() } return hop } @@ -218,7 +221,7 @@ func binaryHtmlBlock(hyphaName string, hd *HyphaData) string { default: return fmt.Sprintf(`
-

This hypha's media cannot be rendered. Access it directly

+

This hypha's media cannot be rendered. Download it

`, hyphaName) } @@ -250,6 +253,7 @@ func Index(path string) { } else { hyphaData = &HyphaData{} HyphaStorage[hyphaName] = hyphaData + hyphae.IncrementCount() } if isText { hyphaData.textPath = hyphaPartPath diff --git a/hypha/count.go b/hypha/count.go deleted file mode 100644 index 3c9aef4..0000000 --- a/hypha/count.go +++ /dev/null @@ -1,33 +0,0 @@ -package hypha - -import ( - "sync" -) - -type count struct { - value uint - sync.Mutex -} - -// Count is a global variable. Its value is number of all existing hyphae. Hypha mutators are expected to manipulate the value. It is concurrent-safe. -var Count = count{} - -// Increment the value of Count. -func (c *count) Increment() { - c.Lock() - c.value++ - c.Unlock() -} - -// Decrement the value of Count. -func (c *count) Decrement() { - c.Lock() - c.value-- - c.Unlock() -} - -// Get value of Count. -func (c *count) Value() uint { - // it is concurrent-safe to not lock here, right? - return c.value -} diff --git a/hyphae/count.go b/hyphae/count.go new file mode 100644 index 0000000..2c1d5f1 --- /dev/null +++ b/hyphae/count.go @@ -0,0 +1,31 @@ +package hyphae + +import ( + "sync" +) + +// Its value is number of all existing hyphae. Hypha mutators are expected to manipulate the value. It is concurrent-safe. +var count = struct { + value int + sync.Mutex +}{} + +// Increment the value of hyphae count. +func IncrementCount() { + count.Lock() + count.value++ + count.Unlock() +} + +// Decrement the value of hyphae count. +func DecrementCount() { + count.Lock() + count.value-- + count.Unlock() +} + +// Count how many hyphae there are. +func Count() int { + // it is concurrent-safe to not lock here, right? + return count.value +} diff --git a/hypha/hypha.go b/hyphae/hypha.go similarity index 53% rename from hypha/hypha.go rename to hyphae/hypha.go index 2ca5ffc..be82302 100644 --- a/hypha/hypha.go +++ b/hyphae/hypha.go @@ -1,4 +1,4 @@ -package hypha +package hyphae import ( "errors" @@ -25,40 +25,35 @@ var schema = &memdb.DBSchema{ "hyphae": &memdb.TableSchema{ Name: "hyphae", Indexes: map[string]*memdb.IndexSchema{ - "name": &memdb.IndexSchema{ - Name: "name", + "id": &memdb.IndexSchema{ + Name: "id", Unique: true, Indexer: &memdb.StringFieldIndex{Field: "Name"}, }, "exists": &memdb.IndexSchema{ - Name: "exists", - Unique: false, - AllowEmpty: true, - Indexer: &memdb.BoolFieldIndex{Field: "Exists"}, + Name: "exists", + Unique: false, + Indexer: &memdb.BoolFieldIndex{Field: "Exists"}, }, "text-path": &memdb.IndexSchema{ - Name: "text-path", - Unique: true, - AllowEmpty: true, - Indexer: &memdb.StringFieldIndex{Field: "TextPath"}, + Name: "text-path", + Unique: true, + Indexer: &memdb.StringFieldIndex{Field: "TextPath"}, }, "binary-path": &memdb.IndexSchema{ - Name: "binary-path", - Unique: true, - AllowEmpty: true, - Indexer: &memdb.StringFieldIndex{Field: "BinaryPath"}, + Name: "binary-path", + Unique: true, + Indexer: &memdb.StringFieldIndex{Field: "BinaryPath"}, }, "out-links": &memdb.IndexSchema{ - Name: "out-links", - Unique: false, - AllowEmpty: true, - Indexer: &memdb.StringSliceFieldIndex{Field: "OutLinks"}, + Name: "out-links", + Unique: false, + Indexer: &memdb.StringSliceFieldIndex{Field: "OutLinks"}, }, "back-links": &memdb.IndexSchema{ - Name: "back-links", - Unique: false, - AllowEmpty: true, - Indexer: &memdb.StringSliceFieldIndex{Field: "BackLinks"}, + Name: "back-links", + Unique: false, + Indexer: &memdb.StringSliceFieldIndex{Field: "BackLinks"}, }, }, }, diff --git a/main.go b/main.go index 0caa1d5..ae1aa0f 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "strings" "github.com/bouncepaw/mycorrhiza/history" + "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/templates" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" @@ -50,7 +51,7 @@ func handlerList(w http.ResponseWriter, rq *http.Request) { log.Println(rq.URL) var ( tbody string - pageCount = len(HyphaStorage) + pageCount = hyphae.Count() ) for hyphaName, data := range HyphaStorage { tbody += templates.HyphaListRowHTML(hyphaName, ExtensionToMime(filepath.Ext(data.binaryPath)), data.binaryPath != "") @@ -73,14 +74,14 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) { log.Println("Wiki storage directory is", WikiDir) log.Println("Start indexing hyphae...") Index(WikiDir) - log.Println("Indexed", len(HyphaStorage), "hyphae") + log.Println("Indexed", hyphae.Count(), "hyphae") } // Redirect to a random hypha. func handlerRandom(w http.ResponseWriter, rq *http.Request) { log.Println(rq.URL) var randomHyphaName string - i := rand.Intn(len(HyphaStorage)) + i := rand.Intn(hyphae.Count()) for hyphaName := range HyphaStorage { if i == 0 { randomHyphaName = hyphaName @@ -148,7 +149,7 @@ func main() { log.Println("Wiki storage directory is", WikiDir) log.Println("Start indexing hyphae...") Index(WikiDir) - log.Println("Indexed", len(HyphaStorage), "hyphae") + log.Println("Indexed", hyphae.Count(), "hyphae") history.Start(WikiDir)