chore: main 병합 — 로그인 없이 댓글·좋아요, 구글 로그인만, 동네 이야기·카드뉴스 등 (차단 표 user_block 하나로 합침, 익명 댓글도 신고 가능)
This commit is contained in:
commit
dba34675c8
408 files changed
+36377
-725
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import hmac
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
@@ -75,3 +76,9 @@ async def current_user(user: Optional[User] = Depends(optional_user)) -> User:
|
||||
if user is None:
|
||||
raise HTTPException(status_code=401, detail=ErrorType.AUTH_REQUIRED.name)
|
||||
return user
|
||||
|
||||
|
||||
async def require_admin(x_admin_key: Optional[str] = Header(None)) -> None:
|
||||
"""관리자 도구(admin/) 전용 API. ADMIN_API_KEY 가 비어 있으면 아무도 못 부른다."""
|
||||
if not settings.admin_api_key or not x_admin_key or not hmac.compare_digest(x_admin_key, settings.admin_api_key):
|
||||
raise HTTPException(status_code=401, detail=ErrorType.AUTH_FAILED.name)
|
||||
@@ -3,7 +3,7 @@ from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import BigInteger, Boolean, Date, DateTime, Integer, Numeric, SmallInteger, String, func
|
||||
from sqlalchemy import BigInteger, Boolean, Date, DateTime, ForeignKey, Integer, LargeBinary, Numeric, SmallInteger, String, Text, func
|
||||
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
@@ -309,6 +309,44 @@ class Post(Base):
|
||||
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
|
||||
|
||||
class CardSet(Base):
|
||||
__tablename__ = "card_set"
|
||||
|
||||
card_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
template: Mapped[str] = mapped_column(String(20))
|
||||
topic: Mapped[Optional[str]] = mapped_column(String(60), nullable=True)
|
||||
title: Mapped[str] = mapped_column(String(80))
|
||||
caption: Mapped[str] = mapped_column(Text, default="")
|
||||
copy: Mapped[dict] = mapped_column(JSONB, default=dict)
|
||||
status: Mapped[int] = mapped_column(SmallInteger, default=1)
|
||||
post_id: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
published_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
ig_media_id: Mapped[Optional[str]] = mapped_column(String(40), nullable=True)
|
||||
hashtags: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
|
||||
|
||||
class CardImage(Base):
|
||||
__tablename__ = "card_image"
|
||||
|
||||
card_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("card_set.card_id", ondelete="CASCADE"), primary_key=True)
|
||||
seq: Mapped[int] = mapped_column(SmallInteger, primary_key=True)
|
||||
data: Mapped[bytes] = mapped_column(LargeBinary)
|
||||
|
||||
|
||||
class Reel(Base):
|
||||
__tablename__ = "reel"
|
||||
|
||||
reel_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(80))
|
||||
caption: Mapped[str] = mapped_column(Text, default="")
|
||||
hashtags: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
video: Mapped[bytes] = mapped_column(LargeBinary)
|
||||
cover: Mapped[Optional[bytes]] = mapped_column(LargeBinary, nullable=True)
|
||||
ig_media_id: Mapped[Optional[str]] = mapped_column(String(40), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
@@ -337,6 +375,14 @@ class PostLike(Base):
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class PostLikeAnon(Base):
|
||||
__tablename__ = "post_like_anon"
|
||||
|
||||
post_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
ip_hash: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class PostSave(Base):
|
||||
__tablename__ = "post_save"
|
||||
|
||||
@@ -350,7 +396,9 @@ class Comment(Base):
|
||||
|
||||
comment_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
post_id: Mapped[int] = mapped_column(BigInteger)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True))
|
||||
user_id: Mapped[Optional[uuid.UUID]] = mapped_column(UUID(as_uuid=True), nullable=True)
|
||||
ip_hash: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
region_name: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
|
||||
body: Mapped[str] = mapped_column(String(60))
|
||||
like_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
report_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
@@ -382,3 +430,65 @@ class CommentLike(Base):
|
||||
comment_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class CommentLikeAnon(Base):
|
||||
__tablename__ = "comment_like_anon"
|
||||
|
||||
comment_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
ip_hash: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class MyGak(Base):
|
||||
__tablename__ = "my_gak"
|
||||
|
||||
my_gak_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True))
|
||||
name: Mapped[str] = mapped_column(String(12))
|
||||
rule: Mapped[dict] = mapped_column(JSONB)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class Talk(Base):
|
||||
__tablename__ = "talk"
|
||||
|
||||
talk_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True))
|
||||
region_code: Mapped[str] = mapped_column(String(10))
|
||||
body: Mapped[str] = mapped_column(String(100))
|
||||
weather: Mapped[Optional[str]] = mapped_column(String(30), nullable=True)
|
||||
image: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)
|
||||
topic: Mapped[int] = mapped_column(SmallInteger, default=1)
|
||||
like_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
report_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
hidden: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
deleted: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
|
||||
|
||||
class TalkLike(Base):
|
||||
__tablename__ = "talk_like"
|
||||
|
||||
talk_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class TalkLikeAnon(Base):
|
||||
__tablename__ = "talk_like_anon"
|
||||
|
||||
talk_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
ip_hash: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
class TalkReport(Base):
|
||||
__tablename__ = "talk_report"
|
||||
|
||||
talk_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
reason: Mapped[int] = mapped_column(SmallInteger)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ class ErrorType(Enum):
|
||||
EXTERNAL_API_ERROR = 2001
|
||||
NOT_FOUND = 3001
|
||||
INVALID_INPUT = 3002
|
||||
DAILY_LIMIT = 3003 # 하루에 쓸 수 있는 수를 넘음
|
||||
LINK_NOT_ALLOWED = 3004 # 글에 링크·전화번호
|
||||
AUTH_REQUIRED = 4001
|
||||
AUTH_FAILED = 4002
|
||||
AUTH_DUPLICATE_ID = 4003
|
||||
@@ -114,6 +116,23 @@ class IndexCategory(CodeEnum):
|
||||
LIFE = 4
|
||||
|
||||
|
||||
class TalkTopic(CodeEnum):
|
||||
"""사람 글 주제. 안 고르면 동네 이야기. 피드에서 같은 주제 포스트 옆에 묶어 보여 준다"""
|
||||
|
||||
LOCAL = 1 # 동네 이야기
|
||||
SKY = 2 # 하늘·노을
|
||||
RAIN = 3 # 비·눈
|
||||
FOLIAGE = 4 # 단풍·꽃
|
||||
OUTFIT = 5 # 오늘 옷차림
|
||||
|
||||
|
||||
class TalkReport(CodeEnum):
|
||||
ABUSE = 1 # 욕설·비하
|
||||
SPAM = 2 # 광고·도배
|
||||
PRIVACY = 3 # 개인정보 노출
|
||||
ETC = 4
|
||||
|
||||
|
||||
class ArticleCategory(CodeEnum):
|
||||
SEASON = 1
|
||||
TIP = 2
|
||||
@@ -137,6 +156,12 @@ class PostType(CodeEnum):
|
||||
DAY_TRIP = 13 # 당일치기: 1~2시간 거리 후보 도시 중 주말 날씨 1위 + 그곳 행사
|
||||
MOON = 14 # 오늘 밤 달 (보름·삭이면 그 이야기)
|
||||
UV = 15 # 오늘 낮 자외선 (아침·낮, '보통' 이상인 날만)
|
||||
HERO = 16 # 히어로(오늘 날씨)의 좋아요·댓글 자리. 피드 목록엔 안 나온다
|
||||
TODAY_GAK = 17 # 오늘의 각 카드의 좋아요·댓글 자리. 피드 목록엔 안 나온다
|
||||
FOLIAGE = 18 # 단풍 지도 카드의 좋아요·댓글 자리. 피드 목록엔 안 나온다
|
||||
EVENTS_BAND = 19 # 이번 주 가까운 행사 띠의 좋아요·댓글 자리
|
||||
SEASON_BAND = 20 # 이번 주 계절 이야기 띠의 좋아요·댓글 자리
|
||||
CARD = 21 # 카드뉴스: 관리자(admin/)가 만든 이미지 묶음을 그날 전국 피드에 올린다
|
||||
|
||||
|
||||
class EventKind(CodeEnum):
|
||||
@@ -155,6 +180,13 @@ class RefType(CodeEnum):
|
||||
INDEX = 2
|
||||
ARTICLE = 3
|
||||
EVENT = 4
|
||||
CARD = 5
|
||||
|
||||
|
||||
class CardStatus(CodeEnum):
|
||||
WAITING = 1 # 승인 대기
|
||||
PUBLISHED = 2
|
||||
REJECTED = 3
|
||||
|
||||
|
||||
class AuthProvider(CodeEnum):
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import hashlib
|
||||
import hmac
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, Request
|
||||
|
||||
from common.auth import optional_user
|
||||
from common.database.model.models import User
|
||||
from config.settings import settings
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Liker:
|
||||
"""좋아요를 누른 사람. 로그인했으면 user_id, 아니면 IP 해시로 한 번만 센다."""
|
||||
user_id: Optional[uuid.UUID]
|
||||
ip_hash: str
|
||||
|
||||
|
||||
def client_ip(request: Request) -> str:
|
||||
"""nginx 가 X-Forwarded-For 맨 끝에 붙인 접속 주소만 믿는다. 앞쪽은 클라이언트가 마음대로 넣을 수 있다."""
|
||||
xff = request.headers.get("x-forwarded-for", "")
|
||||
last = xff.rsplit(",", 1)[-1].strip()
|
||||
if last:
|
||||
return last
|
||||
return request.client.host if request.client else ""
|
||||
|
||||
|
||||
def ip_hash(ip: str) -> str:
|
||||
"""IP 원문은 저장하지 않는다. 서버 비밀값으로 HMAC 한 값만 남긴다."""
|
||||
if not settings.jwt_refresh_secret:
|
||||
raise RuntimeError("JWT_REFRESH_SECRET is empty")
|
||||
return hmac.new(settings.jwt_refresh_secret.encode(), f"like-ip:{ip}".encode(), hashlib.sha256).hexdigest()
|
||||
|
||||
|
||||
async def liker(request: Request, user: Optional[User] = Depends(optional_user)) -> Liker:
|
||||
return Liker(user_id=user.user_id if user else None, ip_hash=ip_hash(client_ip(request)))
|
||||
Reference in new issue
Block a user