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:
parent
301592ad7d
commit
1d5e2e515c
@ -121,6 +121,7 @@ func (hop *HistoryOp) WithMsg(userMsg string) *HistoryOp {
|
|||||||
|
|
||||||
// WithUser sets a user for the commit.
|
// WithUser sets a user for the commit.
|
||||||
func (hop *HistoryOp) WithUser(u *user.User) *HistoryOp {
|
func (hop *HistoryOp) WithUser(u *user.User) *HistoryOp {
|
||||||
|
u = u.OrAnon()
|
||||||
if u.Group != user.UserAnon {
|
if u.Group != user.UserAnon {
|
||||||
hop.name = u.Name
|
hop.name = u.Name
|
||||||
hop.email = u.Name + "@mycorrhiza"
|
hop.email = u.Name + "@mycorrhiza"
|
||||||
|
@ -96,7 +96,7 @@ func handlerDeleteConfirm(w http.ResponseWriter, rq *http.Request) {
|
|||||||
hyphaData, isOld = HyphaStorage[hyphaName]
|
hyphaData, isOld = HyphaStorage[hyphaName]
|
||||||
u = user.FromRequest(rq)
|
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.")
|
HttpErr(w, http.StatusForbidden, hyphaName, "Not enough rights", "You must be a moderator to delete pages.")
|
||||||
log.Println("Rejected", rq.URL)
|
log.Println("Rejected", rq.URL)
|
||||||
return
|
return
|
||||||
|
6
hypha.go
6
hypha.go
@ -11,6 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/history"
|
"github.com/bouncepaw/mycorrhiza/history"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||||
"github.com/bouncepaw/mycorrhiza/markup"
|
"github.com/bouncepaw/mycorrhiza/markup"
|
||||||
"github.com/bouncepaw/mycorrhiza/user"
|
"github.com/bouncepaw/mycorrhiza/user"
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"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
|
// New hyphae must be added to the hypha storage
|
||||||
if !isOld {
|
if !isOld {
|
||||||
HyphaStorage[hyphaName] = hyphaData
|
HyphaStorage[hyphaName] = hyphaData
|
||||||
|
hyphae.IncrementCount()
|
||||||
}
|
}
|
||||||
*originalFullPath = fullPath
|
*originalFullPath = fullPath
|
||||||
return hop.WithFiles(fullPath).
|
return hop.WithFiles(fullPath).
|
||||||
@ -121,6 +123,7 @@ func (hd *HyphaData) DeleteHypha(hyphaName string, u *user.User) *history.Histor
|
|||||||
Apply()
|
Apply()
|
||||||
if len(hop.Errs) == 0 {
|
if len(hop.Errs) == 0 {
|
||||||
delete(HyphaStorage, hyphaName)
|
delete(HyphaStorage, hyphaName)
|
||||||
|
hyphae.DecrementCount()
|
||||||
}
|
}
|
||||||
return hop
|
return hop
|
||||||
}
|
}
|
||||||
@ -218,7 +221,7 @@ func binaryHtmlBlock(hyphaName string, hd *HyphaData) string {
|
|||||||
default:
|
default:
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
<div class="binary-container binary-container_with-nothing">
|
<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>
|
</div>
|
||||||
`, hyphaName)
|
`, hyphaName)
|
||||||
}
|
}
|
||||||
@ -250,6 +253,7 @@ func Index(path string) {
|
|||||||
} else {
|
} else {
|
||||||
hyphaData = &HyphaData{}
|
hyphaData = &HyphaData{}
|
||||||
HyphaStorage[hyphaName] = hyphaData
|
HyphaStorage[hyphaName] = hyphaData
|
||||||
|
hyphae.IncrementCount()
|
||||||
}
|
}
|
||||||
if isText {
|
if isText {
|
||||||
hyphaData.textPath = hyphaPartPath
|
hyphaData.textPath = hyphaPartPath
|
||||||
|
@ -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
31
hyphae/count.go
Normal 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
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package hypha
|
package hyphae
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -25,40 +25,35 @@ var schema = &memdb.DBSchema{
|
|||||||
"hyphae": &memdb.TableSchema{
|
"hyphae": &memdb.TableSchema{
|
||||||
Name: "hyphae",
|
Name: "hyphae",
|
||||||
Indexes: map[string]*memdb.IndexSchema{
|
Indexes: map[string]*memdb.IndexSchema{
|
||||||
"name": &memdb.IndexSchema{
|
"id": &memdb.IndexSchema{
|
||||||
Name: "name",
|
Name: "id",
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: &memdb.StringFieldIndex{Field: "Name"},
|
Indexer: &memdb.StringFieldIndex{Field: "Name"},
|
||||||
},
|
},
|
||||||
"exists": &memdb.IndexSchema{
|
"exists": &memdb.IndexSchema{
|
||||||
Name: "exists",
|
Name: "exists",
|
||||||
Unique: false,
|
Unique: false,
|
||||||
AllowEmpty: true,
|
Indexer: &memdb.BoolFieldIndex{Field: "Exists"},
|
||||||
Indexer: &memdb.BoolFieldIndex{Field: "Exists"},
|
|
||||||
},
|
},
|
||||||
"text-path": &memdb.IndexSchema{
|
"text-path": &memdb.IndexSchema{
|
||||||
Name: "text-path",
|
Name: "text-path",
|
||||||
Unique: true,
|
Unique: true,
|
||||||
AllowEmpty: true,
|
Indexer: &memdb.StringFieldIndex{Field: "TextPath"},
|
||||||
Indexer: &memdb.StringFieldIndex{Field: "TextPath"},
|
|
||||||
},
|
},
|
||||||
"binary-path": &memdb.IndexSchema{
|
"binary-path": &memdb.IndexSchema{
|
||||||
Name: "binary-path",
|
Name: "binary-path",
|
||||||
Unique: true,
|
Unique: true,
|
||||||
AllowEmpty: true,
|
Indexer: &memdb.StringFieldIndex{Field: "BinaryPath"},
|
||||||
Indexer: &memdb.StringFieldIndex{Field: "BinaryPath"},
|
|
||||||
},
|
},
|
||||||
"out-links": &memdb.IndexSchema{
|
"out-links": &memdb.IndexSchema{
|
||||||
Name: "out-links",
|
Name: "out-links",
|
||||||
Unique: false,
|
Unique: false,
|
||||||
AllowEmpty: true,
|
Indexer: &memdb.StringSliceFieldIndex{Field: "OutLinks"},
|
||||||
Indexer: &memdb.StringSliceFieldIndex{Field: "OutLinks"},
|
|
||||||
},
|
},
|
||||||
"back-links": &memdb.IndexSchema{
|
"back-links": &memdb.IndexSchema{
|
||||||
Name: "back-links",
|
Name: "back-links",
|
||||||
Unique: false,
|
Unique: false,
|
||||||
AllowEmpty: true,
|
Indexer: &memdb.StringSliceFieldIndex{Field: "BackLinks"},
|
||||||
Indexer: &memdb.StringSliceFieldIndex{Field: "BackLinks"},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
9
main.go
9
main.go
@ -14,6 +14,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/history"
|
"github.com/bouncepaw/mycorrhiza/history"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||||
"github.com/bouncepaw/mycorrhiza/templates"
|
"github.com/bouncepaw/mycorrhiza/templates"
|
||||||
"github.com/bouncepaw/mycorrhiza/user"
|
"github.com/bouncepaw/mycorrhiza/user"
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
@ -50,7 +51,7 @@ func handlerList(w http.ResponseWriter, rq *http.Request) {
|
|||||||
log.Println(rq.URL)
|
log.Println(rq.URL)
|
||||||
var (
|
var (
|
||||||
tbody string
|
tbody string
|
||||||
pageCount = len(HyphaStorage)
|
pageCount = hyphae.Count()
|
||||||
)
|
)
|
||||||
for hyphaName, data := range HyphaStorage {
|
for hyphaName, data := range HyphaStorage {
|
||||||
tbody += templates.HyphaListRowHTML(hyphaName, ExtensionToMime(filepath.Ext(data.binaryPath)), data.binaryPath != "")
|
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("Wiki storage directory is", WikiDir)
|
||||||
log.Println("Start indexing hyphae...")
|
log.Println("Start indexing hyphae...")
|
||||||
Index(WikiDir)
|
Index(WikiDir)
|
||||||
log.Println("Indexed", len(HyphaStorage), "hyphae")
|
log.Println("Indexed", hyphae.Count(), "hyphae")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect to a random hypha.
|
// Redirect to a random hypha.
|
||||||
func handlerRandom(w http.ResponseWriter, rq *http.Request) {
|
func handlerRandom(w http.ResponseWriter, rq *http.Request) {
|
||||||
log.Println(rq.URL)
|
log.Println(rq.URL)
|
||||||
var randomHyphaName string
|
var randomHyphaName string
|
||||||
i := rand.Intn(len(HyphaStorage))
|
i := rand.Intn(hyphae.Count())
|
||||||
for hyphaName := range HyphaStorage {
|
for hyphaName := range HyphaStorage {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
randomHyphaName = hyphaName
|
randomHyphaName = hyphaName
|
||||||
@ -148,7 +149,7 @@ func main() {
|
|||||||
log.Println("Wiki storage directory is", WikiDir)
|
log.Println("Wiki storage directory is", WikiDir)
|
||||||
log.Println("Start indexing hyphae...")
|
log.Println("Start indexing hyphae...")
|
||||||
Index(WikiDir)
|
Index(WikiDir)
|
||||||
log.Println("Indexed", len(HyphaStorage), "hyphae")
|
log.Println("Indexed", hyphae.Count(), "hyphae")
|
||||||
|
|
||||||
history.Start(WikiDir)
|
history.Start(WikiDir)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user