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
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Any, Dict, Optional
|
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-10-10 03:53:25 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-11-05 05:30:31 +01:00
|
|
|
import urllib
|
2013-06-10 21:35:48 +02:00
|
|
|
|
2018-05-17 16:22:08 +02:00
|
|
|
def avatar_url(user_profile: UserProfile, medium: bool=False, client_gravatar: bool=False) -> Optional[str]:
|
2017-10-10 05:55:03 +02:00
|
|
|
|
|
|
|
return get_avatar_field(
|
|
|
|
user_id=user_profile.id,
|
|
|
|
realm_id=user_profile.realm_id,
|
|
|
|
email=user_profile.email,
|
|
|
|
avatar_source=user_profile.avatar_source,
|
|
|
|
avatar_version=user_profile.avatar_version,
|
|
|
|
medium=medium,
|
|
|
|
client_gravatar=client_gravatar,
|
|
|
|
)
|
2017-05-10 06:54:49 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def avatar_url_from_dict(userdict: Dict[str, Any], medium: bool=False) -> str:
|
2017-10-10 03:53:25 +02:00
|
|
|
'''
|
|
|
|
DEPRECATED: We should start using
|
|
|
|
get_avatar_field to populate users,
|
2017-11-09 16:26:38 +01:00
|
|
|
particularly for codepaths where the
|
2017-10-10 03:53:25 +02:00
|
|
|
client can compute gravatar URLS
|
|
|
|
on the client side.
|
|
|
|
'''
|
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-12-18 15:07:26 +01:00
|
|
|
def get_avatar_field(user_id: int,
|
|
|
|
realm_id: int,
|
2018-05-10 19:13:36 +02:00
|
|
|
email: str,
|
|
|
|
avatar_source: str,
|
2017-12-18 15:07:26 +01:00
|
|
|
avatar_version: int,
|
|
|
|
medium: bool,
|
2018-05-10 19:13:36 +02:00
|
|
|
client_gravatar: bool) -> Optional[str]:
|
2017-10-10 03:53:25 +02:00
|
|
|
'''
|
|
|
|
Most of the parameters to this function map to fields
|
|
|
|
by the same name in UserProfile (avatar_source, realm_id,
|
|
|
|
email, etc.).
|
|
|
|
|
|
|
|
Then there are these:
|
|
|
|
|
|
|
|
medium - This means we want a medium-sized avatar. This can
|
|
|
|
affect the "s" parameter for gravatar avatars, or it
|
|
|
|
can give us something like foo-medium.png for
|
|
|
|
user-uploaded avatars.
|
|
|
|
|
|
|
|
client_gravatar - If the client can compute their own
|
|
|
|
gravatars, this will be set to True, and we'll avoid
|
|
|
|
computing them on the server (mostly to save bandwidth).
|
|
|
|
'''
|
|
|
|
|
|
|
|
if client_gravatar:
|
|
|
|
'''
|
|
|
|
If our client knows how to calculate gravatar hashes, we
|
|
|
|
will return None and let the client compute the gravatar
|
|
|
|
url.
|
|
|
|
'''
|
|
|
|
if settings.ENABLE_GRAVATAR:
|
|
|
|
if avatar_source == UserProfile.AVATAR_FROM_GRAVATAR:
|
|
|
|
return None
|
|
|
|
|
|
|
|
'''
|
|
|
|
If we get this far, we'll compute an avatar URL that may be
|
|
|
|
either user-uploaded or a gravatar, and then we'll add version
|
|
|
|
info to try to avoid stale caches.
|
|
|
|
'''
|
|
|
|
url = _get_unversioned_avatar_url(
|
|
|
|
user_profile_id=user_id,
|
|
|
|
avatar_source=avatar_source,
|
|
|
|
realm_id=realm_id,
|
|
|
|
email=email,
|
|
|
|
medium=medium,
|
|
|
|
)
|
|
|
|
url += '&version=%d' % (avatar_version,)
|
|
|
|
return url
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_gravatar_url(email: str, avatar_version: int, medium: bool=False) -> str:
|
2017-07-17 18:04:36 +02:00
|
|
|
url = _get_unversioned_gravatar_url(email, medium)
|
2017-02-16 22:35:57 +01:00
|
|
|
url += '&version=%d' % (avatar_version,)
|
|
|
|
return url
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def _get_unversioned_gravatar_url(email: str, medium: bool) -> str:
|
2017-07-17 18:04:36 +02:00
|
|
|
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)
|
2017-11-03 03:12:25 +01:00
|
|
|
return "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'
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def _get_unversioned_avatar_url(user_profile_id: int,
|
2018-05-10 19:13:36 +02:00
|
|
|
avatar_source: str,
|
2017-11-05 11:15:10 +01:00
|
|
|
realm_id: int,
|
2018-05-10 19:13:36 +02:00
|
|
|
email: Optional[str]=None,
|
|
|
|
medium: bool=False) -> str:
|
2017-11-03 03:12:25 +01:00
|
|
|
if avatar_source == 'U':
|
2017-07-17 18:04:36 +02:00
|
|
|
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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def absolute_avatar_url(user_profile: UserProfile) -> str:
|
2018-05-17 16:22:08 +02:00
|
|
|
"""
|
|
|
|
Absolute URLs are used to simplify logic for applications that
|
|
|
|
won't be served by browsers, such as rendering GCM notifications.
|
|
|
|
"""
|
|
|
|
avatar = avatar_url(user_profile)
|
|
|
|
# avatar_url can return None if client_gravatar=True, however here we use the default value of False
|
|
|
|
assert avatar is not None
|
|
|
|
return urllib.parse.urljoin(user_profile.realm.uri, avatar)
|