avatar: Ensure system bots' avatar URLs follow convention.

Previously, requesting system bots URLs did not return any -medium.png
variants and SVG file was also used for notification bots' avatar, which
was problematic.

In this commit, the -medium.png variants is added for the avatars of
system bots and zulip-icon-square.svg is also converted into
notification-bot.png for the notification bot. The get_avatar_url method
has been updated to return the "medium" file variants for the system
bots.

Additionally, the system bots' avatar files is moved to a dedicated
directory to simplify the hashing logic for these files. Now, all files
in the "images/static_avatars/" directory will be hashed.
This commit is contained in:
PieterCK
2024-10-22 23:05:32 +07:00
committed by Tim Abbott
parent 81c4e45d01
commit 516d1ab82b
11 changed files with 49 additions and 14 deletions

View File

@@ -12,14 +12,11 @@ from zerver.lib.thumbnail import MEDIUM_AVATAR_SIZE
from zerver.lib.upload import get_avatar_url
from zerver.lib.url_encoding import append_url_query_string
from zerver.models import UserProfile
from zerver.models.users import is_cross_realm_bot_email
SYSTEM_BOTS_AVATAR_FILES = {
# This is also used in zerver/lib/storage.py to ensure
# these files are hashed when served as static files.
settings.WELCOME_BOT: "images/welcome-bot.png",
settings.NOTIFICATION_BOT: "images/logo/zulip-icon-square.svg",
settings.EMAIL_GATEWAY_BOT: "images/email-gateway-bot.png",
}
STATIC_AVATARS_DIR = "images/static_avatars/"
DEFAULT_AVATAR_FILE = "images/default-avatar.png"
def avatar_url(
@@ -36,6 +33,22 @@ def avatar_url(
)
def get_system_bots_avatar_file_name(email: str) -> str:
system_bot_avatar_name_map = {
settings.WELCOME_BOT: "welcome-bot",
settings.NOTIFICATION_BOT: "notification-bot",
settings.EMAIL_GATEWAY_BOT: "emailgateway",
}
return urljoin(STATIC_AVATARS_DIR, system_bot_avatar_name_map.get(email, "unknown"))
def get_static_avatar_url(email: str, medium: bool) -> str:
avatar_file_name = get_system_bots_avatar_file_name(email)
avatar_file_name += "-medium.png" if medium else ".png"
return staticfiles_storage.url(avatar_file_name)
def get_avatar_field(
user_id: int,
realm_id: int,
@@ -63,9 +76,8 @@ def get_avatar_field(
"""
# System bots have hardcoded avatars
system_bot_avatar = SYSTEM_BOTS_AVATAR_FILES.get(email)
if system_bot_avatar:
return staticfiles_storage.url(system_bot_avatar)
if is_cross_realm_bot_email(email):
return get_static_avatar_url(email, medium)
"""
If our client knows how to calculate gravatar hashes, we