BREAKING CHANGE: The original Go module name was `github.com/ArtalkJS/Artalk`, which has now been modified to `github.com/artalkjs/artalk/v2` (all lowercase). This change may affect the CI pipeline or other areas if you are compiling the Artalk server and have specified the fixed Go module name. Regarding the original `-ldflags -X github.com/ArtalkJS/Artalk/internal/config.Version` and `CommitHash` definitions, please remove them, as they are now obsolete. The `Version` has been hardcoded into the Go code, and the `CommitHash` is now read from the Go build info via runtime/debug’s vcs.revision value. This change only affects the server developers and does not impact the frontend UI client.
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package comments_get
|
|
|
|
import (
|
|
"github.com/artalkjs/artalk/v2/internal/dao"
|
|
"github.com/artalkjs/artalk/v2/internal/entity"
|
|
"github.com/samber/lo"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Find all nested children (for nested mode)
|
|
func findNestedChildren(dao *dao.Dao, comments []entity.CookedComment, commonScopes []func(*gorm.DB) *gorm.DB) []entity.CookedComment {
|
|
allRootIDs := lo.Map(comments, func(c entity.CookedComment, _ int) uint { return c.ID })
|
|
// TODO: Add pagination for nested mode
|
|
// All children will be loaded at once without pagination, which may cause performance issues.
|
|
// The backend will response all to the client-side, and render by the client-side itself.
|
|
var children []*entity.Comment
|
|
dao.DB().Model(&entity.Comment{}).
|
|
Scopes(commonScopes...).
|
|
Where("root_id IN ? AND rid != 0", allRootIDs).
|
|
Find(&children)
|
|
comments = append(comments, dao.CookAllComments(children)...)
|
|
return comments
|
|
}
|
|
|
|
// Find all linked comments (for flat mode)
|
|
func findFlatLinkedComments(dao *dao.Dao, comments []entity.CookedComment, commonScopes []func(*gorm.DB) *gorm.DB) []entity.CookedComment {
|
|
allCommentIDs := map[uint]bool{}
|
|
for _, c := range comments {
|
|
allCommentIDs[c.ID] = true
|
|
}
|
|
|
|
missCommentIDs := lo.Map(lo.Filter(comments, func(c entity.CookedComment, i int) bool {
|
|
return c.Rid != 0 && !allCommentIDs[c.Rid]
|
|
}), func(c entity.CookedComment, i int) uint {
|
|
return c.Rid
|
|
})
|
|
|
|
// Find linked comments which `id` is the same as `rid`
|
|
var linkedComments []entity.Comment
|
|
if len(missCommentIDs) > 0 {
|
|
dao.DB().Where("id IN ?", missCommentIDs).
|
|
Scopes(commonScopes...).
|
|
Find(&linkedComments)
|
|
}
|
|
|
|
// Linked comments could be invisible but included in the list
|
|
for _, c := range linkedComments {
|
|
rCooked := dao.CookComment(&c)
|
|
rCooked.Visible = false // Set invisible
|
|
comments = append(comments, rCooked)
|
|
}
|
|
|
|
return comments
|
|
}
|