Fix crash on invalid id and non-existent profiles

This commit is contained in:
Zed 2019-06-25 13:07:49 +02:00
parent 684489ee21
commit 8000a814df
2 changed files with 12 additions and 7 deletions

View File

@ -161,21 +161,23 @@ proc getTimeline*(username: string; after=""): Future[Timeline] {.async.} =
}) })
var url = timelineUrl % username var url = timelineUrl % username
if after.len > 0: let cleanAfter = after.replace(re"[^\d]*(\d+)[^\d]*", "$1")
url &= "&max_position=" & after if cleanAfter.len > 0:
url &= "&max_position=" & cleanAfter
let json = await fetchJson(base / url, headers) let json = await fetchJson(base / url, headers)
let html = parseHtml(json["items_html"].to(string)) let html = parseHtml(json["items_html"].to(string))
result = Timeline( result = Timeline(
tweets: parseTweets(html),
minId: json["min_position"].to(string),
hasMore: json["has_more_items"].to(bool), hasMore: json["has_more_items"].to(bool),
maxId: json.getOrDefault("max_position").getStr(""),
minId: json.getOrDefault("min_position").getStr(""),
) )
if json.hasKey("max_position"): if json["new_latent_count"].to(int) == 0:
result.maxId = json["max_position"].to(string) return
result.tweets = parseTweets(html)
await getVideos(result.tweets) await getVideos(result.tweets)
proc getTweet*(id: string): Future[Conversation] {.async.} = proc getTweet*(id: string): Future[Conversation] {.async.} =
@ -194,5 +196,8 @@ proc getTweet*(id: string): Future[Conversation] {.async.} =
url = base / tweetUrl / id url = base / tweetUrl / id
html = await fetchHtml(url, headers) html = await fetchHtml(url, headers)
if html.isNil:
return
result = parseConversation(html) result = parseConversation(html)
await getConversationVideos(result) await getConversationVideos(result)

View File

@ -44,7 +44,7 @@ routes:
cond '.' notin @"name" cond '.' notin @"name"
let conversation = await getTweet(@"id") let conversation = await getTweet(@"id")
if conversation.tweet.id.len == 0: if conversation.isNil or conversation.tweet.id.len == 0:
resp Http404, showError("Tweet not found") resp Http404, showError("Tweet not found")
let title = pageTitle(conversation.tweet.profile) let title = pageTitle(conversation.tweet.profile)