update: tg

This commit is contained in:
Kerwin
2025-09-17 14:31:12 +08:00
parent 1eb37baa87
commit cd8c519b3a
11 changed files with 804 additions and 62 deletions

View File

@@ -17,6 +17,8 @@ func TelegramChannelToResponse(channel entity.TelegramChannel) dto.TelegramChann
ChatType: channel.ChatType,
PushEnabled: channel.PushEnabled,
PushFrequency: channel.PushFrequency,
PushStartTime: channel.PushStartTime,
PushEndTime: channel.PushEndTime,
ContentCategories: channel.ContentCategories,
ContentTags: channel.ContentTags,
IsActive: channel.IsActive,
@@ -43,6 +45,8 @@ func RequestToTelegramChannel(req dto.TelegramChannelRequest, registeredBy strin
ChatType: req.ChatType,
PushEnabled: req.PushEnabled,
PushFrequency: req.PushFrequency,
PushStartTime: req.PushStartTime,
PushEndTime: req.PushEndTime,
ContentCategories: req.ContentCategories,
ContentTags: req.ContentTags,
IsActive: req.IsActive,

View File

@@ -9,6 +9,8 @@ type TelegramChannelRequest struct {
ChatType string `json:"chat_type" binding:"required"` // channel 或 group
PushEnabled bool `json:"push_enabled"`
PushFrequency int `json:"push_frequency"`
PushStartTime string `json:"push_start_time"`
PushEndTime string `json:"push_end_time"`
ContentCategories string `json:"content_categories"`
ContentTags string `json:"content_tags"`
IsActive bool `json:"is_active"`
@@ -22,6 +24,8 @@ type TelegramChannelResponse struct {
ChatType string `json:"chat_type"`
PushEnabled bool `json:"push_enabled"`
PushFrequency int `json:"push_frequency"`
PushStartTime string `json:"push_start_time"`
PushEndTime string `json:"push_end_time"`
ContentCategories string `json:"content_categories"`
ContentTags string `json:"content_tags"`
IsActive bool `json:"is_active"`

View File

@@ -18,6 +18,8 @@ type TelegramChannel struct {
// 推送配置
PushEnabled bool `json:"push_enabled" gorm:"default:true;comment:是否启用推送"`
PushFrequency int `json:"push_frequency" gorm:"default:24;comment:推送频率(小时)"`
PushStartTime string `json:"push_start_time" gorm:"size:10;comment:推送开始时间格式HH:mm"`
PushEndTime string `json:"push_end_time" gorm:"size:10;comment:推送结束时间格式HH:mm"`
ContentCategories string `json:"content_categories" gorm:"type:text;comment:推送的内容分类,用逗号分隔"`
ContentTags string `json:"content_tags" gorm:"type:text;comment:推送的标签,用逗号分隔"`

View File

@@ -88,7 +88,29 @@ func (r *TelegramChannelRepositoryImpl) UpdateLastPushAt(id uint, lastPushAt tim
func (r *TelegramChannelRepositoryImpl) FindDueForPush() ([]entity.TelegramChannel, error) {
var channels []entity.TelegramChannel
// 查找活跃、启用推送的频道,且距离上次推送已超过推送频率小时的记录
err := r.db.Where("is_active = ? AND push_enabled = ? AND (last_push_at IS NULL OR last_push_at < DATE_SUB(NOW(), INTERVAL push_frequency HOUR))",
true, true).Find(&channels).Error
return channels, err
// 先获取所有活跃且启用推送的频道
err := r.db.Where("is_active = ? AND push_enabled = ?", true, true).Find(&channels).Error
if err != nil {
return nil, err
}
// 在内存中过滤出需要推送的频道(更可靠的跨数据库方案)
var dueChannels []entity.TelegramChannel
now := time.Now()
for _, channel := range channels {
// 如果从未推送过,或者距离上次推送已超过推送频率小时
if channel.LastPushAt == nil {
dueChannels = append(dueChannels, channel)
} else {
// 计算下次推送时间:上次推送时间 + 推送频率小时
nextPushTime := channel.LastPushAt.Add(time.Duration(channel.PushFrequency) * time.Hour)
if now.After(nextPushTime) {
dueChannels = append(dueChannels, channel)
}
}
}
return dueChannels, nil
}