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 branch 'feat/1.1.0/sql' into feat/1.1.0/report

LinkinStars01bd603570c7
diff --cc internal/controller/question_controller.go
index 72d3df00,9ab267ef..813809c8
--- a/internal/controller/question_controller.go
+++ b/internal/controller/question_controller.go
@@@ -500,6 -538,65 +538,51 @@@ func (qc *QuestionController) UpdateQue
handler.HandleResponse(ctx, nil, &schema.UpdateQuestionResp{WaitForReview: !req.NoNeedReview})
}
+ // UpdateQuestionInviteUser update question invite user
+ // @Summary update question invite user
+ // @Description update question invite user
+ // @Tags Question
+ // @Accept json
+ // @Produce json
+ // @Security ApiKeyAuth
+ // @Param data body schema.QuestionUpdateInviteUser true "question"
+ // @Success 200 {object} handler.RespBody
+ // @Router /answer/api/v1/question/invite [put]
+ func (qc *QuestionController) UpdateQuestionInviteUser(ctx *gin.Context) {
+ req := &schema.QuestionUpdateInviteUser{}
+ errFields := handler.BindAndCheckReturnErr(ctx, req)
+ if ctx.IsAborted() {
+ return
+ }
+ req.ID = uid.DeShortID(req.ID)
+ req.UserID = middleware.GetLoginUserIDFromContext(ctx)
+
+ canList, err := qc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
+ permission.QuestionEdit,
+ })
+ if err != nil {
+ handler.HandleResponse(ctx, err, nil)
+ return
+ }
+
+ objectOwner := qc.rankService.CheckOperationObjectOwner(ctx, req.UserID, req.ID)
+ req.CanEdit = canList[0] || objectOwner
+ if !req.CanEdit {
+ handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
+ return
+ }
+ if len(errFields) > 0 {
+ handler.HandleResponse(ctx, errors.BadRequest(reason.RequestFormatError), errFields)
+ return
+ }
+ err = qc.questionService.UpdateQuestionInviteUser(ctx, req)
+ if err != nil {
+ handler.HandleResponse(ctx, err, nil)
+ return
+ }
+ handler.HandleResponse(ctx, nil, nil)
+ }
+
-// CloseMsgList close question msg list
-// @Summary close question msg list
-// @Description close question msg list
-// @Tags Question
-// @Accept json
-// @Produce json
-// @Security ApiKeyAuth
-// @Success 200 {object} handler.RespBody
-// @Router /answer/api/v1/question/closemsglist [get]
-func (qc *QuestionController) CloseMsgList(ctx *gin.Context) {
- resp, err := qc.questionService.CloseMsgList(ctx, handler.GetLang(ctx))
- handler.HandleResponse(ctx, err, resp)
-}
-
// SearchByTitleLike add question title like
// @Summary add question title like
// @Description add question title like
diff --cc internal/migrations/v13.go
index 16547220,74ae8aea..877469b2
--- a/internal/migrations/v13.go
+++ b/internal/migrations/v13.go
@@@ -1,99 -1,252 +1,339 @@@
package migrations
import (
+ "encoding/json"
"fmt"
+ "time"
+ "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/entity"
+ "github.com/answerdev/answer/internal/schema"
+ "github.com/answerdev/answer/internal/service/permission"
+ "github.com/segmentfault/pacman/log"
"xorm.io/xorm"
)
func updateCount(x *xorm.Engine) error {
+ addPrivilegeForInviteSomeoneToAnswer(x)
+ addGravatarBaseURL(x)
+ updateQuestionCount(x)
+ updateTagCount(x)
+ updateUserQuestionCount(x)
+ updateUserAnswerCount(x)
+ inviteAnswer(x)
return nil
}
+func addGravatarBaseURL(x *xorm.Engine) error {
+ usersSiteInfo := &entity.SiteInfo{
+ Type: constant.SiteTypeUsers,
+ }
+ exist, err := x.Get(usersSiteInfo)
+ if err != nil {
+ return fmt.Errorf("get config failed: %w", err)
+ }
+ if exist {
+ content := &schema.SiteUsersReq{}
+ _ = json.Unmarshal([]byte(usersSiteInfo.Content), content)
+ content.GravatarBaseURL = "https://www.gravatar.com/avatar/"
+ data, _ := json.Marshal(content)
+ usersSiteInfo.Content = string(data)
+
+ _, err = x.ID(usersSiteInfo.ID).Cols("content").Update(usersSiteInfo)
+ if err != nil {
+ return fmt.Errorf("update site info failed: %w", err)
+ }
+ }
+ return nil
+}
+
+func addPrivilegeForInviteSomeoneToAnswer(x *xorm.Engine) error {
+ // add rank for invite to answer
+ powers := []*entity.Power{
+ {ID: 38, Name: "invite someone to answer", PowerType: permission.AnswerInviteSomeoneToAnswer, Description: "invite someone to answer"},
+ }
+ for _, power := range powers {
+ exist, err := x.Get(&entity.Power{PowerType: power.PowerType})
+ if err != nil {
+ return err
+ }
+ if exist {
+ _, err = x.ID(power.ID).Update(power)
+ } else {
+ _, err = x.Insert(power)
+ }
+ if err != nil {
+ return err
+ }
+ }
+ rolePowerRels := []*entity.RolePowerRel{
+ {RoleID: 2, PowerType: permission.AnswerInviteSomeoneToAnswer},
+ {RoleID: 3, PowerType: permission.AnswerInviteSomeoneToAnswer},
+ }
+ for _, rel := range rolePowerRels {
+ exist, err := x.Get(&entity.RolePowerRel{RoleID: rel.RoleID, PowerType: rel.PowerType})
+ if err != nil {
+ return err
+ }
+ if exist {
+ continue
+ }
+ _, err = x.Insert(rel)
+ if err != nil {
+ return err
+ }
+ }
+
+ defaultConfigTable := []*entity.Config{
+ {ID: 127, Key: "rank.answer.invite_someone_to_answer", Value: `1000`},
+ }
+ for _, c := range defaultConfigTable {
+ exist, err := x.Get(&entity.Config{ID: c.ID})
+ if err != nil {
+ return fmt.Errorf("get config failed: %w", err)
+ }
+ if exist {
+ if _, err = x.Update(c, &entity.Config{ID: c.ID}); err != nil {
+ return fmt.Errorf("update config failed: %w", err)
+ }
+ continue
+ }
+ if _, err = x.Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {
+ return fmt.Errorf("add config failed: %w", err)
+ }
+ }
+ return nil
+}
++
+ func updateQuestionCount(x *xorm.Engine) error {
+ //question answer count
+ answers := make([]entity.Answer, 0)
+ err := x.Find(&answers, &entity.Answer{Status: entity.AnswerStatusAvailable})
+ if err != nil {
+ return fmt.Errorf("get answers failed: %w", err)
+ }
+ questionAnswerCount := make(map[string]int)
+ for _, answer := range answers {
+ _, ok := questionAnswerCount[answer.QuestionID]
+ if !ok {
+ questionAnswerCount[answer.QuestionID] = 1
+ } else {
+ questionAnswerCount[answer.QuestionID]++
+ }
+ }
+ questionList := make([]entity.Question, 0)
+ err = x.Find(&questionList, &entity.Question{})
+ if err != nil {
+ return fmt.Errorf("get questions failed: %w", err)
+ }
+ for _, item := range questionList {
+ _, ok := questionAnswerCount[item.ID]
+ if ok {
+ item.AnswerCount = questionAnswerCount[item.ID]
+ if _, err = x.Update(item, &entity.Question{ID: item.ID}); err != nil {
+ log.Errorf("update %+v config failed: %s", item, err)
+ return fmt.Errorf("update question failed: %w", err)
+ }
+ }
+ }
+
+ return nil
+ }
+
+ // updateTagCount update tag count
+ func updateTagCount(x *xorm.Engine) error {
+ tagRelList := make([]entity.TagRel, 0)
+ err := x.Find(&tagRelList, &entity.TagRel{})
+ if err != nil {
+ return fmt.Errorf("get tag rel failed: %w", err)
+ }
+ questionIDs := make([]string, 0)
+ questionsAvailableMap := make(map[string]bool)
+ questionsHideMap := make(map[string]bool)
+ for _, item := range tagRelList {
+ questionIDs = append(questionIDs, item.ObjectID)
+ questionsAvailableMap[item.ObjectID] = false
+ questionsHideMap[item.ObjectID] = false
+ }
+ questionList := make([]entity.Question, 0)
+ err = x.In("id", questionIDs).In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).Find(&questionList, &entity.Question{})
+ if err != nil {
+ return fmt.Errorf("get questions failed: %w", err)
+ }
+ for _, question := range questionList {
+ _, ok := questionsAvailableMap[question.ID]
+ if ok {
+ questionsAvailableMap[question.ID] = true
+ if question.Show == entity.QuestionHide {
+ questionsHideMap[question.ID] = true
+ }
+ }
+ }
+
+ for id, ok := range questionsHideMap {
+ if ok {
+ if _, err = x.Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusHide}, &entity.TagRel{ObjectID: id}); err != nil {
+ log.Errorf("update %+v config failed: %s", id, err)
+ }
+ }
+ }
+
+ for id, ok := range questionsAvailableMap {
+ if !ok {
+ if _, err = x.Cols("status").Update(&entity.TagRel{Status: entity.TagRelStatusDeleted}, &entity.TagRel{ObjectID: id}); err != nil {
+ log.Errorf("update %+v config failed: %s", id, err)
+ }
+ }
+ }
+
+ //select tag count
+ newTagRelList := make([]entity.TagRel, 0)
+ err = x.Find(&newTagRelList, &entity.TagRel{Status: entity.TagRelStatusAvailable})
+ if err != nil {
+ return fmt.Errorf("get tag rel failed: %w", err)
+ }
+ tagCountMap := make(map[string]int)
+ for _, v := range newTagRelList {
+ _, ok := tagCountMap[v.TagID]
+ if !ok {
+ tagCountMap[v.TagID] = 1
+ } else {
+ tagCountMap[v.TagID]++
+ }
+ }
+ TagList := make([]entity.Tag, 0)
+ err = x.Find(&TagList, &entity.Tag{})
+ if err != nil {
+ return fmt.Errorf("get tag failed: %w", err)
+ }
+ for _, tag := range TagList {
+ _, ok := tagCountMap[tag.ID]
+ if ok {
+ tag.QuestionCount = tagCountMap[tag.ID]
+ if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
+ log.Errorf("update %+v tag failed: %s", tag.ID, err)
+ return fmt.Errorf("update tag failed: %w", err)
+ }
+ } else {
+ tag.QuestionCount = 0
+ if _, err = x.Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
+ log.Errorf("update %+v tag failed: %s", tag.ID, err)
+ return fmt.Errorf("update tag failed: %w", err)
+ }
+ }
+ }
+ return nil
+ }
+
+ // updateUserQuestionCount update user question count
+ func updateUserQuestionCount(x *xorm.Engine) error {
+ questionList := make([]entity.Question, 0)
+ err := x.In("status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).Find(&questionList, &entity.Question{})
+ if err != nil {
+ return fmt.Errorf("get question failed: %w", err)
+ }
+ userQuestionCountMap := make(map[string]int)
+ for _, question := range questionList {
+ _, ok := userQuestionCountMap[question.UserID]
+ if !ok {
+ userQuestionCountMap[question.UserID] = 1
+ } else {
+ userQuestionCountMap[question.UserID]++
+ }
+ }
+ userList := make([]entity.User, 0)
+ err = x.Find(&userList, &entity.User{})
+ if err != nil {
+ return fmt.Errorf("get user failed: %w", err)
+ }
+ for _, user := range userList {
+ _, ok := userQuestionCountMap[user.ID]
+ if ok {
+ user.QuestionCount = userQuestionCountMap[user.ID]
+ if _, err = x.Cols("question_count").Update(user, &entity.User{ID: user.ID}); err != nil {
+ log.Errorf("update %+v user failed: %s", user.ID, err)
+ return fmt.Errorf("update user failed: %w", err)
+ }
+ } else {
+ user.QuestionCount = 0
+ if _, err = x.Cols("question_count").Update(user, &entity.User{ID: user.ID}); err != nil {
+ log.Errorf("update %+v user failed: %s", user.ID, err)
+ return fmt.Errorf("update user failed: %w", err)
+ }
+ }
+ }
+ return nil
+ }
+
+ // updateUserAnswerCount update user answer count
+ func updateUserAnswerCount(x *xorm.Engine) error {
+ answers := make([]entity.Answer, 0)
+ err := x.Find(&answers, &entity.Answer{Status: entity.AnswerStatusAvailable})
+ if err != nil {
+ return fmt.Errorf("get answers failed: %w", err)
+ }
+ userAnswerCount := make(map[string]int)
+ for _, answer := range answers {
+ _, ok := userAnswerCount[answer.UserID]
+ if !ok {
+ userAnswerCount[answer.UserID] = 1
+ } else {
+ userAnswerCount[answer.UserID]++
+ }
+ }
+ userList := make([]entity.User, 0)
+ err = x.Find(&userList, &entity.User{})
+ if err != nil {
+ return fmt.Errorf("get user failed: %w", err)
+ }
+ for _, user := range userList {
+ _, ok := userAnswerCount[user.ID]
+ if ok {
+ user.AnswerCount = userAnswerCount[user.ID]
+ if _, err = x.Cols("answer_count").Update(user, &entity.User{ID: user.ID}); err != nil {
+ log.Errorf("update %+v user failed: %s", user.ID, err)
+ return fmt.Errorf("update user failed: %w", err)
+ }
+ } else {
+ user.AnswerCount = 0
+ if _, err = x.Cols("answer_count").Update(user, &entity.User{ID: user.ID}); err != nil {
+ log.Errorf("update %+v user failed: %s", user.ID, err)
+ return fmt.Errorf("update user failed: %w", err)
+ }
+ }
+ }
+ return nil
+ }
+
+ func inviteAnswer(x *xorm.Engine) error {
+ type Question struct {
+ ID string `xorm:"not null pk BIGINT(20) id"`
+ CreatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP created_at"`
+ UpdatedAt time.Time `xorm:"updated_at TIMESTAMP"`
+ UserID string `xorm:"not null default 0 BIGINT(20) INDEX user_id"`
+ InviteUserID string `xorm:"TEXT invite_user_id"`
+ LastEditUserID string `xorm:"not null default 0 BIGINT(20) last_edit_user_id"`
+ Title string `xorm:"not null default '' VARCHAR(150) title"`
+ OriginalText string `xorm:"not null MEDIUMTEXT original_text"`
+ ParsedText string `xorm:"not null MEDIUMTEXT parsed_text"`
+ Status int `xorm:"not null default 1 INT(11) status"`
+ Pin int `xorm:"not null default 1 INT(11) pin"`
+ Show int `xorm:"not null default 1 INT(11) show"`
+ ViewCount int `xorm:"not null default 0 INT(11) view_count"`
+ UniqueViewCount int `xorm:"not null default 0 INT(11) unique_view_count"`
+ VoteCount int `xorm:"not null default 0 INT(11) vote_count"`
+ AnswerCount int `xorm:"not null default 0 INT(11) answer_count"`
+ CollectionCount int `xorm:"not null default 0 INT(11) collection_count"`
+ FollowCount int `xorm:"not null default 0 INT(11) follow_count"`
+ AcceptedAnswerID string `xorm:"not null default 0 BIGINT(20) accepted_answer_id"`
+ LastAnswerID string `xorm:"not null default 0 BIGINT(20) last_answer_id"`
+ PostUpdateTime time.Time `xorm:"post_update_time TIMESTAMP"`
+ RevisionID string `xorm:"not null default 0 BIGINT(20) revision_id"`
+ }
+ err := x.Sync(new(Question))
+ if err != nil {
+ return err
+ }
+
+ return nil
+ }
diff --cc internal/service/question_service.go
index c98d1885,5ec5f99d..a705265f
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@@ -404,18 -442,39 +425,41 @@@ func (qs *QuestionService) RemoveQuesti
return err
}
- // user add question count
- err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, -1)
+ userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, questionInfo.UserID)
+ if err != nil {
+ log.Error("user GetUserQuestionCount error", err.Error())
+ } else {
+ err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, userQuestionCount)
+ if err != nil {
+ log.Error("user IncreaseQuestionCount error", err.Error())
+ }
+ }
+
+ //tag count
+ tagIDs := make([]string, 0)
+ Tags, tagerr := qs.tagCommon.GetObjectEntityTag(ctx, req.ID)
+ if tagerr != nil {
+ log.Error("GetObjectEntityTag error", tagerr)
+ return nil
+ }
+ for _, v := range Tags {
+ tagIDs = append(tagIDs, v.ID)
+ }
+ err = qs.tagCommon.RemoveTagRelListByObjectID(ctx, req.ID)
+ if err != nil {
+ log.Error("RemoveTagRelListByObjectID error", err.Error())
+ }
+ err = qs.tagCommon.RefreshTagQuestionCount(ctx, tagIDs)
if err != nil {
- log.Error("user IncreaseQuestionCount error", err.Error())
+ log.Error("efreshTagQuestionCount error", err.Error())
}
- err = qs.answerActivityService.DeleteQuestion(ctx, questionInfo.ID, questionInfo.CreatedAt, questionInfo.VoteCount)
- if err != nil {
- log.Errorf("user DeleteQuestion rank rollback error %s", err.Error())
- }
+ // #2372 In order to simplify the process and complexity, as well as to consider if it is in-house,
+ // facing the problem of recovery.
+ // err = qs.answerActivityService.DeleteQuestion(ctx, questionInfo.ID, questionInfo.CreatedAt, questionInfo.VoteCount)
+ // if err != nil {
+ // log.Errorf("user DeleteQuestion rank rollback error %s", err.Error())
+ // }
activity_queue.AddActivity(&schema.ActivityMsg{
UserID: req.UserID,
ObjectID: questionInfo.ID,