diff --git a/app/main.go b/app/main.go index ee8779bb..655d1c76 100644 --- a/app/main.go +++ b/app/main.go @@ -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} diff --git a/app/store/service.go b/app/store/service.go index 55a42ab9..7b90ae12 100644 --- a/app/store/service.go +++ b/app/store/service.go @@ -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) } diff --git a/app/store/service_test.go b/app/store/service_test.go index 1de37b56..d6394ae9 100644 --- a/app/store/service_test.go +++ b/app/store/service_test.go @@ -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, link`, + 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) +}