Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes large pypi image in Slack link unfurling #17282

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/unit/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,36 @@ def test_remove_invalid_xml_unicode(inp, expected):
Test that invalid XML unicode characters are removed.
"""
assert filters.remove_invalid_xml_unicode(inp) == expected


def test_show_share_image():
# Missing user-agent header
assert filters.show_share_image(None) is True
# Empty user-agent header
assert filters.show_share_image("") is True

# Twitter/X - shows image
# https://developer.x.com/en/docs/x-for-websites/cards/guides/troubleshooting-cards#validate_twitterbot
assert filters.show_share_image("Twitterbot/1.0") is True

# Facebook - shows image
# https://developers.facebook.com/docs/sharing/webmasters/web-crawlers
assert (
filters.show_share_image(
"facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
)
is True
)
assert filters.show_share_image("facebookexternalhit/1.1") is True
assert filters.show_share_image("facebookcatalog/1.0") is True

# LinkedIn - shows image (https://www.linkedin.com/robots.txt)
assert filters.show_share_image("LinkedInBot") is True

# Slack - don't show image (https://api.slack.com/robots)
assert (
filters.show_share_image(
"Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)"
)
is False
)
1 change: 1 addition & 0 deletions warehouse/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ def configure(settings=None):
filters.setdefault(
"remove_invalid_xml_unicode", "warehouse.filters:remove_invalid_xml_unicode"
)
filters.setdefault("show_share_image", "warehouse.filters:show_share_image")

# We also want to register some global functions for Jinja
jglobals = config.get_settings().setdefault("jinja2.globals", {})
Expand Down
19 changes: 19 additions & 0 deletions warehouse/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,22 @@ def remove_invalid_xml_unicode(value: str | None) -> str | None:

def includeme(config):
config.add_request_method(_camo_url, name="camo_url")


def show_share_image(user_agent: str | None) -> bool:
"""
Whether the og:image meta-tag should be included based on the user-agent

Used to exclude the image from Slack link-expansion.

"""

# User agent header not included or empty
if not user_agent:
return True

# Don't show the og:image for Slackbot link-expansion requests
if user_agent.strip().startswith("Slackbot-LinkExpanding"):
return False

return True
4 changes: 4 additions & 0 deletions warehouse/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@
<meta property="og:url" content="{% if request.matched_route %}{{ request.current_route_url() }}{% else %}{{ request.url }}{% endif %}">
<meta property="og:site_name" content="PyPI">
<meta property="og:type" content="website">
{% if request.user_agent | show_share_image %}
<meta property="og:image" content="{% block image %}{{ request.static_url('warehouse:static/dist/images/twitter.jpg') }}{% endblock %}">
{% endif %}
<meta property="og:title" content="{{ self.title()|default('Python Package Index') }}">
<meta property="og:description" content="{{ self.description() }}">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@pypi">
{% block extra_meta %}{% endblock %}

<link rel="search" type="application/opensearchdescription+xml" title="PyPI" href="{{ request.route_path('opensearch.xml') }}">
Expand Down