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.
This commit is contained in:
Edward Loveall 2023-03-25 16:32:37 -04:00
parent cef1bc256d
commit e86108e18f
No known key found for this signature in database
2 changed files with 9 additions and 1 deletions

View file

@ -78,6 +78,14 @@ describe ArticleIdParser do
result.should eq(Monads::Just.new("888888abcdef")) result.should eq(Monads::Just.new("888888abcdef"))
end 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 it "returns Nothing if path is a username" do
request = resource_request("/@ba5eba11") request = resource_request("/@ba5eba11")

View file

@ -10,7 +10,7 @@ class ArticleIdParser
def parse(request : HTTP::Request) : Maybe def parse(request : HTTP::Request) : Maybe
from_params = post_id_from_params(request.query_params) from_params = post_id_from_params(request.query_params)
from_path = post_id_from_path(request.path) from_path = post_id_from_path(request.path)
from_path.or(from_params) from_params.or(from_path)
end end
private def post_id_from_path(request_path : String) private def post_id_from_path(request_path : String)