diff --git a/src/twit2blogpkg/server.nim b/src/twit2blogpkg/server.nim index 24b111f..16a8e60 100644 --- a/src/twit2blogpkg/server.nim +++ b/src/twit2blogpkg/server.nim @@ -1,4 +1,4 @@ -import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite +import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, strformat import twitter import xander @@ -43,6 +43,14 @@ proc threadExists(threadID : string, author : string) : Option[TwitterThread] = tweets: row[3]) ) +iterator allAuthors() : string = + for author in db.getAllRows(sql"SELECT DISTINCT author FROM threads"): + yield author[0] + +iterator threadIDs(author : string) : string = + for threadID in db.getAllRows(sql"SELECT tid from threads WHERE author=?", author): + yield threadID[0] + proc insertThread(thread : TwitterThread) = db.exec(sql"INSERT INTO threads (tid, author, tweets) VALUES (?, ?, ?)", thread.tweetID, @@ -52,17 +60,29 @@ proc insertThread(thread : TwitterThread) = get "/thread/:author/status/:tweetID": let tweetID = data{"tweetID"}.getStr() let author = data{"author"}.getStr() - let thread = threadExists(tweetID, author) if thread.isSome: - respond thread.get.tweets + data["title"] = fmt"Thread by {author}" + data["tweets"] = thread.get.tweets.split("\n") + respond tmplt("thread", data) else: - chan.send( - ThreadRequest(tweetID: data{"tweetID"}.getStr(), - author: data{"author"}.getStr()) - ) - respond "Hang on, we're grabbing your thread :) Come back to this page later." + chan.send(ThreadRequest(tweetID: tweetID, author: author)) + data["title"] = "Check back later" + respond tmplt("nonexistent", data) + +get "/": + # lists all authors + data["authors"] = allAuthors.toSeq + data["title"] = "Authors" + respond tmplt("authors", data) + +get "/author/:author/threads": + let author = data{"author"}.getStr() + data["author"] = author + data["title"] = fmt"Threads for {author}" + data["threads"] = toSeq(threadIDs(author)) + respond tmplt("threads", data) proc startServer* = createTweetTable() diff --git a/templates/authors.html b/templates/authors.html new file mode 100644 index 0000000..7a6adbf --- /dev/null +++ b/templates/authors.html @@ -0,0 +1,8 @@ +

+ {[ title ]} +

+ diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..eec1c9e --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,10 @@ + + + + {[ title ]} + + +

Main Page

+ {[ content ]} + + diff --git a/templates/nonexistent.html b/templates/nonexistent.html new file mode 100644 index 0000000..2ed360f --- /dev/null +++ b/templates/nonexistent.html @@ -0,0 +1,3 @@ +

+ Check back later +

diff --git a/templates/thread.html b/templates/thread.html new file mode 100644 index 0000000..1ee7a2e --- /dev/null +++ b/templates/thread.html @@ -0,0 +1,9 @@ +See all of {[ author ]}'s threads +

+ {[ title ]} +

+ diff --git a/templates/threads.html b/templates/threads.html new file mode 100644 index 0000000..067825d --- /dev/null +++ b/templates/threads.html @@ -0,0 +1,8 @@ +

+ {[ title ]} +

+