in internal/service/comment/comment_service.go [123:205]
func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddCommentReq) (
resp *schema.GetCommentResp, err error) {
comment := &entity.Comment{}
_ = copier.Copy(comment, req)
comment.Status = entity.CommentStatusAvailable
objInfo, err := cs.objectInfoService.GetInfo(ctx, req.ObjectID)
if err != nil {
return nil, err
}
if objInfo.IsDeleted() {
return nil, errors.BadRequest(reason.NewObjectAlreadyDeleted)
}
objInfo.ObjectID = uid.DeShortID(objInfo.ObjectID)
objInfo.QuestionID = uid.DeShortID(objInfo.QuestionID)
objInfo.AnswerID = uid.DeShortID(objInfo.AnswerID)
if objInfo.ObjectType == constant.QuestionObjectType || objInfo.ObjectType == constant.AnswerObjectType {
comment.QuestionID = objInfo.QuestionID
}
if len(req.ReplyCommentID) > 0 {
replyComment, exist, err := cs.commentCommonRepo.GetComment(ctx, req.ReplyCommentID)
if err != nil {
return nil, err
}
if !exist {
return nil, errors.BadRequest(reason.CommentNotFound)
}
comment.SetReplyUserID(replyComment.UserID)
comment.SetReplyCommentID(replyComment.ID)
} else {
comment.SetReplyUserID("")
comment.SetReplyCommentID("")
}
err = cs.commentRepo.AddComment(ctx, comment)
if err != nil {
return nil, err
}
resp = &schema.GetCommentResp{}
resp.SetFromComment(comment)
resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
time.Now(), req.CanEdit, req.CanDelete)
commentResp, err := cs.addCommentNotification(ctx, req, resp, comment, objInfo)
if err != nil {
return commentResp, err
}
// get user info
userInfo, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, resp.UserID)
if err != nil {
return nil, err
}
if exist {
resp.Username = userInfo.Username
resp.UserDisplayName = userInfo.DisplayName
resp.UserAvatar = userInfo.Avatar
resp.UserStatus = userInfo.Status
}
activityMsg := &schema.ActivityMsg{
UserID: comment.UserID,
ObjectID: comment.ID,
OriginalObjectID: req.ObjectID,
ActivityTypeKey: constant.ActQuestionCommented,
}
var event *schema.EventMsg
switch objInfo.ObjectType {
case constant.QuestionObjectType:
activityMsg.ActivityTypeKey = constant.ActQuestionCommented
event = schema.NewEvent(constant.EventCommentCreate, req.UserID).TID(comment.ID).
CID(comment.ID, comment.UserID).QID(objInfo.QuestionID, objInfo.ObjectCreatorUserID)
case constant.AnswerObjectType:
activityMsg.ActivityTypeKey = constant.ActAnswerCommented
event = schema.NewEvent(constant.EventCommentCreate, req.UserID).TID(comment.ID).
CID(comment.ID, comment.UserID).AID(objInfo.AnswerID, objInfo.ObjectCreatorUserID)
}
cs.activityQueueService.Send(ctx, activityMsg)
cs.eventQueueService.Send(ctx, event)
return resp, nil
}