Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/posts/service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import uuid

from fastapi import HTTPException
from fastapi import BackgroundTasks, HTTPException

from app.category.enums import Category
from app.comments.repository import CommentRepository
Expand All @@ -27,6 +27,7 @@ class PostService:
post_like_repository: PostLikeRepository
comment_repository: CommentRepository
slack_service: SlackService
background_tasks: BackgroundTasks

async def get_post_by_id(self, *, post_id: int, current_user: User) -> PostRead:
post = await self.post_repository.find_by_id(post_id=post_id)
Expand Down Expand Up @@ -170,7 +171,9 @@ async def create_post(
)

if result.new_univ_major:
self.slack_service.notify_new_univ_major(result.new_univ_major)
self.background_tasks.add_task(
self.slack_service.notify_new_univ_major, result.new_univ_major
)

return PostRead.from_post(
result.post,
Expand Down Expand Up @@ -221,7 +224,9 @@ async def update_post(
)

if result.new_univ_major:
self.slack_service.notify_new_univ_major(result.new_univ_major)
self.background_tasks.add_task(
self.slack_service.notify_new_univ_major, result.new_univ_major
)

[like_count, comment_count, is_liked, univ_major] = await asyncio.gather(
self.post_like_repository.count_by_id(post_id=post_id),
Expand Down
15 changes: 4 additions & 11 deletions app/slack/service.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
from typing import Annotated

import httpx
from fastapi import BackgroundTasks, Depends

from app.core.config import Environment, core_settings
from app.utils.dependency import dependency

from .config import slack_settings

BackgroundTasksDep = Annotated[BackgroundTasks, Depends()]


@dependency
class SlackService:
background_tasks: BackgroundTasksDep

async def _send_message(self, text: str) -> None:
"""슬랙으로 메시지를 전송합니다. (내부용)"""
"""슬랙으로 메시지를 전송합니다."""
webhook_url = slack_settings.SLACK_WEBHOOK_URL

if not webhook_url:
Expand All @@ -37,8 +30,8 @@ async def _send_message(self, text: str) -> None:
# 슬랙 알림 실패가 메인 로직에 영향 주지 않도록 무시
pass

def notify_new_univ_major(self, univ_major: str) -> None:
"""새로운 학교/전공이 등록되었을 때 슬랙 알림을 백그라운드로 보냅니다."""
async def notify_new_univ_major(self, univ_major: str) -> None:
"""새로운 학교/전공이 등록되었을 때 슬랙 알림을 보냅니다."""
phase = core_settings.ENVIRONMENT.value.upper()
message = f"[{phase}] 🎓 새로운 학교/전공 검수 요청: *{univ_major}*"
self.background_tasks.add_task(self._send_message, message)
await self._send_message(message)
Loading