fix:小组件- PUSH

This commit is contained in:
吕新雨
2026-02-03 17:43:58 +08:00
parent d742b398ef
commit c1c2c6197d
66 changed files with 4888 additions and 479 deletions

View File

@@ -9,6 +9,9 @@
from app.db.models.content import Content
from app.db.models.content_profile import ContentProfile
from app.db.models.content_risk_flag import ContentRiskFlag
from app.db.models.push_preference import PushPreference
from app.db.models.push_send_log import PushSendLog
from app.db.models.push_token import PushToken
__all__ = ["Content", "ContentProfile", "ContentRiskFlag"]
__all__ = ["Content", "ContentProfile", "ContentRiskFlag", "PushPreference", "PushSendLog", "PushToken"]

View File

@@ -0,0 +1,63 @@
from __future__ import annotations
from datetime import datetime
from sqlalchemy import Boolean, DateTime, JSON, SmallInteger, String, func
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base
class PushPreference(Base):
"""
用户推送偏好(以 client_user_id 为主键)。
"""
__tablename__ = "push_preferences"
client_user_id: Mapped[str] = mapped_column(
String(length=64),
primary_key=True,
comment="客户端用户标识UUID",
)
enabled: Mapped[bool] = mapped_column(
Boolean,
nullable=False,
server_default="0",
comment="是否开启每日提醒enabled=false 或 times_per_day=0 均视为关闭)",
)
times_per_day: Mapped[int] = mapped_column(
SmallInteger,
nullable=False,
server_default="0",
comment="每天推送次数05",
)
timezone: Mapped[str | None] = mapped_column(
String(length=64),
nullable=True,
comment="IANA 时区(例如 Asia/Shanghai来自客户端上报",
)
locale: Mapped[str | None] = mapped_column(
String(length=32),
nullable=True,
comment="客户端语言(例如 zh-CN/en/zh-TW用于文案语言选择",
)
user_profile_json: Mapped[dict | None] = mapped_column(
JSON,
nullable=True,
comment="用户画像V1.2;可选)。用于 Push 场景推荐文案生成。",
)
updated_at: Mapped[datetime] = mapped_column(
DateTime,
nullable=False,
server_default=func.now(),
server_onupdate=func.now(),
comment="更新时间",
)

View File

@@ -0,0 +1,37 @@
from __future__ import annotations
from datetime import date, datetime
from sqlalchemy import Date, DateTime, Index, SmallInteger, String, Text, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base
class PushSendLog(Base):
"""
推送发送日志(用于幂等防重复 + 可观测)。
"""
__tablename__ = "push_send_log"
__table_args__ = (
UniqueConstraint("client_user_id", "local_date", "slot_index", name="uniq_user_date_slot"),
Index("idx_push_log_user_date", "client_user_id", "local_date"),
Index("idx_push_log_status", "status"),
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, comment="主键")
client_user_id: Mapped[str] = mapped_column(String(length=64), nullable=False, comment="客户端用户标识UUID")
local_date: Mapped[date] = mapped_column(Date, nullable=False, comment="用户时区的本地日期(用于幂等)")
slot_index: Mapped[int] = mapped_column(SmallInteger, nullable=False, comment="当天第几条1..times_per_day")
scheduled_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, comment="计划发送时间UTC")
sent_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="实际发送时间UTC")
status: Mapped[str] = mapped_column(String(length=16), nullable=False, server_default="scheduled", comment="scheduled/sent/failed")
error: Mapped[str | None] = mapped_column(Text, nullable=True, comment="失败原因(可选)")
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.now(), comment="创建时间")

View File

@@ -0,0 +1,63 @@
from __future__ import annotations
from datetime import datetime
from typing import Literal
from sqlalchemy import Boolean, DateTime, Enum, Index, String, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base
PushEnv = Literal["dev", "prod"]
PushPlatform = Literal["ios", "android"]
class PushToken(Base):
"""
Push Token 绑定表Expo Push Token
约束:
- token 在同一 env + app_id 下必须唯一(避免重复推送)
- 同一 client_user_id 可能会更新 token重装/轮换/重新授权)
"""
__tablename__ = "push_tokens"
__table_args__ = (
UniqueConstraint("env", "app_id", "push_token", name="uniq_env_app_token"),
Index("idx_push_tokens_client_user_id", "client_user_id"),
Index("idx_push_tokens_is_active", "is_active"),
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, comment="主键")
client_user_id: Mapped[str] = mapped_column(String(length=64), nullable=False, comment="客户端用户标识UUID")
platform: Mapped[PushPlatform] = mapped_column(
Enum("ios", "android", name="push_platform"),
nullable=False,
comment="平台",
)
push_token: Mapped[str] = mapped_column(String(length=255), nullable=False, comment="Expo Push Token")
app_id: Mapped[str] = mapped_column(String(length=255), nullable=False, comment="bundle id / package name用于隔离")
env: Mapped[PushEnv] = mapped_column(
Enum("dev", "prod", name="push_env"),
nullable=False,
comment="环境隔离",
)
is_active: Mapped[bool] = mapped_column(
Boolean,
nullable=False,
server_default="1",
comment="是否有效(发送失败且不可恢复时置为 false",
)
last_seen_at: Mapped[datetime] = mapped_column(
DateTime,
nullable=False,
server_default=func.now(),
server_onupdate=func.now(),
comment="最后一次上报时间",
)