Skip to content

Commit

Permalink
重构存储数据结构
Browse files Browse the repository at this point in the history
  • Loading branch information
assimon committed Nov 14, 2023
1 parent 82b2006 commit b20779b
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 237 deletions.
10 changes: 3 additions & 7 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bootstrap
import (
"github.com/assimon/captcha-bot/telegram"
"github.com/assimon/captcha-bot/util/config"
E "github.com/assimon/captcha-bot/util/error"
"github.com/assimon/captcha-bot/util/log"
"github.com/assimon/captcha-bot/util/orm"
"github.com/assimon/captcha-bot/util/sensitiveword"
Expand All @@ -18,14 +19,9 @@ func Start() {
orm.InitDb()
sensitiveword.InitSensitiveWord()
// 机器人启动
go func() {
defer func() {
if err := recover(); err != nil {
log.Sugar.Error("server bot err:", err)
}
}()
go E.MustPanicErrorFunc(func() {
telegram.BotStart()
}()
})
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
Expand Down
13 changes: 13 additions & 0 deletions model/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model

import (
"github.com/golang-module/carbon/v2"
"gorm.io/gorm"
)

type BaseModel struct {
ID int64 `gorm:"column:id;primary_key" json:"id"`
CreatedAt carbon.Timestamp `gorm:"column:created_at" json:"created_at"`
UpdatedAt carbon.Timestamp `gorm:"column:updated_at" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"`
}
33 changes: 33 additions & 0 deletions model/user_captcha_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package model

import "github.com/golang-module/carbon/v2"

const (
CaptchaStatusPending = -1
CaptchaStatusSuccess = 1
CaptchaStatusTimeout = 2
)

type UserCaptchaRecord struct {
BaseModel
CaptchaId string `gorm:"column:captcha_id;unique_index:captcha_id_index" json:"captcha_id"` //验证id
TelegramChatName string `gorm:"column:telegram_chat_name" json:"telegram_chat_name"`
TelegramUserLastName string `gorm:"column:telegram_user_last_name" json:"telegram_user_last_name"`
TelegramUserFirstName string `gorm:"column:telegram_user_first_name" json:"telegram_user_first_name"`
TelegramUserId int64 `gorm:"column:telegram_user_id" json:"telegram_user_id"`
TelegramChatId int64 `gorm:"column:telegram_chat_id" json:"telegram_chat_id"`
CaptchaMessageId int `gorm:"column:captcha_message_id" json:"captcha_message_id"` // 群-待验证消息id
CaptchaStatus int `gorm:"column:captcha_status" json:"captcha_status"` // 验证状态 -1待验证 1已验证 2已超时
CaptchaTimeoutMessageId int `gorm:"column:captcha_timeout_message_id" json:"captcha_timeout_message_id"` // 群-验证超时消息id
CaptchaTimeoutTime carbon.Timestamp `gorm:"column:captcha_timeout_time" json:"captcha_timeout_time"` // 验证超时时间
CaptchaSuccessMessageId int `gorm:"column:captcha_success_message_id" json:"captcha_success_message_id"` // 群-验证成功的消息id
CaptchaSuccessTime carbon.Timestamp `gorm:"column:captcha_success_time" json:"captcha_success_time"` // 验证成功时间

CaptchaCodeMessageId int `gorm:"column:captcha_code_message_id" json:"captcha_code_message_id"` // 验证码消息id
CaptchaCode string `gorm:"column:captcha_code" json:"captcha_code"` // 验证码内容

}

func (UserCaptchaRecord) TableName() string {
return "user_captcha_record"
}
84 changes: 0 additions & 84 deletions service/captchaService.go

This file was deleted.

62 changes: 62 additions & 0 deletions service/userCaptchaRecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package service

import (
"github.com/assimon/captcha-bot/model"
"github.com/assimon/captcha-bot/util/config"
"github.com/assimon/captcha-bot/util/orm"
"github.com/golang-module/carbon/v2"
)

// CreateCaptchaRecord 创建验证记录
func CreateCaptchaRecord(record *model.UserCaptchaRecord) error {
err := orm.Gdb.Model(record).Create(record).Error
return err
}

// GetRecordByCaptchaId 通过载荷唯一标识获取记录
func GetRecordByCaptchaId(cId string) (record *model.UserCaptchaRecord, err error) {
err = orm.Gdb.Model(record).Where("captcha_id = ?", cId).Find(record).Error
return
}

// TimeoutRecordByCaptchaId 设置某条验证消息已经超时
func TimeoutRecordByCaptchaId(cId string) error {
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId).
UpdateColumns(map[string]interface{}{
"captcha_status": model.CaptchaStatusTimeout,
"captcha_timeout_time": carbon.Now().Timestamp(),
}).Error
return err
}

// SuccessRecordByCaptchaId 设置某条消息已经验证成功
func SuccessRecordByCaptchaId(cId string) error {
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId).
UpdateColumns(map[string]interface{}{
"captcha_status": model.CaptchaStatusSuccess,
"captcha_success_time": carbon.Now().Timestamp(),
}).Error
return err
}

// SetCaptchaCodeByCaptchaId 设置或刷新一个验证消息的code
func SetCaptchaCodeByCaptchaId(cId, code string) error {
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId).
Update("captcha_code", code).Error
return err
}

// SetCaptchaCodeMessageIdByCaptchaId 设置私聊验证消息id
func SetCaptchaCodeMessageIdByCaptchaId(cId string, msgId int) error {
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId).
Update("captcha_code_message_id", msgId).Error
return err
}

// GetTimeoutCaptchaRecords 获取已经超时的待验证记录
func GetTimeoutCaptchaRecords() (records []model.UserCaptchaRecord, err error) {
now := carbon.Now().SubSeconds(config.SystemC.CaptchaTimeout)
err = orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_status = ?", model.CaptchaStatusPending).
Where("created_at > ?", now).Find(&records).Error
return
}
Loading

0 comments on commit b20779b

Please sign in to comment.