2016-09-28 00:12:18 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.conf import settings
|
2016-12-21 13:17:53 +01:00
|
|
|
from typing import Text
|
2016-09-28 00:12:18 +02:00
|
|
|
|
|
|
|
from zerver.lib.utils import make_safe_digest
|
|
|
|
|
2017-03-05 05:24:47 +01:00
|
|
|
if False:
|
|
|
|
# Typing import inside `if False` to avoid import loop.
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2016-09-28 00:12:18 +02:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
def gravatar_hash(email):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Text
|
2016-09-28 00:12:18 +02:00
|
|
|
"""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 make_safe_digest(email.lower(), hashlib.md5)
|
|
|
|
|
2017-03-03 00:15:05 +01:00
|
|
|
def user_avatar_hash(uid):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Text
|
2017-06-27 00:12:15 +02:00
|
|
|
|
|
|
|
# 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 probably doesn't serve any purpose now. In the past we
|
|
|
|
# used a hash of the email address, not the user ID, and we salted
|
|
|
|
# it in order to make the hashing scheme different from Gravatar's.
|
2017-03-03 00:15:05 +01:00
|
|
|
user_key = uid + settings.AVATAR_SALT
|
2016-09-28 00:12:18 +02:00
|
|
|
return make_safe_digest(user_key, hashlib.sha1)
|
2017-03-02 23:45:57 +01:00
|
|
|
|
|
|
|
def user_avatar_path(user_profile):
|
|
|
|
# type: (UserProfile) -> Text
|
2017-06-27 00:12:15 +02:00
|
|
|
|
|
|
|
# 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 .
|
2017-05-10 06:40:07 +02:00
|
|
|
return user_avatar_path_from_ids(user_profile.id, user_profile.realm_id)
|
|
|
|
|
|
|
|
def user_avatar_path_from_ids(user_profile_id, realm_id):
|
|
|
|
# type: (int, int) -> Text
|
|
|
|
user_id_hash = user_avatar_hash(str(user_profile_id))
|
|
|
|
return '%s/%s' % (str(realm_id), user_id_hash)
|