|
|
@@ -2,6 +2,7 @@ package logic
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "slowwild/internal/constants"
|
|
|
"slowwild/internal/errorx"
|
|
|
"slowwild/internal/model"
|
|
|
"slowwild/internal/svc"
|
|
|
@@ -30,12 +31,18 @@ func NewPostReplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostRep
|
|
|
|
|
|
// 发布回复
|
|
|
func (l *PostReplyLogic) PostReply(in *slowwildserver.PostReplyReq) (*slowwildserver.PostReplyRsp, error) {
|
|
|
- if in.UserId <= 0 || in.PostId <= 0 || in.CommentId <= 0 || in.Content == "" {
|
|
|
+ // 从context中获取userId
|
|
|
+ userId, err := cast.ToInt64E(l.ctx.Value(constants.UserIDKey))
|
|
|
+ if err != nil {
|
|
|
+ return nil, errorx.ErrInvalidParam
|
|
|
+ }
|
|
|
+
|
|
|
+ if in.PostId <= 0 || in.CommentId <= 0 || in.Content == "" {
|
|
|
return nil, errorx.ErrInvalidParam
|
|
|
}
|
|
|
|
|
|
// 开启事务
|
|
|
- err := l.svcCtx.UserModel.Transaction(l.ctx, func(tx *gorm.DB) error {
|
|
|
+ err = l.svcCtx.UserModel.Transaction(l.ctx, func(tx *gorm.DB) error {
|
|
|
postModel := model.NewPostModel(tx, l.svcCtx.Redis)
|
|
|
commentModel := model.NewCommentModel(tx, l.svcCtx.Redis)
|
|
|
replyModel := model.NewReplyModel(tx, l.svcCtx.Redis)
|
|
|
@@ -54,7 +61,7 @@ func (l *PostReplyLogic) PostReply(in *slowwildserver.PostReplyReq) (*slowwildse
|
|
|
ModifiedOn: time.Now().Unix(),
|
|
|
},
|
|
|
PostId: in.PostId,
|
|
|
- UserId: in.UserId,
|
|
|
+ UserId: userId,
|
|
|
CommentId: in.CommentId,
|
|
|
Content: in.Content,
|
|
|
Image: in.Image,
|
|
|
@@ -85,8 +92,8 @@ func (l *PostReplyLogic) PostReply(in *slowwildserver.PostReplyReq) (*slowwildse
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- if parentReply.UserId != in.UserId {
|
|
|
- err = messageModel.SendNotification(l.ctx, in.UserId, parentReply.UserId, 3,
|
|
|
+ if parentReply.UserId != userId {
|
|
|
+ err = messageModel.SendNotification(l.ctx, userId, parentReply.UserId, 3,
|
|
|
"回复通知",
|
|
|
"有人回复了你的回复",
|
|
|
in.PostId, in.CommentId, reply.ID)
|
|
|
@@ -94,8 +101,8 @@ func (l *PostReplyLogic) PostReply(in *slowwildserver.PostReplyReq) (*slowwildse
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
- } else if parentComment.UserId != in.UserId { // 如果是回复评论,需要通知评论作者
|
|
|
- err = messageModel.SendNotification(l.ctx, in.UserId, parentComment.UserId, 3,
|
|
|
+ } else if parentComment.UserId != userId { // 如果是回复评论,需要通知评论作者
|
|
|
+ err = messageModel.SendNotification(l.ctx, userId, parentComment.UserId, 3,
|
|
|
"回复通知",
|
|
|
"有人回复了你的评论",
|
|
|
in.PostId, in.CommentId, reply.ID)
|
|
|
@@ -105,8 +112,8 @@ func (l *PostReplyLogic) PostReply(in *slowwildserver.PostReplyReq) (*slowwildse
|
|
|
}
|
|
|
|
|
|
// 如果有@用户,给被@的用户发送消息通知
|
|
|
- if in.AtUserId > 0 && in.AtUserId != in.UserId {
|
|
|
- err = messageModel.SendNotification(l.ctx, in.UserId, in.AtUserId, 4,
|
|
|
+ if in.AtUserId > 0 && in.AtUserId != userId {
|
|
|
+ err = messageModel.SendNotification(l.ctx, userId, in.AtUserId, 4,
|
|
|
"@通知",
|
|
|
"有人在回复中@了你",
|
|
|
in.PostId, in.CommentId, reply.ID)
|