answer/internal/service/question_common/question.go

448 lines
14 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package questioncommon
import (
"context"
"encoding/json"
"time"
2022-11-22 19:48:27 +08:00
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/service/activity_common"
2022-11-22 19:48:27 +08:00
"github.com/answerdev/answer/internal/service/activity_queue"
"github.com/answerdev/answer/internal/service/config"
"github.com/answerdev/answer/internal/service/meta"
"github.com/segmentfault/pacman/errors"
2022-09-27 17:59:05 +08:00
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
answercommon "github.com/answerdev/answer/internal/service/answer_common"
collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
usercommon "github.com/answerdev/answer/internal/service/user_common"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/log"
)
// QuestionRepo question repository
type QuestionRepo interface {
AddQuestion(ctx context.Context, question *entity.Question) (err error)
RemoveQuestion(ctx context.Context, id string) (err error)
UpdateQuestion(ctx context.Context, question *entity.Question, Cols []string) (err error)
GetQuestion(ctx context.Context, id string) (question *entity.Question, exist bool, err error)
GetQuestionList(ctx context.Context, question *entity.Question) (questions []*entity.Question, err error)
GetQuestionPage(ctx context.Context, page, pageSize int, question *entity.Question) (questions []*entity.Question, total int64, err error)
SearchList(ctx context.Context, search *schema.QuestionSearch) ([]*entity.QuestionTag, int64, error)
UpdateQuestionStatus(ctx context.Context, question *entity.Question) (err error)
SearchByTitleLike(ctx context.Context, title string) (questionList []*entity.Question, err error)
UpdatePvCount(ctx context.Context, questionID string) (err error)
UpdateAnswerCount(ctx context.Context, questionID string, num int) (err error)
UpdateCollectionCount(ctx context.Context, questionID string, num int) (err error)
2022-09-27 17:59:05 +08:00
UpdateAccepted(ctx context.Context, question *entity.Question) (err error)
UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error)
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
CmsSearchList(ctx context.Context, search *schema.CmsQuestionSearch) ([]*entity.Question, int64, error)
2022-11-02 15:50:32 +08:00
GetQuestionCount(ctx context.Context) (count int64, err error)
2022-12-12 18:21:03 +08:00
GetQuestionIDsPage(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error)
2022-09-27 17:59:05 +08:00
}
// QuestionCommon user service
type QuestionCommon struct {
questionRepo QuestionRepo
answerRepo answercommon.AnswerRepo
voteRepo activity_common.VoteRepo
followCommon activity_common.FollowRepo
tagCommon *tagcommon.TagCommonService
userCommon *usercommon.UserCommon
collectionCommon *collectioncommon.CollectionCommon
AnswerCommon *answercommon.AnswerCommon
metaService *meta.MetaService
configRepo config.ConfigRepo
}
func NewQuestionCommon(questionRepo QuestionRepo,
answerRepo answercommon.AnswerRepo,
voteRepo activity_common.VoteRepo,
followCommon activity_common.FollowRepo,
tagCommon *tagcommon.TagCommonService,
userCommon *usercommon.UserCommon,
collectionCommon *collectioncommon.CollectionCommon,
answerCommon *answercommon.AnswerCommon,
metaService *meta.MetaService,
configRepo config.ConfigRepo,
) *QuestionCommon {
return &QuestionCommon{
questionRepo: questionRepo,
answerRepo: answerRepo,
voteRepo: voteRepo,
followCommon: followCommon,
tagCommon: tagCommon,
userCommon: userCommon,
collectionCommon: collectionCommon,
AnswerCommon: answerCommon,
metaService: metaService,
configRepo: configRepo,
}
}
func (qs *QuestionCommon) UpdataPv(ctx context.Context, questionID string) error {
return qs.questionRepo.UpdatePvCount(ctx, questionID)
2022-09-27 17:59:05 +08:00
}
func (qs *QuestionCommon) UpdateAnswerCount(ctx context.Context, questionID string, num int) error {
return qs.questionRepo.UpdateAnswerCount(ctx, questionID, num)
2022-09-27 17:59:05 +08:00
}
func (qs *QuestionCommon) UpdateCollectionCount(ctx context.Context, questionID string, num int) error {
return qs.questionRepo.UpdateCollectionCount(ctx, questionID, num)
2022-09-27 17:59:05 +08:00
}
func (qs *QuestionCommon) UpdateAccepted(ctx context.Context, questionID, AnswerID string) error {
2022-09-27 17:59:05 +08:00
question := &entity.Question{}
question.ID = questionID
question.AcceptedAnswerID = AnswerID
2022-09-27 17:59:05 +08:00
return qs.questionRepo.UpdateAccepted(ctx, question)
}
func (qs *QuestionCommon) UpdateLastAnswer(ctx context.Context, questionID, AnswerID string) error {
2022-09-27 17:59:05 +08:00
question := &entity.Question{}
question.ID = questionID
question.LastAnswerID = AnswerID
2022-09-27 17:59:05 +08:00
return qs.questionRepo.UpdateLastAnswer(ctx, question)
}
func (qs *QuestionCommon) UpdataPostTime(ctx context.Context, questionID string) error {
2022-09-27 17:59:05 +08:00
questioninfo := &entity.Question{}
now := time.Now()
questioninfo.ID = questionID
2022-09-27 17:59:05 +08:00
questioninfo.PostUpdateTime = now
return qs.questionRepo.UpdateQuestion(ctx, questioninfo, []string{"post_update_time"})
2022-12-05 13:18:34 +08:00
}
func (qs *QuestionCommon) UpdataPostSetTime(ctx context.Context, questionID string, setTime time.Time) error {
questioninfo := &entity.Question{}
questioninfo.ID = questionID
questioninfo.PostUpdateTime = setTime
return qs.questionRepo.UpdateQuestion(ctx, questioninfo, []string{"post_update_time"})
2022-09-27 17:59:05 +08:00
}
func (qs *QuestionCommon) FindInfoByID(ctx context.Context, questionIDs []string, loginUserID string) (map[string]*schema.QuestionInfo, error) {
2022-09-27 17:59:05 +08:00
list := make(map[string]*schema.QuestionInfo)
listAddTag := make([]*entity.QuestionTag, 0)
questionList, err := qs.questionRepo.FindByID(ctx, questionIDs)
2022-09-27 17:59:05 +08:00
if err != nil {
return list, err
}
for _, item := range questionList {
itemAddTag := &entity.QuestionTag{}
itemAddTag.Question = *item
listAddTag = append(listAddTag, itemAddTag)
}
QuestionInfo, err := qs.ListFormat(ctx, listAddTag, loginUserID)
if err != nil {
return list, err
}
for _, item := range QuestionInfo {
list[item.ID] = item
}
return list, nil
}
func (qs *QuestionCommon) Info(ctx context.Context, questionID string, loginUserID string) (showinfo *schema.QuestionInfo, err error) {
dbinfo, has, err := qs.questionRepo.GetQuestion(ctx, questionID)
2022-09-27 17:59:05 +08:00
if err != nil {
return showinfo, err
}
if !has {
return showinfo, errors.BadRequest(reason.QuestionNotFound)
2022-09-27 17:59:05 +08:00
}
showinfo = qs.ShowFormat(ctx, dbinfo)
if showinfo.Status == 2 {
var metainfo *entity.Meta
metainfo, err = qs.metaService.GetMetaByObjectIdAndKey(ctx, dbinfo.ID, entity.QuestionCloseReasonKey)
2022-09-27 17:59:05 +08:00
if err != nil {
log.Error(err)
} else {
// metainfo.Value
2022-09-27 17:59:05 +08:00
closemsg := &schema.CloseQuestionMeta{}
err = json.Unmarshal([]byte(metainfo.Value), closemsg)
2022-09-27 17:59:05 +08:00
if err != nil {
log.Error("json.Unmarshal CloseQuestionMeta error", err.Error())
} else {
closeinfo := &schema.GetReportTypeResp{}
2022-10-27 12:00:03 +08:00
err = qs.configRepo.GetJsonConfigByIDAndSetToObject(closemsg.CloseType, closeinfo)
2022-09-27 17:59:05 +08:00
if err != nil {
log.Error("json.Unmarshal QuestionCloseJson error", err.Error())
} else {
operation := &schema.Operation{}
operation.OperationType = closeinfo.Name
operation.OperationDescription = closeinfo.Description
operation.OperationMsg = closemsg.CloseMsg
operation.OperationTime = metainfo.CreatedAt.Unix()
2022-09-27 17:59:05 +08:00
showinfo.Operation = operation
}
}
}
}
tagmap, err := qs.tagCommon.GetObjectTag(ctx, questionID)
2022-09-27 17:59:05 +08:00
if err != nil {
return showinfo, err
}
showinfo.Tags = tagmap
2022-12-01 18:36:20 +08:00
userIds := make([]string, 0)
userIds = append(userIds, dbinfo.UserID)
userIds = append(userIds, dbinfo.LastEditUserID)
userIds = append(userIds, showinfo.LastAnsweredUserID)
userInfoMap, err := qs.userCommon.BatchUserBasicInfoByID(ctx, userIds)
2022-09-27 17:59:05 +08:00
if err != nil {
return showinfo, err
}
2022-12-01 18:36:20 +08:00
_, ok := userInfoMap[dbinfo.UserID]
if ok {
showinfo.UserInfo = userInfoMap[dbinfo.UserID]
}
_, ok = userInfoMap[dbinfo.LastEditUserID]
if ok {
showinfo.UpdateUserInfo = userInfoMap[dbinfo.LastEditUserID]
}
_, ok = userInfoMap[showinfo.LastAnsweredUserID]
if ok {
showinfo.LastAnsweredUserInfo = userInfoMap[showinfo.LastAnsweredUserID]
2022-09-27 17:59:05 +08:00
}
if loginUserID == "" {
return showinfo, nil
}
showinfo.VoteStatus = qs.voteRepo.GetVoteStatus(ctx, questionID, loginUserID)
2022-09-27 17:59:05 +08:00
// // check is followed
isFollowed, _ := qs.followCommon.IsFollowed(loginUserID, questionID)
2022-09-27 17:59:05 +08:00
showinfo.IsFollowed = isFollowed
has, err = qs.AnswerCommon.SearchAnswered(ctx, loginUserID, dbinfo.ID)
if err != nil {
log.Error("AnswerFunc.SearchAnswered", err)
}
showinfo.Answered = has
// login user Collected information
2022-09-27 17:59:05 +08:00
CollectedMap, err := qs.collectionCommon.SearchObjectCollected(ctx, loginUserID, []string{dbinfo.ID})
if err != nil {
log.Error("CollectionFunc.SearchObjectCollected", err)
}
2022-12-01 18:36:20 +08:00
_, ok = CollectedMap[dbinfo.ID]
2022-09-27 17:59:05 +08:00
if ok {
showinfo.Collected = true
}
return showinfo, nil
}
func (qs *QuestionCommon) ListFormat(ctx context.Context, questionList []*entity.QuestionTag, loginUserID string) ([]*schema.QuestionInfo, error) {
list := make([]*schema.QuestionInfo, 0)
objectIds := make([]string, 0)
userIds := make([]string, 0)
for _, questionInfo := range questionList {
item := qs.ShowListFormat(ctx, questionInfo)
list = append(list, item)
objectIds = append(objectIds, item.ID)
2022-12-01 18:36:20 +08:00
userIds = append(userIds, item.UserID)
userIds = append(userIds, item.LastEditUserID)
2022-12-01 15:26:20 +08:00
userIds = append(userIds, item.LastAnsweredUserID)
2022-09-27 17:59:05 +08:00
}
tagsMap, err := qs.tagCommon.BatchGetObjectTag(ctx, objectIds)
if err != nil {
return list, err
}
userInfoMap, err := qs.userCommon.BatchUserBasicInfoByID(ctx, userIds)
if err != nil {
return list, err
}
for _, item := range list {
_, ok := tagsMap[item.ID]
if ok {
item.Tags = tagsMap[item.ID]
}
_, ok = userInfoMap[item.UserID]
2022-09-27 17:59:05 +08:00
if ok {
item.UserInfo = userInfoMap[item.UserID]
2022-12-01 15:26:20 +08:00
}
_, ok = userInfoMap[item.LastEditUserID]
if ok {
2022-12-01 18:36:20 +08:00
item.UpdateUserInfo = userInfoMap[item.LastEditUserID]
2022-12-01 15:26:20 +08:00
}
_, ok = userInfoMap[item.LastAnsweredUserID]
if ok {
item.LastAnsweredUserInfo = userInfoMap[item.LastAnsweredUserID]
2022-09-27 17:59:05 +08:00
}
}
if loginUserID == "" {
return list, nil
}
// //login user Collected information
CollectedMap, err := qs.collectionCommon.SearchObjectCollected(ctx, loginUserID, objectIds)
if err != nil {
log.Error("CollectionFunc.SearchObjectCollected", err)
}
for _, item := range list {
_, ok := CollectedMap[item.ID]
if ok {
item.Collected = true
}
}
return list, nil
}
// RemoveQuestion delete question
func (qs *QuestionCommon) RemoveQuestion(ctx context.Context, req *schema.RemoveQuestionReq) (err error) {
questionInfo, has, err := qs.questionRepo.GetQuestion(ctx, req.ID)
if err != nil {
return err
}
if !has {
return nil
}
questionInfo.Status = entity.QuestionStatusDeleted
err = qs.questionRepo.UpdateQuestionStatus(ctx, questionInfo)
if err != nil {
return err
}
// user add question count
2022-09-27 17:59:05 +08:00
err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, -1)
if err != nil {
log.Error("user UpdateQuestionCount error", err.Error())
}
// todo rank remove
return nil
}
func (qs *QuestionCommon) CloseQuestion(ctx context.Context, req *schema.CloseQuestionReq) error {
questionInfo, has, err := qs.questionRepo.GetQuestion(ctx, req.ID)
if err != nil {
return err
}
if !has {
return nil
}
2022-11-22 19:48:27 +08:00
questionInfo.Status = entity.QuestionStatusClosed
2022-09-27 17:59:05 +08:00
err = qs.questionRepo.UpdateQuestionStatus(ctx, questionInfo)
if err != nil {
return err
}
closeMeta, _ := json.Marshal(schema.CloseQuestionMeta{
CloseType: req.CloseType,
CloseMsg: req.CloseMsg,
})
err = qs.metaService.AddMeta(ctx, req.ID, entity.QuestionCloseReasonKey, string(closeMeta))
if err != nil {
return err
}
2022-11-22 19:48:27 +08:00
activity_queue.AddActivity(&schema.ActivityMsg{
UserID: questionInfo.UserID,
ObjectID: questionInfo.ID,
OriginalObjectID: questionInfo.ID,
ActivityTypeKey: constant.ActQuestionClosed,
})
2022-09-27 17:59:05 +08:00
return nil
}
// RemoveAnswer delete answer
func (as *QuestionCommon) RemoveAnswer(ctx context.Context, id string) (err error) {
answerinfo, has, err := as.answerRepo.GetByID(ctx, id)
if err != nil {
return err
}
if !has {
return nil
}
// user add question count
2022-09-27 17:59:05 +08:00
err = as.UpdateAnswerCount(ctx, answerinfo.QuestionID, -1)
if err != nil {
log.Error("UpdateAnswerCount error", err.Error())
}
err = as.userCommon.UpdateAnswerCount(ctx, answerinfo.UserID, -1)
if err != nil {
log.Error("user UpdateAnswerCount error", err.Error())
}
return as.answerRepo.RemoveAnswer(ctx, id)
}
func (qs *QuestionCommon) ShowListFormat(ctx context.Context, data *entity.QuestionTag) *schema.QuestionInfo {
return qs.ShowFormat(ctx, &data.Question)
}
func (qs *QuestionCommon) ShowFormat(ctx context.Context, data *entity.Question) *schema.QuestionInfo {
info := schema.QuestionInfo{}
info.ID = data.ID
info.Title = data.Title
info.Content = data.OriginalText
info.HTML = data.ParsedText
2022-09-27 17:59:05 +08:00
info.ViewCount = data.ViewCount
info.UniqueViewCount = data.UniqueViewCount
info.VoteCount = data.VoteCount
info.AnswerCount = data.AnswerCount
info.CollectionCount = data.CollectionCount
info.FollowCount = data.FollowCount
info.AcceptedAnswerID = data.AcceptedAnswerID
info.LastAnswerID = data.LastAnswerID
2022-09-27 17:59:05 +08:00
info.CreateTime = data.CreatedAt.Unix()
info.UpdateTime = data.UpdatedAt.Unix()
info.PostUpdateTime = data.PostUpdateTime.Unix()
2022-11-29 14:56:22 +08:00
if data.PostUpdateTime.Unix() < 1 {
info.PostUpdateTime = 0
}
2022-09-27 17:59:05 +08:00
info.QuestionUpdateTime = data.UpdatedAt.Unix()
2022-11-29 14:56:22 +08:00
if data.UpdatedAt.Unix() < 1 {
info.QuestionUpdateTime = 0
}
2022-09-27 17:59:05 +08:00
info.Status = data.Status
info.UserID = data.UserID
2022-12-01 15:26:20 +08:00
info.LastEditUserID = data.LastEditUserID
if data.LastAnswerID != "0" {
answerInfo, exist, err := qs.answerRepo.GetAnswer(ctx, data.LastAnswerID)
if err == nil && exist {
if answerInfo.LastEditUserID != "0" {
info.LastAnsweredUserID = answerInfo.LastEditUserID
} else {
info.LastAnsweredUserID = answerInfo.UserID
}
}
}
2022-09-27 17:59:05 +08:00
info.Tags = make([]*schema.TagResp, 0)
return &info
}
2022-11-23 18:10:34 +08:00
func (qs *QuestionCommon) ShowFormatWithTag(ctx context.Context, data *entity.QuestionWithTagsRevision) *schema.QuestionInfo {
info := qs.ShowFormat(ctx, &data.Question)
Tags := make([]*schema.TagResp, 0)
for _, tag := range data.Tags {
item := &schema.TagResp{}
item.SlugName = tag.SlugName
item.DisplayName = tag.DisplayName
item.Recommend = tag.Recommend
item.Reserved = tag.Reserved
Tags = append(Tags, item)
}
info.Tags = Tags
return info
}