2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2013-06-10 21:35:48 +02:00
|
|
|
from django.conf import settings
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2012-10-17 04:07:35 +02:00
|
|
|
import hashlib
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.utils import make_safe_digest
|
2016-06-05 04:20:00 +02:00
|
|
|
if False:
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
from six import text_type
|
2012-10-17 04:07:35 +02:00
|
|
|
|
|
|
|
def gravatar_hash(email):
|
2016-06-12 14:22:20 +02:00
|
|
|
# type: (text_type) -> text_type
|
2012-10-17 04:07:35 +02:00
|
|
|
"""Compute the Gravatar hash for an email address."""
|
2013-03-20 15:31:27 +01:00
|
|
|
# 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)
|
2013-06-10 21:35:48 +02:00
|
|
|
|
|
|
|
def user_avatar_hash(email):
|
2016-06-12 14:22:20 +02:00
|
|
|
# type: (text_type) -> text_type
|
2013-06-10 21:35:48 +02:00
|
|
|
# Salting the user_key may be overkill, but it prevents us from
|
|
|
|
# basically mimicking Gravatar's hashing scheme, which could lead
|
|
|
|
# to some abuse scenarios like folks using us as a free Gravatar
|
|
|
|
# replacement.
|
|
|
|
user_key = email.lower() + settings.AVATAR_SALT
|
|
|
|
return make_safe_digest(user_key, hashlib.sha1)
|
|
|
|
|
|
|
|
def avatar_url(user_profile):
|
2016-06-05 04:20:00 +02:00
|
|
|
# type: (UserProfile) -> text_type
|
2013-09-21 16:32:29 +02:00
|
|
|
return get_avatar_url(
|
|
|
|
user_profile.avatar_source,
|
|
|
|
user_profile.email
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_avatar_url(avatar_source, email):
|
2016-06-12 14:22:20 +02:00
|
|
|
# type: (text_type, text_type) -> text_type
|
|
|
|
if avatar_source == u'U':
|
2013-09-21 16:32:29 +02:00
|
|
|
hash_key = user_avatar_hash(email)
|
2013-10-28 16:13:53 +01:00
|
|
|
if settings.LOCAL_UPLOADS_DIR is not None:
|
|
|
|
# ?x=x allows templates to append additional parameters with &s
|
2016-06-12 14:22:20 +02:00
|
|
|
return u"/user_avatars/%s.png?x=x" % (hash_key)
|
2013-10-28 16:13:53 +01:00
|
|
|
else:
|
|
|
|
bucket = settings.S3_AVATAR_BUCKET
|
2016-06-12 14:22:20 +02:00
|
|
|
return u"https://%s.s3.amazonaws.com/%s?x=x" % (bucket, hash_key)
|
2013-11-15 22:25:02 +01:00
|
|
|
elif settings.ENABLE_GRAVATAR:
|
2013-09-21 16:32:29 +02:00
|
|
|
hash_key = gravatar_hash(email)
|
2016-06-12 14:22:20 +02:00
|
|
|
return u"https://secure.gravatar.com/avatar/%s?d=identicon" % (hash_key,)
|
2013-11-15 22:25:02 +01:00
|
|
|
else:
|
2013-11-18 16:58:39 +01:00
|
|
|
return settings.DEFAULT_AVATAR_URI+'?x=x'
|