diff --git a/src/templates/layout.mustache b/src/templates/layout.mustache index c38fcb6..e7b56a8 100644 --- a/src/templates/layout.mustache +++ b/src/templates/layout.mustache @@ -9,5 +9,8 @@ submit

{{ title }}

+ +
+ {{{ body }}} diff --git a/src/templates/tweets.mustache b/src/templates/tweets.mustache new file mode 100644 index 0000000..c749d9f --- /dev/null +++ b/src/templates/tweets.mustache @@ -0,0 +1,20 @@ +{{# user }} + feed +
+
+{{/ user }} + +{{# tweets }} +
+ {{ username }} - {{ time }} +

{{ tweet }}

+ permalink +
+
+{{/ tweets }} + +{{# paging }} +
+ {{# isnt1}}prev {{/ isnt1 }} this next +
+{{/ paging }} diff --git a/src/templates/users.mustache b/src/templates/users.mustache index 3949d3b..ebaac3e 100644 --- a/src/templates/users.mustache +++ b/src/templates/users.mustache @@ -1,5 +1,5 @@ diff --git a/src/twtxtlist.nim b/src/twtxtlist.nim index 3729d2b..c1af7b6 100644 --- a/src/twtxtlist.nim +++ b/src/twtxtlist.nim @@ -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()