commit
9c5fef1a4d
412 files changed
+94582
No files matched your search
@@ -0,0 +1,208 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, status
|
||||
from sqlalchemy import select
|
||||
|
||||
from sakura_cloud.api.deps import CurrentUser, DbSession
|
||||
from sakura_cloud.models.follow import CreatorFollow, UpdateAlert
|
||||
from sakura_cloud.models.mixins import utc_now
|
||||
from sakura_cloud.schemas.common import ApiResponse
|
||||
from sakura_cloud.schemas.follow import (
|
||||
CreatorFollowBulkUpsert,
|
||||
CreatorFollowBulkUpsertResult,
|
||||
CreatorFollowOut,
|
||||
CreatorFollowUpsert,
|
||||
UpdateAlertCreate,
|
||||
UpdateAlertOut,
|
||||
UpdateAlertReadUpdate,
|
||||
)
|
||||
from sakura_cloud.services.push import send_update_alert_push
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[list[CreatorFollowOut]])
|
||||
def list_follows(
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
platform: str | None = Query(default=None),
|
||||
enabled: bool | None = Query(default=None),
|
||||
) -> ApiResponse[list[CreatorFollowOut]]:
|
||||
query = select(CreatorFollow).where(CreatorFollow.user_id == current_user.id)
|
||||
if platform:
|
||||
query = query.where(CreatorFollow.platform == platform)
|
||||
if enabled is not None:
|
||||
query = query.where(CreatorFollow.enabled == enabled)
|
||||
rows = db.scalars(query.order_by(CreatorFollow.updated_at.desc())).all()
|
||||
return ApiResponse(data=[CreatorFollowOut.from_row(row) for row in rows])
|
||||
|
||||
|
||||
@router.post("", response_model=ApiResponse[CreatorFollowOut], status_code=status.HTTP_201_CREATED)
|
||||
def upsert_follow(
|
||||
payload: CreatorFollowUpsert,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[CreatorFollowOut]:
|
||||
row, _created = _upsert_follow_row(payload, current_user.id, db)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return ApiResponse(code=201, message="created", data=CreatorFollowOut.from_row(row))
|
||||
|
||||
|
||||
@router.post("/bulk-upsert", response_model=ApiResponse[CreatorFollowBulkUpsertResult])
|
||||
def bulk_upsert_follows(
|
||||
payload: CreatorFollowBulkUpsert,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[CreatorFollowBulkUpsertResult]:
|
||||
created = 0
|
||||
updated = 0
|
||||
skipped = 0
|
||||
rows: list[CreatorFollow] = []
|
||||
seen: set[tuple[str, str]] = set()
|
||||
for item in payload.items:
|
||||
key = (item.platform.strip(), item.creator_id.strip())
|
||||
if not key[0] or not key[1] or key in seen:
|
||||
skipped += 1
|
||||
continue
|
||||
seen.add(key)
|
||||
row, was_created = _upsert_follow_row(item, current_user.id, db)
|
||||
rows.append(row)
|
||||
if was_created:
|
||||
created += 1
|
||||
else:
|
||||
updated += 1
|
||||
db.commit()
|
||||
for row in rows:
|
||||
db.refresh(row)
|
||||
return ApiResponse(
|
||||
data=CreatorFollowBulkUpsertResult(
|
||||
created=created,
|
||||
updated=updated,
|
||||
skipped=skipped,
|
||||
items=[CreatorFollowOut.from_row(row) for row in rows],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _upsert_follow_row(
|
||||
payload: CreatorFollowUpsert,
|
||||
user_id: str,
|
||||
db: DbSession,
|
||||
) -> tuple[CreatorFollow, bool]:
|
||||
row = db.scalar(
|
||||
select(CreatorFollow).where(
|
||||
CreatorFollow.user_id == user_id,
|
||||
CreatorFollow.platform == payload.platform,
|
||||
CreatorFollow.creator_id == payload.creator_id,
|
||||
)
|
||||
)
|
||||
created = row is None
|
||||
if row is None:
|
||||
row = CreatorFollow(
|
||||
user_id=user_id,
|
||||
platform=payload.platform,
|
||||
creator_id=payload.creator_id,
|
||||
)
|
||||
db.add(row)
|
||||
row.creator_name = payload.creator_name
|
||||
row.creator_handle = payload.creator_handle
|
||||
row.profile_url = payload.profile_url
|
||||
row.avatar_url = payload.avatar_url
|
||||
row.enabled = payload.enabled
|
||||
row.latest_published_at = payload.latest_published_at
|
||||
row.metadata_json = payload.metadata
|
||||
row.last_checked_at = payload.last_checked_at or utc_now()
|
||||
return row, created
|
||||
|
||||
|
||||
@router.delete("/{follow_id}", response_model=ApiResponse[dict[str, bool]])
|
||||
def disable_follow(follow_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]:
|
||||
row = db.get(CreatorFollow, follow_id)
|
||||
if row is None or row.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Follow not found")
|
||||
row.enabled = False
|
||||
row.last_checked_at = utc_now()
|
||||
db.commit()
|
||||
return ApiResponse(data={"ok": True})
|
||||
|
||||
|
||||
@router.get("/alerts", response_model=ApiResponse[list[UpdateAlertOut]])
|
||||
def list_alerts(
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
unread_only: bool = Query(default=False),
|
||||
limit: int = Query(default=50, ge=1, le=200),
|
||||
) -> ApiResponse[list[UpdateAlertOut]]:
|
||||
query = select(UpdateAlert).where(UpdateAlert.user_id == current_user.id)
|
||||
if unread_only:
|
||||
query = query.where(UpdateAlert.is_read == False)
|
||||
rows = db.scalars(query.order_by(UpdateAlert.created_at.desc()).limit(limit)).all()
|
||||
return ApiResponse(data=[UpdateAlertOut.model_validate(row) for row in rows])
|
||||
|
||||
|
||||
@router.post("/alerts", response_model=ApiResponse[UpdateAlertOut], status_code=status.HTTP_201_CREATED)
|
||||
def create_alert(
|
||||
payload: UpdateAlertCreate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[UpdateAlertOut]:
|
||||
follow = db.scalar(
|
||||
select(CreatorFollow).where(
|
||||
CreatorFollow.user_id == current_user.id,
|
||||
CreatorFollow.platform == payload.platform,
|
||||
CreatorFollow.creator_id == payload.creator_id,
|
||||
)
|
||||
)
|
||||
existing = db.scalar(
|
||||
select(UpdateAlert).where(
|
||||
UpdateAlert.user_id == current_user.id,
|
||||
UpdateAlert.platform == payload.platform,
|
||||
UpdateAlert.creator_id == payload.creator_id,
|
||||
UpdateAlert.title == payload.title,
|
||||
UpdateAlert.target_url == payload.target_url,
|
||||
)
|
||||
)
|
||||
if existing is not None:
|
||||
if existing.follow_id is None and follow is not None:
|
||||
existing.follow_id = follow.id
|
||||
db.commit()
|
||||
db.refresh(existing)
|
||||
return ApiResponse(code=200, message="exists", data=UpdateAlertOut.model_validate(existing))
|
||||
row = UpdateAlert(
|
||||
user_id=current_user.id,
|
||||
follow_id=follow.id if follow else None,
|
||||
platform=payload.platform,
|
||||
creator_id=payload.creator_id,
|
||||
title=payload.title,
|
||||
body=payload.body,
|
||||
content_type=payload.content_type,
|
||||
target_url=payload.target_url,
|
||||
cover_url=payload.cover_url,
|
||||
payload_json=payload.payload,
|
||||
)
|
||||
if payload.created_at is not None:
|
||||
row.created_at = payload.created_at
|
||||
row.updated_at = payload.created_at
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
send_update_alert_push(db, row)
|
||||
return ApiResponse(code=201, message="created", data=UpdateAlertOut.model_validate(row))
|
||||
|
||||
|
||||
@router.post("/alerts/{alert_id}/read", response_model=ApiResponse[UpdateAlertOut])
|
||||
def mark_alert_read(
|
||||
alert_id: str,
|
||||
payload: UpdateAlertReadUpdate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[UpdateAlertOut]:
|
||||
row = db.get(UpdateAlert, alert_id)
|
||||
if row is None or row.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Alert not found")
|
||||
row.is_read = payload.is_read
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return ApiResponse(data=UpdateAlertOut.model_validate(row))
|
||||
Reference in new issue
Block a user