Update URL parser to handle 'x.com' links

This commit is contained in:
Alessio 2023-10-09 19:40:34 -07:00
parent 0907fe7fa8
commit 392e836689
3 changed files with 10 additions and 6 deletions

View File

@ -257,7 +257,7 @@ func TestSearchRedirectOnUserFeedLink(t *testing.T) {
assert.Equal(resp.Header.Get("Location"), "/agsdf") assert.Equal(resp.Header.Get("Location"), "/agsdf")
// "With Replies" page // "With Replies" page
resp = do_request(httptest.NewRequest("GET", fmt.Sprintf("/search/%s", url.PathEscape("https://twitter.com/agsdf/with_replies")), nil)) resp = do_request(httptest.NewRequest("GET", fmt.Sprintf("/search/%s", url.PathEscape("https://x.com/agsdf/with_replies")), nil))
assert.Equal(resp.StatusCode, 302) assert.Equal(resp.StatusCode, 302)
assert.Equal(resp.Header.Get("Location"), "/agsdf") assert.Equal(resp.Header.Get("Location"), "/agsdf")

View File

@ -97,17 +97,15 @@ func get_thumbnail_local_path(remote_url string) string {
) )
} }
/** // Given an URL, try to parse it as a tweet url.
* Given an URL, try to parse it as a tweet url. // The bool is an `is_ok` value; true if the parse was successful, false if it didn't match
* The bool is an `is_ok` value; true if the parse was successful, false if it didn't match
*/
func TryParseTweetUrl(s string) (UserHandle, TweetID, bool) { func TryParseTweetUrl(s string) (UserHandle, TweetID, bool) {
parsed_url, err := url.Parse(s) parsed_url, err := url.Parse(s)
if err != nil { if err != nil {
return UserHandle(""), TweetID(0), false return UserHandle(""), TweetID(0), false
} }
if parsed_url.Host != "twitter.com" && parsed_url.Host != "mobile.twitter.com" { if parsed_url.Host != "twitter.com" && parsed_url.Host != "mobile.twitter.com" && parsed_url.Host != "x.com" {
return UserHandle(""), TweetID(0), false return UserHandle(""), TweetID(0), false
} }

View File

@ -123,6 +123,12 @@ func TestParseTweetUrl(t *testing.T) {
assert.Equal(UserHandle("APhilosophae"), handle) assert.Equal(UserHandle("APhilosophae"), handle)
assert.Equal(TweetID(1497720548540964864), id) assert.Equal(TweetID(1497720548540964864), id)
// Test a `x.com` url
handle, id, is_ok = TryParseTweetUrl("https://x.com/brutedeforce/status/1579695139425222657?s=46")
assert.True(is_ok)
assert.Equal(UserHandle("brutedeforce"), handle)
assert.Equal(TweetID(1579695139425222657), id)
// Test invalid url // Test invalid url
_, _, is_ok = TryParseTweetUrl("https://twitter.com/NerdNoticing/status/1263192389050654720s=20") _, _, is_ok = TryParseTweetUrl("https://twitter.com/NerdNoticing/status/1263192389050654720s=20")
assert.False(is_ok) assert.False(is_ok)