show user and global timeline
This commit is contained in:
parent
829371ccbd
commit
55ca42fd9b
|
@ -9,5 +9,8 @@
|
|||
<a href="/submit">submit</a>
|
||||
</div>
|
||||
<h2>{{ title }}</h2>
|
||||
|
||||
<br />
|
||||
|
||||
{{{ body }}}
|
||||
</div>
|
||||
|
|
|
@ -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 }}
|
|
@ -1,5 +1,5 @@
|
|||
<ul>
|
||||
{{# 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 }}
|
||||
</ul>
|
||||
|
|
|
@ -6,6 +6,7 @@ const
|
|||
errorTemplate*: string = staticRead "./templates/error.mustache"
|
||||
indexTemplate*: string = staticRead "./templates/index.mustache"
|
||||
usersTemplate*: string = staticRead "./templates/users.mustache"
|
||||
tweetsTemplate*: string = staticRead "./templates/tweets.mustache"
|
||||
|
||||
let
|
||||
db = open("data/twtxt.db", nil, nil, nil)
|
||||
|
@ -49,6 +50,12 @@ template fail*(): expr =
|
|||
|
||||
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:
|
||||
port = getEnv("PORT").parseInt().Port
|
||||
bindAddr = "0.0.0.0"
|
||||
|
@ -57,6 +64,16 @@ routes:
|
|||
get "/":
|
||||
renderMustache("home", indexTemplate, newContext())
|
||||
|
||||
get "/content/system":
|
||||
let myHeaders = {
|
||||
"Content-Type": "text/plain",
|
||||
}
|
||||
|
||||
resp Http200, myHeaders, "hi"
|
||||
|
||||
get "/timeline":
|
||||
redirect "/timeline/0"
|
||||
|
||||
get "/users":
|
||||
var
|
||||
users = db.getAllRows(sql"select username, url from users")
|
||||
|
@ -74,4 +91,82 @@ routes:
|
|||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue