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.
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
// This package's main job is to build the `where` conditions of SQL.
|
|
// Call `GetQueryScopes` to create a LiteDB instance. This can be converted into a `gorm.DB` instance filled with `where` conditions.
|
|
// Call the functions in `expose.go` to get the whole query result, not just the `where` conditions (via Gorm).
|
|
package comments_get
|
|
|
|
import (
|
|
"github.com/artalkjs/artalk/v2/internal/dao"
|
|
"github.com/artalkjs/artalk/v2/internal/entity"
|
|
)
|
|
|
|
type Scope string
|
|
|
|
const (
|
|
ScopePage Scope = "page"
|
|
ScopeUser Scope = "user"
|
|
ScopeSite Scope = "site"
|
|
)
|
|
|
|
type QueryOptions struct {
|
|
User entity.User
|
|
|
|
Scope Scope
|
|
|
|
PagePayload PageScopePayload
|
|
UserPayload UserScopePayload
|
|
SitePayload SitePayload
|
|
|
|
SortBy SortRule
|
|
|
|
Search string
|
|
}
|
|
|
|
// Get query scope by params
|
|
//
|
|
// Please be aware that only `WHERE` conditions are permissible in this function.
|
|
// For `ORDER BY`, `LIMIT`, and `OFFSET`, please utilize separate functions, as this
|
|
// function is invoked in both `Find` and `Count`. `ORDER BY`, `LIMIT`, and `OFFSET` cannot
|
|
// be employed within `Count`.
|
|
//
|
|
// Updated: The `*gorm.DB` had been refactored to `liteDB`, which is a subset of `*gorm.DB`.
|
|
// (only contains `WHERE` conditions)
|
|
func GetQueryScopes(dao *dao.Dao, opts QueryOptions) func(liteDB) liteDB {
|
|
return func(q liteDB) liteDB {
|
|
// Basic scope
|
|
q.Scopes(CommonScope(opts.User))
|
|
|
|
// Search function
|
|
if opts.Search != "" {
|
|
q.Scopes(SearchScope(dao, opts.Search))
|
|
}
|
|
|
|
// Scopes
|
|
q.Scopes(map[Scope]func(liteDB) liteDB{
|
|
ScopePage: PageScopeQuery(opts.PagePayload, PageScopeOpts{
|
|
AdminUserIDs: dao.GetAllAdminIDs(),
|
|
}),
|
|
ScopeUser: UserScopeQuery(opts.UserPayload, UserScopeOpts{
|
|
User: opts.User,
|
|
GetUserComments: func(userID uint) []uint {
|
|
return dao.GetUserAllCommentIDs(userID)
|
|
},
|
|
}),
|
|
ScopeSite: SiteScopeQuery(opts.SitePayload, opts.User),
|
|
}[opts.Scope])
|
|
|
|
return q
|
|
}
|
|
}
|