pass edit duration from outside

This commit is contained in:
Umputun
2018-01-03 23:43:25 -06:00
parent 79196d13a0
commit 43676bc90e
3 changed files with 34 additions and 4 deletions
+1 -1
View File
@@ -80,7 +80,7 @@ func main() {
return
}
dataService := store.Service{Interface: dataStore}
dataService := store.Service{Interface: dataStore, EditDuration: 5 * time.Minute}
sessionStore := sessions.NewFilesystemStore(opts.ServerCommand.SessionStore, []byte(opts.ServerCommand.StoreKey))
exporter := migrator.Remark{DataStore: dataStore}
+2 -3
View File
@@ -6,11 +6,10 @@ import (
"github.com/pkg/errors"
)
var editDuration = 5 * time.Minute
// Service wraps store.Interface with additional methods
type Service struct {
Interface
EditDuration time.Duration
}
// SetPin pin/un-pin comment as special
@@ -58,7 +57,7 @@ func (s *Service) EditComment(locator Locator, commentID string, text string, ed
}
// edit allowed in editDuration window only
if comment.Timestamp.Add(editDuration).Before(time.Now()) {
if s.EditDuration > 0 && time.Now().After(comment.Timestamp.Add(s.EditDuration)) {
return comment, errors.Errorf("too late to edit %s", commentID)
}
+31
View File
@@ -3,6 +3,7 @@ package store
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@@ -76,3 +77,33 @@ func TestBoltDB_EditComment(t *testing.T) {
assert.Equal(t, "my edit", c.Edit.Summary)
assert.Equal(t, "xxx", c.Text)
}
func TestBoltDB_EditCommentDurationFailed(t *testing.T) {
defer os.Remove(testDb)
blt, err := NewBoltDB(BoltSite{FileName: "/tmp/test-remark.db", SiteID: "radio-t"})
assert.Nil(t, err)
comment := Comment{
ID: "id-1",
Text: `some text, <a href="http://radio-t.com">link</a>`,
Locator: Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
User: User{ID: "user1", Name: "user name"},
}
_, err = blt.Create(comment)
assert.Nil(t, err)
b := Service{Interface: blt, EditDuration: 100 * time.Millisecond}
res, err := b.Last(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, 0)
t.Logf("%+v", res[0])
assert.Nil(t, err)
assert.Equal(t, 1, len(res))
assert.Nil(t, res[0].Edit)
time.Sleep(time.Second)
comment, err = b.EditComment(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, "xxx",
Edit{Summary: "my edit"})
assert.NotNil(t, err)
}