2018-05-10 19:13:36 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2023-12-05 21:14:17 +01:00
|
|
|
from urllib.parse import urljoin
|
2012-10-17 04:07:35 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
2023-01-25 00:30:24 +01:00
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from zerver.lib.avatar_hash import (
|
|
|
|
gravatar_hash,
|
|
|
|
user_avatar_content_hash,
|
|
|
|
user_avatar_path_from_ids,
|
|
|
|
)
|
2022-12-16 15:33:49 +01:00
|
|
|
from zerver.lib.upload import get_avatar_url
|
2022-12-14 21:51:37 +01:00
|
|
|
from zerver.lib.upload.base import MEDIUM_AVATAR_SIZE
|
2021-10-14 01:51:27 +02:00
|
|
|
from zerver.lib.url_encoding import append_url_query_string
|
2017-10-10 03:53:25 +02:00
|
|
|
from zerver.models import UserProfile
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2013-06-10 21:35:48 +02:00
|
|
|
|
2021-02-12 08:19:30 +01: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,
|
2019-11-05 20:23:58 +01:00
|
|
|
email=user_profile.delivery_email,
|
2017-10-10 05:55:03 +02:00
|
|
|
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
|
|
|
|
2021-02-12 08:19:30 +01: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
|
2020-10-23 02:43:28 +02:00
|
|
|
client can compute gravatar URLs
|
2017-10-10 03:53:25 +02:00
|
|
|
on the client side.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-05-10 06:44:26 +02:00
|
|
|
url = _get_unversioned_avatar_url(
|
2021-02-12 08:20:45 +01:00
|
|
|
userdict["id"],
|
|
|
|
userdict["avatar_source"],
|
|
|
|
userdict["realm_id"],
|
|
|
|
email=userdict["email"],
|
2021-02-12 08:19:30 +01:00
|
|
|
medium=medium,
|
|
|
|
)
|
2021-10-14 01:51:27 +02:00
|
|
|
return append_url_query_string(url, "version={:d}".format(userdict["avatar_version"]))
|
2013-09-21 16:32:29 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def get_avatar_field(
|
|
|
|
user_id: int,
|
|
|
|
realm_id: int,
|
|
|
|
email: str,
|
|
|
|
avatar_source: str,
|
|
|
|
avatar_version: int,
|
|
|
|
medium: bool,
|
|
|
|
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).
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-10-10 03:53:25 +02:00
|
|
|
|
|
|
|
if client_gravatar:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-10-10 03:53:25 +02:00
|
|
|
If our client knows how to calculate gravatar hashes, we
|
|
|
|
will return None and let the client compute the gravatar
|
|
|
|
url.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2023-01-18 02:59:37 +01:00
|
|
|
if settings.ENABLE_GRAVATAR and avatar_source == UserProfile.AVATAR_FROM_GRAVATAR:
|
|
|
|
return None
|
2017-10-10 03:53:25 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2017-10-10 03:53:25 +02:00
|
|
|
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.
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2017-10-10 03:53:25 +02:00
|
|
|
url = _get_unversioned_avatar_url(
|
|
|
|
user_profile_id=user_id,
|
|
|
|
avatar_source=avatar_source,
|
|
|
|
realm_id=realm_id,
|
|
|
|
email=email,
|
|
|
|
medium=medium,
|
|
|
|
)
|
2021-10-14 01:51:27 +02:00
|
|
|
return append_url_query_string(url, f"version={avatar_version:d}")
|
2017-10-10 03:53:25 +02:00
|
|
|
|
2021-02-12 08:19:30 +01: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)
|
2021-10-14 01:51:27 +02:00
|
|
|
return append_url_query_string(url, f"version={avatar_version:d}")
|
2017-02-16 22:35:57 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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:
|
2020-06-10 06:41:04 +02:00
|
|
|
gravitar_query_suffix = f"&s={MEDIUM_AVATAR_SIZE}" if medium else ""
|
2013-09-21 16:32:29 +02:00
|
|
|
hash_key = gravatar_hash(email)
|
2020-06-10 06:41:04 +02:00
|
|
|
return f"https://secure.gravatar.com/avatar/{hash_key}?d=identicon{gravitar_query_suffix}"
|
2023-01-25 00:30:24 +01:00
|
|
|
elif settings.DEFAULT_AVATAR_URI is not None:
|
|
|
|
return settings.DEFAULT_AVATAR_URI
|
|
|
|
else:
|
|
|
|
return staticfiles_storage.url("images/default-avatar.png")
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-07-17 18:04:36 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def _get_unversioned_avatar_url(
|
|
|
|
user_profile_id: int,
|
|
|
|
avatar_source: str,
|
|
|
|
realm_id: int,
|
|
|
|
email: Optional[str] = None,
|
|
|
|
medium: bool = False,
|
|
|
|
) -> str:
|
2021-02-12 08:20:45 +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)
|
2022-12-16 15:33:49 +01:00
|
|
|
return get_avatar_url(hash_key, medium=medium)
|
2017-07-17 18:04:36 +02:00
|
|
|
assert email is not None
|
|
|
|
return _get_unversioned_gravatar_url(email, medium)
|
2017-06-15 07:44:00 +02:00
|
|
|
|
2021-02-12 08:19:30 +01: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
|
2023-12-05 21:14:17 +01:00
|
|
|
return urljoin(user_profile.realm.uri, avatar)
|
2019-06-28 00:10:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-06-28 00:10:58 +02:00
|
|
|
def is_avatar_new(ldap_avatar: bytes, user_profile: UserProfile) -> bool:
|
|
|
|
new_avatar_hash = user_avatar_content_hash(ldap_avatar)
|
|
|
|
|
2023-01-18 02:59:37 +01:00
|
|
|
if user_profile.avatar_hash and user_profile.avatar_hash == new_avatar_hash:
|
|
|
|
# If an avatar exists and is the same as the new avatar,
|
|
|
|
# then, no need to change the avatar.
|
|
|
|
return False
|
2019-06-28 00:10:58 +02:00
|
|
|
|
|
|
|
return True
|
2023-03-27 16:32:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_avatar_for_inaccessible_user() -> str:
|
2023-11-10 07:46:49 +01:00
|
|
|
return staticfiles_storage.url("images/unknown-user-avatar.png")
|