diff --git a/daily/push_gate.py b/daily/push_gate.py new file mode 100644 index 0000000..b360de6 --- /dev/null +++ b/daily/push_gate.py @@ -0,0 +1,51 @@ +"""企微早报推送闸门。""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Any + +from daily.config import force_push, skip_push_when_silent + + +@dataclass +class PushGateResult: + should_push: bool + reasons: list[str] = field(default_factory=list) + silent: bool = False + + +def _has_board_moves(movement: dict[str, Any]) -> bool: + keys = ( + "skills_trending_moves", + "skills_hot_moves", + "github_trending_moves", + "github_emerging_moves", + "github_topic_moves", + ) + return any(movement.get(k) for k in keys) + + +def evaluate_push_gate( + *, + movement: dict[str, Any], + ai_news_items: list[dict[str, Any]], + cn_ai_news_items: list[dict[str, Any]], + featured_pick: dict[str, Any] | None, +) -> PushGateResult: + if force_push(): + return PushGateResult(should_push=True, reasons=["force_push"], silent=False) + + reasons: list[str] = [] + if _has_board_moves(movement): + reasons.append("board_moves") + if ai_news_items: + reasons.append("ai_news") + if cn_ai_news_items: + reasons.append("cn_ai_news") + if featured_pick: + reasons.append("featured_pick") + + should = bool(reasons) + silent = not should and skip_push_when_silent() + return PushGateResult(should_push=should, reasons=reasons, silent=silent)