Use comment timestamp for submitting image ttl

This commit is contained in:
vdimir
2020-07-09 12:43:07 -05:00
committed by Umputun
parent 9510962e25
commit 1f9566bb47
4 changed files with 25 additions and 20 deletions
+7 -2
View File
@@ -102,7 +102,7 @@ func NewService(s Store, p ServiceParams) *Service {
}
// Submit multiple ids via function for delayed commit
func (s *Service) Submit(idsFn func() []string) {
func (s *Service) Submit(idsFn func() []string, ts time.Time) {
if idsFn == nil || s == nil {
return
}
@@ -130,7 +130,12 @@ func (s *Service) Submit(idsFn func() []string) {
})
atomic.AddInt32(&s.submitCount, 1)
s.submitCh <- submitReq{idsFn: idsFn, TS: time.Now()}
now := time.Now()
if ts.IsZero() || ts.After(now) {
ts = now
}
s.submitCh <- submitReq{idsFn: idsFn, TS: ts}
}
// ExtractPictures gets list of images from the doc html and convert from urls to ids, i.e. user/pic.png
+9 -9
View File
@@ -137,9 +137,9 @@ func TestService_Submit(t *testing.T) {
store := MockStore{}
store.On("Commit", mock.Anything, mock.Anything).Times(5).Return(nil)
svc := Service{store: &store, ServiceParams: ServiceParams{ImageAPI: "/blah/", EditDuration: time.Millisecond * 100}}
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} })
svc.Submit(func() []string { return []string{"id4", "id5"} })
svc.Submit(nil)
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} }, time.Now())
svc.Submit(func() []string { return []string{"id4", "id5"} }, time.Now())
svc.Submit(nil, time.Now())
store.AssertNumberOfCalls(t, "Commit", 0)
time.Sleep(time.Millisecond * 150)
store.AssertNumberOfCalls(t, "Commit", 5)
@@ -150,9 +150,9 @@ func TestService_Close(t *testing.T) {
store := MockStore{}
store.On("Commit", mock.Anything, mock.Anything).Times(5).Return(nil)
svc := Service{store: &store, ServiceParams: ServiceParams{ImageAPI: "/blah/", EditDuration: time.Hour * 24}}
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} })
svc.Submit(func() []string { return []string{"id4", "id5"} })
svc.Submit(nil)
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} }, time.Now())
svc.Submit(func() []string { return []string{"id4", "id5"} }, time.Now())
svc.Submit(nil, time.Now())
svc.Close(context.TODO())
store.AssertNumberOfCalls(t, "Commit", 5)
}
@@ -161,10 +161,10 @@ func TestService_SubmitDelay(t *testing.T) {
store := MockStore{}
store.On("Commit", mock.Anything, mock.Anything).Times(5).Return(nil)
svc := NewService(&store, ServiceParams{EditDuration: 20 * time.Millisecond})
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} })
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} }, time.Now())
time.Sleep(150 * time.Millisecond) // let first batch to pass TTL
svc.Submit(func() []string { return []string{"id4", "id5"} })
svc.Submit(nil)
svc.Submit(func() []string { return []string{"id4", "id5"} }, time.Now())
svc.Submit(nil, time.Now())
store.AssertNumberOfCalls(t, "Commit", 3)
svc.Close(context.TODO())
store.AssertNumberOfCalls(t, "Commit", 5)
+8 -8
View File
@@ -101,7 +101,7 @@ func (s *DataStore) Create(comment store.Comment) (commentID string, err error)
comment.PostTitle = title
}()
s.submitImages(comment.Locator, comment.ID)
s.submitImages(comment)
if e := s.AdminStore.OnEvent(comment.Locator.SiteID, admin.EvCreate); e != nil {
log.Printf("[WARN] failed to send create event, %s", e)
}
@@ -217,32 +217,32 @@ func (s *DataStore) ResubmitStagingImages(sites []string) error {
comments, err := s.FindSince(locator, "time", store.User{}, ts)
result = multierror.Append(result, errors.Wrapf(err, "problem finding comments for site %s", site))
for _, c := range comments {
s.submitImages(c.Locator, c.ID)
s.submitImages(c)
}
}
return result.ErrorOrNil()
}
// submitImages initiated delayed commit of all images from the comment uploaded to remark42
func (s *DataStore) submitImages(locator store.Locator, commentID string) {
func (s *DataStore) submitImages(comment store.Comment) {
s.ImageService.Submit(func() []string { // get all ids from comment's text
// this can be called after last edit, we have to retrieve fresh comment
cc, err := s.Engine.Get(engine.GetRequest{Locator: locator, CommentID: commentID})
cc, err := s.Engine.Get(engine.GetRequest{Locator: comment.Locator, CommentID: comment.ID})
if err != nil {
log.Printf("[WARN] can't get comment's %s text for image extraction, %v", commentID, err)
log.Printf("[WARN] can't get comment's %s text for image extraction, %v", comment.ID, err)
return nil
}
imgIds, err := s.ImageService.ExtractPictures(cc.Text)
if err != nil {
log.Printf("[WARN] can't get extract pictures from %s, %v", commentID, err)
log.Printf("[WARN] can't get extract pictures from %s, %v", comment.ID, err)
return nil
}
if len(imgIds) > 0 {
log.Printf("[DEBUG] image ids extracted from %s - %+v", commentID, imgIds)
log.Printf("[DEBUG] image ids extracted from %s - %+v", comment.ID, imgIds)
}
return imgIds
})
}, comment.Timestamp)
}
// prepareNewComment sets new comment fields, hashing and sanitizing data
+1 -1
View File
@@ -1336,7 +1336,7 @@ func TestService_submitImages(t *testing.T) {
_, err := b.Engine.Create(c) // create directly with engine, doesn't call submitImages
assert.NoError(t, err)
b.submitImages(c.Locator, c.ID)
b.submitImages(c)
time.Sleep(250 * time.Millisecond)
mockStore.AssertNumberOfCalls(t, "Commit", 2)
}