feat: 添加飞书用户模块和订阅功能支持

- 新增feishu_users模块用于处理飞书用户身份验证和权限管理
- 新增subscriptions模块用于处理订阅相关功能
- 新增personalization模块用于个性化服务
- 在alembic迁移配置中注册新的模型模块
- 在API路由器中添加feishu_users和subscriptions路由
- 实现事件调度服务的改进,包括错误处理和状态更新优化
- 添加飞书命令处理的权限检查机制
- 实现飞书应用票据事件处理
- 改进审计日志记录功能
```
This commit is contained in:
2026-07-27 08:02:17 +08:00
parent db751f03b4
commit d7db84571d
148 changed files with 17110 additions and 765 deletions

View File

@@ -7,7 +7,7 @@ from sqlalchemy.orm import Session
from app.core.database import get_db
from app.core.background.task_queue.market import enqueue_market_report
from app.core.security import ApiPrincipal, require_api_key, require_operations_enabled
from app.core.security import ApiPrincipal, require_api_key
from app.modules.market.service import MarketService
router = APIRouter(dependencies=[Depends(require_api_key)])
@@ -89,13 +89,11 @@ def announcements(
@router.post("/sync/daily")
def sync_daily(trade_date: date, db: Session = Depends(get_db)) -> dict:
require_operations_enabled()
return MarketService(db).sync_daily(trade_date)
@router.post("/sync/macro")
def sync_macro(reference_date: date | None = None, db: Session = Depends(get_db)) -> dict:
require_operations_enabled()
return MarketService(db).sync_macro(reference_date)
@@ -103,13 +101,11 @@ def sync_macro(reference_date: date | None = None, db: Session = Depends(get_db)
def sync_announcements(
start_date: date, end_date: date, db: Session = Depends(get_db)
) -> dict:
require_operations_enabled()
return {"processed": MarketService(db).sync_announcements(start_date, end_date)}
@router.post("/reports/enqueue")
def enqueue_report(payload: MarketReportRequest) -> dict:
require_operations_enabled()
return enqueue_market_report(payload.report_type, payload.reference_date, payload.force)
@@ -119,7 +115,6 @@ def add_watchlist(
db: Session = Depends(get_db),
principal: ApiPrincipal = Depends(require_api_key),
) -> dict:
require_operations_enabled()
return MarketService(db).add_watchlist(principal.actor, payload.symbol)

View File

@@ -780,15 +780,31 @@ class MarketService:
report["content"] = "\n".join(lines)
return report
def add_watchlist(self, actor: str, symbol: str) -> dict[str, Any]:
def add_watchlist(
self,
actor: str,
symbol: str,
owner_id: int | None = None,
) -> dict[str, Any]:
code = normalize_symbol(symbol)
owner_clause = (
MarketWatchlist.owner_id.is_(None)
if owner_id is None
else MarketWatchlist.owner_id == owner_id
)
record = self.db.execute(
select(MarketWatchlist).where(
MarketWatchlist.actor == actor, MarketWatchlist.symbol == code
owner_clause,
MarketWatchlist.symbol == code,
*(
(MarketWatchlist.actor == actor,)
if owner_id is None
else ()
),
)
).scalar_one_or_none()
if record is None:
record = MarketWatchlist(actor=actor, symbol=code)
record = MarketWatchlist(owner_id=owner_id, actor=actor, symbol=code)
self.db.add(record)
else:
record.enabled = True
@@ -804,16 +820,77 @@ class MarketService:
response_payload={"enabled": True},
)
)
return {"actor": actor, "symbol": code, "enabled": True}
return {
"actor": actor,
"owner_id": owner_id,
"symbol": code,
"enabled": True,
}
def watchlist(self, actor: str) -> list[dict[str, Any]]:
def watchlist(
self,
actor: str,
owner_id: int | None = None,
) -> list[dict[str, Any]]:
owner_clause = (
MarketWatchlist.owner_id.is_(None)
if owner_id is None
else MarketWatchlist.owner_id == owner_id
)
records = self.db.execute(
select(MarketWatchlist).where(
MarketWatchlist.actor == actor, MarketWatchlist.enabled.is_(True)
owner_clause,
MarketWatchlist.enabled.is_(True),
*(
(MarketWatchlist.actor == actor,)
if owner_id is None
else ()
),
)
).scalars()
return [{"symbol": r.symbol} for r in records]
def claim_legacy_watchlist(self, owner_id: int, open_id: str) -> int:
"""Claim still-unowned rows created by the verified legacy Feishu actor."""
legacy_records = list(
self.db.execute(
select(MarketWatchlist).where(
MarketWatchlist.owner_id.is_(None),
MarketWatchlist.actor == open_id,
)
).scalars()
)
claimed = 0
for legacy in legacy_records:
existing = self.db.execute(
select(MarketWatchlist).where(
MarketWatchlist.owner_id == owner_id,
MarketWatchlist.symbol == legacy.symbol,
)
).scalar_one_or_none()
if existing is not None:
existing.enabled = existing.enabled or legacy.enabled
self.db.delete(legacy)
continue
legacy.owner_id = owner_id
claimed += 1
self.db.commit()
return claimed
def delete_owner_watchlist(self, owner_id: int) -> int:
"""Stage deletion of all personal watchlist rows for an owner."""
records = list(
self.db.execute(
select(MarketWatchlist).where(MarketWatchlist.owner_id == owner_id)
).scalars()
)
for record in records:
self.db.delete(record)
self.db.flush()
return len(records)
def _ai(self, skill: AISkillId, report: dict[str, Any], actor: str) -> dict[str, Any]:
try:
result = AIService(self.db).run_skill(