mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-08 03:00:26 +08:00
增加忽略相似消息周期设置
This commit is contained in:
@@ -79,7 +79,7 @@ func (this *MessageMediaInstanceDAO) FindEnabledMessageMediaInstance(tx *dbs.Tx,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateMediaInstance 创建媒介实例
|
// CreateMediaInstance 创建媒介实例
|
||||||
func (this *MessageMediaInstanceDAO) CreateMediaInstance(tx *dbs.Tx, name string, mediaType string, params maps.Map, description string, rateJSON []byte) (int64, error) {
|
func (this *MessageMediaInstanceDAO) CreateMediaInstance(tx *dbs.Tx, name string, mediaType string, params maps.Map, description string, rateJSON []byte, hashLifeSeconds int32) (int64, error) {
|
||||||
op := NewMessageMediaInstanceOperator()
|
op := NewMessageMediaInstanceOperator()
|
||||||
op.Name = name
|
op.Name = name
|
||||||
op.MediaType = mediaType
|
op.MediaType = mediaType
|
||||||
@@ -99,6 +99,7 @@ func (this *MessageMediaInstanceDAO) CreateMediaInstance(tx *dbs.Tx, name string
|
|||||||
if len(rateJSON) > 0 {
|
if len(rateJSON) > 0 {
|
||||||
op.Rate = rateJSON
|
op.Rate = rateJSON
|
||||||
}
|
}
|
||||||
|
op.HashLife = hashLifeSeconds
|
||||||
|
|
||||||
op.IsOn = true
|
op.IsOn = true
|
||||||
op.State = MessageMediaInstanceStateEnabled
|
op.State = MessageMediaInstanceStateEnabled
|
||||||
@@ -106,7 +107,7 @@ func (this *MessageMediaInstanceDAO) CreateMediaInstance(tx *dbs.Tx, name string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateMediaInstance 修改媒介实例
|
// UpdateMediaInstance 修改媒介实例
|
||||||
func (this *MessageMediaInstanceDAO) UpdateMediaInstance(tx *dbs.Tx, instanceId int64, name string, mediaType string, params maps.Map, description string, rateJSON []byte, isOn bool) error {
|
func (this *MessageMediaInstanceDAO) UpdateMediaInstance(tx *dbs.Tx, instanceId int64, name string, mediaType string, params maps.Map, description string, rateJSON []byte, hashLifeSeconds int32, isOn bool) error {
|
||||||
if instanceId <= 0 {
|
if instanceId <= 0 {
|
||||||
return errors.New("invalid instanceId")
|
return errors.New("invalid instanceId")
|
||||||
}
|
}
|
||||||
@@ -129,8 +130,10 @@ func (this *MessageMediaInstanceDAO) UpdateMediaInstance(tx *dbs.Tx, instanceId
|
|||||||
if len(rateJSON) > 0 {
|
if len(rateJSON) > 0 {
|
||||||
op.Rate = rateJSON
|
op.Rate = rateJSON
|
||||||
}
|
}
|
||||||
|
op.HashLife = hashLifeSeconds
|
||||||
|
|
||||||
op.Description = description
|
op.Description = description
|
||||||
|
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
return this.Save(tx, op)
|
return this.Save(tx, op)
|
||||||
}
|
}
|
||||||
@@ -171,3 +174,15 @@ func (this *MessageMediaInstanceDAO) ListAllEnabledMediaInstances(tx *dbs.Tx, me
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindInstanceHashLifeSeconds 获取单个实例的HashLife
|
||||||
|
func (this *MessageMediaInstanceDAO) FindInstanceHashLifeSeconds(tx *dbs.Tx, instanceId int64) (int32, error) {
|
||||||
|
hashLife, err := this.Query(tx).
|
||||||
|
Pk(instanceId).
|
||||||
|
Result("hashLife").
|
||||||
|
FindIntCol(0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return types.Int32(hashLife), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ type MessageMediaInstance struct {
|
|||||||
Description string `field:"description"` // 备注
|
Description string `field:"description"` // 备注
|
||||||
Rate string `field:"rate"` // 发送频率
|
Rate string `field:"rate"` // 发送频率
|
||||||
State uint8 `field:"state"` // 状态
|
State uint8 `field:"state"` // 状态
|
||||||
|
HashLife int32 `field:"hashLife"` // HASH有效期(秒)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageMediaInstanceOperator struct {
|
type MessageMediaInstanceOperator struct {
|
||||||
@@ -21,6 +22,7 @@ type MessageMediaInstanceOperator struct {
|
|||||||
Description interface{} // 备注
|
Description interface{} // 备注
|
||||||
Rate interface{} // 发送频率
|
Rate interface{} // 发送频率
|
||||||
State interface{} // 状态
|
State interface{} // 状态
|
||||||
|
HashLife interface{} // HASH有效期(秒)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMessageMediaInstanceOperator() *MessageMediaInstanceOperator {
|
func NewMessageMediaInstanceOperator() *MessageMediaInstanceOperator {
|
||||||
|
|||||||
@@ -226,3 +226,11 @@ func (this *MessageRecipientDAO) FindAllEnabledAndOnRecipientIdsWithGroup(tx *db
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindRecipientInstanceId 查找接收人的媒介
|
||||||
|
func (this *MessageRecipientDAO) FindRecipientInstanceId(tx *dbs.Tx, recipientId int64) (int64, error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
Pk(recipientId).
|
||||||
|
Result("instanceId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -92,9 +94,41 @@ func (this *MessageTaskDAO) FindEnabledMessageTask(tx *dbs.Tx, id int64) (*Messa
|
|||||||
|
|
||||||
// CreateMessageTask 创建任务
|
// CreateMessageTask 创建任务
|
||||||
func (this *MessageTaskDAO) CreateMessageTask(tx *dbs.Tx, recipientId int64, instanceId int64, user string, subject string, body string, isPrimary bool) (int64, error) {
|
func (this *MessageTaskDAO) CreateMessageTask(tx *dbs.Tx, recipientId int64, instanceId int64, user string, subject string, body string, isPrimary bool) (int64, error) {
|
||||||
|
var hash = stringutil.Md5(types.String(recipientId) + "@" + types.String(instanceId) + "@" + user + "@" + subject + "@" + types.String(isPrimary))
|
||||||
|
recipientInstanceId, err := SharedMessageRecipientDAO.FindRecipientInstanceId(tx, recipientId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if recipientInstanceId > 0 {
|
||||||
|
hashLifeSeconds, err := SharedMessageMediaInstanceDAO.FindInstanceHashLifeSeconds(tx, recipientInstanceId)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if hashLifeSeconds >= 0 { // 意味着此值如果小于0,则不做判断
|
||||||
|
lastMessageAt, err := this.Query(tx).
|
||||||
|
Attr("hash", hash).
|
||||||
|
Result("createdAt").
|
||||||
|
DescPk().
|
||||||
|
FindInt64Col(0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对于同一个人N分钟内消息不重复发送
|
||||||
|
if hashLifeSeconds <= 0 {
|
||||||
|
hashLifeSeconds = 60
|
||||||
|
}
|
||||||
|
if lastMessageAt > 0 && time.Now().Unix()-lastMessageAt < int64(hashLifeSeconds) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
op := NewMessageTaskOperator()
|
op := NewMessageTaskOperator()
|
||||||
op.RecipientId = recipientId
|
op.RecipientId = recipientId
|
||||||
op.InstanceId = instanceId
|
op.InstanceId = instanceId
|
||||||
|
op.Hash = hash
|
||||||
op.User = user
|
op.User = user
|
||||||
op.Subject = subject
|
op.Subject = subject
|
||||||
op.Body = body
|
op.Body = body
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package models
|
|||||||
type MessageTask struct {
|
type MessageTask struct {
|
||||||
Id uint64 `field:"id"` // ID
|
Id uint64 `field:"id"` // ID
|
||||||
RecipientId uint32 `field:"recipientId"` // 接收人ID
|
RecipientId uint32 `field:"recipientId"` // 接收人ID
|
||||||
|
Hash string `field:"hash"` // SUM标识
|
||||||
InstanceId uint32 `field:"instanceId"` // 媒介实例ID
|
InstanceId uint32 `field:"instanceId"` // 媒介实例ID
|
||||||
User string `field:"user"` // 接收用户标识
|
User string `field:"user"` // 接收用户标识
|
||||||
Subject string `field:"subject"` // 标题
|
Subject string `field:"subject"` // 标题
|
||||||
@@ -20,6 +21,7 @@ type MessageTask struct {
|
|||||||
type MessageTaskOperator struct {
|
type MessageTaskOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
RecipientId interface{} // 接收人ID
|
RecipientId interface{} // 接收人ID
|
||||||
|
Hash interface{} // SUM标识
|
||||||
InstanceId interface{} // 媒介实例ID
|
InstanceId interface{} // 媒介实例ID
|
||||||
User interface{} // 接收用户标识
|
User interface{} // 接收用户标识
|
||||||
Subject interface{} // 标题
|
Subject interface{} // 标题
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MessageMediaInstanceService 消息媒介实例服务
|
// MessageMediaInstanceService 消息媒介实例服务
|
||||||
@@ -30,7 +31,7 @@ func (this *MessageMediaInstanceService) CreateMessageMediaInstance(ctx context.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
instanceId, err := models.SharedMessageMediaInstanceDAO.CreateMediaInstance(tx, req.Name, req.MediaType, params, req.Description, req.RateJSON)
|
instanceId, err := models.SharedMessageMediaInstanceDAO.CreateMediaInstance(tx, req.Name, req.MediaType, params, req.Description, req.RateJSON, req.HashLife)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -54,7 +55,7 @@ func (this *MessageMediaInstanceService) UpdateMessageMediaInstance(ctx context.
|
|||||||
}
|
}
|
||||||
|
|
||||||
var tx = this.NullTx()
|
var tx = this.NullTx()
|
||||||
err = models.SharedMessageMediaInstanceDAO.UpdateMediaInstance(tx, req.MessageMediaInstanceId, req.Name, req.MediaType, params, req.Description, req.RateJSON, req.IsOn)
|
err = models.SharedMessageMediaInstanceDAO.UpdateMediaInstance(tx, req.MessageMediaInstanceId, req.Name, req.MediaType, params, req.Description, req.RateJSON, req.HashLife, req.IsOn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -180,5 +181,6 @@ func (this *MessageMediaInstanceService) FindEnabledMessageMediaInstance(ctx con
|
|||||||
ParamsJSON: []byte(instance.Params),
|
ParamsJSON: []byte(instance.Params),
|
||||||
Description: instance.Description,
|
Description: instance.Description,
|
||||||
RateJSON: []byte(instance.Rate),
|
RateJSON: []byte(instance.Rate),
|
||||||
|
HashLife: types.Int32(instance.HashLife),
|
||||||
}}, nil
|
}}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user