Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

Merge remote-tracking branch 'github/feat/plugin/search' into feat/1.1.1/state

LinkinStars93995369bafe
diff --cc internal/repo/answer/answer_repo.go
index 2cf18c9d,d1e890ed..48a3166d
--- a/internal/repo/answer/answer_repo.go
+++ b/internal/repo/answer/answer_repo.go
@@@ -2,7 -2,12 +2,9 @@@ package answe
import (
"context"
+ "github.com/answerdev/answer/plugin"
+ "strings"
"time"
- "unicode"
-
- "xorm.io/builder"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
@@@ -55,10 -59,9 +57,11 @@@ func (ar *answerRepo) AddAnswer(ctx con
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- answer.ID = uid.EnShortID(answer.ID)
- answer.QuestionID = uid.EnShortID(answer.QuestionID)
+ if handler.GetEnableShortID(ctx) {
+ answer.ID = uid.EnShortID(answer.ID)
+ answer.QuestionID = uid.EnShortID(answer.QuestionID)
+ }
+ _ = ar.updateSearch(ctx, answer.ID)
return nil
}
@@@ -291,31 -287,149 +298,100 @@@ func (ar *answerRepo) SearchList(ctx co
return rows, count, nil
}
-func (ar *answerRepo) AdminSearchList(ctx context.Context, search *entity.AdminAnswerSearch) ([]*entity.Answer, int64, error) {
- var (
- count int64
- err error
- session = ar.data.DB.Context(ctx).Table([]string{entity.Answer{}.TableName(), "a"}).Select("a.*")
- )
- if search.QuestionID != "" {
- search.QuestionID = uid.DeShortID(search.QuestionID)
- }
-
- session.Where(builder.Eq{
- "a.status": search.Status,
- })
-
- rows := make([]*entity.Answer, 0)
- if search.Page > 0 {
- search.Page = search.Page - 1
- } else {
- search.Page = 0
+func (ar *answerRepo) AdminSearchList(ctx context.Context, req *schema.AdminAnswerPageReq) (
+ resp []*entity.Answer, total int64, err error) {
+ cond := &entity.Answer{}
+ session := ar.data.DB.Context(ctx)
+ if len(req.QuestionID) == 0 && len(req.AnswerID) == 0 {
+ session.Join("INNER", "question", "answer.question_id = question.id")
+ if len(req.QuestionTitle) > 0 {
+ session.Where("question.title like ?", "%"+req.QuestionTitle+"%")
+ }
}
- if search.PageSize == 0 {
- search.PageSize = constant.DefaultPageSize
+ if len(req.AnswerID) > 0 {
+ cond.ID = req.AnswerID
}
-
- // search by question title like or answer id
- if len(search.Query) > 0 {
- // check id search
- var (
- idSearch = false
- id = ""
- )
-
- if strings.Contains(search.Query, "answer:") {
- idSearch = true
- id = strings.TrimSpace(strings.TrimPrefix(search.Query, "answer:"))
- id = uid.DeShortID(id)
- for _, r := range id {
- if !unicode.IsDigit(r) {
- idSearch = false
- break
- }
- }
- }
-
- if idSearch {
- session.And(builder.Eq{
- "id": id,
- })
- } else {
- session.Join("LEFT", []string{entity.Question{}.TableName(), "q"}, "q.id = a.question_id")
- session.And(builder.Like{
- "q.title", search.Query,
- })
- }
+ if len(req.QuestionID) > 0 {
+ session.Where("answer.question_id = ?", req.QuestionID)
}
-
- // check search by question id
- if len(search.QuestionID) > 0 {
- session.And(builder.Eq{
- "question_id": search.QuestionID,
- })
+ if req.Status > 0 {
+ cond.Status = req.Status
}
+ session.Desc("answer.created_at")
- offset := search.Page * search.PageSize
- session.
- OrderBy("a.created_at desc").
- Limit(search.PageSize, offset)
- count, err = session.FindAndCount(&rows)
+ resp = make([]*entity.Answer, 0)
+ total, err = pager.Help(req.Page, req.PageSize, &resp, cond, session)
if err != nil {
- return rows, count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ return nil, 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- for _, item := range rows {
- item.ID = uid.EnShortID(item.ID)
- item.QuestionID = uid.EnShortID(item.QuestionID)
- }
- return rows, count, nil
+ return resp, total, nil
}
+
+ // updateSearch update search, if search plugin not enable, do nothing
+ func (ar *answerRepo) updateSearch(ctx context.Context, answerID string) (err error) {
+ answerID = uid.DeShortID(answerID)
+ // check search plugin
+ var (
+ s plugin.Search
+ )
+ _ = plugin.CallSearch(func(search plugin.Search) error {
+ s = search
+ return nil
+ })
+ if s == nil {
+ return
+ }
+ answer, exist, err := ar.GetAnswer(ctx, answerID)
+ if !exist {
+ return
+ }
+ if err != nil {
+ return err
+ }
+
+ // get question
+ var (
+ question *entity.Question
+ )
+ exist, err = ar.data.DB.Where("id = ?", answer.QuestionID).Get(&question)
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ if !exist {
+ return
+ }
+
+ // get tags
+ var (
+ tagListList = make([]*entity.TagRel, 0)
+ tags = make([]string, 0)
+ )
+ st := ar.data.DB.Where("object_id = ?", uid.DeShortID(question.ID))
+ st.Where("status = ?", entity.TagRelStatusAvailable)
+ err = st.Find(&tagListList)
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ for _, tag := range tagListList {
+ tags = append(tags, tag.TagID)
+ }
+
+ content := &plugin.SearchContent{
+ ObjectID: answerID,
+ Title: question.Title,
+ Type: "question",
+ Content: answer.ParsedText,
+ Answers: 0,
+ Status: int64(answer.Status),
+ Tags: tags,
+ QuesionID: answerID,
+ UserID: answer.UserID,
+ Views: int64(question.ViewCount),
+ Created: answer.CreatedAt.Unix(),
+ Active: answer.UpdatedAt.Unix(),
+ Score: int64(answer.VoteCount),
+ HasAccepted: answer.Accepted == schema.AnswerAcceptedEnable,
+ }
+ err = s.UpdateContent(ctx, answerID, content)
+ return
+ }
diff --cc internal/repo/question/question_repo.go
index 7f1c8b28,4b652a36..aa0dd3a2
--- a/internal/repo/question/question_repo.go
+++ b/internal/repo/question/question_repo.go
@@@ -2,9 -2,8 +2,10 @@@ package questio
import (
"context"
+ "encoding/json"
"fmt"
+ "github.com/segmentfault/pacman/log"
+ "github.com/answerdev/answer/plugin"
"strings"
"time"
"unicode"
@@@ -53,9 -51,8 +54,10 @@@ func (qr *questionRepo) AddQuestion(ct
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- question.ID = uid.EnShortID(question.ID)
+ if handler.GetEnableShortID(ctx) {
+ question.ID = uid.EnShortID(question.ID)
+ }
+ _ = qr.updateSearch(ctx, question.ID)
return
}
@@@ -76,9 -73,8 +78,10 @@@ func (qr *questionRepo) UpdateQuestion(
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- question.ID = uid.EnShortID(question.ID)
+ if handler.GetEnableShortID(ctx) {
+ question.ID = uid.EnShortID(question.ID)
+ }
+ _ = qr.updateSearch(ctx, question.ID)
return
}