Browse Source

fixing comment keying, fixing comment storage location

Stephen Searles 1 year ago
parent
commit
3f941de9c8
2 changed files with 18 additions and 7 deletions
  1. 17
    6
      comments/comments.go
  2. 1
    1
      setup.go

+ 17
- 6
comments/comments.go View File

@@ -8,6 +8,7 @@ import (
8 8
 	"html/template"
9 9
 	"io"
10 10
 	"net/http"
11
+	"strconv"
11 12
 	"sync"
12 13
 	"time"
13 14
 
@@ -175,17 +176,27 @@ func (d *diskvStorage) nextKey(post string) (string, error) {
175 176
 	d.keyMtx.Lock()
176 177
 	defer d.keyMtx.Unlock()
177 178
 
178
-	var i uint64
179
-
180 179
 	prefix := post + "-"
181 180
 
181
+	lastKey := prefix
182
+
182 183
 	keys := d.KeysPrefix(prefix, nil)
183
-	for range keys {
184
-		i++
184
+	for key := range keys {
185
+		lastKey = key
186
+	}
187
+
188
+	if lastKey == prefix {
189
+		return prefix + "1", nil
190
+	}
191
+
192
+	minusPrefix := lastKey[len(prefix):]
193
+	lastIdx, err := strconv.Atoi(minusPrefix)
194
+	if err != nil {
195
+		return "", fmt.Errorf("comment key %q seems malformed", lastKey)
185 196
 	}
186 197
 
187
-	key := prefix + fmt.Sprint(i)
188
-	err := d.WriteStream(key, &bytes.Buffer{}, true)
198
+	key := prefix + fmt.Sprint(lastIdx+1)
199
+	err = d.WriteStream(key, &bytes.Buffer{}, true)
189 200
 
190 201
 	return key, err
191 202
 }

+ 1
- 1
setup.go View File

@@ -38,7 +38,7 @@ func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) {
38 38
 		if c.Val() == "hugo" {
39 39
 			for c.NextBlock() {
40 40
 				if c.Val() == "comments" {
41
-					ch.Comments = comments.Default()
41
+					ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
42 42
 					if c.NextArg() {
43 43
 						ch.Comments.Password = c.Val()
44 44
 					}

Loading…
Cancel
Save