From e86108e18f8ecc26d39d504b53cd08e46a1fe25e Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Sat, 25 Mar 2023 16:32:37 -0400 Subject: [PATCH] Rearrange article id parsing to be more reliable The article ID parser looks for a string at the end of a URL path with a bunch of hex digits. But it also has to handle user, tag, and search URLs. * /@ba5eba11 * /tag/0ddba11 * /search?q=ba5eba11 Some URLs are encoded as params. The parser used to look at the result of the path first, then the params. But paths that ended in `global-identity-2` messed that up because `2` is a hex digit at the end of the path. This changes the logic to parse params first and paths second which gets around this. --- spec/classes/article_id_parser_spec.cr | 8 ++++++++ src/classes/article_id_parser.cr | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/classes/article_id_parser_spec.cr b/spec/classes/article_id_parser_spec.cr index 2baef20..044aebf 100644 --- a/spec/classes/article_id_parser_spec.cr +++ b/spec/classes/article_id_parser_spec.cr @@ -78,6 +78,14 @@ describe ArticleIdParser do result.should eq(Monads::Just.new("888888abcdef")) end + it "parses the post id for global identity 2 redirects" do + request = resource_request("/m/global-identity-2?redirectUrl=https%3A%2F%2Fexample.com%2Fmy-post-999999abcdef") + + result = ArticleIdParser.parse(request) + + result.should eq(Monads::Just.new("999999abcdef")) + end + it "returns Nothing if path is a username" do request = resource_request("/@ba5eba11") diff --git a/src/classes/article_id_parser.cr b/src/classes/article_id_parser.cr index 1db19de..639013c 100644 --- a/src/classes/article_id_parser.cr +++ b/src/classes/article_id_parser.cr @@ -10,7 +10,7 @@ class ArticleIdParser def parse(request : HTTP::Request) : Maybe from_params = post_id_from_params(request.query_params) from_path = post_id_from_path(request.path) - from_path.or(from_params) + from_params.or(from_path) end private def post_id_from_path(request_path : String)