answer/internal/service/vote_service.go

205 lines
6.1 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package service
import (
"context"
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/activity_type"
"github.com/answerdev/answer/internal/service/comment_common"
"github.com/answerdev/answer/internal/service/config"
"github.com/answerdev/answer/internal/service/object_info"
2023-03-13 11:28:35 +08:00
"github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/pkg/obj"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/log"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/schema"
answercommon "github.com/answerdev/answer/internal/service/answer_common"
questioncommon "github.com/answerdev/answer/internal/service/question_common"
"github.com/answerdev/answer/internal/service/unique"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/errors"
)
// VoteRepo activity repository
type VoteRepo interface {
VoteUp(ctx context.Context, objectID string, userID, objectUserID string) (resp *schema.VoteResp, err error)
VoteDown(ctx context.Context, objectID string, userID, objectUserID string) (resp *schema.VoteResp, err error)
VoteUpCancel(ctx context.Context, objectID string, userID, objectUserID string) (resp *schema.VoteResp, err error)
VoteDownCancel(ctx context.Context, objectID string, userID, objectUserID string) (resp *schema.VoteResp, err error)
GetVoteResultByObjectId(ctx context.Context, objectID string) (resp *schema.VoteResp, err error)
ListUserVotes(ctx context.Context, userID string, req schema.GetVoteWithPageReq, activityTypes []int) (voteList []entity.Activity, total int64, err error)
}
// VoteService user service
type VoteService struct {
voteRepo VoteRepo
UniqueIDRepo unique.UniqueIDRepo
configService *config.ConfigService
2022-09-27 17:59:05 +08:00
questionRepo questioncommon.QuestionRepo
answerRepo answercommon.AnswerRepo
commentCommonRepo comment_common.CommentCommonRepo
objectService *object_info.ObjService
}
func NewVoteService(
VoteRepo VoteRepo,
uniqueIDRepo unique.UniqueIDRepo,
configService *config.ConfigService,
2022-09-27 17:59:05 +08:00
questionRepo questioncommon.QuestionRepo,
answerRepo answercommon.AnswerRepo,
commentCommonRepo comment_common.CommentCommonRepo,
objectService *object_info.ObjService,
) *VoteService {
return &VoteService{
voteRepo: VoteRepo,
UniqueIDRepo: uniqueIDRepo,
configService: configService,
2022-09-27 17:59:05 +08:00
questionRepo: questionRepo,
answerRepo: answerRepo,
commentCommonRepo: commentCommonRepo,
objectService: objectService,
}
}
// VoteUp vote up
func (vs *VoteService) VoteUp(ctx context.Context, dto *schema.VoteDTO) (voteResp *schema.VoteResp, err error) {
2022-09-27 17:59:05 +08:00
voteResp = &schema.VoteResp{}
var objectUserID string
objectUserID, err = vs.GetObjectUserID(ctx, dto.ObjectID)
2022-09-27 17:59:05 +08:00
if err != nil {
return
}
// check user is voting self or not
if objectUserID == dto.UserID {
err = errors.BadRequest(reason.DisallowVoteYourSelf)
return
}
if dto.IsCancel {
return vs.voteRepo.VoteUpCancel(ctx, dto.ObjectID, dto.UserID, objectUserID)
2022-09-27 17:59:05 +08:00
} else {
return vs.voteRepo.VoteUp(ctx, dto.ObjectID, dto.UserID, objectUserID)
2022-09-27 17:59:05 +08:00
}
}
// VoteDown vote down
func (vs *VoteService) VoteDown(ctx context.Context, dto *schema.VoteDTO) (voteResp *schema.VoteResp, err error) {
2022-09-27 17:59:05 +08:00
voteResp = &schema.VoteResp{}
var objectUserID string
objectUserID, err = vs.GetObjectUserID(ctx, dto.ObjectID)
2022-09-27 17:59:05 +08:00
if err != nil {
return
}
// check user is voting self or not
if objectUserID == dto.UserID {
err = errors.BadRequest(reason.DisallowVoteYourSelf)
return
}
if dto.IsCancel {
return vs.voteRepo.VoteDownCancel(ctx, dto.ObjectID, dto.UserID, objectUserID)
2022-09-27 17:59:05 +08:00
} else {
return vs.voteRepo.VoteDown(ctx, dto.ObjectID, dto.UserID, objectUserID)
2022-09-27 17:59:05 +08:00
}
}
func (vs *VoteService) GetObjectUserID(ctx context.Context, objectID string) (userID string, err error) {
2022-09-27 17:59:05 +08:00
var objectKey string
objectKey, err = obj.GetObjectTypeStrByObjectID(objectID)
if err != nil {
err = nil
return
}
switch objectKey {
case "question":
object, has, e := vs.questionRepo.GetQuestion(ctx, objectID)
if e != nil || !has {
err = errors.BadRequest(reason.QuestionNotFound).WithError(e).WithStack()
return
}
userID = object.UserID
case "answer":
object, has, e := vs.answerRepo.GetAnswer(ctx, objectID)
if e != nil || !has {
err = errors.BadRequest(reason.AnswerNotFound).WithError(e).WithStack()
return
}
userID = object.UserID
case "comment":
object, has, e := vs.commentCommonRepo.GetComment(ctx, objectID)
if e != nil || !has {
err = errors.BadRequest(reason.CommentNotFound).WithError(e).WithStack()
return
}
userID = object.UserID
default:
err = errors.BadRequest(reason.DisallowVote).WithError(err).WithStack()
return
}
return
}
// ListUserVotes list user's votes
func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithPageReq) (model *pager.PageModel, err error) {
var (
resp []schema.GetVoteWithPageResp
typeKeys = []string{
"question.vote_up",
"question.vote_down",
"answer.vote_up",
"answer.vote_down",
}
activityTypes []int
)
for _, typeKey := range typeKeys {
cfg, err := vs.configService.GetConfigByKey(ctx, typeKey)
2022-09-27 17:59:05 +08:00
if err != nil {
continue
}
activityTypes = append(activityTypes, cfg.GetIntValue())
2022-09-27 17:59:05 +08:00
}
voteList, total, err := vs.voteRepo.ListUserVotes(ctx, req.UserID, req, activityTypes)
if err != nil {
return
}
for _, voteInfo := range voteList {
var objInfo *schema.SimpleObjectInfo
objInfo, err = vs.objectService.GetInfo(ctx, voteInfo.ObjectID)
2022-09-27 17:59:05 +08:00
if err != nil {
log.Error(err)
continue
2022-09-27 17:59:05 +08:00
}
item := schema.GetVoteWithPageResp{
CreatedAt: voteInfo.CreatedAt.Unix(),
ObjectID: objInfo.ObjectID,
QuestionID: objInfo.QuestionID,
AnswerID: objInfo.AnswerID,
ObjectType: objInfo.ObjectType,
Title: objInfo.Title,
2023-03-13 11:28:35 +08:00
UrlTitle: htmltext.UrlTitle(objInfo.Title),
2022-09-27 17:59:05 +08:00
Content: objInfo.Content,
VoteType: activity_type.Format(voteInfo.ActivityType),
}
2023-05-19 15:58:56 +08:00
if objInfo.QuestionStatus == entity.QuestionStatusDeleted {
item.Title = "Deleted question"
}
2022-09-27 17:59:05 +08:00
resp = append(resp, item)
}
return pager.NewPageModel(total, resp), err
2022-09-27 17:59:05 +08:00
}