Skip to content

Commit

Permalink
fix: notification after poll ruling
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 29, 2023
1 parent 7668e68 commit b51258c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/logic/notification/poll_finished_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ type PollFinishedNotificationMessage struct {
// TODO: 加入审核员投票的意见标签?
}

func (s *sNotification) PollFinishedNotification(ctx context.Context, poll *model.PollElement, pollLogs []entity.PollLog) error {
func (s *sNotification) PollFinishedNotification(
ctx context.Context,
poll *model.PollElement,
pollLogs []entity.PollLog,
) error {
usersThatShouldDoNotification, err := s.GetUsersShouldDoNotification(ctx, PollFinishedNotificationSettingField)
if err != nil {
return err
Expand Down
67 changes: 64 additions & 3 deletions internal/logic/poll/ruling.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"math"

"github.com/gogf/gf/v2/os/gtime"

"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
Expand All @@ -12,6 +14,7 @@ import (
"github.com/hitokoto-osc/reviewer/internal/model/do"
"github.com/hitokoto-osc/reviewer/internal/model/entity"
"github.com/hitokoto-osc/reviewer/internal/service"
"github.com/hitokoto-osc/reviewer/utility/time"

"github.com/hitokoto-osc/reviewer/internal/consts"
)
Expand Down Expand Up @@ -107,9 +110,9 @@ func (s *sPoll) DoRuling(ctx context.Context, poll *entity.Poll, target consts.P
}
}
// 更新投票状态
_, e = dao.Poll.Ctx(ctx).TX(tx).Where(dao.Poll.Columns().Id, poll.Id).Update(do.Poll{
Status: int(target),
})
poll.UpdatedAt = gtime.Now()
poll.Status = int(target)
_, e = dao.Poll.Ctx(ctx).TX(tx).Where(dao.Poll.Columns().Id, poll.Id).Update(poll)
if e != nil {
return gerror.Wrapf(e, "更新投票 %d 状态失败", poll.Id)
}
Expand All @@ -122,6 +125,33 @@ func (s *sPoll) DoRuling(ctx context.Context, poll *entity.Poll, target consts.P
if e != nil {
return gerror.Wrapf(e, "新增操作日记失败")
}

pollElement := &model.PollElement{
ID: uint(poll.Id),
SentenceUUID: poll.SentenceUuid,
Sentence: &model.HitokotoV1Schema{
ID: uint(pending.Id),
UUID: pending.Uuid,
Hitokoto: pending.Hitokoto,
Type: consts.HitokotoType(pending.Type),
From: pending.From,
FromWho: pending.FromWho,
Creator: pending.Creator,
CreatorUID: uint(pending.CreatorUid),
Reviewer: 0,
Status: consts.HitokotoStatusPending,
PollStatus: consts.PollStatus(poll.Status),
CreatedAt: pending.CreatedAt,
},
Status: consts.PollStatus(poll.Status),
Approve: poll.Accept,
Reject: poll.Reject,
NeedModify: poll.NeedEdited,
NeedCommonUserPoll: poll.NeedUserPoll,
CreatedAt: (*time.Time)(poll.CreatedAt),
UpdatedAt: (*time.Time)(poll.UpdatedAt),
}

// 赋予用户积分
for _, pollLog := range pollLogs {
var score int
Expand All @@ -143,6 +173,12 @@ func (s *sPoll) DoRuling(ctx context.Context, poll *entity.Poll, target consts.P
}
service.Cache().ClearPollUserCache(ctx, uint(pollLog.UserId))
}

// DoReviewedNotification
if err = doNotificationAfterPollRuling(ctx, pollElement, pollLogs); err != nil {
return gerror.Wrap(err, "发送投票结果通知失败")
}

service.Cache().ClearPollListCache(ctx)
// 提交句子到搜索引擎
e = service.Search().AddSentenceToSearch(ctx, pending)
Expand All @@ -152,3 +188,28 @@ func (s *sPoll) DoRuling(ctx context.Context, poll *entity.Poll, target consts.P
return nil
})
}

func doNotificationAfterPollRuling(ctx context.Context, pollElement *model.PollElement, pollLogs []entity.PollLog) error {
if err := service.Notification().PollFinishedNotification(
ctx,
pollElement,
pollLogs,
); err != nil {
return gerror.Wrap(err, "发送投票结果通知失败")
}
if pollElement.Status != consts.PollStatusNeedModify {
if err := doReviewedNotification(ctx, pollElement); err != nil {
return gerror.Wrap(err, "发送审核结果通知失败")
}
}
return nil
}

func doReviewedNotification(ctx context.Context, pollElement *model.PollElement) error {
return service.Notification().SentenceReviewedNotification(
ctx,
pollElement,
consts.PollRulingUserID,
consts.PollRulingUsername,
)
}

0 comments on commit b51258c

Please sign in to comment.