Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
import hashlib
|
|
|
|
from django.conf import settings
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
def gravatar_hash(email: str) -> str:
|
|
"""Compute the Gravatar hash for an email address."""
|
|
# Non-ASCII characters aren't permitted by the currently active e-mail
|
|
# RFCs. However, the IETF has published https://tools.ietf.org/html/rfc4952,
|
|
# outlining internationalization of email addresses, and regardless if we
|
|
# typo an address or someone manages to give us a non-ASCII address, let's
|
|
# not error out on it.
|
|
return hashlib.md5(email.lower().encode()).hexdigest()
|
|
|
|
|
|
def user_avatar_hash(uid: str, version: str) -> str:
|
|
# WARNING: If this method is changed, you may need to do a migration
|
|
# similar to zerver/migrations/0060_move_avatars_to_be_uid_based.py .
|
|
|
|
# The salt prevents unauthenticated clients from enumerating the
|
|
# avatars of all users.
|
|
user_key = uid + ":" + version + ":" + settings.AVATAR_SALT
|
|
return hashlib.sha256(user_key.encode()).hexdigest()[:40]
|
|
|
|
|
|
def user_avatar_path(user_profile: UserProfile, future: bool = False) -> str:
|
|
# 'future' is if this is for the current avatar version, or the next one.
|
|
return user_avatar_base_path_from_ids(
|
|
user_profile.id, user_profile.avatar_version + (1 if future else 0), user_profile.realm_id
|
|
)
|
|
|
|
|
|
def user_avatar_base_path_from_ids(user_profile_id: int, version: int, realm_id: int) -> str:
|
|
user_id_hash = user_avatar_hash(str(user_profile_id), str(version))
|
|
return f"{realm_id}/{user_id_hash}"
|
|
|
|
|
|
def user_avatar_content_hash(ldap_avatar: bytes) -> str:
|
|
return hashlib.sha256(ldap_avatar).hexdigest()
|