Implemented sorting by latest reply in thread

pull/37/head
マリウス 2 years ago
parent a9643b47b3
commit 51a79a3dce
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F

@ -257,15 +257,32 @@ func (db *Database) ListArticles() ([]*models.Article, []*models.Article, error)
var articlesRoots []*models.Article
for i := 0; i < len(articles); i++ {
if articles[i].InReplyToID != "" {
if _, exist := articlesMap[articles[i].InReplyToID]; exist == true {
(*articlesMap[articles[i].InReplyToID]).Replies =
append((*articlesMap[articles[i].InReplyToID]).Replies, articles[i])
inReplyTo := articles[i].InReplyToID
if _, exist := articlesMap[inReplyTo]; exist == true {
(*articlesMap[inReplyTo]).Replies =
append((*articlesMap[inReplyTo]).Replies, articles[i])
(*articlesMap[inReplyTo]).LatestReply = articles[i].Date
continue
}
} else {
articlesRoots = append(articlesRoots, articles[i])
}
articlesRoots = append(articlesRoots, articles[i])
}
sort.SliceStable(articlesRoots, func(i, j int) bool {
iLatest := articlesRoots[i].LatestReply
if iLatest <= 0 {
iLatest = articlesRoots[i].Date
}
jLatest := articlesRoots[j].LatestReply
if jLatest <= 0 {
jLatest = articlesRoots[j].Date
}
return iLatest > jLatest
})
return articles, articlesRoots, nil
}

@ -18,7 +18,9 @@ type Article struct {
Body string `mapstructure:"body" json:"-" validate:"required,min=3,max=524288"`
Replies []*Article `mapstructure:"-" json:"-" validate:"-"`
Read bool `mapstructure:"-" json:"read" validate:"-"`
LatestReply int64 `mapstructure:"-" json:"-" validate:"-"`
Read bool `mapstructure:"-" json:"read" validate:"-"`
}
func NewArticle() (*Article) {

Loading…
Cancel
Save