From 32c93da2281b0378011c700aea10c94f56a5d749 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 2 Feb 2020 15:01:26 -0500 Subject: [PATCH] Properly parse tweet urls when submitting --- src/twit2blogpkg/server.nim | 28 ++++++++++++++++++---------- src/twit2blogpkg/templates.nim | 6 ++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/twit2blogpkg/server.nim b/src/twit2blogpkg/server.nim index a8bfed4..f4dc6aa 100644 --- a/src/twit2blogpkg/server.nim +++ b/src/twit2blogpkg/server.nim @@ -1,4 +1,4 @@ -import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, json +import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, json, strformat, uri, strscans import twitter import templates import jester @@ -12,6 +12,15 @@ type TwitterThread = ref object of RootObj author: string tweets: string +proc parseTweetUrl(url : string) : Option[ThreadRequest] = + let path = url.parseUri.path + var author : string + var tweetID : int + if scanf(path, "/$w/status/$i", author, tweetID): + some(ThreadRequest(tweetID : $tweetID, author: author)) + else: + none(ThreadRequest) + var chan : Channel[ThreadRequest] # Max 20 items processing @@ -60,17 +69,16 @@ router twitblog: resp authors.mainPage post "/thread": - let params = request.formData - if not ("tweetID" in params and "author" in params): - resp "nope" - - let tweetID = params["tweetID"].body - let author = params["author"].body + let params = request.params + if not ("tweetURL" in params): + resp "Invalid" - let thread = threadExists(tweetID, author) + let threadURL = params["tweetURL"].parseTweetUrl - chan.send(ThreadRequest(tweetID: tweetID, author: author)) - resp checkBack() + if threadURL.isSome: + redirect (fmt"/thread/{threadURL.get.author}/status/{threadURL.get.tweetID}") + else: + resp "Invalid" get "/thread/@author/status/@tweetID": let tweetID = @"tweetID" diff --git a/src/twit2blogpkg/templates.nim b/src/twit2blogpkg/templates.nim index ce49f2c..630c2ff 100644 --- a/src/twit2blogpkg/templates.nim +++ b/src/twit2blogpkg/templates.nim @@ -31,7 +31,6 @@ proc listThreads*(author : string, li: a(href = fmt"/thread/{author}/status/{thread}"): text thread result = $vnode - ## Main page proc listAuthors*(authors : seq[string]) : VNode = let title = "Authors" @@ -45,7 +44,10 @@ proc listAuthors*(authors : seq[string]) : VNode = proc submitThread() : VNode = let vnode = buildHtml(tdiv): form(action = "/thread", `method`="POST", class="submit-thread"): - text "blah" + tdiv: + label(`for`="tweetUrl"): + text "Tweet URL" + input(`type`="text", name="tweetURL", id="tweeturl", required="true") result = vnode proc mainPage*(authors : seq[string]) : string =