2013-06-10 21:35:48 +02:00
|
|
|
from django.conf import settings
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-06-05 04:20:00 +02:00
|
|
|
if False:
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2017-05-10 06:44:26 +02:00
|
|
|
from typing import Any, Dict, Optional, Text
|
2012-10-17 04:07:35 +02:00
|
|
|
|
2017-07-17 18:04:36 +02:00
|
|
|
from zerver.lib.avatar_hash import gravatar_hash, user_avatar_path_from_ids
|
2016-09-20 21:48:48 +02:00
|
|
|
from zerver.lib.upload import upload_backend, MEDIUM_AVATAR_SIZE
|
2017-06-15 07:44:00 +02:00
|
|
|
from six.moves import urllib
|
2013-06-10 21:35:48 +02:00
|
|
|
|
2016-09-20 21:48:48 +02:00
|
|
|
def avatar_url(user_profile, medium=False):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (UserProfile, bool) -> Text
|
2017-05-10 06:54:49 +02:00
|
|
|
return avatar_url_from_dict(
|
|
|
|
dict(
|
|
|
|
avatar_source=user_profile.avatar_source,
|
|
|
|
avatar_version=user_profile.avatar_version,
|
|
|
|
email=user_profile.email,
|
|
|
|
id=user_profile.id,
|
|
|
|
realm_id=user_profile.realm_id),
|
|
|
|
medium=medium)
|
|
|
|
|
|
|
|
def avatar_url_from_dict(userdict, medium=False):
|
|
|
|
# type: (Dict[str, Any], bool) -> Text
|
2017-05-10 06:44:26 +02:00
|
|
|
url = _get_unversioned_avatar_url(
|
2017-07-17 18:04:36 +02:00
|
|
|
userdict['id'],
|
2017-05-10 06:54:49 +02:00
|
|
|
userdict['avatar_source'],
|
2017-07-17 18:04:36 +02:00
|
|
|
userdict['realm_id'],
|
2017-05-10 06:54:49 +02:00
|
|
|
email=userdict['email'],
|
2017-01-24 07:06:13 +01:00
|
|
|
medium=medium)
|
2017-05-10 06:54:49 +02:00
|
|
|
url += '&version=%d' % (userdict['avatar_version'],)
|
2017-05-10 06:44:26 +02:00
|
|
|
return url
|
2013-09-21 16:32:29 +02:00
|
|
|
|
2017-07-17 18:04:36 +02:00
|
|
|
def get_gravatar_url(email, avatar_version, medium=False):
|
|
|
|
# type: (Text, int, bool) -> Text
|
|
|
|
url = _get_unversioned_gravatar_url(email, medium)
|
2017-02-16 22:35:57 +01:00
|
|
|
url += '&version=%d' % (avatar_version,)
|
|
|
|
return url
|
|
|
|
|
2017-07-17 18:04:36 +02:00
|
|
|
def _get_unversioned_gravatar_url(email, medium):
|
|
|
|
# type: (Text, bool) -> Text
|
|
|
|
if settings.ENABLE_GRAVATAR:
|
2016-09-20 21:48:48 +02:00
|
|
|
gravitar_query_suffix = "&s=%s" % (MEDIUM_AVATAR_SIZE,) if medium else ""
|
2013-09-21 16:32:29 +02:00
|
|
|
hash_key = gravatar_hash(email)
|
2016-09-20 21:48:48 +02:00
|
|
|
return u"https://secure.gravatar.com/avatar/%s?d=identicon%s" % (hash_key, gravitar_query_suffix)
|
2017-07-17 18:04:36 +02:00
|
|
|
return settings.DEFAULT_AVATAR_URI+'?x=x'
|
|
|
|
|
|
|
|
def _get_unversioned_avatar_url(user_profile_id, avatar_source, realm_id, email=None, medium=False):
|
|
|
|
# type: (int, Text, int, Optional[Text], bool) -> Text
|
|
|
|
if avatar_source == u'U':
|
|
|
|
hash_key = user_avatar_path_from_ids(user_profile_id, realm_id)
|
|
|
|
return upload_backend.get_avatar_url(hash_key, medium=medium)
|
|
|
|
assert email is not None
|
|
|
|
return _get_unversioned_gravatar_url(email, medium)
|
2017-06-15 07:44:00 +02:00
|
|
|
|
|
|
|
def absolute_avatar_url(user_profile):
|
|
|
|
# type: (UserProfile) -> Text
|
|
|
|
"""Absolute URLs are used to simplify logic for applications that
|
|
|
|
won't be served by browsers, such as rendering GCM notifications."""
|
|
|
|
return urllib.parse.urljoin(user_profile.realm.uri, avatar_url(user_profile))
|