Browse Source

add createdAt field to threads

master
Wesley Kerfoot 5 years ago
parent
commit
a03d8dcd51
  1. 21
      src/tweetlogpkg/server.nim
  2. 4
      src/tweetlogpkg/templates.nim

21
src/tweetlogpkg/server.nim

@ -1,4 +1,4 @@
import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, json, strformat, uri, strscans import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, json, strformat, uri, strscans, times
import twitter import twitter
import templates import templates
import jester import jester
@ -15,6 +15,9 @@ type TwitterThread = ref object of RootObj
tweetID: string tweetID: string
tweets: string tweets: string
author: Author author: Author
createdAt: DateTime
const dateFmt = "YYYY-MM-dd hh:mm:ss"
proc parseTweetUrl(url : string) : Option[ThreadRequest] = proc parseTweetUrl(url : string) : Option[ThreadRequest] =
let path = url.parseUri.path let path = url.parseUri.path
@ -44,6 +47,7 @@ proc createTweetTables() =
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
tid TEXT, tid TEXT,
tweets TEXT, tweets TEXT,
collectedAt TEXT,
authorID INTEGER authorID INTEGER
)""") )""")
@ -78,11 +82,14 @@ proc threadExists(threadID : string, authorName : string) : Option[TwitterThread
if row.all(col => col == ""): if row.all(col => col == ""):
return none(TwitterThread) return none(TwitterThread)
let f = initTimeFormat("yyyy-MM-dd")
some( some(
TwitterThread( TwitterThread(
tweetID: row[1], tweetID: row[1],
author: author.get, author: author.get,
tweets: row[2] tweets: row[2],
createdAt: row[3].parse(dateFmt)
) )
) )
@ -107,9 +114,10 @@ proc insertThread(thread : TwitterThread) =
if not author.isSome: if not author.isSome:
return return
db.exec(sql"INSERT INTO threads (tid, tweets, authorID) VALUES (?, ?, ?)", db.exec(sql"INSERT INTO threads (tid, tweets, collectedAt, authorID) VALUES (?, ?, ?, ?)",
thread.tweetID, thread.tweetID,
thread.tweets, thread.tweets,
thread.createdAt.format(dateFmt),
author.get.authorID) author.get.authorID)
# Routes # Routes
@ -141,7 +149,9 @@ router twitblog:
if thread.isSome: if thread.isSome:
# Lists all the tweets in a thread # Lists all the tweets in a thread
let tweets = thread.get.tweets.split("\n") let tweets = thread.get.tweets.split("\n")
resp tweetThread(author, thread.get.tweets.split("\n")) resp tweetThread(author,
thread.get.tweets.split("\n"),
thread.get.createdAt.format(dateFmt))
else: else:
# Send it off to the rendering thread for processing # Send it off to the rendering thread for processing
# Let them know to check back later # Let them know to check back later
@ -184,6 +194,7 @@ proc handleRenders* =
TwitterThread( TwitterThread(
tweetID: t.tweetID, tweetID: t.tweetID,
author: t.author, author: t.author,
tweets: tweets.get.join("\n") tweets: tweets.get.join("\n"),
createdAt: now().utc
) )
) )

4
src/tweetlogpkg/templates.nim

@ -13,7 +13,8 @@ proc layout(inner : VNode, title : string) : string =
"<!DOCTYPE html>\n" & $vnode "<!DOCTYPE html>\n" & $vnode
proc tweetThread*(author : string, proc tweetThread*(author : string,
tweets : seq[string]): string = tweets : seq[string],
createdAt : string): string =
let title = fmt"Thread by {author}" let title = fmt"Thread by {author}"
let vnode = buildHtml(tdiv(class="")): let vnode = buildHtml(tdiv(class="")):
@ -21,6 +22,7 @@ proc tweetThread*(author : string,
ul: ul:
li: a(href="/"): text "Main Page" li: a(href="/"): text "Main Page"
li: a(href=fmt"/author/{author}/threads"): text (fmt"See all of {author}'s threads") li: a(href=fmt"/author/{author}/threads"): text (fmt"See all of {author}'s threads")
h3: text createdAt
ul(class="m-auto max-w-md list-decimal text-left"): ul(class="m-auto max-w-md list-decimal text-left"):
for tweet in tweets: for tweet in tweets:
li: text tweet li: text tweet

Loading…
Cancel
Save