feat: 오늘 피드 — 사람 글을 묶음 대신 포스트처럼 두 장마다 한 장씩(큰 카드·댓글), 행사는 띠 대신 축제 전부와 팝업·전시·공연 2개씩 포스트로
- 사람 글 큰 카드: 머리줄·4:5 본문(사진, 없으면 글을 크게)·좋아요·댓글·보내기 - 사람 글 댓글: 로그인한 사람만, comment 표에 talk_id, talk.comment_count (init.sql) - '오늘 내 글'·'동네 사람들'·주제별 묶음, '이번 주 가까운 행사' 띠 제거 - 행사 포스트 자리 최대 40개, 앞쪽 절반에 다 못 들어가면 끝까지 한 장씩 걸러 - GA: feed_item_view(talk·event 노출), talk_comment
This commit is contained in:
1 parent
f6cb6f2deb
commit
dba3d78f14
22 files changed
+653
-318
No files matched your search
@@ -6,12 +6,20 @@ from sqlalchemy import delete, func, or_, select, update
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from common.database.model.models import Comment, CommentLike, CommentLikeAnon, CommentReport, Post, PostLike, PostLikeAnon, PostSave, User, UserBlock
|
||||
from common.database.model.models import Comment, CommentLike, CommentLikeAnon, CommentReport, Post, PostLike, PostLikeAnon, PostSave, Talk, User, UserBlock
|
||||
from common.liker import Liker
|
||||
|
||||
HIDE_AT = 3 # 신고가 이만큼 쌓이면 모두에게서 숨긴다
|
||||
|
||||
|
||||
async def _count(s: AsyncSession, post_id: Optional[int], talk_id: Optional[int], delta: int) -> None:
|
||||
"""댓글이 달린 곳(사람 글 또는 포스트)의 댓글 수를 고친다."""
|
||||
if talk_id is not None:
|
||||
await s.execute(update(Talk).where(Talk.talk_id == talk_id).values(comment_count=Talk.comment_count + delta))
|
||||
elif post_id is not None:
|
||||
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count + delta))
|
||||
|
||||
|
||||
async def toggle_like_row(s: AsyncSession, who: Liker, member: type, anon: type, id_name: str, id_value: int, on: bool) -> bool:
|
||||
"""로그인했으면 member 표에 user_id 로, 아니면 anon 표에 ip_hash 로 넣거나 지운다. 실제로 바뀌었으면 True."""
|
||||
model, keys = (member, {id_name: id_value, "user_id": who.user_id}) if who.user_id is not None else (anon, {id_name: id_value, "ip_hash": who.ip_hash})
|
||||
@@ -54,10 +62,13 @@ class ReactionCRUD:
|
||||
stmt = stmt.where(Post.post_id < cursor)
|
||||
return list((await s.execute(stmt.order_by(Post.post_id.desc()).limit(limit))).all())
|
||||
|
||||
async def comments(self, s: AsyncSession, post_id: int, viewer: Optional[uuid.UUID], cursor: Optional[int], limit: int) -> list[tuple[Comment, Optional[str], Optional[str], Optional[str], Optional[str]]]:
|
||||
"""로그인 없이 쓴 댓글은 User 칸이 모두 None 이다. 숨긴 댓글은 빼고, 보는 사람이 신고한 댓글·차단한 사람의 댓글도 뺀다."""
|
||||
async def comments(
|
||||
self, s: AsyncSession, post_id: Optional[int], viewer: Optional[uuid.UUID], cursor: Optional[int], limit: int, talk_id: Optional[int] = None,
|
||||
) -> list[tuple[Comment, Optional[str], Optional[str], Optional[str], Optional[str]]]:
|
||||
"""포스트(post_id) 또는 사람 글(talk_id)의 댓글. 로그인 없이 쓴 댓글은 User 칸이 모두 None 이다. 숨긴 댓글은 빼고, 보는 사람이 신고한 댓글·차단한 사람의 댓글도 뺀다."""
|
||||
target = Comment.talk_id == talk_id if talk_id is not None else Comment.post_id == post_id
|
||||
stmt = select(Comment, User.nickname, User.character, User.color, User.mood).outerjoin(User, User.user_id == Comment.user_id).where(
|
||||
Comment.post_id == post_id, Comment.deleted.is_(False), Comment.is_hidden.is_(False)
|
||||
target, Comment.deleted.is_(False), Comment.is_hidden.is_(False)
|
||||
)
|
||||
if viewer is not None:
|
||||
stmt = stmt.where(
|
||||
@@ -76,10 +87,11 @@ class ReactionCRUD:
|
||||
stmt = select(CommentLikeAnon.comment_id).where(CommentLikeAnon.ip_hash == ip_hash, CommentLikeAnon.comment_id.in_(comment_ids))
|
||||
return set((await s.scalars(stmt)).all())
|
||||
|
||||
async def add_comment(self, s: AsyncSession, post_id: int, who: Liker, body: str, region_name: Optional[str]) -> Comment:
|
||||
c = Comment(post_id=post_id, user_id=who.user_id, body=body) if who.user_id is not None else Comment(post_id=post_id, ip_hash=who.ip_hash, region_name=region_name, body=body)
|
||||
async def add_comment(self, s: AsyncSession, post_id: Optional[int], who: Liker, body: str, region_name: Optional[str], talk_id: Optional[int] = None) -> Comment:
|
||||
c = (Comment(post_id=post_id, talk_id=talk_id, user_id=who.user_id, body=body) if who.user_id is not None
|
||||
else Comment(post_id=post_id, talk_id=talk_id, ip_hash=who.ip_hash, region_name=region_name, body=body))
|
||||
s.add(c)
|
||||
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count + 1))
|
||||
await _count(s, post_id, talk_id, 1)
|
||||
await s.flush()
|
||||
return c
|
||||
|
||||
@@ -90,14 +102,14 @@ class ReactionCRUD:
|
||||
owner = Comment.user_id == who.user_id if who.user_id is not None else (Comment.user_id.is_(None) & (Comment.ip_hash == who.ip_hash))
|
||||
res = await s.execute(
|
||||
update(Comment).where(Comment.comment_id == comment_id, owner, Comment.deleted.is_(False))
|
||||
.values(deleted=True).returning(Comment.post_id, Comment.is_hidden)
|
||||
.values(deleted=True).returning(Comment.post_id, Comment.talk_id, Comment.is_hidden)
|
||||
)
|
||||
row = res.first()
|
||||
if row is None:
|
||||
return False
|
||||
# 숨긴 댓글은 이미 댓글 수에서 빠져 있다
|
||||
if not row.is_hidden:
|
||||
await s.execute(update(Post).where(Post.post_id == row.post_id).values(comment_count=Post.comment_count - 1))
|
||||
await _count(s, row.post_id, row.talk_id, -1)
|
||||
return True
|
||||
|
||||
async def toggle_comment_like(self, s: AsyncSession, comment_id: int, who: Liker, on: bool) -> Optional[int]:
|
||||
@@ -120,22 +132,22 @@ class ReactionCRUD:
|
||||
await s.execute(update(Comment).where(Comment.comment_id == comment_id).values(report_count=Comment.report_count + 1))
|
||||
res = await s.execute(
|
||||
update(Comment).where(Comment.comment_id == comment_id, Comment.report_count >= HIDE_AT, Comment.is_hidden.is_(False), Comment.deleted.is_(False))
|
||||
.values(is_hidden=True).returning(Comment.post_id)
|
||||
.values(is_hidden=True).returning(Comment.post_id, Comment.talk_id)
|
||||
)
|
||||
post_id = res.scalar()
|
||||
if post_id is not None:
|
||||
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count - 1))
|
||||
row = res.first()
|
||||
if row is not None:
|
||||
await _count(s, row.post_id, row.talk_id, -1)
|
||||
|
||||
async def set_hidden(self, s: AsyncSession, comment_id: int, hidden: bool) -> bool:
|
||||
"""운영자가 댓글을 숨기거나 되살린다. 실제로 바뀌었을 때만 댓글 수를 고친다."""
|
||||
res = await s.execute(
|
||||
update(Comment).where(Comment.comment_id == comment_id, Comment.is_hidden.is_(not hidden), Comment.deleted.is_(False))
|
||||
.values(is_hidden=hidden).returning(Comment.post_id)
|
||||
.values(is_hidden=hidden).returning(Comment.post_id, Comment.talk_id)
|
||||
)
|
||||
post_id = res.scalar()
|
||||
if post_id is None:
|
||||
row = res.first()
|
||||
if row is None:
|
||||
return False
|
||||
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count + (-1 if hidden else 1)))
|
||||
await _count(s, row.post_id, row.talk_id, -1 if hidden else 1)
|
||||
return True
|
||||
|
||||
async def block(self, s: AsyncSession, user_id: uuid.UUID, blocked_id: uuid.UUID) -> None:
|
||||
|
||||
@@ -6,7 +6,7 @@ from typing import Optional
|
||||
from sqlalchemy import delete, func, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from common.database.model.models import Comment, CommentLike, CommentReport, PointLog, Post, PostLike, PostSave, User, UserBlock
|
||||
from common.database.model.models import Comment, CommentLike, CommentReport, PointLog, Post, PostLike, PostSave, Talk, User, UserBlock
|
||||
|
||||
|
||||
class UserCRUD:
|
||||
@@ -74,12 +74,17 @@ class UserCRUD:
|
||||
if reported:
|
||||
await s.execute(update(Comment).where(Comment.comment_id.in_(reported)).values(report_count=Comment.report_count - 1))
|
||||
await s.execute(delete(UserBlock).where((UserBlock.user_id == user_id) | (UserBlock.blocked_id == user_id)))
|
||||
mine = (await s.execute(delete(Comment).where(Comment.user_id == user_id).returning(Comment.comment_id, Comment.post_id, Comment.deleted, Comment.is_hidden))).all()
|
||||
mine = (await s.execute(
|
||||
delete(Comment).where(Comment.user_id == user_id).returning(Comment.comment_id, Comment.post_id, Comment.talk_id, Comment.deleted, Comment.is_hidden)
|
||||
)).all()
|
||||
if mine:
|
||||
ids = [c for c, _, _, _ in mine]
|
||||
ids = [c for c, *_ in mine]
|
||||
await s.execute(delete(CommentLike).where(CommentLike.comment_id.in_(ids)))
|
||||
await s.execute(delete(CommentReport).where(CommentReport.comment_id.in_(ids)))
|
||||
# 이미 지웠거나 숨긴 댓글은 댓글 수에서 빠져 있다
|
||||
for post_id, n in Counter(p for _, p, deleted, hidden in mine if not deleted and not hidden).items():
|
||||
shown = [(p, t) for _, p, t, deleted, hidden in mine if not deleted and not hidden]
|
||||
for post_id, n in Counter(p for p, t in shown if t is None).items():
|
||||
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count - n))
|
||||
for talk_id, n in Counter(t for _, t in shown if t is not None).items():
|
||||
await s.execute(update(Talk).where(Talk.talk_id == talk_id).values(comment_count=Talk.comment_count - n))
|
||||
await s.execute(delete(User).where(User.user_id == user_id))
|
||||
Reference in new issue
Block a user