2 Commits

Author SHA1 Message Date
ctwj
0e88374905 Merge branch 'main' of https://github.com/ctwj/urldb 2025-11-11 01:37:45 +08:00
ctwj
6b8d2b3cf0 update: 优化推送策略 2025-11-07 23:21:04 +08:00
2 changed files with 43 additions and 31 deletions

View File

@@ -279,6 +279,20 @@ func (r *ResourceRepositoryImpl) SearchWithFilters(params map[string]interface{}
db = db.Where("pan_id = ?", panEntity.ID)
}
}
case "exclude_ids": // 添加exclude_ids参数支持
if excludeIDs, ok := value.([]uint); ok && len(excludeIDs) > 0 {
// 限制排除ID的数量避免SQL语句过长
maxExcludeIDs := 5000 // 限制排除ID数量避免SQL语句过长
if len(excludeIDs) > maxExcludeIDs {
// 只取最近的maxExcludeIDs个ID进行排除
startIndex := len(excludeIDs) - maxExcludeIDs
truncatedExcludeIDs := excludeIDs[startIndex:]
db = db.Where("id NOT IN ?", truncatedExcludeIDs)
utils.Debug("SearchWithFilters: 排除ID数量过多截取最近%d个ID", len(truncatedExcludeIDs))
} else {
db = db.Where("id NOT IN ?", excludeIDs)
}
}
}
}

View File

@@ -1049,16 +1049,16 @@ func (s *TelegramBotServiceImpl) findResourcesForChannel(channel entity.Telegram
func (s *TelegramBotServiceImpl) findLatestResources(channel entity.TelegramChannel, excludeResourceIDs []uint) []interface{} {
params := s.buildFilterParams(channel)
// 在数据库查询中排除已推送的资源
if len(excludeResourceIDs) > 0 {
params["exclude_ids"] = excludeResourceIDs
}
// 使用现有的搜索功能,按更新时间倒序获取最新资源
resources, _, err := s.resourceRepo.SearchWithFilters(params)
if err != nil {
utils.Error("[TELEGRAM:PUSH] 获取最新资源失败: %v", err)
return []interface{}{}
}
// 排除最近推送过的资源
if len(excludeResourceIDs) > 0 {
resources = s.excludePushedResources(resources, excludeResourceIDs)
return s.findRandomResources(channel, excludeResourceIDs) // 回退到随机策略
}
// 应用时间限制
@@ -1067,8 +1067,8 @@ func (s *TelegramBotServiceImpl) findLatestResources(channel entity.TelegramChan
}
if len(resources) == 0 {
utils.Info("[TELEGRAM:PUSH] 没有找到符合条件的最新资源")
return []interface{}{}
utils.Info("[TELEGRAM:PUSH] 没有找到符合条件的最新资源,尝试获取随机资源")
return s.findRandomResources(channel, excludeResourceIDs) // 回退到随机策略
}
// 返回最新资源(第一条)
@@ -1083,6 +1083,11 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
// 添加转存链接条件
params["has_save_url"] = true
// 在数据库查询中排除已推送的资源
if len(excludeResourceIDs) > 0 {
params["exclude_ids"] = excludeResourceIDs
}
// 优先获取有转存链接的资源
resources, _, err := s.resourceRepo.SearchWithFilters(params)
if err != nil {
@@ -1090,11 +1095,6 @@ func (s *TelegramBotServiceImpl) findTransferredResources(channel entity.Telegra
return []interface{}{}
}
// 排除最近推送过的资源
if len(excludeResourceIDs) > 0 {
resources = s.excludePushedResources(resources, excludeResourceIDs)
}
// 应用时间限制
if channel.TimeLimit != "none" && len(resources) > 0 {
resources = s.applyTimeFilter(resources, channel.TimeLimit)
@@ -1118,23 +1118,19 @@ func (s *TelegramBotServiceImpl) findRandomResources(channel entity.TelegramChan
// 如果是已转存优先策略但没有找到转存资源,这里会回退到随机策略
// 此时不需要额外的转存链接条件,让随机函数处理
// 先尝试获取候选资源列表,然后从中排除已推送的资源
var candidateResources []entity.Resource
var err error
// 在数据库查询中排除已推送的资源
if len(excludeResourceIDs) > 0 {
params["exclude_ids"] = excludeResourceIDs
}
// 使用搜索功能获取候选资源,然后过滤
params["limit"] = 100 // 获取更多候选资源
candidateResources, _, err = s.resourceRepo.SearchWithFilters(params)
candidateResources, _, err := s.resourceRepo.SearchWithFilters(params)
if err != nil {
utils.Error("[TELEGRAM:PUSH] 获取候选资源失败: %v", err)
return []interface{}{}
}
// 排除最近推送过的资源
if len(excludeResourceIDs) > 0 {
candidateResources = s.excludePushedResources(candidateResources, excludeResourceIDs)
}
// 应用时间限制
if channel.TimeLimit != "none" && len(candidateResources) > 0 {
candidateResources = s.applyTimeFilter(candidateResources, channel.TimeLimit)
@@ -1809,11 +1805,12 @@ func (s *TelegramBotServiceImpl) addPushedResourceID(chatID int64, resourceID ui
history = []uint{}
}
// 检查是否已经超过100条记录
if len(history) >= 10000 {
// 清空历史记录,重新开始
history = []uint{}
utils.Info("[TELEGRAM:PUSH] 频道 %d 推送历史记录已满(10000条),清空重置", chatID)
// 检查是否已经超过5000条记录
if len(history) >= 5000 {
// 移除旧的2500条记录保留最新的2500条记录
startIndex := len(history) - 2500
history = history[startIndex:]
utils.Info("[TELEGRAM:PUSH] 频道 %d 推送历史记录已满(5000条)移除旧的2500条记录保留最新的2500条", chatID)
}
// 添加新的资源ID到历史记录
@@ -1896,10 +1893,11 @@ func (s *TelegramBotServiceImpl) loadPushHistory() error {
resourceIDs = append(resourceIDs, uint(resourceID))
}
// 只保留最多100条记录
if len(resourceIDs) > 100 {
// 保留最新的100条记录
resourceIDs = resourceIDs[len(resourceIDs)-100:]
// 只保留最多5000条记录
if len(resourceIDs) > 5000 {
// 保留最新的5000条记录
startIndex := len(resourceIDs) - 5000
resourceIDs = resourceIDs[startIndex:]
}
s.pushHistory[chatID] = resourceIDs