1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00

Fix anon hypha deletion bug and start using hypha.Count()

This commit is contained in:
bouncepaw 2021-01-03 02:25:04 +05:00
parent 301592ad7d
commit 1d5e2e515c
7 changed files with 61 additions and 62 deletions

View File

@ -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"

View File

@ -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

View File

@ -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(`
<div class="binary-container binary-container_with-nothing">
<p>This hypha's media cannot be rendered. Access it <a href="/binary/%s">directly</a></p>
<p>This hypha's media cannot be rendered. <a href="/binary/%s">Download it</a></p>
</div>
`, hyphaName)
}
@ -250,6 +253,7 @@ func Index(path string) {
} else {
hyphaData = &HyphaData{}
HyphaStorage[hyphaName] = hyphaData
hyphae.IncrementCount()
}
if isText {
hyphaData.textPath = hyphaPartPath

View File

@ -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
}

31
hyphae/count.go Normal file
View File

@ -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
}

View File

@ -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"},
},
},
},

View File

@ -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)