feat(friendships): add structured logging to FriendshipService

This commit is contained in:
qzl
2026-02-28 12:08:01 +08:00
parent b66a8499ed
commit ea4a50d79c
+62
View File
@@ -87,6 +87,11 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
logger.info(
"friend_request_sent",
extra={"initiator_id": str(user_id), "target_id": str(target_user_id)},
)
sender = await self._user_repository.get_by_user_id(user_id)
recipient = await self._user_repository.get_by_user_id(target_user_id)
@@ -119,6 +124,13 @@ class FriendshipService(BaseService):
)
if recipient_id != user_id:
logger.warning(
"friend_request_accept_unauthorized",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship_id),
},
)
raise HTTPException(
status_code=403, detail="Not authorized to accept this request"
)
@@ -149,6 +161,15 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
logger.info(
"friend_request_accepted",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship_id),
"initiator_id": str(sender_id),
},
)
sender = await self._user_repository.get_by_user_id(sender_id)
recipient = await self._user_repository.get_by_user_id(user_id)
@@ -181,6 +202,13 @@ class FriendshipService(BaseService):
)
if recipient_id != user_id:
logger.warning(
"friend_request_decline_unauthorized",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship_id),
},
)
raise HTTPException(
status_code=403, detail="Not authorized to decline this request"
)
@@ -210,6 +238,15 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
logger.info(
"friend_request_declined",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship_id),
"initiator_id": str(sender_id),
},
)
sender = await self._user_repository.get_by_user_id(sender_id)
recipient = await self._user_repository.get_by_user_id(user_id)
@@ -236,6 +273,13 @@ class FriendshipService(BaseService):
raise HTTPException(status_code=404, detail="Friend request not found")
if friendship.initiator_id != user_id:
logger.warning(
"friend_request_cancel_unauthorized",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship_id),
},
)
raise HTTPException(
status_code=403, detail="Not authorized to cancel this request"
)
@@ -267,6 +311,15 @@ class FriendshipService(BaseService):
)
recipient = await self._user_repository.get_by_user_id(recipient_id)
logger.info(
"friend_request_canceled",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship_id),
"target_id": str(recipient_id),
},
)
return FriendRequestResponse(
id=friendship.id,
sender=self._build_user_basic_info(sender),
@@ -415,6 +468,15 @@ class FriendshipService(BaseService):
status_code=503, detail="Friendship service unavailable"
)
logger.info(
"friend_removed",
extra={
"actor_id": str(user_id),
"friendship_id": str(friendship.id),
"removed_friend_id": str(friend_id),
},
)
friend = await self._user_repository.get_by_user_id(friend_id)
return FriendResponse(