answer/internal/service/search/author.go

91 lines
1.7 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package search
import (
"context"
"regexp"
"strings"
"github.com/segmentfault/answer/internal/schema"
"github.com/segmentfault/answer/internal/service/search_common"
usercommon "github.com/segmentfault/answer/internal/service/user_common"
)
type AuthorSearch struct {
repo search_common.SearchRepo
userCommon *usercommon.UserCommon
exp string
w string
page int
size int
2022-10-09 18:44:56 +08:00
order string
2022-09-27 17:59:05 +08:00
}
func NewAuthorSearch(repo search_common.SearchRepo, userCommon *usercommon.UserCommon) *AuthorSearch {
2022-09-27 17:59:05 +08:00
return &AuthorSearch{
repo: repo,
userCommon: userCommon,
2022-09-27 17:59:05 +08:00
}
}
// Parse
// example: "user:12345" -> {exp="" w="12345"}
func (s *AuthorSearch) Parse(dto *schema.SearchDTO) (ok bool) {
var (
exp,
q,
w,
p,
me,
name string
)
exp = ""
q = dto.Query
w = q
p = `(?m)^user:([a-z0-9._-]+)`
me = "user:me"
re := regexp.MustCompile(p)
res := re.FindStringSubmatch(q)
if len(res) == 2 {
name = res[1]
user, has, err := s.userCommon.GetUserBasicInfoByUserName(nil, name)
2022-09-27 17:59:05 +08:00
if err == nil && has {
exp = user.ID
trimLen := len(res[0])
w = q[trimLen:]
ok = true
}
} else if strings.Index(q, me) == 0 {
exp = dto.UserID
w = strings.TrimPrefix(q, me)
ok = true
}
w = strings.TrimSpace(w)
s.exp = exp
s.w = w
s.page = dto.Page
s.size = dto.Size
2022-10-09 18:44:56 +08:00
s.order = dto.Order
2022-09-27 17:59:05 +08:00
return
}
func (s *AuthorSearch) Search(ctx context.Context) (resp []schema.SearchResp, total int64, err error) {
var (
words []string
)
if len(s.exp) == 0 {
return
}
words = strings.Split(s.w, " ")
if len(words) > 3 {
words = words[:4]
}
2022-10-09 18:44:56 +08:00
resp, total, err = s.repo.SearchContents(ctx, words, "", s.exp, -1, s.page, s.size, s.order)
2022-09-27 17:59:05 +08:00
return
}