Browse Source

Properly parse tweet urls when submitting

master
Wesley Kerfoot 4 years ago
parent
commit
32c93da228
  1. 28
      src/twit2blogpkg/server.nim
  2. 6
      src/twit2blogpkg/templates.nim

28
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"

6
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 =

Loading…
Cancel
Save