Documentation
¶
Index ¶
- Constants
- Variables
- func NewRSSDigestFlush(db *gorm.DB, notifySvc NotificationServiceForRSS, ...) notify.DigestFlushFunc
- type AuditDTO
- type AuditEntry
- type AuditQuery
- type AuditService
- type AuditStatsDTO
- type BindCodeDTO
- type BindingDTO
- type BindingService
- type ChannelStater
- type CreateConfReq
- type JobLister
- type JobStatusDTO
- type Notification
- type NotificationConfDTO
- type NotificationService
- type NotificationServiceForRSS
- type NotifyManager
- type QuietLookupFunc
- type RSSCallbackActions
- type RSSFilteredEvent
- type RSSItemEvent
- type RSSNotifier
- type RSSRetryWorker
- type SiteLister
- type SiteService
- type SiteSummaryDTO
- type TaskService
- type TorrentDTO
- type TorrentDataFetcher
- type TorrentService
- type UpdateConfReq
- type UserInfoDTO
- type UserInfoSource
Constants ¶
const ( // ChannelStateConnected 实例在跑,且适配器确认对端真的接上了。 ChannelStateConnected = "connected" // ChannelStateRunning 实例在跑,但连通性未知(适配器给不出这个判断)。 ChannelStateRunning = "running" // ChannelStateError 该跑却没跑起来,或实例自报不健康。 ChannelStateError = "error" // ChannelStateDisabled 配置本身停用了 —— 没跑不是故障。 ChannelStateDisabled = "disabled" )
* 运行态取值。落到前端是通知通道页画板 22 那条分段器。 * * 四档而不是三档,是因为「在跑」和「接上了」不是一回事: * 四个适配器的 Healthy() 含义都只是**构造/启动成功**(QQ 绑上端口就 true,而 NapCat * 没握手时发送会明确失败;Telegram 造出 bot 就 true,没做任何网络确认;Webhook 只判 * config != nil;WeCom 恒 true)。把 Healthy() 直接说成「已连接」就是在界面上说假话 —— * 这正是上一轮评审查出的缺陷。 * * 所以只有实现了 notify.LinkStater 并确认对端接上的通道才配 Connected; * 其余在跑的一律 Running(界面写「运行中」,不承诺连通性)。
Variables ¶
var ( ErrTooManyActiveCodes = errors.New("too many active bind codes for this admin") ErrCodeUsedOrExpired = errors.New("bind code is invalid, expired, or already used") // ErrCodeWrongChannel 表示绑定码不是这个通知通道签发的:绑定码只能在签发它的通道上兑换。 ErrCodeWrongChannel = errors.New("bind code was issued for a different notification channel") ErrInvalidReplyLang = errors.New("invalid reply_lang; only 'zh' or 'en' allowed") )
var ( ErrTorrentNotFound = errors.New("torrent not found") ErrDownloaderNotFound = errors.New("downloader not found") )
var ErrConfNotFound = errors.New("notification conf not found")
ErrConfNotFound 当 conf id 不存在时返回。
var ErrJobNotWired = errors.New("task service start/stop not yet wired (pending T32)")
ErrJobNotWired 表示 StartJob/StopJob 尚未在 T32(DI 接线)阶段完成。 接口契约稳定,方便上层(chatops 命令、web API)先编程对接。
var ErrSiteNotFound = errors.New("site not found")
ErrSiteNotFound 表示请求的站点未在 registry / ConfigStore 中找到。
ErrUserInfoUnavailable 表示站点尚未抓取到用户信息,或 driver 不支持。
Functions ¶
func NewRSSDigestFlush ¶
func NewRSSDigestFlush(db *gorm.DB, notifySvc NotificationServiceForRSS, logf func(format string, args ...any)) notify.DigestFlushFunc
NewRSSDigestFlush 返回 DigestBuffer 的刷写函数。
- 只发送仍是 pending 的行:被 filtered 通知抑制(suppressed)或已由其他路径发出的行跳过, DigestBuffer 里的内存条目不会再把它们发出去
- 只剩一条时按单条通知发送并保留「立即下载 / 忽略」按钮
- 成功后把这些行条件更新为 sent;失败时 5 秒后交回重试 worker 逐条重试
Types ¶
type AuditDTO ¶
type AuditDTO struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
ChannelType string `json:"channel_type"`
ChannelUserID string `json:"channel_user_id"`
Command string `json:"command"`
ArgsJSON string `json:"args_json"`
Result string `json:"result"`
LatencyMs int64 `json:"latency_ms"`
}
type AuditEntry ¶
type AuditQuery ¶
type AuditQuery struct {
// Keyword 模糊匹配命令与触发用户(画板 25 的 q)
Keyword string
Since time.Time
Until time.Time
ChannelUserID string
Command string
// Result / ChannelType 支持逗号分隔的多值(前端的多选筛选就是这么发的)。
// 原先 Result 按单值等值匹配,于是「成功,出错」被当成一个字面量,一行都匹配不到 ——
// 界面上是「筛完什么都没有」,读起来像真的没有记录。ChannelType 更彻底:
// handler 压根没读这个参数,那枚通道筛选是个空控件。
Result string
ChannelType string
Page int
PageSize int
}
type AuditService ¶
type AuditService interface {
Record(ctx context.Context, e AuditEntry) error
Query(ctx context.Context, q AuditQuery) (items []AuditDTO, total int, err error)
Stats(ctx context.Context) (AuditStatsDTO, error)
Prune(ctx context.Context) (deleted int64, err error)
}
func NewAuditService ¶
func NewAuditService(db *gorm.DB) AuditService
func NewAuditServiceWithCap ¶
NewAuditServiceWithCap 暴露 rowCap / retention 供测试注入小阈值;生产请用 NewAuditService。
type AuditStatsDTO ¶
type AuditStatsDTO struct {
TodayCount int64 `json:"today_count"`
TotalCount int64 `json:"total_count"`
SuccessRate float64 `json:"success_rate"`
MaxLatencyMs int64 `json:"max_latency_ms"`
AvgLatencyMs float64 `json:"avg_latency_ms"`
}
AuditStatsDTO 聚合指标,用于 ChatOps 审计页顶部的 stat chip。 SuccessRate 单位为百分比 (0..100),已四舍五入到 2 位小数。
type BindCodeDTO ¶
type BindingDTO ¶
type BindingDTO struct {
ID uint `json:"id"`
ConfID uint `json:"conf_id"`
ChannelType string `json:"channel_type"`
ChannelUserID string `json:"channel_user_id"`
Label string `json:"label"`
ReplyLang string `json:"reply_lang"`
PtAdmin bool `json:"admin"`
Allowed bool `json:"allowed"`
CreatedAt time.Time `json:"created_at"`
LastActiveAt time.Time `json:"last_active"`
}
type BindingService ¶
type BindingService interface {
IssueCode(ctx context.Context, confID uint, label string, ttl time.Duration) (BindCodeDTO, error)
ListPendingCodes(ctx context.Context) ([]BindCodeDTO, error)
ConsumeCode(ctx context.Context, code string, confID uint, channelType, channelUserID string) (BindingDTO, error)
ListBindings(ctx context.Context) ([]BindingDTO, error)
Revoke(ctx context.Context, bindingID uint) error
SetReplyLang(ctx context.Context, bindingID uint, lang string) error
}
func NewBindingService ¶
func NewBindingService(db *gorm.DB, createdBy string) BindingService
type ChannelStater ¶ added in v0.48.0
ChannelStater 由持有实时通道实例的一方实现(cmd 层的 liveNotifyManager)。 返回 Connected / Running / Error,或空串表示这一条没有实例在跑。
做成可选能力而不是塞进 NotifyManager:测试里的假 manager 只关心 Send, 不该被迫编造运行态。
type CreateConfReq ¶
type CreateConfReq struct {
ChannelType string
Name string
ConfigJSON json.RawMessage
Enabled bool
}
CreateConfReq 创建通知通道的请求;ConfigJSON 为通道原始配置,进入 service 后会被 AES-GCM 加密落库。
type JobLister ¶
JobLister 是 scheduler.Manager 的最小读视图,便于单元测试 mock。 *scheduler.Manager 自动满足该接口(ListJobs 已在 T8 公开)。
type JobStatusDTO ¶
type JobStatusDTO struct {
SiteName string `json:"site_name"`
RSSName string `json:"rss_name"`
Running bool `json:"running"`
StartedAt time.Time `json:"started_at"`
}
JobStatusDTO 是 RSS 订阅任务的运行时快照,供 chatops `/tasks`、`/jobs` 等命令使用。
type Notification ¶
type Notification struct {
Title string `json:"title"`
Text string `json:"text"`
SourceConfID uint `json:"source_conf_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Targets map[string]string `json:"targets,omitempty"`
Buttons [][]notify.Button `json:"buttons,omitempty"`
}
Notification 是 NotificationService 与 NotifyManager 之间传递的最小消息载荷。 TODO(T15): 替换为 internal/notify 包内的 notify.Notification 完整结构。
type NotificationConfDTO ¶
type NotificationConfDTO struct {
ID uint `json:"id"`
ChannelType string `json:"channel_type"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
QuietHoursStart string `json:"quiet_hours_start,omitempty"`
QuietHoursEnd string `json:"quiet_hours_end,omitempty"`
ConfigJSON json.RawMessage `json:"config_json,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// RuntimeState 是进程内的实时运行态:connected / error / disabled。
// 不落库 —— 它描述的是「现在这个进程里跑着什么」,重启即重算。
// manager 不提供这个信号时为空串(前端据此不画那枚状态点)。
RuntimeState string `json:"runtime_state,omitempty"`
}
NotificationConfDTO 是对 models.NotificationConf 的对外只读视图,不含密文字段。
type NotificationService ¶
type NotificationService interface {
ListConfs(ctx context.Context) ([]NotificationConfDTO, error)
GetConf(ctx context.Context, id uint) (NotificationConfDTO, error)
CreateConf(ctx context.Context, req CreateConfReq) (NotificationConfDTO, error)
UpdateConf(ctx context.Context, id uint, req UpdateConfReq) error
DeleteConf(ctx context.Context, id uint) error
TestConf(ctx context.Context, id uint) error
Push(ctx context.Context, n Notification) error
PushSync(ctx context.Context, n Notification) error
Enqueue(ctx context.Context, n Notification, confID uint) error
}
NotificationService 管理通知通道配置与消息投递。
func NewNotificationService ¶
func NewNotificationService(db *gorm.DB, manager NotifyManager, pushTimeout time.Duration) NotificationService
NewNotificationService 构造一个 NotificationService。pushTimeout 为同步投递的最长等待时间, 超时后会落到 notification_outbox 表由后台 worker 异步重试。
type NotificationServiceForRSS ¶
type NotificationServiceForRSS interface {
Push(ctx context.Context, n Notification) error
}
type NotifyManager ¶
type NotifyManager interface {
Send(ctx context.Context, confID uint, n Notification) error
}
NotifyManager 抽象底层投递。 TODO(T15): 替换为 internal/notify.Manager 接口的真实实现。
type QuietLookupFunc ¶
QuietLookupFunc 返回指定 NotificationConf 的 quiet_hours_start / quiet_hours_end。 用于在 tryDispatch 中按通道判断是否处于静默窗口。
type RSSCallbackActions ¶
type RSSCallbackActions struct {
// contains filtered or unexported fields
}
RSSCallbackActions resolves Telegram inline-button callbacks (dl/ig) against the rss_notification_log row referenced by the button payload.
func NewRSSCallbackActions ¶
func NewRSSCallbackActions(db *gorm.DB, fetcher TorrentDataFetcher) *RSSCallbackActions
NewRSSCallbackActions wires the dependencies needed by Telegram's CallbackActionHandler. fetcher may be nil; OnRSSDownload will then short-circuit with an explanatory error so callers see the wiring gap.
func (*RSSCallbackActions) OnRSSDownload ¶
OnRSSDownload looks up the torrent referenced by logID and pushes it via the existing internal.PushTorrentToDownloader path. The downloader is resolved from RSSSubscription.DownloaderID; when nil, the default downloader is used. last_error is cleared on success and populated on failure so the row in /chatops/rss-notifications reflects reality.
func (*RSSCallbackActions) OnRSSIgnore ¶
OnRSSIgnore marks the underlying log row as suppressed. It is idempotent and safe to call against a row that is already in any terminal state.
type RSSFilteredEvent ¶
type RSSFilteredEvent struct {
RSS *models.RSSConfig
Torrent *v2.TorrentItem
Rule *models.FilterRule
SiteName string
TorrentID string
}
type RSSItemEvent ¶
type RSSNotifier ¶
type RSSNotifier interface {
NotifyNewItem(ctx context.Context, ev RSSItemEvent) error
NotifyFilteredItem(ctx context.Context, ev RSSFilteredEvent) error
}
func NewRSSNotifier ¶
func NewRSSNotifier(db *gorm.DB, notifySvc NotificationServiceForRSS) RSSNotifier
type RSSRetryWorker ¶
type RSSRetryWorker struct {
// contains filtered or unexported fields
}
RSSRetryWorker 周期性扫描 rss_notification_log 中 result='pending' 且 next_retry_at <= now 的行,复用 payload_json 重新尝试投递;连续失败超过 rssRetryMaxAttempts 后标记 'failed'。
func NewRSSRetryWorker ¶
func NewRSSRetryWorker(db *gorm.DB, notifySvc NotificationServiceForRSS) *RSSRetryWorker
func (*RSSRetryWorker) Run ¶
func (w *RSSRetryWorker) Run(ctx context.Context)
Run 阻塞直到 ctx 取消,应作为 goroutine 启动。
func (*RSSRetryWorker) SetLogf ¶
func (w *RSSRetryWorker) SetLogf(fn func(format string, args ...any))
SetLogf 注入日志函数,用于记录查询与状态写回失败。
type SiteLister ¶
type SiteLister interface {
ListSites() (map[models.SiteGroup]models.SiteConfig, error)
}
SiteLister 是 ConfigStore 的最小读视图(便于测试 mock)。*core.ConfigStore 自动满足。
type SiteService ¶
type SiteService interface {
ListSites(ctx context.Context) ([]SiteSummaryDTO, error)
GetSiteUserInfo(ctx context.Context, siteName string) (UserInfoDTO, error)
}
SiteService 为 chatops `/sites` `/userinfo` 等命令提供站点元数据与用户统计。
func NewSiteService ¶
func NewSiteService(store *core.ConfigStore, users UserInfoSource) SiteService
NewSiteService 注入 *core.ConfigStore 与 v2.UserInfoRepo(典型为 *v2.DBUserInfoRepo)。 users 可为 nil,此时 GetSiteUserInfo 永远返回 ErrUserInfoUnavailable。
type SiteSummaryDTO ¶
type SiteSummaryDTO struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
LastScrapedAt time.Time `json:"last_scraped_at"`
Status string `json:"status"`
}
SiteSummaryDTO 用于 chatops `/sites` 命令展示已配置的站点列表概览。
type TaskService ¶
type TaskService interface {
ListJobs(ctx context.Context) ([]JobStatusDTO, error)
StartJob(ctx context.Context, siteName, rssName string) error
StopJob(ctx context.Context, siteName, rssName string) error
}
TaskService 封装 scheduler.Manager 的 RSS 任务生命周期,剥离上层对底层 manager 的直接依赖。
func NewTaskService ¶
func NewTaskService(mgr *scheduler.Manager) TaskService
NewTaskService 构造 TaskService,注入 *scheduler.Manager。
type TorrentDTO ¶
type TorrentDataFetcher ¶
TorrentDataFetcher fetches the raw .torrent payload for a (siteName, torrentID) pair. Callers wire in a closure backed by the v2 site registry so this package stays free of web / site/v2 imports.
type TorrentService ¶
type TorrentService interface {
ListByDownloader(ctx context.Context, downloaderName string, page, pageSize int) (items []TorrentDTO, total int, err error)
Get(ctx context.Context, downloaderName, torrentID string) (TorrentDTO, error)
Pause(ctx context.Context, downloaderName, torrentID string) error
Resume(ctx context.Context, downloaderName, torrentID string) error
Delete(ctx context.Context, downloaderName, torrentID string, removeData bool) error
}
func NewTorrentService ¶
func NewTorrentService(mgr *downloader.DownloaderManager) TorrentService
type UpdateConfReq ¶
type UpdateConfReq struct {
ChannelType *string
Name *string
ConfigJSON json.RawMessage
Enabled *bool
QuietHoursStart *string
QuietHoursEnd *string
}
UpdateConfReq 更新请求,ConfigJSON 为空时不更新对应字段。
type UserInfoDTO ¶
type UserInfoDTO struct {
SiteName string `json:"site_name"`
Username string `json:"username"`
Uploaded string `json:"uploaded"`
Downloaded string `json:"downloaded"`
Ratio string `json:"ratio"`
Bonus string `json:"bonus"`
Class string `json:"class"`
}
UserInfoDTO 是站点用户信息的 chatops 友好快照(字段全部转字符串便于格式化展示)。