cmvr-visual/backend/app/services/events.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

75 lines
2.3 KiB
Python

from sqlalchemy import select
from app.core.security import token_hash
from app.services.snapshots import job_dto
from app.models import Event, Session, User, utcnow
# 与业务更新共用调用方事务,禁止改成提交前发往后台的临时通知。
async def emit_job(session, job, *, increment=True):
if increment:
job.version += 1
await session.flush()
session.add(
Event(
owner_id=job.owner_id,
job_id=job.id,
event_type="job.updated",
data={"job": await job_dto(session, job)},
)
)
def emit_asset(session, owner_id, event_type, data):
session.add(Event(owner_id=owner_id, event_type=event_type, data=data))
async def event_batch(db, owner_id, after, limit=100):
async with db.session() as session:
events = (
await session.scalars(
select(Event)
.where(Event.owner_id == owner_id, Event.id > after)
.order_by(Event.id)
.limit(limit)
)
).all()
return [{"id": event.id, "event": event.event_type, "data": event.data} for event in events]
async def session_is_active(db, token):
if not token:
return False
if token.startswith("Bearer "):
from app.models import ApiToken
async with db.session() as session:
return (
await session.scalar(
select(ApiToken.id)
.join(User, User.id == ApiToken.owner_id)
.where(
ApiToken.token_hash == token_hash(token[7:]),
ApiToken.revoked_at.is_(None),
User.disabled.is_(False),
ApiToken.expires_at.is_(None) | (ApiToken.expires_at > utcnow()),
)
)
is not None
)
async with db.session() as session:
return (
await session.scalar(
select(Session.token_hash)
.join(User, User.id == Session.user_id)
.where(
Session.token_hash == token_hash(token),
Session.expires_at > utcnow(),
User.disabled.is_(False),
)
)
is not None
)
emit_event = emit_asset