show user and global timeline

This commit is contained in:
Christine Dodrill 2016-02-06 09:49:14 -08:00
parent 829371ccbd
commit 55ca42fd9b
4 changed files with 119 additions and 1 deletions

View File

@ -9,5 +9,8 @@
<a href="/submit">submit</a> <a href="/submit">submit</a>
</div> </div>
<h2>{{ title }}</h2> <h2>{{ title }}</h2>
<br />
{{{ body }}} {{{ body }}}
</div> </div>

View File

@ -0,0 +1,20 @@
{{# user }}
<a href="{{ url }}">feed</a>
<br />
<br />
{{/ user }}
{{# tweets }}
<div class="entries">
<a href="/users/{{ username }}/0">{{ username }}</a> - {{ time }}
<p>{{ tweet }}</p>
<small><a href="/tweets/{{ permalink }}">permalink</a></small>
</div>
<br />
{{/ tweets }}
{{# paging }}
<center>
{{# isnt1}}<a href="{{ prev }}">prev</a> {{/ isnt1 }} this <a href="{{ next }}">next</a>
</center>
{{/ paging }}

View File

@ -1,5 +1,5 @@
<ul> <ul>
{{# users }} {{# users }}
<li><a href="/users/{{ username }}">{{ username }}</a> <a href="{{ url }}">feed</a></li> <li><a href="/users/{{ username }}/0">{{ username }}</a> <a href="{{ url }}">feed</a></li>
{{/ users }} {{/ users }}
</ul> </ul>

View File

@ -6,6 +6,7 @@ const
errorTemplate*: string = staticRead "./templates/error.mustache" errorTemplate*: string = staticRead "./templates/error.mustache"
indexTemplate*: string = staticRead "./templates/index.mustache" indexTemplate*: string = staticRead "./templates/index.mustache"
usersTemplate*: string = staticRead "./templates/users.mustache" usersTemplate*: string = staticRead "./templates/users.mustache"
tweetsTemplate*: string = staticRead "./templates/tweets.mustache"
let let
db = open("data/twtxt.db", nil, nil, nil) db = open("data/twtxt.db", nil, nil, nil)
@ -49,6 +50,12 @@ template fail*(): expr =
halt render(baseTemplate, layoutCtx) halt render(baseTemplate, layoutCtx)
template pagination(ctx: Context, isnt1: bool, prev, next: string): expr =
ctx["paging"] = true
ctx["isnt1"] = isnt1
ctx["prev"] = prev
ctx["next"] = next
settings: settings:
port = getEnv("PORT").parseInt().Port port = getEnv("PORT").parseInt().Port
bindAddr = "0.0.0.0" bindAddr = "0.0.0.0"
@ -57,6 +64,16 @@ routes:
get "/": get "/":
renderMustache("home", indexTemplate, newContext()) renderMustache("home", indexTemplate, newContext())
get "/content/system":
let myHeaders = {
"Content-Type": "text/plain",
}
resp Http200, myHeaders, "hi"
get "/timeline":
redirect "/timeline/0"
get "/users": get "/users":
var var
users = db.getAllRows(sql"select username, url from users") users = db.getAllRows(sql"select username, url from users")
@ -74,4 +91,82 @@ routes:
renderMustache "users", usersTemplate, ctx renderMustache "users", usersTemplate, ctx
get "/users/@name/@page":
try:
var
tweets = db.getAllRows(sql"select * from tweets where username=? order by time desc limit 20 offset ?", @"name", (@"page").parseInt() * 20)
ctx = newContext()
tweetList = newSeq[Context]()
if len(tweets) == 0:
raise newException(ValueError, "no such page")
var
userctx = newContext()
userurl = db.getValue(sql"select url from users where username=?", @"name")
userctx["url"] = userurl
ctx["user"] = userctx
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
let id = (@"page").parseInt()
if @"page" == "0":
ctx.pagination(false, "", "/users/" & @"name" & "/" & $(id + 1))
else:
ctx.pagination(true, "/users/" & @"name" & "/" & $(id - 1), "/users/" & @"name" & "/" & $(id + 1))
renderMustache(@"name" & " page " & @"page", tweetsTemplate, ctx)
except: fail()
get "/timeline/@page":
try:
var
tweets = db.getAllRows(sql"select * from tweets order by time desc limit 20 offset ?", (@"page").parseInt() * 20)
ctx = newContext()
tweetList = newSeq[Context]()
if tweets.len == 0:
raise newException(ValueError, "no such page")
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
let id = (@"page").parseInt()
if @"page" == "0":
ctx.pagination(false, "", "/timeline/" & $(id + 1))
else:
ctx.pagination(true, "/timeline/" & $(id - 1), "/timeline/" & $(id + 1))
renderMustache("timeline page " & @"page", tweetsTemplate, ctx)
except: fail()
runForever() runForever()