From 78c447829a605cfc6b82fb9dcb4e01057a17cec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Fri, 25 Feb 2022 02:11:30 +0100 Subject: [PATCH 1/6] Increase size of links displayed in video description --- src/invidious/comments.cr | 6 +++--- src/invidious/helpers/utils.cr | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index ab9fcc8b..e2c7b3a0 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -587,7 +587,7 @@ def content_to_comment_html(content) end end - text = %(#{text}) + text = %(#{reduce_uri(url)}) elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]? length_seconds = watch_endpoint["startTimeSeconds"]? video_id = watch_endpoint["videoId"].as_s @@ -595,10 +595,10 @@ def content_to_comment_html(content) if length_seconds && length_seconds.as_i > 0 text = %(#{text}) else - text = %(#{text}) + text = %(#{reduce_uri("/watch?v=#{video_id}")}) end elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s - text = %(#{text}) + text = %(#{reduce_uri(url)}) end end diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index a58a21b1..f8a7873d 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -365,3 +365,14 @@ def fetch_random_instance return filtered_instance_list.sample(1)[0] end + +def reduce_uri(uri : URI | String, max_length : Int32? = 50, suffix : String? = "...") : String + str = uri.to_s.sub(/https?:\/\//, "") + if !max_length.nil? && str.size > max_length + str = str[0, max_length] + if !suffix.nil? + str = "#{str}#{suffix}" + end + end + return str +end From 0f1bb3fb3be085b3234d4baa3e512ef927aff4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Fri, 25 Feb 2022 11:47:07 +0100 Subject: [PATCH 2/6] Update reduce_uri signature Following request_change at : - https://github.com/iv-org/invidious/pull/2936#discussion_r814436660 --- src/invidious/helpers/utils.cr | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index f8a7873d..8180ab6f 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -366,13 +366,10 @@ def fetch_random_instance return filtered_instance_list.sample(1)[0] end -def reduce_uri(uri : URI | String, max_length : Int32? = 50, suffix : String? = "...") : String +def reduce_uri(uri : URI | String, max_length : Int32 = 50, suffix : String = "…") : String str = uri.to_s.sub(/https?:\/\//, "") - if !max_length.nil? && str.size > max_length - str = str[0, max_length] - if !suffix.nil? - str = "#{str}#{suffix}" - end + if str.size > max_length + str = "#{str[0, max_length]}#{suffix}" end return str end From 420c458b6adab8a6e35a7612c9eb6a0ba2382440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Fri, 25 Feb 2022 21:07:12 +0100 Subject: [PATCH 3/6] Update links related to youtube.com Following comment at : - https://github.com/iv-org/invidious/pull/2936#discussion_r814435888 --- src/invidious/comments.cr | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index e2c7b3a0..ae8f052a 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -573,10 +573,11 @@ def content_to_comment_html(content) if run["navigationEndpoint"]? if url = run["navigationEndpoint"]["urlEndpoint"]?.try &.["url"].as_s + base_url = URI.parse(url) url = URI.parse(url) if url.host == "youtu.be" - url = "/watch?v=#{url.request_target.lstrip('/')}" + url = "youtube.com/watch?v=#{url.request_target.lstrip('/')}" elsif url.host.nil? || url.host.not_nil!.ends_with?("youtube.com") if url.path == "/redirect" # Sometimes, links can be corrupted (why?) so make sure to fallback @@ -587,7 +588,12 @@ def content_to_comment_html(content) end end - text = %(#{reduce_uri(url)}) + if base_url.host.not_nil!.ends_with?("youtube.com") && base_url.path != "/redirect" + displayed_url = "youtube.com#{base_url.request_target}" + else + displayed_url = url + end + text = %(#{reduce_uri(displayed_url)}) elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]? length_seconds = watch_endpoint["startTimeSeconds"]? video_id = watch_endpoint["videoId"].as_s @@ -595,7 +601,7 @@ def content_to_comment_html(content) if length_seconds && length_seconds.as_i > 0 text = %(#{text}) else - text = %(#{reduce_uri("/watch?v=#{video_id}")}) + text = %(#{reduce_uri("youtube.com/watch?v=#{video_id}")}) end elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s text = %(#{reduce_uri(url)}) From 19805b91d90cc6e8648a213d0b46636392484f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Sat, 26 Feb 2022 17:53:39 +0100 Subject: [PATCH 4/6] Patch links related to youtube.com Related to followings comments : - https://github.com/iv-org/invidious/pull/2936#discussion_r815253405 --- src/invidious/comments.cr | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index ae8f052a..1fd3dcfd 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -573,26 +573,24 @@ def content_to_comment_html(content) if run["navigationEndpoint"]? if url = run["navigationEndpoint"]["urlEndpoint"]?.try &.["url"].as_s - base_url = URI.parse(url) url = URI.parse(url) + displayed_url = url if url.host == "youtu.be" - url = "youtube.com/watch?v=#{url.request_target.lstrip('/')}" + url = "/watch?v=#{url.request_target.lstrip('/')}" + displayed_url = "youtube.com#{url}" elsif url.host.nil? || url.host.not_nil!.ends_with?("youtube.com") if url.path == "/redirect" # Sometimes, links can be corrupted (why?) so make sure to fallback # nicely. See https://github.com/iv-org/invidious/issues/2682 url = HTTP::Params.parse(url.query.not_nil!)["q"]? || "" + displayed_url = url else url = url.request_target + displayed_url = "youtube.com#{url}" end end - if base_url.host.not_nil!.ends_with?("youtube.com") && base_url.path != "/redirect" - displayed_url = "youtube.com#{base_url.request_target}" - else - displayed_url = url - end text = %(#{reduce_uri(displayed_url)}) elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]? length_seconds = watch_endpoint["startTimeSeconds"]? From 68ac18dc9876d8b4328a75b608a8a15e3f322720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Sun, 3 Apr 2022 23:26:34 +0200 Subject: [PATCH 5/6] Remove useless call Follow this comment : https://github.com/iv-org/invidious/pull/2936#discussion_r841277735 --- src/invidious/comments.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index 1fd3dcfd..66cbc4fc 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -599,7 +599,7 @@ def content_to_comment_html(content) if length_seconds && length_seconds.as_i > 0 text = %(#{text}) else - text = %(#{reduce_uri("youtube.com/watch?v=#{video_id}")}) + text = %(#{"youtube.com/watch?v=#{video_id}"}) end elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s text = %(#{reduce_uri(url)}) From 6c122248f595a338e565bf73b1b6e5a2b761b894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9ry=20Mathieu=20=28Mathius=29?= Date: Thu, 14 Apr 2022 22:42:21 +0200 Subject: [PATCH 6/6] Update regex reduce_uri utils Follow this comment : https://github.com/iv-org/invidious/pull/2936#discussion_r850712676 --- src/invidious/helpers/utils.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 8180ab6f..9d403ddc 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -367,7 +367,7 @@ def fetch_random_instance end def reduce_uri(uri : URI | String, max_length : Int32 = 50, suffix : String = "…") : String - str = uri.to_s.sub(/https?:\/\//, "") + str = uri.to_s.sub(/^https?:\/\//, "") if str.size > max_length str = "#{str[0, max_length]}#{suffix}" end