Skip to content

Commit

Permalink
Fix reblog attribution linking to external domain
Browse files Browse the repository at this point in the history
On blogs with a custom domain the url given in rebloggedFrom*
and rebloggedRoot* will use their custom domain instead of a
regular tumblr domain.

In order to preserve the slug of the post priviblur uses the url given
from rebloggedFrom* and rebloggedRoot* which meant that users can be
directed to an external site unknownly.

This commit fixes that by using the url given in the parentPostUrl
which should always be a regular tumblr link. Redundancy is also added
in that priviblur will check if the URL is a Tumblr domain and return
a span if it isn't and priviblur is unable to create a url to the original
post
  • Loading branch information
syeopite committed Jan 2, 2025
1 parent 53f11f4 commit eabe1a4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
48 changes: 46 additions & 2 deletions src/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
import urllib.parse
from typing import Sequence

import sanic
import dominate.tags

from ..cache import get_poll_results


def is_tumblr_url(url : str | urllib.parse.ParseResult):
"""Checks URL is a tumblr URL"""
if isinstance(url, str):
url = urllib.parse.urlparse(url)
elif isinstance(url, urllib.parse.ParseResult):
url = url
else:
raise False

hostname = url.hostname

if hostname and (hostname == "tumblr.com" or hostname.endswith(".tumblr.com")):
return True
return False


def url_handler(url : str | urllib.parse.ParseResult):
"""Change URLs found in posts to privacy-friendly alternatives"""
if isinstance(url, str):
Expand Down Expand Up @@ -65,7 +81,35 @@ def url_handler(url : str | urllib.parse.ParseResult):
else:
return f"{url.path}"

return raw_url
return url.geturl()


def create_reblog_attribution_link(post):
"""Creates an attribution of who the author reblogged the post from"""
reblog_from_url = urllib.parse.urlparse(post.reblog_from.post_url)
reblog_attribution_element_classes = ["link", "blog-name"]

if post.reblog_from.blog_name:
reblogged_from_name = post.reblog_from.blog_name
else:
reblogged_from_name = "reblogged"
reblog_attribution_element_classes.append("hidden-reblog")

if not is_tumblr_url(reblog_from_url):
if (post.reblog_root.post_id == post.reblog_from.post_id) and post.reblog_root.blog_name:
reblog_from_url = f"/{post.reblog_root.blog_name}/{post.reblog_from.id}"
else:
# In case we are unable to find a tumblr URL to use
return dominate.tags.span(
reblogged_from_name,
cls="blog-name hidden-reblog"
)

return dominate.tags.a(
reblogged_from_name,
href=url_handler(reblog_from_url),
cls=' '.join(reblog_attribution_element_classes)
)


def update_query_params(base_query_args, key, value):
Expand Down
5 changes: 3 additions & 2 deletions src/priviblur_extractor/parse/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def process(cls, initial_data):
else:
return None


@staticmethod
def parse_community_label(initial_data):
community_labels = []
Expand Down Expand Up @@ -201,7 +200,9 @@ def parse(self):
if reblogged_from_id := self.target.get("rebloggedFromId"):
reblog_from_information = models.post.ReblogAttribution(
post_id=reblogged_from_id,
post_url=self.target["rebloggedFromUrl"],
# If a blog uses a custom domain then the rebloggedFromUrl will be that domain
# thus we'll try to extract the original tumblr URL from the parentPostUrl attr instead.
post_url=self.target["parentPostUrl"],
blog_name=self.target["rebloggedFromName"],
blog_title=self.target["rebloggedFromTitle"],
)
Expand Down
1 change: 1 addition & 0 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def create_image_client(url, timeout):
app.ext.environment.globals["url_handler"] = helpers.url_handler
app.ext.environment.globals["format_npf"] = ext_npf_renderer.format_npf
app.ext.environment.globals["create_poll_callback"] = helpers.create_poll_callback
app.ext.environment.globals["create_reblog_attribution"] = helpers.create_reblog_attribution_link

app.ext.environment.tests["a_post"] = lambda element : isinstance(element, priviblur_extractor.models.post.Post)

Expand Down
14 changes: 2 additions & 12 deletions src/templates/post/components/header.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,8 @@
{#- The reblog from data can sometimes have an empty blog name. Thus we'll extract the name from the root reblog info if they're the same -#}
{# Though sometimes the reblog data is just hidden #}
<div class="reblog-attribution">
{%- if not element.reblog_from.blog_name -%}
{%- if element.reblog_root and element.reblog_root.post_id == element.reblog_from.post_id -%}
{{reblog_icon(16, 16)}}
<a class="link blog-name" href="{{url_handler(element.reblog_root.post_url)}}">{{element.reblog_root.blog_name}}</a>
{%- else -%}
{{reblog_icon(16, 16)}}
<a class="link blog-name hidden-reblog" href="{{url_handler(element.reblog_from.post_url)}}">reblogged</a>
{%- endif -%}
{%- else -%}
{{reblog_icon(16, 16)}}
<a class="link blog-name" href="{{url_handler(element.reblog_from.post_url)}}">{{element.reblog_from.blog_name}}</a>
{%- endif -%}
{{reblog_icon(16, 16)}}
{{create_reblog_attribution(element)}}
</div>
{%- endif -%}
</div>
Expand Down

0 comments on commit eabe1a4

Please sign in to comment.