Skip to content

Commit

Permalink
refactor: use custom func to parse msg and validate, intended to pars…
Browse files Browse the repository at this point in the history
…e enum, carbon directly
  • Loading branch information
greenhat616 committed Oct 3, 2023
1 parent 25d3033 commit 4cd7f9a
Show file tree
Hide file tree
Showing 22 changed files with 626 additions and 264 deletions.
18 changes: 18 additions & 0 deletions consts/hitokoto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package consts

type HitokotoType string

const (
HitokotoTypeAnime HitokotoType = "a" // Anime - 动画
HitokotoTypeComic HitokotoType = "b" // Comic – 漫画
HitokotoTypeGame HitokotoType = "c" // Game – 游戏
HitokotoTypeLiterature HitokotoType = "d" // Literature – 文学
HitokotoTypeOriginal HitokotoType = "e" // Original – 原创
HitokotoTypeInternet HitokotoType = "f" // Internet – 来自网络
HitokotoTypeOther HitokotoType = "g" // Other – 其他
HitokotoTypeVideo HitokotoType = "h" // Video – 影视
HitokotoTypePoetry HitokotoType = "i" // Poetry – 古诗词
HitokotoTypeNetEase HitokotoType = "j" // NetEase – 网易云音乐
HitokotoTypePhilosophy HitokotoType = "k" // Philosophy – 哲学
HitokotoTypeJoke HitokotoType = "l" // Joke - 抖机灵
)
25 changes: 25 additions & 0 deletions consts/poll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package consts

type PollMethod int

const (
PollMethodApprove PollMethod = iota + 1 // 赞同
PollMethodReject // 驳回
PollMethodNeedModify // 需要修改
PollMethodNeedCommonUserPoll // 需要普通用户投票
)

type PollStatus int

const (
PollStatusUnknown PollStatus = -1 // 未知,比如投票不存在
PollStatusNotOpen PollStatus = 0 // 未开放投票
PollStatusOpen PollStatus = 1 // 投票正常开放
PollStatusProcessing PollStatus = 2 // 处理中,停止投票
PollStatusSuspended PollStatus = 100 // 暂停投票
PollStatusClosed PollStatus = 101 // 关闭投票
PollStatusOpenForCommonUser PollStatus = 102 // 开放给普通用户投票
PollStatusApproved PollStatus = 200 // 赞同
PollStatusRejected PollStatus = 201 // 驳回
PollStatusNeedModify PollStatus = 202 // 需要修改
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package notification
package v1

import (
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/golang-module/carbon/v2"
"github.com/hitokoto-osc/notification-worker/consumers/notification/v1/internal/model"
"github.com/hitokoto-osc/notification-worker/consumers/provider"
"github.com/hitokoto-osc/notification-worker/django"
"github.com/hitokoto-osc/notification-worker/logging"
"github.com/hitokoto-osc/notification-worker/mail"
"github.com/hitokoto-osc/notification-worker/mail/mailer"
"github.com/hitokoto-osc/notification-worker/rabbitmq"
"github.com/hitokoto-osc/notification-worker/utils"
"github.com/hitokoto-osc/notification-worker/utils/formatter"
"github.com/hitokoto-osc/notification-worker/utils/validator"
amqp "github.com/rabbitmq/amqp091-go"
"go.uber.org/zap"
"strconv"
)

func init() {
Expand Down Expand Up @@ -47,29 +46,21 @@ func HitokotoAppendedEvent() *rabbitmq.ConsumerRegisterOptions {
logger := logging.WithContext(ctx)
defer logger.Sync()
logger.Debug("[hitokoto_appended] 收到消息: %v \n", zap.ByteString("body", delivery.Body))
message := hitokotoAppendedMessage{}
err := json.Unmarshal(delivery.Body, &message)
message, err := validator.UnmarshalV[model.HitokotoAppendedMessage](ctx, delivery.Body)
if err != nil {
return err
}
// 转换成时间戳
ts, err := strconv.ParseInt(message.CreatedAt, 10, 64)
if err != nil {
return err
return errors.Wrap(err, "解析消息失败")
}
html, err := django.RenderTemplate("email/hitokoto_appended", django.Context{
"username": message.Creator,
"created_at": carbon.CreateFromTimestamp(ts).Format("Y-m-d H:i:s"),
"created_at": message.CreatedAt.Format("Y-m-d H:i:s"),
"hitokoto": message.Hitokoto,
"from": message.From,
"from_who": message.FromWho,
"type": utils.FormatHitokotoType(message.Type),
"now": carbon.Now().Format("Y 年 n 月 j 日"),
"type": formatter.FormatHitokotoType(message.Type),
})
if err != nil {
return errors.Wrap(err, "渲染模板失败")
}

err = mail.SendSingle(ctx, &mailer.Mailer{
Type: mailer.TypeNormal,
Mail: mailer.Mail{
Expand All @@ -82,14 +73,3 @@ func HitokotoAppendedEvent() *rabbitmq.ConsumerRegisterOptions {
},
}
}

type hitokotoAppendedMessage struct {
To string `json:"to"` // 对象邮件地址
UUID string `json:"uuid"` // 句子 UUID
Hitokoto string `json:"hitokoto"` // 句子
From string `json:"from"` // 来源
Type string `json:"type"` // 分类
FromWho string `json:"from_who"` // 作者
Creator string `json:"creator"` // 提交者名称
CreatedAt string `json:"created_at"` // 提交时间
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package notification
package v1

import (
"github.com/hitokoto-osc/notification-worker/consumers/provider"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package notification
package v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package notification
package v1

import (
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/golang-module/carbon/v2"
"github.com/hitokoto-osc/notification-worker/consumers/notification/v1/internal/model"
"github.com/hitokoto-osc/notification-worker/consumers/provider"
"github.com/hitokoto-osc/notification-worker/django"
"github.com/hitokoto-osc/notification-worker/logging"
"github.com/hitokoto-osc/notification-worker/mail"
"github.com/hitokoto-osc/notification-worker/mail/mailer"
"github.com/hitokoto-osc/notification-worker/rabbitmq"
"github.com/hitokoto-osc/notification-worker/utils"
"github.com/hitokoto-osc/notification-worker/utils/formatter"
"github.com/hitokoto-osc/notification-worker/utils/validator"
amqp "github.com/rabbitmq/amqp091-go"
"go.uber.org/zap"
"strconv"
)

func init() {
Expand Down Expand Up @@ -47,33 +46,18 @@ func HitokotoMovedEvent() *rabbitmq.ConsumerRegisterOptions {
logger := logging.WithContext(ctx)
defer logger.Sync()
logger.Debug("收到消息:", zap.ByteString("body", delivery.Body))
message := new(HitokotoMovedMessage)
err := json.Unmarshal(delivery.Body, &message)
message, err := validator.UnmarshalV[model.HitokotoMovedMessage](ctx, delivery.Body)
if err != nil {
return errors.Wrap(err, "无法解析消息体")
return errors.Wrap(err, "解析消息失败")
}
// 转换成时间戳
ts, err := strconv.ParseInt(message.CreatedAt, 10, 64)
if err != nil {
return errors.Wrap(err, "无法解析时间戳")
}

// 处理数据
var operate string
if message.Operate == 200 { // 只可能通过或者驳回,目前。
operate = "通过"
} else {
operate = "驳回"
}

html, err := django.RenderTemplate("email/hitokoto_reviewed", django.Context{
"username": message.Creator,
"created_at": carbon.CreateFromTimestamp(ts).Format("Y-m-d H:i:s"),
"created_at": message.CreatedAt.Format("Y-m-d H:i:s"),
"hitokoto": message.Hitokoto,
"from_who": message.FromWho,
"from": message.From,
"type": utils.FormatHitokotoType(message.Type),
"operate": operate,
"type": formatter.FormatHitokotoType(message.Type),
"operate": formatter.FormatPollStatus(message.Operate),
"operator_username": message.OperatorUsername,
"operator_uid": message.OperatorUID,
"operated_at": message.OperatedAt,
Expand All @@ -93,11 +77,3 @@ func HitokotoMovedEvent() *rabbitmq.ConsumerRegisterOptions {
},
}
}

type HitokotoMovedMessage struct {
hitokotoAppendedMessage
OperatedAt string `json:"operated_at"` // 操作时间
OperatorUsername string `json:"operator_username"` // 操作者用户名
OperatorUID string `json:"operator_uid"` // 操作者 UID
Operate int `json:"operate"` // 操作
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package notification
package v1

import (
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/golang-module/carbon/v2"
"github.com/hitokoto-osc/notification-worker/consumers/notification/v1/internal/model"
"github.com/hitokoto-osc/notification-worker/consumers/provider"
"github.com/hitokoto-osc/notification-worker/django"
"github.com/hitokoto-osc/notification-worker/logging"
"github.com/hitokoto-osc/notification-worker/mail"
"github.com/hitokoto-osc/notification-worker/mail/mailer"
"github.com/hitokoto-osc/notification-worker/rabbitmq"
"github.com/hitokoto-osc/notification-worker/utils"
"github.com/hitokoto-osc/notification-worker/utils/formatter"
"github.com/hitokoto-osc/notification-worker/utils/validator"
amqp "github.com/rabbitmq/amqp091-go"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -46,22 +46,19 @@ func HitokotoPollCreatedEvent() *rabbitmq.ConsumerRegisterOptions {
logger := logging.WithContext(ctx)
defer logger.Sync()
logger.Debug("[hitokoto_poll_created]收到消息:", zap.ByteString("body", delivery.Body))
message := hitokotoPollCreatedMessage{}
err := json.Unmarshal(delivery.Body, &message)
message, err := validator.UnmarshalV[model.PollCreatedMessage](ctx, delivery.Body)
if err != nil {
return err
return errors.Wrap(err, "解析消息失败")
}

html, err := django.RenderTemplate("email/poll_created", django.Context{
"username": message.UserName,
"created_at": carbon.Parse(message.CreatedAt).Format("Y-m-d H:i:s"),
"poll_id": message.Id,
"username": message.Username,
"created_at": message.CreatedAt.Format("Y-m-d H:i:s"),
"poll_id": message.ID,
"hitokoto": message.Hitokoto,
"from": message.From,
"from_who": message.FromWho,
"type": utils.FormatHitokotoType(message.Type),
"type": formatter.FormatHitokotoType(message.Type),
"creator": message.Creator,
"now": carbon.Now().Format("Y 年 n 月 j 日"),
})
if err != nil {
return errors.Wrap(err, "无法渲染模板")
Expand All @@ -78,10 +75,3 @@ func HitokotoPollCreatedEvent() *rabbitmq.ConsumerRegisterOptions {
},
}
}

type hitokotoPollCreatedMessage struct {
hitokotoAppendedMessage
UserName string `json:"user_name"` // 收信人
Id int `json:"id"` // 投票标识
CreatedAt string `json:"created_at"` // 这里是投票创建时间, ISO 时间
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package notification
package v1

import (
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/golang-module/carbon/v2"
"github.com/hitokoto-osc/notification-worker/consumers/notification/v1/internal/model"
"github.com/hitokoto-osc/notification-worker/consumers/provider"
"github.com/hitokoto-osc/notification-worker/django"
"github.com/hitokoto-osc/notification-worker/logging"
"github.com/hitokoto-osc/notification-worker/mail"
"github.com/hitokoto-osc/notification-worker/mail/mailer"
"github.com/hitokoto-osc/notification-worker/rabbitmq"
"github.com/hitokoto-osc/notification-worker/utils/validator"
amqp "github.com/rabbitmq/amqp091-go"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -45,15 +45,14 @@ func HitokotoPollDailyReportEvent() *rabbitmq.ConsumerRegisterOptions {
logger := logging.WithContext(ctx)
defer logger.Sync()
logger.Debug("[hitokoto_poll_daily_report]收到消息: ", zap.ByteString("body", delivery.Body))
message := hitokotoPollDailyReportMessage{}
err := json.Unmarshal(delivery.Body, &message)
message, err := validator.UnmarshalV[model.PollDailyReportMessage](ctx, delivery.Body)
if err != nil {
return err
return errors.Wrap(err, "解析消息失败")
}

html, err := django.RenderTemplate("email/poll_daily_report", django.Context{
"username": message.UserName,
"created_at": carbon.Parse(message.CreatedAt).Format("Y-m-d H:i:s"),
"username": message.Username,
"created_at": message.CreatedAt.Format("Y-m-d H:i:s"),
"system": django.Context{
"total": message.SystemInformation.Total,
"processed": message.SystemInformation.ProcessTotal,
Expand All @@ -75,7 +74,6 @@ func HitokotoPollDailyReportEvent() *rabbitmq.ConsumerRegisterOptions {
"need_modify": message.UserInformation.InNeedEdited,
},
})

if err != nil {
return errors.Wrap(err, "无法渲染模板")
}
Expand All @@ -91,35 +89,3 @@ func HitokotoPollDailyReportEvent() *rabbitmq.ConsumerRegisterOptions {
},
}
}

type hitokotoPollDailyReportMessage struct {
CreatedAt string `json:"created_at"` // 报告生成时间
To string `json:"to"` // 接收人地址
UserName string `json:"user_name"` // 接收人名称
SystemInformation hitokotoPollDailyReportMessageSystemInformation `json:"system_information"` // 系统信息
UserInformation hitokotoPollDailyReportMessageUserInformation `json:"user_information"` // 用户信息
}

type hitokotoPollDailyReportMessageSystemInformation struct {
Total int `json:"total"` // 平台当前剩余的投票数目
ProcessTotal int `json:"process_total"` // 平台处理了的投票数目(过去 24 小时)
ProcessAccept int `json:"process_accept"` // 平台处理为入库的投票数目(过去 24 小时)
ProcessReject int `json:"process_reject"` // 平台处理为驳回的投票数目(过去 24 小时)
ProcessNeedEdited int `json:"process_need_edited"` // 平台处理为亟待修改的投票数目(过去 24 小时)
}

type hitokotoPollDailyReportMessageUserInformation struct {
Polled hitokotoPollDailyReportMessageUserInformationPolled `json:"polled"` // 用户参与了的投票数目(过去 24 小时)
Waiting int `json:"waiting"` // 等待其他用户参与的投票数目(基于已投票的数目)
Accepted int `json:"accepted"` // 已入库的投票数目(基于已投票的数目)
Rejected int `json:"rejected"` // 已驳回的投票数目(基于已投票的数目)
InNeedEdited int `json:"in_need_edited"` // 已进入亟待修改状态的投票数目(基于已投票的数目)
WaitForPolling int `json:"wait_for_polling"` // 基于剩余投票数目,计算出来的等待投票数目。
}

type hitokotoPollDailyReportMessageUserInformationPolled struct {
Total int `json:"total"` // 投票参与的总数
Accept int `json:"accept"` // 投批准票的投票数目
Reject int `json:"reject"` // 投拒绝票的投票数目
NeedEdited int `json:"need_edited"` // 投需要修改的投票数目
}
Loading

0 comments on commit 4cd7f9a

Please sign in to comment.