def avatar(request, email_or_id, medium=None): # type: (HttpRequest, str, bool) -> HttpResponse """Accepts an email address or user ID and returns the avatar""" try: int(email_or_id) except ValueError: get_user_func = get_user_profile_by_email else: get_user_func = get_user_profile_by_id try: # If there is a valid user account passed in, use its avatar user_profile = get_user_func(email_or_id) url = avatar_url(user_profile, medium=medium) except UserProfile.DoesNotExist: # If there is no such user, treat it as a new gravatar email = email_or_id avatar_source = 'G' avatar_version = 1 url = get_avatar_url(avatar_source, email, avatar_version, medium=medium) # We can rely on the url already having query parameters. Because # our templates depend on being able to use the ampersand to # add query parameters to our url, get_avatar_url does '?x=x' # hacks to prevent us from having to jump through decode/encode hoops. assert '?' in url url += '&' + request.META['QUERY_STRING'] return redirect(url)
def avatar_url(userdict): # type: (Dict[str, Any]) -> Text return get_avatar_url( userdict['avatar_source'], userdict['email'], userdict['avatar_version'], )
def avatar(request, email_or_id, medium=False): # type: (HttpRequest, str, bool) -> HttpResponse """Accepts an email address or user ID and returns the avatar""" try: int(email_or_id) except ValueError: get_user_func = get_user_profile_by_email # type: Callable[..., UserProfile] else: get_user_func = get_user_profile_by_id try: # If there is a valid user account passed in, use its avatar user_profile = get_user_func(email_or_id) url = avatar_url(user_profile, medium=medium) except UserProfile.DoesNotExist: # If there is no such user, treat it as a new gravatar email = email_or_id avatar_source = 'G' avatar_version = 1 url = get_avatar_url(avatar_source, email, avatar_version, medium=medium) # We can rely on the url already having query parameters. Because # our templates depend on being able to use the ampersand to # add query parameters to our url, get_avatar_url does '?x=x' # hacks to prevent us from having to jump through decode/encode hoops. assert '?' in url url += '&' + request.META['QUERY_STRING'] return redirect(url)
def avatar(request, email): try: user_profile = get_user_profile_by_email(email) avatar_source = user_profile.avatar_source except UserProfile.DoesNotExist: avatar_source = 'G' url = get_avatar_url(avatar_source, email) if '?' in url: sep = '&' else: sep = '?' url += sep + request.META['QUERY_STRING'] return redirect(url)
def avatar(request, email): # type: (HttpRequest, str) -> HttpResponse try: user_profile = get_user_profile_by_email(email) avatar_source = user_profile.avatar_source except UserProfile.DoesNotExist: avatar_source = 'G' url = get_avatar_url(avatar_source, email) # We can rely on the url already having query parameters. Because # our templates depend on being able to use the ampersand to # add query parameters to our url, get_avatar_url does '?x=x' # hacks to prevent us from having to jump through decode/encode hoops. assert '?' in url url += '&' + request.META['QUERY_STRING'] return redirect(url)
def avatar(request, email): # type: (HttpRequest, str) -> HttpResponse try: user_profile = get_user_profile_by_email(email) avatar_source = user_profile.avatar_source except UserProfile.DoesNotExist: avatar_source = "G" url = get_avatar_url(avatar_source, email) # We can rely on the url already having query parameters. Because # our templates depend on being able to use the ampersand to # add query parameters to our url, get_avatar_url does '?x=x' # hacks to prevent us from having to jump through decode/encode hoops. assert "?" in url url += "&" + request.META["QUERY_STRING"] return redirect(url)
def get_members_backend(request, user_profile): realm = user_profile.realm admins = set(user_profile.realm.get_admin_users()) members = [] for profile in UserProfile.objects.select_related().filter(realm=realm): avatar_url = get_avatar_url(profile.avatar_source, profile.email) member = { "full_name": profile.full_name, "is_bot": profile.is_bot, "is_active": profile.is_active, "is_admin": (profile in admins), "email": profile.email, "avatar_url": avatar_url, } if profile.is_bot and profile.bot_owner is not None: member["bot_owner"] = profile.bot_owner.email members.append(member) return json_success({'members': members})
def get_members_backend(request, user_profile): # type: (HttpRequest, UserProfile) -> HttpResponse realm = user_profile.realm admins = set(user_profile.realm.get_admin_users()) members = [] for profile in UserProfile.objects.select_related().filter(realm=realm): avatar_url = get_avatar_url(profile.avatar_source, profile.email) member = { "full_name": profile.full_name, "is_bot": profile.is_bot, "is_active": profile.is_active, "is_admin": (profile in admins), "email": profile.email, "avatar_url": avatar_url, } if profile.is_bot and profile.bot_owner is not None: member["bot_owner"] = profile.bot_owner.email members.append(member) return json_success({"members": members})
def build_message_dict( apply_markdown, message, message_id, last_edit_time, edit_history, content, subject, pub_date, rendered_content, rendered_content_version, sender_id, sender_email, sender_realm_domain, sender_full_name, sender_short_name, sender_avatar_source, sender_is_mirror_dummy, sending_client_name, recipient_id, recipient_type, recipient_type_id, ): global bugdown if bugdown is None: from zerver.lib import bugdown avatar_url = get_avatar_url(sender_avatar_source, sender_email) display_recipient = get_display_recipient_by_id( recipient_id, recipient_type, recipient_type_id ) if recipient_type == Recipient.STREAM: display_type = "stream" elif recipient_type in (Recipient.HUDDLE, Recipient.PERSONAL): display_type = "private" if len(display_recipient) == 1: # add the sender in if this isn't a message between # someone and his self, preserving ordering recip = {'email': sender_email, 'domain': sender_realm_domain, 'full_name': sender_full_name, 'short_name': sender_short_name, 'id': sender_id, 'is_mirror_dummy': sender_is_mirror_dummy} if recip['email'] < display_recipient[0]['email']: display_recipient = [recip, display_recipient[0]] elif recip['email'] > display_recipient[0]['email']: display_recipient = [display_recipient[0], recip] obj = dict( id = message_id, sender_email = sender_email, sender_full_name = sender_full_name, sender_short_name = sender_short_name, sender_domain = sender_realm_domain, sender_id = sender_id, type = display_type, display_recipient = display_recipient, recipient_id = recipient_id, subject = subject, timestamp = datetime_to_timestamp(pub_date), gravatar_hash = gravatar_hash(sender_email), # Deprecated June 2013 avatar_url = avatar_url, client = sending_client_name) obj['subject_links'] = bugdown.subject_links(sender_realm_domain.lower(), subject) if last_edit_time != None: obj['last_edit_timestamp'] = datetime_to_timestamp(last_edit_time) obj['edit_history'] = ujson.loads(edit_history) if apply_markdown: if Message.need_to_render_content(rendered_content, rendered_content_version): if message is None: # We really shouldn't be rendering objects in this method, but there is # a scenario where we upgrade the version of bugdown and fail to run # management commands to re-render historical messages, and then we # need to have side effects. This method is optimized to not need full # blown ORM objects, but the bugdown renderer is unfortunately highly # coupled to Message, and we also need to persist the new rendered content. # If we don't have a message object passed in, we get one here. The cost # of going to the DB here should be overshadowed by the cost of rendering # and updating the row. message = Message.objects.select_related().get(id=message_id) # It's unfortunate that we need to have side effects on the message # in some cases. rendered_content = message.render_markdown(content, sender_realm_domain) message.set_rendered_content(rendered_content, True) if rendered_content is not None: obj['content'] = rendered_content else: obj['content'] = '<p>[Zulip note: Sorry, we could not understand the formatting of your message]</p>' obj['content_type'] = 'text/html' else: obj['content'] = content obj['content_type'] = 'text/x-markdown' return obj
def build_message_dict( apply_markdown, message, message_id, last_edit_time, edit_history, content, subject, pub_date, rendered_content, rendered_content_version, sender_id, sender_email, sender_realm_domain, sender_full_name, sender_short_name, sender_avatar_source, sender_is_mirror_dummy, sending_client_name, recipient_id, recipient_type, recipient_type_id, ): global bugdown if bugdown is None: from zerver.lib import bugdown avatar_url = get_avatar_url(sender_avatar_source, sender_email) display_recipient = get_display_recipient_by_id( recipient_id, recipient_type, recipient_type_id) if recipient_type == Recipient.STREAM: display_type = "stream" elif recipient_type in (Recipient.HUDDLE, Recipient.PERSONAL): display_type = "private" if len(display_recipient) == 1: # add the sender in if this isn't a message between # someone and his self, preserving ordering recip = { 'email': sender_email, 'domain': sender_realm_domain, 'full_name': sender_full_name, 'short_name': sender_short_name, 'id': sender_id, 'is_mirror_dummy': sender_is_mirror_dummy } if recip['email'] < display_recipient[0]['email']: display_recipient = [recip, display_recipient[0]] elif recip['email'] > display_recipient[0]['email']: display_recipient = [display_recipient[0], recip] obj = dict( id=message_id, sender_email=sender_email, sender_full_name=sender_full_name, sender_short_name=sender_short_name, sender_domain=sender_realm_domain, sender_id=sender_id, type=display_type, display_recipient=display_recipient, recipient_id=recipient_id, subject=subject, timestamp=datetime_to_timestamp(pub_date), gravatar_hash=gravatar_hash(sender_email), # Deprecated June 2013 avatar_url=avatar_url, client=sending_client_name) obj['subject_links'] = bugdown.subject_links( sender_realm_domain.lower(), subject) if last_edit_time != None: obj['last_edit_timestamp'] = datetime_to_timestamp(last_edit_time) obj['edit_history'] = ujson.loads(edit_history) if apply_markdown: if Message.need_to_render_content(rendered_content, rendered_content_version): if message is None: # We really shouldn't be rendering objects in this method, but there is # a scenario where we upgrade the version of bugdown and fail to run # management commands to re-render historical messages, and then we # need to have side effects. This method is optimized to not need full # blown ORM objects, but the bugdown renderer is unfortunately highly # coupled to Message, and we also need to persist the new rendered content. # If we don't have a message object passed in, we get one here. The cost # of going to the DB here should be overshadowed by the cost of rendering # and updating the row. message = Message.objects.select_related().get( id=message_id) # It's unfortunate that we need to have side effects on the message # in some cases. rendered_content = message.render_markdown( content, sender_realm_domain) message.set_rendered_content(rendered_content, True) if rendered_content is not None: obj['content'] = rendered_content else: obj['content'] = '<p>[Zulip note: Sorry, we could not understand the formatting of your message]</p>' obj['content_type'] = 'text/html' else: obj['content'] = content obj['content_type'] = 'text/x-markdown' return obj
def build_message_dict(apply_markdown, message, message_id, last_edit_time, edit_history, content, subject, pub_date, rendered_content, rendered_content_version, sender_id, sender_email, sender_realm_id, sender_realm_str, sender_full_name, sender_short_name, sender_avatar_source, sender_avatar_version, sender_is_mirror_dummy, sending_client_name, recipient_id, recipient_type, recipient_type_id, reactions): # type: (bool, Optional[Message], int, Optional[datetime.datetime], Optional[Text], Text, Text, datetime.datetime, Optional[Text], Optional[int], int, Text, int, Text, Text, Text, Text, int, bool, Text, int, int, int, List[Dict[str, Any]]) -> Dict[str, Any] avatar_url = get_avatar_url(sender_avatar_source, sender_email, sender_avatar_version) display_recipient = get_display_recipient_by_id( recipient_id, recipient_type, recipient_type_id) if recipient_type == Recipient.STREAM: display_type = "stream" elif recipient_type in (Recipient.HUDDLE, Recipient.PERSONAL): assert not isinstance(display_recipient, Text) display_type = "private" if len(display_recipient) == 1: # add the sender in if this isn't a message between # someone and his self, preserving ordering recip = { 'email': sender_email, 'full_name': sender_full_name, 'short_name': sender_short_name, 'id': sender_id, 'is_mirror_dummy': sender_is_mirror_dummy } if recip['email'] < display_recipient[0]['email']: display_recipient = [recip, display_recipient[0]] elif recip['email'] > display_recipient[0]['email']: display_recipient = [display_recipient[0], recip] obj = dict( id=message_id, sender_email=sender_email, sender_full_name=sender_full_name, sender_short_name=sender_short_name, sender_realm_str=sender_realm_str, sender_id=sender_id, type=display_type, display_recipient=display_recipient, recipient_id=recipient_id, subject=subject, timestamp=datetime_to_timestamp(pub_date), gravatar_hash=gravatar_hash(sender_email), # Deprecated June 2013 avatar_url=avatar_url, client=sending_client_name) if obj['type'] == 'stream': obj['stream_id'] = recipient_type_id obj['subject_links'] = bugdown.subject_links(sender_realm_id, subject) if last_edit_time is not None: obj['last_edit_timestamp'] = datetime_to_timestamp(last_edit_time) obj['edit_history'] = ujson.loads(edit_history) if apply_markdown: if Message.need_to_render_content(rendered_content, rendered_content_version, bugdown.version): if message is None: # We really shouldn't be rendering objects in this method, but there is # a scenario where we upgrade the version of bugdown and fail to run # management commands to re-render historical messages, and then we # need to have side effects. This method is optimized to not need full # blown ORM objects, but the bugdown renderer is unfortunately highly # coupled to Message, and we also need to persist the new rendered content. # If we don't have a message object passed in, we get one here. The cost # of going to the DB here should be overshadowed by the cost of rendering # and updating the row. # TODO: see #1379 to eliminate bugdown dependencies message = Message.objects.select_related().get( id=message_id) # It's unfortunate that we need to have side effects on the message # in some cases. rendered_content = render_markdown(message, content, realm=message.get_realm()) message.rendered_content = rendered_content message.rendered_content_version = bugdown.version message.save_rendered_content() if rendered_content is not None: obj['content'] = rendered_content else: obj['content'] = u'<p>[Zulip note: Sorry, we could not understand the formatting of your message]</p>' obj['content_type'] = 'text/html' else: obj['content'] = content obj['content_type'] = 'text/x-markdown' obj['reactions'] = [ ReactionDict.build_dict_from_raw_db_row(reaction) for reaction in reactions ] return obj
def avatar_url(userdict): # type: (Dict[str, Any]) -> Text return get_avatar_url(userdict['avatar_source'], userdict['email'], userdict['avatar_version'], )
def build_message_dict( apply_markdown, message, message_id, last_edit_time, edit_history, content, subject, pub_date, rendered_content, rendered_content_version, sender_id, sender_email, sender_realm_id, sender_realm_domain, sender_full_name, sender_short_name, sender_avatar_source, sender_avatar_version, sender_is_mirror_dummy, sending_client_name, recipient_id, recipient_type, recipient_type_id, reactions ): # type: (bool, Optional[Message], int, Optional[datetime.datetime], Optional[Text], Text, Text, datetime.datetime, Optional[Text], Optional[int], int, Text, int, Text, Text, Text, Text, int, bool, Text, int, int, int, List[Dict[str, Any]]) -> Dict[str, Any] avatar_url = get_avatar_url( sender_avatar_source, sender_email, sender_avatar_version ) display_recipient = get_display_recipient_by_id( recipient_id, recipient_type, recipient_type_id ) if recipient_type == Recipient.STREAM: display_type = "stream" elif recipient_type in (Recipient.HUDDLE, Recipient.PERSONAL): assert not isinstance(display_recipient, Text) display_type = "private" if len(display_recipient) == 1: # add the sender in if this isn't a message between # someone and his self, preserving ordering recip = {'email': sender_email, 'domain': sender_realm_domain, 'full_name': sender_full_name, 'short_name': sender_short_name, 'id': sender_id, 'is_mirror_dummy': sender_is_mirror_dummy} if recip['email'] < display_recipient[0]['email']: display_recipient = [recip, display_recipient[0]] elif recip['email'] > display_recipient[0]['email']: display_recipient = [display_recipient[0], recip] obj = dict( id = message_id, sender_email = sender_email, sender_full_name = sender_full_name, sender_short_name = sender_short_name, sender_domain = sender_realm_domain, sender_id = sender_id, type = display_type, display_recipient = display_recipient, recipient_id = recipient_id, subject = subject, timestamp = datetime_to_timestamp(pub_date), gravatar_hash = gravatar_hash(sender_email), # Deprecated June 2013 avatar_url = avatar_url, client = sending_client_name) if obj['type'] == 'stream': obj['stream_id'] = recipient_type_id obj['subject_links'] = bugdown.subject_links(sender_realm_id, subject) if last_edit_time is not None: obj['last_edit_timestamp'] = datetime_to_timestamp(last_edit_time) obj['edit_history'] = ujson.loads(edit_history) if apply_markdown: if Message.need_to_render_content(rendered_content, rendered_content_version, bugdown.version): if message is None: # We really shouldn't be rendering objects in this method, but there is # a scenario where we upgrade the version of bugdown and fail to run # management commands to re-render historical messages, and then we # need to have side effects. This method is optimized to not need full # blown ORM objects, but the bugdown renderer is unfortunately highly # coupled to Message, and we also need to persist the new rendered content. # If we don't have a message object passed in, we get one here. The cost # of going to the DB here should be overshadowed by the cost of rendering # and updating the row. # TODO: see #1379 to eliminate bugdown dependencies message = Message.objects.select_related().get(id=message_id) # It's unfortunate that we need to have side effects on the message # in some cases. rendered_content = render_markdown(message, content, realm=message.get_realm()) message.rendered_content = rendered_content message.rendered_content_version = bugdown.version message.save_rendered_content() if rendered_content is not None: obj['content'] = rendered_content else: obj['content'] = u'<p>[Zulip note: Sorry, we could not understand the formatting of your message]</p>' obj['content_type'] = 'text/html' else: obj['content'] = content obj['content_type'] = 'text/x-markdown' obj['reactions'] = [ReactionDict.build_dict_from_raw_db_row(reaction) for reaction in reactions] return obj