Skip to content

Commit 427e446

Browse files
committed
correctly combine multiple post info in DataStore.Info
Previously, only the first one was returned for site-wide requests, and now all returned information will be correctly aggregated, and the PostInfo.URL parameter will be dropped.
1 parent 49d6785 commit 427e446

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

backend/app/store/comment.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ type Edit struct {
4545

4646
// PostInfo holds summary for given post url
4747
type PostInfo struct {
48-
URL string `json:"url"`
48+
URL string `json:"url,omitempty"` // can be attached to site-wide comments but won't be set then
4949
Count int `json:"count"`
50-
ReadOnly bool `json:"read_only,omitempty" bson:"read_only,omitempty"`
50+
ReadOnly bool `json:"read_only,omitempty" bson:"read_only,omitempty"` // can be attached to site-wide comments but won't be set then
5151
FirstTS time.Time `json:"first_time,omitempty" bson:"first_time,omitempty"`
5252
LastTS time.Time `json:"last_time,omitempty" bson:"last_time,omitempty"`
5353
}
@@ -98,6 +98,7 @@ func (c *Comment) SetDeleted(mode DeleteMode) {
9898
c.Text = ""
9999
c.Orig = ""
100100
c.Score = 0
101+
c.Controversy = 0
101102
c.Votes = map[string]bool{}
102103
c.VotedIPs = make(map[string]VotedIPInfo)
103104
c.Edit = nil

backend/app/store/service/service.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,23 @@ func (s *DataStore) Info(locator store.Locator, readonlyAge int) (store.PostInfo
754754
if len(res) == 0 {
755755
return store.PostInfo{}, fmt.Errorf("post %+v not found", locator)
756756
}
757-
return res[0], nil
757+
// URL request
758+
if locator.URL != "" {
759+
return res[0], nil
760+
}
761+
// site-wide request which returned multiple store.PostInfo, so that URL and ReadOnly flags don't make sense
762+
var info store.PostInfo
763+
for _, i := range res {
764+
info.Count += i.Count
765+
if info.FirstTS.IsZero() || i.FirstTS.Before(info.FirstTS) {
766+
info.FirstTS = i.FirstTS
767+
}
768+
if info.LastTS.IsZero() || i.LastTS.After(info.LastTS) {
769+
info.LastTS = i.LastTS
770+
}
771+
}
772+
return info, nil
773+
758774
}
759775

760776
// Delete comment by id

0 commit comments

Comments
 (0)