From 4ac0e3ee2a83066d78607822c6725723e448dcb1 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 6 Feb 2016 10:18:57 -0800 Subject: [PATCH] submissions, individual tweet viewing --- src/templates/submit.mustache | 10 +++++++ src/templates/tweets.mustache | 2 +- src/twtxt.nim | 32 +++++++++++----------- src/twtxtlist.nim | 51 +++++++++++++++++++++++++++++++++-- src/twtxtlist.nims | 1 + 5 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 src/templates/submit.mustache diff --git a/src/templates/submit.mustache b/src/templates/submit.mustache new file mode 100644 index 0000000..aa2857c --- /dev/null +++ b/src/templates/submit.mustache @@ -0,0 +1,10 @@ +
+
+
username
+
+ +
url
+
+ +
+ diff --git a/src/templates/tweets.mustache b/src/templates/tweets.mustache index c749d9f..b9efda7 100644 --- a/src/templates/tweets.mustache +++ b/src/templates/tweets.mustache @@ -8,7 +8,7 @@
{{ username }} - {{ time }}

{{ tweet }}

- permalink + permalink

{{/ tweets }} diff --git a/src/twtxt.nim b/src/twtxt.nim index 97c696a..9325a06 100644 --- a/src/twtxt.nim +++ b/src/twtxt.nim @@ -52,26 +52,26 @@ proc getTweetsFrom*(url: string, username: string): seq[Tweet] = return res -proc updateTweets*() = +proc updateTweetsOnce*() = var db = open("./data/twtxt.db", nil, nil, nil) - while true: - var users = db.getAllRows sql"select * from users" + var users = db.getAllRows sql"select * from users" - for user in users.items(): - echo "updating " & $user + for user in users.items(): + echo "updating " & $user - let - username = user[1] - url = user[2] - tweets = getTweetsFrom(url, username) + let + username = user[1] + url = user[2] + tweets = getTweetsFrom(url, username) - for tweet in tweets.items(): - try: - db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds(), tweet.message) - except: discard - - sleep 300_000 # 5 minutes + for tweet in tweets.items(): + try: + db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message) + except: discard when isMainModule: - updateTweets() + while true: + updateTweetsOnce() + + sleep 300_000 # 5 minutes diff --git a/src/twtxtlist.nim b/src/twtxtlist.nim index c1af7b6..a721f09 100644 --- a/src/twtxtlist.nim +++ b/src/twtxtlist.nim @@ -1,4 +1,4 @@ -import asyncdispatch, db_sqlite, jester, moustachu, os, shorturl, +import asyncdispatch, db_sqlite, httpclient, jester, moustachu, os, shorturl, strutils, tables, times, twtxt, typetraits const @@ -7,6 +7,7 @@ const indexTemplate*: string = staticRead "./templates/index.mustache" usersTemplate*: string = staticRead "./templates/users.mustache" tweetsTemplate*: string = staticRead "./templates/tweets.mustache" + submitTemplate*: string = staticRead "./templates/submit.mustache" let db = open("data/twtxt.db", nil, nil, nil) @@ -74,9 +75,27 @@ routes: get "/timeline": redirect "/timeline/0" + get "/submit": + renderMustache "submit", submitTemplate, newContext() + + post "/submit": + try: + var + username = $(request.formData.mget "username").body + url = $(request.formData.mget "url").body + + discard url.getTweetsFrom(username) + + db.exec(sql"insert into users values (null, ?, ?)", username, url) + + updateTweetsOnce() + + redirect "/users/" & username & "/0" + except: fail() + get "/users": var - users = db.getAllRows(sql"select username, url from users") + users = db.getAllRows(sql"select username, url from users order by username") ctx = newContext() userList = newSeq[Context]() @@ -169,4 +188,32 @@ routes: except: fail() + get "/tweet/@tid": + try: + var + tweets = db.getAllRows(sql"select * from tweets where id=? limit 1", (@"tid").decodeURLSimple()) + ctx = newContext() + tweetList = newSeq[Context]() + + if tweets.len == 0: + raise newException(ValueError, "no such tweet") + + for tweet in tweets.items(): + var c = newContext() + + let time = tweet[2].split(".")[0].parseInt() + + c["id"] = tweet[0] + c["permalink"] = tweet[0].parseInt().encodeURLSimple() + c["username"] = tweet[1] + c["time"] = time + c["tweet"] = tweet[3] + + tweetList.add c + + ctx["tweets"] = tweetList + + renderMustache("tweet " & @"tid", tweetsTemplate, ctx) + except: fail() + runForever() diff --git a/src/twtxtlist.nims b/src/twtxtlist.nims index e69de29..fc86e83 100644 --- a/src/twtxtlist.nims +++ b/src/twtxtlist.nims @@ -0,0 +1 @@ +--define:ssl