diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 9700de77..3078a595 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1940,7 +1940,6 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json post.image = image else: post.type = POST_TYPE_LINK - post.url = remove_tracking_from_link(post.url) domain = domain_from_url(post.url) # notify about links to banned websites. already_notified = set() # often admins and mods are the same people - avoid notifying them twice @@ -1995,7 +1994,10 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json post.image = image if post.image is None and post.type == POST_TYPE_LINK: # This is a link post but the source instance has not provided a thumbnail image # Let's see if we can do better than the source instance did! - opengraph = opengraph_parse(post.url) + tn_url = post.url + if tn_url[:32] == 'https://www.youtube.com/watch?v=': + tn_url = 'https://youtu.be/' + tn_url[32:43] # better chance of thumbnail from youtu.be than youtube.com + opengraph = opengraph_parse(tn_url) if opengraph and (opengraph.get('og:image', '') != '' or opengraph.get('og:image:url', '') != ''): filename = opengraph.get('og:image') or opengraph.get('og:image:url') if not filename.startswith('/'): @@ -2003,6 +2005,8 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json post.image = file db.session.add(file) + if post.url: + post.url = remove_tracking_from_link(post.url) # moved here as changes youtu.be to youtube.com db.session.add(post) post.ranking = post_ranking(post.score, post.posted_at) community.post_count += 1 @@ -2184,6 +2188,8 @@ def update_post_from_activity(post: Post, request_json: dict): alt_text = request_json['object']['attachment'][0]['name'] if post.url == '': post.type = POST_TYPE_ARTICLE + else: + post.url = remove_tracking_from_link(post.url) if (post.url and post.url != old_url) or (post.url == '' and old_url != ''): if post.image_id: old_image = File.query.get(post.image_id) @@ -2209,7 +2215,6 @@ def update_post_from_activity(post: Post, request_json: dict): post.image = image else: post.type = POST_TYPE_LINK - post.url = remove_tracking_from_link(post.url) domain = domain_from_url(post.url) # notify about links to banned websites. already_notified = set() # often admins and mods are the same people - avoid notifying them twice diff --git a/app/community/util.py b/app/community/util.py index b3b8841d..62b9d617 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -287,7 +287,10 @@ def save_post(form, post: Post, type: int): else: # check opengraph tags on the page and make a thumbnail if an image is available in the og:image meta tag if not post.type == POST_TYPE_VIDEO: - opengraph = opengraph_parse(form.link_url.data) + tn_url = form.link_url.data + if tn_url[:32] == 'https://www.youtube.com/watch?v=': + tn_url = 'https://youtu.be/' + tn_url[32:43] # better chance of thumbnail from youtu.be than youtube.com + opengraph = opengraph_parse(tn_url) if opengraph and (opengraph.get('og:image', '') != '' or opengraph.get('og:image:url', '') != ''): filename = opengraph.get('og:image') or opengraph.get('og:image:url') if not filename.startswith('/'): diff --git a/app/models.py b/app/models.py index c3f5d776..19da705c 100644 --- a/app/models.py +++ b/app/models.py @@ -1059,7 +1059,7 @@ class Post(db.Model): if self.url: vpos = self.url.find('v=') if vpos != -1: - return self.url[vpos + 2:vpos + 13] + return self.url[vpos + 2:vpos + 13] + '?rel=0' + self.url[vpos + 13:] def peertube_embed(self): if self.url: diff --git a/app/templates/post/_post_full.html b/app/templates/post/_post_full.html index 46fef021..34679292 100644 --- a/app/templates/post/_post_full.html +++ b/app/templates/post/_post_full.html @@ -99,7 +99,7 @@ {% endif -%} {% if 'youtube.com' in post.url -%}

{{ _('Watch on piped.video') }}

-
+
{% endif -%} {% elif post.type == POST_TYPE_VIDEO -%}

{{ post.url|shorten_url }} @@ -120,7 +120,7 @@ {% endif -%} {% if 'youtube.com' in post.url -%}

{{ _('Watch on piped.video') }}

-
+
{% endif -%} {% if 'videos/watch' in post.url -%}
diff --git a/app/utils.py b/app/utils.py index 4cfb7288..fdcb062b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1060,9 +1060,10 @@ def remove_tracking_from_link(url): else: new_query_string = '' - cleaned_url = f"https://youtu.be/{video_id}" + cleaned_url = f"https://youtube.com/watch?v={video_id}" if new_query_string: - cleaned_url += f"?{new_query_string}" + new_query_string = new_query_string.replace('t=', 'start=') + cleaned_url += f"&{new_query_string}" return cleaned_url else: