Add fallback for user avatar URL in notifications

Ensures that a default avatar URL is used if the user object lacks an avatar_url attribute or if an exception occurs, improving robustness of notification cover images.
This commit is contained in:
咕谷酱
2025-08-24 21:45:07 +08:00
committed by MingxuanGame
parent 7625cd99f5
commit ccfafd9c5f

View File

@@ -117,10 +117,14 @@ class ChannelMessageBase(NotificationDetail):
receiver: list[int],
channel_type: "ChannelType",
) -> Self:
try:
avatar_url = getattr(user, 'avatar_url', 'https://lazer-data.g0v0.top/default.jpg') or 'https://lazer-data.g0v0.top/default.jpg'
except Exception:
avatar_url = 'https://lazer-data.g0v0.top/default.jpg'
instance = cls(
title=truncate(message.content, CONTENT_TRUNCATE),
type=channel_type.value.lower(),
cover_url=user.avatar_url,
cover_url=avatar_url,
)
instance._message = message
instance._user = user