65 lines
2.6 KiB
Python
65 lines
2.6 KiB
Python
"""운영자용 댓글·사용자 조치.
|
|
|
|
python scripts/moderate.py reports 신고 받은 댓글 (많은 순 30개)
|
|
python scripts/moderate.py hide <comment_id> 댓글 숨김
|
|
python scripts/moderate.py show <comment_id> 숨긴 댓글 되살림
|
|
python scripts/moderate.py suspend <login_id> 이용 정지 (로그인·토큰 막힘)
|
|
python scripts/moderate.py resume <login_id> 정지 풀기
|
|
"""
|
|
import asyncio
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from sqlalchemy import select, update # noqa: E402
|
|
|
|
from common.database.db_session_manager import DB_SESSION_MNG # noqa: E402
|
|
from common.database.model.models import Comment, User # noqa: E402
|
|
from common.enums import ErrorType # noqa: E402
|
|
from crud.reaction_crud import ReactionCRUD # noqa: E402
|
|
|
|
|
|
async def _reports(s):
|
|
stmt = (
|
|
select(Comment.comment_id, Comment.report_count, Comment.is_hidden, User.login_id, Comment.body)
|
|
.join(User, User.user_id == Comment.user_id)
|
|
.where(Comment.report_count > 0, Comment.deleted.is_(False))
|
|
.order_by(Comment.report_count.desc(), Comment.comment_id.desc())
|
|
.limit(30)
|
|
)
|
|
return (await s.execute(stmt)).all()
|
|
|
|
|
|
async def _set_status(s, login_id: str, status: int) -> bool:
|
|
res = await s.execute(
|
|
update(User).where(User.login_id == login_id, User.deleted.is_(False)).values(status=status, token_version=User.token_version + 1).returning(User.user_id)
|
|
)
|
|
return res.first() is not None
|
|
|
|
|
|
async def main(argv: list[str]) -> int:
|
|
if len(argv) < 1 or argv[0] not in {"reports", "hide", "show", "suspend", "resume"} or (argv[0] != "reports" and len(argv) != 2):
|
|
print(__doc__)
|
|
return 2
|
|
cmd = argv[0]
|
|
if cmd == "reports":
|
|
err, rows = await DB_SESSION_MNG.execute_lambda(_reports)
|
|
for comment_id, count, hidden, login_id, body in rows or []:
|
|
print(f"{comment_id}\t신고 {count}\t{'숨김' if hidden else '보임'}\t{login_id}\t{body}")
|
|
elif cmd in {"hide", "show"}:
|
|
err, ok = await DB_SESSION_MNG.execute_lambda_write(lambda s: ReactionCRUD().set_hidden(s, int(argv[1]), cmd == "hide"))
|
|
print("바뀜" if ok else "그대로 (없는 댓글이거나 이미 그 상태)")
|
|
else:
|
|
err, ok = await DB_SESSION_MNG.execute_lambda_write(lambda s: _set_status(s, argv[1], 2 if cmd == "suspend" else 1))
|
|
print("바뀜" if ok else "없는 아이디")
|
|
await DB_SESSION_MNG.stop()
|
|
if err is not ErrorType.SUCCESS:
|
|
print(err.name)
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(asyncio.run(main(sys.argv[1:])))
|