From 0de26f62c8f57ed07ca32851fd210339df2ec1d9 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Sat, 4 Nov 2023 15:51:00 +0100 Subject: [PATCH] combine multiple post info in DataStore.Info instead of returning first Previously, only the first one was returned for site-wide requests, and now all returned information will be correctly aggregated, and the PostInfo.URL and PostInfo.ReadOnly parameters will be dropped. --- backend/app/store/comment.go | 5 +++-- backend/app/store/service/service.go | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/backend/app/store/comment.go b/backend/app/store/comment.go index ef026a4d6f..877f942a27 100644 --- a/backend/app/store/comment.go +++ b/backend/app/store/comment.go @@ -45,9 +45,9 @@ type Edit struct { // PostInfo holds summary for given post url type PostInfo struct { - URL string `json:"url"` + URL string `json:"url,omitempty"` // can be attached to site-wide comments but won't be set then Count int `json:"count"` - ReadOnly bool `json:"read_only,omitempty" bson:"read_only,omitempty"` + ReadOnly bool `json:"read_only,omitempty" bson:"read_only,omitempty"` // can be attached to site-wide comments but won't be set then FirstTS time.Time `json:"first_time,omitempty" bson:"first_time,omitempty"` LastTS time.Time `json:"last_time,omitempty" bson:"last_time,omitempty"` } @@ -98,6 +98,7 @@ func (c *Comment) SetDeleted(mode DeleteMode) { c.Text = "" c.Orig = "" c.Score = 0 + c.Controversy = 0 c.Votes = map[string]bool{} c.VotedIPs = make(map[string]VotedIPInfo) c.Edit = nil diff --git a/backend/app/store/service/service.go b/backend/app/store/service/service.go index 5503712b52..964e9a307d 100644 --- a/backend/app/store/service/service.go +++ b/backend/app/store/service/service.go @@ -754,7 +754,23 @@ func (s *DataStore) Info(locator store.Locator, readonlyAge int) (store.PostInfo if len(res) == 0 { return store.PostInfo{}, fmt.Errorf("post %+v not found", locator) } - return res[0], nil + // URL request + if locator.URL != "" { + return res[0], nil + } + // site-wide request which returned multiple store.PostInfo, so that URL and ReadOnly flags don't make sense + var info store.PostInfo + for _, i := range res { + info.Count += i.Count + if info.FirstTS.IsZero() || i.FirstTS.Before(info.FirstTS) { + info.FirstTS = i.FirstTS + } + if info.LastTS.IsZero() || i.LastTS.After(info.LastTS) { + info.LastTS = i.LastTS + } + } + return info, nil + } // Delete comment by id