From 753e946817b69bdddf1995a004997b1efc1167ee Mon Sep 17 00:00:00 2001 From: dom96 Date: Wed, 16 May 2012 16:38:23 +0100 Subject: [PATCH] The time a user was last online is now tracked and the main page displays the "online" users as well as some other stats. --- editdb.nim | 11 +++++++++++ forms.tmpl | 31 ++++++++++++++++++++++++++++++- forum.nim | 42 ++++++++++++++++++++++++++++++++++++++---- main.tmpl | 11 +++++++++-- public/css/style.css | 39 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 editdb.nim diff --git a/editdb.nim b/editdb.nim new file mode 100644 index 0000000..ea28f93 --- /dev/null +++ b/editdb.nim @@ -0,0 +1,11 @@ + +import strutils, db_sqlite + +var db = Open(connection="nimforum.db", user="postgres", password="", + database="nimforum") + +db.exec(sql"""ALTER TABLE person add column + lastOnline timestamp +""", []) + +close(db) \ No newline at end of file diff --git a/forms.tmpl b/forms.tmpl index c233e62..4eaab58 100644 --- a/forms.tmpl +++ b/forms.tmpl @@ -186,4 +186,33 @@ # else: You're already logged in! # end if -#end proc \ No newline at end of file +#end proc +# +# +#proc genListOnline(c: var TForumData): string = +# result = "" +# let stats = c.getStats() +
+
+ Who is online? +
+
+ Out of ${stats.totalUsers} users ${stats.activeUsers.len} are online: + #for index, usr in stats.activeUsers: + # if usr.isAdmin: + #if index != 0: result.add ',' + #end if + ${usr.nick} + # else: + #if index != 0: result.add ',' + #end if + ${usr.nick} + # end if + #end for + +
+ Total threads: ${stats.totalThreads} | Total posts: ${stats.totalPosts} | Newest member: ${stats.newestMember.nick} +
+
+ +#end proc diff --git a/forum.nim b/forum.nim index 2e3ef94..22f0255 100644 --- a/forum.nim +++ b/forum.nim @@ -37,6 +37,13 @@ type TStyledButton = tuple[text: string, link: string] + TForumStats = object + totalUsers: int + totalPosts: int + totalThreads: int + newestMember: tuple[nick: string, id: int, isAdmin: bool] + activeUsers: seq[tuple[nick: string, id: int, isAdmin: bool]] + var db: TDbConn docConfig: PStringTable @@ -211,8 +218,8 @@ proc register(c: var TForumData, name, pass, antibot, email: string): bool = # perform registration: var salt = makeSalt() - Exec(db, sql("INSERT INTO person(name, password, email, salt, status) " & - "VALUES (?, ?, ?, ?, 'user')"), name, + Exec(db, sql("INSERT INTO person(name, password, email, salt, status, lastOnline) " & + "VALUES (?, ?, ?, ?, 'user', DATETIME('now'))"), name, makePassword(pass, salt), email, salt) # return setError(c, "", "Could not create your account!") return true @@ -234,6 +241,10 @@ proc checkLoggedIn(c: var TForumData) = c.username = ||row[0] c.email = ||row[1] c.isAdmin = parseBool(||row[2]) + # Update lastOnline + db.exec(sql"update person set lastOnline = DATETIME('now') where id = ?", + c.userid) + else: echo("SID not found in sessions. Assuming logged out.") @@ -405,6 +416,29 @@ proc genActionMenu(c: var TForumData): string = btns.add(("New Thread", c.req.makeUri("/newthread", false))) result = c.genButtons(btns) +proc getStats(c: var TForumData): TForumStats = + const totalUsersQuery = + sql"select count(*) from person" + result.totalUsers = getValue(db, totalUsersQuery).parseInt + const totalPostsQuery = + sql"select count(*) from post" + result.totalPosts = getValue(db, totalPostsQuery).parseInt + const totalThreadsQuery = + sql"select count(*) from thread" + result.totalThreads = getValue(db, totalThreadsQuery).parseInt + + var newestMemberCreation = 0 + result.activeUsers = @[] + const getUsersQuery = + sql"select id, name, admin, strftime('%s', lastOnline), strftime('%s', creation) from person" + for row in fastRows(db, getUsersQuery): + let lastOnlineSeconds = getTime() - TTime(row[3].parseInt) + if lastOnlineSeconds < (60 * 5): # 5 minutes + result.activeUsers.add((row[1], row[0].parseInt, row[2].parseBool)) + if row[4].parseInt > newestMemberCreation: + result.newestMember = (row[1], row[0].parseInt, row[2].parseBool) + newestMemberCreation = row[4].parseInt + include "forms.tmpl" include "main.tmpl" @@ -424,7 +458,7 @@ template createTFD(): stmt = get "/": createTFD() - resp genMain(c, genThreadsList(c)) + resp genMain(c, genThreadsList(c), true) get "/t/@threadid/?": createTFD() @@ -541,6 +575,6 @@ when isMainModule: if paramCount() > 0: if paramStr(1) == "scgi": http = false - run(websiteLoc, port = TPort(9001), http = http) + run(websiteLoc, port = TPort(9000), http = http) db.close() diff --git a/main.tmpl b/main.tmpl index bc4ec04..70a20fc 100644 --- a/main.tmpl +++ b/main.tmpl @@ -1,5 +1,5 @@ #! stdtmpl -#proc genMain(c: var TForumData, content: string): string = +#proc genMain(c: var TForumData, content: string, mainPage = false): string = # result = "" @@ -38,9 +38,16 @@
${c.genActionMenu}
+ + #if mainPage: + ${c.genListOnline} + #end if + diff --git a/public/css/style.css b/public/css/style.css index c252992..888f8f2 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -122,13 +122,50 @@ div#header a:hover, #nimbtn a:hover { width: 15%; } +#whoisonline { + margin: 5pt; + font-size: 9pt; +} + +#whoisonline .wioHeader { + background-color: #5D5D5D; + background: -moz-linear-gradient(top, #5D5D5D, #4D4D4D); + background: -webkit-linear-gradient(top, #5D5D5D, #4d4d4d); + background: -o-linear-gradient(top, #5D5D5D, #4d4d4d); + color: #ffffff; + border-bottom: #2d2d2d solid 1px; + padding: 3px; + padding-left: 5pt; +} + +#whoisonline .content { + border: #9d9d9d solid 1px; + border-top: #2D2D2D solid 1px; + padding: 5pt; +} + +#whoisonline .content hr { + margin-top: 5pt; + margin-bottom: 5pt; +} + #footer { background-color: #5D5D5D; background: -moz-linear-gradient(top, #5D5D5D, #4D4D4D); background: -webkit-linear-gradient(top, #5D5D5D, #4d4d4d); background: -o-linear-gradient(top, #5D5D5D, #4d4d4d); color: #ffffff; - text-align: center; + + padding-left: 5px; + padding-right: 5px; +} + +#footer a:link, #footer a:visited { + color: #ffffff; +} + +#footer a:hover { + text-decoration: none; } #topbar {