Files
qwqcode cf7725edba refactor(go): bump the module version and support go install (#979)
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.
2024-09-18 21:43:43 +08:00

59 lines
1.4 KiB
Go

package comments_get
import (
"github.com/artalkjs/artalk/v2/internal/entity"
)
// User Scope Query Tag
type UserScopeType string
const (
UserAll UserScopeType = "all"
UserMentions UserScopeType = "mentions"
UserMine UserScopeType = "mine"
UserPending UserScopeType = "pending"
)
type UserScopePayload struct {
Type UserScopeType
}
type UserScopeOpts struct {
User entity.User
GetUserComments func(userID uint) []uint
}
// User Scope (for message center)
func UserScopeQuery(payload UserScopePayload, opts UserScopeOpts) func(liteDB) liteDB {
return func(q liteDB) liteDB {
// If user not found, return empty query
if opts.User.IsEmpty() {
return q.Where("1 = 0")
}
// Get user all comment ids which someone could reply to him
userCommentIDs := opts.GetUserComments(opts.User.ID)
scopes := map[UserScopeType]func(liteDB) liteDB{
UserAll: func(d liteDB) liteDB {
return q.Where("user_id = ? OR rid IN (?)", opts.User.ID, userCommentIDs)
},
UserMentions: func(d liteDB) liteDB {
return q.Where("user_id != ? AND rid IN (?)", opts.User.ID, userCommentIDs)
},
UserMine: func(d liteDB) liteDB {
return q.Where("user_id = ?", opts.User.ID)
},
UserPending: func(d liteDB) liteDB {
return q.Where("user_id = ? AND is_pending = ?", opts.User.ID, true)
},
}
if scope, ok := scopes[payload.Type]; ok {
q.Scopes(scope)
}
return q
}
}