mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	webhooks: Support no_previews argument for markdown messages.
This commit enables skipping inline image previews by passing the no_previews field to check_send_webhook_message.
This commit is contained in:
		@@ -157,6 +157,7 @@ def render_incoming_message(
 | 
				
			|||||||
    url_embed_data: dict[str, UrlEmbedData | None] | None = None,
 | 
					    url_embed_data: dict[str, UrlEmbedData | None] | None = None,
 | 
				
			||||||
    email_gateway: bool = False,
 | 
					    email_gateway: bool = False,
 | 
				
			||||||
    acting_user: UserProfile | None = None,
 | 
					    acting_user: UserProfile | None = None,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> MessageRenderingResult:
 | 
					) -> MessageRenderingResult:
 | 
				
			||||||
    realm_alert_words_automaton = get_alert_word_automaton(realm)
 | 
					    realm_alert_words_automaton = get_alert_word_automaton(realm)
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
@@ -168,6 +169,7 @@ def render_incoming_message(
 | 
				
			|||||||
            mention_data=mention_data,
 | 
					            mention_data=mention_data,
 | 
				
			||||||
            url_embed_data=url_embed_data,
 | 
					            url_embed_data=url_embed_data,
 | 
				
			||||||
            email_gateway=email_gateway,
 | 
					            email_gateway=email_gateway,
 | 
				
			||||||
 | 
					            no_previews=no_previews,
 | 
				
			||||||
            acting_user=acting_user,
 | 
					            acting_user=acting_user,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
    except MarkdownRenderingError:
 | 
					    except MarkdownRenderingError:
 | 
				
			||||||
@@ -579,6 +581,7 @@ def build_message_send_dict(
 | 
				
			|||||||
    disable_external_notifications: bool = False,
 | 
					    disable_external_notifications: bool = False,
 | 
				
			||||||
    recipients_for_user_creation_events: dict[UserProfile, set[int]] | None = None,
 | 
					    recipients_for_user_creation_events: dict[UserProfile, set[int]] | None = None,
 | 
				
			||||||
    acting_user: UserProfile | None = None,
 | 
					    acting_user: UserProfile | None = None,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> SendMessageRequest:
 | 
					) -> SendMessageRequest:
 | 
				
			||||||
    """Returns a dictionary that can be passed into do_send_messages.  In
 | 
					    """Returns a dictionary that can be passed into do_send_messages.  In
 | 
				
			||||||
    production, this is always called by check_message, but some
 | 
					    production, this is always called by check_message, but some
 | 
				
			||||||
@@ -624,6 +627,7 @@ def build_message_send_dict(
 | 
				
			|||||||
        mention_data=mention_data,
 | 
					        mention_data=mention_data,
 | 
				
			||||||
        email_gateway=email_gateway,
 | 
					        email_gateway=email_gateway,
 | 
				
			||||||
        acting_user=acting_user,
 | 
					        acting_user=acting_user,
 | 
				
			||||||
 | 
					        no_previews=no_previews,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    message.rendered_content = rendering_result.rendered_content
 | 
					    message.rendered_content = rendering_result.rendered_content
 | 
				
			||||||
    message.rendered_content_version = markdown_version
 | 
					    message.rendered_content_version = markdown_version
 | 
				
			||||||
@@ -1365,9 +1369,10 @@ def check_send_stream_message(
 | 
				
			|||||||
    *,
 | 
					    *,
 | 
				
			||||||
    realm: Realm | None = None,
 | 
					    realm: Realm | None = None,
 | 
				
			||||||
    read_by_sender: bool = False,
 | 
					    read_by_sender: bool = False,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> int:
 | 
					) -> int:
 | 
				
			||||||
    addressee = Addressee.for_stream_name(stream_name, topic_name)
 | 
					    addressee = Addressee.for_stream_name(stream_name, topic_name)
 | 
				
			||||||
    message = check_message(sender, client, addressee, body, realm)
 | 
					    message = check_message(sender, client, addressee, body, realm, no_previews=no_previews)
 | 
				
			||||||
    sent_message_result = do_send_messages(
 | 
					    sent_message_result = do_send_messages(
 | 
				
			||||||
        [message], mark_as_read=[sender.id] if read_by_sender else []
 | 
					        [message], mark_as_read=[sender.id] if read_by_sender else []
 | 
				
			||||||
    )[0]
 | 
					    )[0]
 | 
				
			||||||
@@ -1381,18 +1386,23 @@ def check_send_stream_message_by_id(
 | 
				
			|||||||
    topic_name: str,
 | 
					    topic_name: str,
 | 
				
			||||||
    body: str,
 | 
					    body: str,
 | 
				
			||||||
    realm: Realm | None = None,
 | 
					    realm: Realm | None = None,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> int:
 | 
					) -> int:
 | 
				
			||||||
    addressee = Addressee.for_stream_id(stream_id, topic_name)
 | 
					    addressee = Addressee.for_stream_id(stream_id, topic_name)
 | 
				
			||||||
    message = check_message(sender, client, addressee, body, realm)
 | 
					    message = check_message(sender, client, addressee, body, realm, no_previews=no_previews)
 | 
				
			||||||
    sent_message_result = do_send_messages([message])[0]
 | 
					    sent_message_result = do_send_messages([message])[0]
 | 
				
			||||||
    return sent_message_result.message_id
 | 
					    return sent_message_result.message_id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def check_send_private_message(
 | 
					def check_send_private_message(
 | 
				
			||||||
    sender: UserProfile, client: Client, receiving_user: UserProfile, body: str
 | 
					    sender: UserProfile,
 | 
				
			||||||
 | 
					    client: Client,
 | 
				
			||||||
 | 
					    receiving_user: UserProfile,
 | 
				
			||||||
 | 
					    body: str,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> int:
 | 
					) -> int:
 | 
				
			||||||
    addressee = Addressee.for_user_profile(receiving_user)
 | 
					    addressee = Addressee.for_user_profile(receiving_user)
 | 
				
			||||||
    message = check_message(sender, client, addressee, body)
 | 
					    message = check_message(sender, client, addressee, body, no_previews=no_previews)
 | 
				
			||||||
    sent_message_result = do_send_messages([message])[0]
 | 
					    sent_message_result = do_send_messages([message])[0]
 | 
				
			||||||
    return sent_message_result.message_id
 | 
					    return sent_message_result.message_id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1704,6 +1714,7 @@ def check_message(
 | 
				
			|||||||
    limit_unread_user_ids: set[int] | None = None,
 | 
					    limit_unread_user_ids: set[int] | None = None,
 | 
				
			||||||
    disable_external_notifications: bool = False,
 | 
					    disable_external_notifications: bool = False,
 | 
				
			||||||
    archived_channel_notice: bool = False,
 | 
					    archived_channel_notice: bool = False,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
    acting_user: UserProfile | None = None,
 | 
					    acting_user: UserProfile | None = None,
 | 
				
			||||||
) -> SendMessageRequest:
 | 
					) -> SendMessageRequest:
 | 
				
			||||||
    """See
 | 
					    """See
 | 
				
			||||||
@@ -1852,6 +1863,7 @@ def check_message(
 | 
				
			|||||||
        disable_external_notifications=disable_external_notifications,
 | 
					        disable_external_notifications=disable_external_notifications,
 | 
				
			||||||
        recipients_for_user_creation_events=recipients_for_user_creation_events,
 | 
					        recipients_for_user_creation_events=recipients_for_user_creation_events,
 | 
				
			||||||
        acting_user=acting_user,
 | 
					        acting_user=acting_user,
 | 
				
			||||||
 | 
					        no_previews=no_previews,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (
 | 
					    if (
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2909,6 +2909,7 @@ def render_message_markdown(
 | 
				
			|||||||
    mention_data: MentionData | None = None,
 | 
					    mention_data: MentionData | None = None,
 | 
				
			||||||
    email_gateway: bool = False,
 | 
					    email_gateway: bool = False,
 | 
				
			||||||
    acting_user: UserProfile | None = None,
 | 
					    acting_user: UserProfile | None = None,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> MessageRenderingResult:
 | 
					) -> MessageRenderingResult:
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    This is basically just a wrapper for do_render_markdown.
 | 
					    This is basically just a wrapper for do_render_markdown.
 | 
				
			||||||
@@ -2931,6 +2932,7 @@ def render_message_markdown(
 | 
				
			|||||||
        url_embed_data=url_embed_data,
 | 
					        url_embed_data=url_embed_data,
 | 
				
			||||||
        mention_data=mention_data,
 | 
					        mention_data=mention_data,
 | 
				
			||||||
        email_gateway=email_gateway,
 | 
					        email_gateway=email_gateway,
 | 
				
			||||||
 | 
					        no_previews=no_previews,
 | 
				
			||||||
        acting_user=acting_user,
 | 
					        acting_user=acting_user,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -101,6 +101,7 @@ def check_send_webhook_message(
 | 
				
			|||||||
    only_events: Json[list[str]] | None = None,
 | 
					    only_events: Json[list[str]] | None = None,
 | 
				
			||||||
    exclude_events: Json[list[str]] | None = None,
 | 
					    exclude_events: Json[list[str]] | None = None,
 | 
				
			||||||
    unquote_url_parameters: bool = False,
 | 
					    unquote_url_parameters: bool = False,
 | 
				
			||||||
 | 
					    no_previews: bool = False,
 | 
				
			||||||
) -> None:
 | 
					) -> None:
 | 
				
			||||||
    if complete_event_type is not None and (
 | 
					    if complete_event_type is not None and (
 | 
				
			||||||
        # Here, we implement Zulip's generic support for filtering
 | 
					        # Here, we implement Zulip's generic support for filtering
 | 
				
			||||||
@@ -128,7 +129,9 @@ def check_send_webhook_message(
 | 
				
			|||||||
    assert client is not None
 | 
					    assert client is not None
 | 
				
			||||||
    if stream is None:
 | 
					    if stream is None:
 | 
				
			||||||
        assert user_profile.bot_owner is not None
 | 
					        assert user_profile.bot_owner is not None
 | 
				
			||||||
        check_send_private_message(user_profile, client, user_profile.bot_owner, body)
 | 
					        check_send_private_message(
 | 
				
			||||||
 | 
					            user_profile, client, user_profile.bot_owner, body, no_previews=no_previews
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        # Some third-party websites (such as Atlassian's Jira), tend to
 | 
					        # Some third-party websites (such as Atlassian's Jira), tend to
 | 
				
			||||||
        # double escape their URLs in a manner that escaped space characters
 | 
					        # double escape their URLs in a manner that escaped space characters
 | 
				
			||||||
@@ -144,9 +147,13 @@ def check_send_webhook_message(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            if stream.isdecimal():
 | 
					            if stream.isdecimal():
 | 
				
			||||||
                check_send_stream_message_by_id(user_profile, client, int(stream), topic, body)
 | 
					                check_send_stream_message_by_id(
 | 
				
			||||||
 | 
					                    user_profile, client, int(stream), topic, body, no_previews=no_previews
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                check_send_stream_message(user_profile, client, stream, topic, body)
 | 
					                check_send_stream_message(
 | 
				
			||||||
 | 
					                    user_profile, client, stream, topic, body, no_previews=no_previews
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
        except StreamDoesNotExistError:
 | 
					        except StreamDoesNotExistError:
 | 
				
			||||||
            # A direct message will be sent to the bot_owner by check_message,
 | 
					            # A direct message will be sent to the bot_owner by check_message,
 | 
				
			||||||
            # notifying that the webhook bot just tried to send a message to a
 | 
					            # notifying that the webhook bot just tried to send a message to a
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user