feat: initial MVP - FastAPI backend + Vue3 frontend + docker-compose

- backend: FastAPI + SQLAlchemy 2.0(async) + asyncpg + Alembic
- 7 API routes: auth/me/articles/sources/bookmarks/subscriptions/admin
- models: User/Source/Article/Bookmark/Subscription/ApiToken
- services: RSS fetcher (feedparser) + Tencent TMT translator with quota + cache + local NLLB fallback
- workers: APScheduler + asyncio pipeline (fetch -> dedupe -> insert -> translate)
- seed scripts: create_user, seed_sources (5 RSS: Reuters/BBC/Al Jazeera/NHK/DW)
- frontend: Vue 3 + Vite + Naive UI + Pinia + vue-router
- pages: Login, Feed (24h), ArticleDetail, Sources, Bookmarks, AdminSources
- deploy: docker-compose (postgres/redis/api/worker/frontend/caddy)
- docs: README, DEPLOY, architecture, acceptance
This commit is contained in:
Mavis
2026-06-07 21:51:01 +08:00
commit 60b062daf2
81 changed files with 5540 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
"""所有 ORM 模型。
新模型请在这里 import,确保 Alembic 自动发现。
"""
from app.models.api_token import ApiToken # noqa: F401
from app.models.article import Article # noqa: F401
from app.models.bookmark import Bookmark # noqa: F401
from app.models.source import Source, SourceKind # noqa: F401
from app.models.subscription import Subscription # noqa: F401
from app.models.user import User, UserRole # noqa: F401
__all__ = [
"ApiToken",
"Article",
"Bookmark",
"Source",
"SourceKind",
"Subscription",
"User",
"UserRole",
]

View File

@@ -0,0 +1,27 @@
"""API Token(给 Android 用,可独立撤销)。"""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, String, func
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class ApiToken(Base):
__tablename__ = "api_tokens"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(
ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(64), nullable=False) # "Xiaomi-14"
token_hash: Mapped[str] = mapped_column(String(128), unique=True, nullable=False, index=True)
# 只存 hash,原始 token 一次性返回给用户
last_used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
revoked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)

View File

@@ -0,0 +1,91 @@
"""文章主表:原文 + 译文 + ML 字段预留。"""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import (
BigInteger,
DateTime,
Float,
ForeignKey,
Index,
Integer,
String,
Text,
func,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class Article(Base):
__tablename__ = "articles"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
# === 来源 ===
source_id: Mapped[int] = mapped_column(
ForeignKey("sources.id", ondelete="CASCADE"), nullable=False, index=True
)
source: Mapped["Source"] = relationship(back_populates="articles", lazy="joined") # noqa: F821
# === 原文标识 ===
url: Mapped[str] = mapped_column(Text, nullable=False)
url_hash: Mapped[str] = mapped_column(String(40), unique=True, nullable=False, index=True)
guid: Mapped[str | None] = mapped_column(String(255), index=True) # 源站给的 ID
# === 原文内容 ===
title: Mapped[str] = mapped_column(Text, nullable=False)
body_html: Mapped[str | None] = mapped_column(Text) # 抽取后保留结构
body_text: Mapped[str] = mapped_column(Text, nullable=False, default="")
lang_src: Mapped[str | None] = mapped_column(String(8))
author: Mapped[str | None] = mapped_column(String(255))
image_url: Mapped[str | None] = mapped_column(Text)
# === 译文 ===
title_zh: Mapped[str | None] = mapped_column(Text)
body_zh_html: Mapped[str | None] = mapped_column(Text)
body_zh_text: Mapped[str | None] = mapped_column(Text)
summary_zh: Mapped[str | None] = mapped_column(Text)
# === 翻译状态 ===
translation_status: Mapped[str] = mapped_column(
String(16), default="pending", nullable=False, index=True
)
# pending / ok / partial / failed / n/a
translation_engine: Mapped[str | None] = mapped_column(String(16))
# tencent / nllb / cache / skip
translation_chars: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
translated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
# === ML 字段(预留,MVP 全 null)===
category: Mapped[str | None] = mapped_column(String(32), index=True)
commentary: Mapped[str | None] = mapped_column(Text)
entities: Mapped[dict | None] = mapped_column(JSONB)
sentiment: Mapped[float | None] = mapped_column(Float)
topic_id: Mapped[str | None] = mapped_column(String(64), index=True)
bias: Mapped[str | None] = mapped_column(String(16)) # left/center/right
# === 去重 ===
duplicate_of: Mapped[int | None] = mapped_column(
ForeignKey("articles.id", ondelete="SET NULL"), index=True
)
# === 时间 ===
published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), index=True)
fetched_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False, index=True
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
__table_args__ = (
Index("ix_articles_source_published", "source_id", "published_at"),
Index("ix_articles_status_published", "translation_status", "published_at"),
)
def __repr__(self) -> str:
return f"<Article id={self.id} src={self.source_id} status={self.translation_status}>"

View File

@@ -0,0 +1,27 @@
"""收藏。"""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class Bookmark(Base):
__tablename__ = "bookmarks"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(
ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
article_id: Mapped[int] = mapped_column(
ForeignKey("articles.id", ondelete="CASCADE"), nullable=False, index=True
)
note: Mapped[str | None] = mapped_column()
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
__table_args__ = (UniqueConstraint("user_id", "article_id", name="uq_bookmark_user_article"),)

View File

@@ -0,0 +1,64 @@
"""采集源模型。"""
from __future__ import annotations
import enum
from datetime import datetime
from sqlalchemy import (
JSON,
Boolean,
DateTime,
Enum,
Integer,
String,
Text,
func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class SourceKind(str, enum.Enum):
RSS = "rss"
HTML_LIST = "html_list"
TG_CHANNEL = "tg_channel"
class Source(Base):
__tablename__ = "sources"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(128), nullable=False)
slug: Mapped[str] = mapped_column(String(128), unique=True, index=True, nullable=False)
kind: Mapped[SourceKind] = mapped_column(
Enum(SourceKind, name="source_kind"),
default=SourceKind.RSS,
nullable=False,
)
url: Mapped[str] = mapped_column(Text, nullable=False)
detail_selector: Mapped[dict | None] = mapped_column(JSON)
fetch_interval_min: Mapped[int] = mapped_column(Integer, default=60, nullable=False)
fetch_cron: Mapped[str | None] = mapped_column(String(64)) # 5 段 cron
translate_to: Mapped[str] = mapped_column(String(8), default="zh", nullable=False)
enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
region: Mapped[str | None] = mapped_column(String(32), index=True)
language_src: Mapped[str | None] = mapped_column(String(8))
priority: Mapped[int] = mapped_column(Integer, default=50, nullable=False, index=True)
headers_json: Mapped[dict | None] = mapped_column(JSON)
last_fetched_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
last_status: Mapped[str | None] = mapped_column(String(64))
consecutive_failures: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
)
articles: Mapped[list["Article"]] = relationship( # noqa: F821
back_populates="source", cascade="all, delete-orphan", lazy="noload"
)
def __repr__(self) -> str:
return f"<Source id={self.id} slug={self.slug} kind={self.kind.value}>"

View File

@@ -0,0 +1,48 @@
"""关键词订阅(命中即通知)。"""
from __future__ import annotations
import enum
from datetime import datetime
from sqlalchemy import (
Boolean,
DateTime,
Enum,
ForeignKey,
String,
Text,
func,
)
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class SubscriptionMatch(str, enum.Enum):
ANY = "any" # 标题或正文
TITLE = "title"
BODY = "body"
class Subscription(Base):
__tablename__ = "subscriptions"
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(
ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
keyword: Mapped[str] = mapped_column(String(255), nullable=False)
# 简单关键词,匹配走 ILIKE '%kw%';后续可加 regex/lucene
match_in: Mapped[SubscriptionMatch] = mapped_column(
Enum(SubscriptionMatch, name="subscription_match"),
default=SubscriptionMatch.ANY,
nullable=False,
)
channel: Mapped[str] = mapped_column(String(32), default="telegram", nullable=False)
# telegram / email / web
target: Mapped[str | None] = mapped_column(Text) # chat_id / email
enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
last_hit_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)

View File

@@ -0,0 +1,41 @@
"""用户模型。
Phase 1 仅 owner + member 两级,后续扩展。
"""
from __future__ import annotations
import enum
from datetime import datetime
from sqlalchemy import Boolean, DateTime, Enum, String, func
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class UserRole(str, enum.Enum):
OWNER = "owner"
MEMBER = "member"
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
username: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
email: Mapped[str | None] = mapped_column(String(255), unique=True, index=True)
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
role: Mapped[UserRole] = mapped_column(
Enum(UserRole, name="user_role"),
default=UserRole.MEMBER,
nullable=False,
)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
display_name: Mapped[str | None] = mapped_column(String(128))
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
last_login_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
def __repr__(self) -> str:
return f"<User id={self.id} username={self.username} role={self.role.value}>"