Skip to content

Commit

Permalink
Refactored to use a jinja filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rmasters committed Dec 19, 2024
1 parent 52bf053 commit e2333a2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 74 deletions.
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
)
46 changes: 0 additions & 46 deletions tests/unit/utils/test_user_agents.py

This file was deleted.

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
2 changes: 0 additions & 2 deletions warehouse/packaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from warehouse.observations.models import ObservationKind
from warehouse.packaging.forms import SubmitMalwareObservationForm
from warehouse.packaging.models import Description, File, Project, Release, Role
from warehouse.utils.user_agents import should_show_share_image


class PEP740AttestationViewer:
Expand Down Expand Up @@ -289,7 +288,6 @@ def release_detail(release, request):
"license": license,
# Additional function to format the attestations
"PEP740AttestationViewer": PEP740AttestationViewer,
"show_share_image": should_show_share_image(request),
}


Expand Down
2 changes: 1 addition & 1 deletion warehouse/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<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 show_share_image %}
{% 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') }}">
Expand Down
25 changes: 0 additions & 25 deletions warehouse/utils/user_agents.py

This file was deleted.

0 comments on commit e2333a2

Please sign in to comment.