1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # View and controller of Tnitter
26 var user
: nullable String = null
33 var db_path
= "tnitter.db"
34 var db
= new DB.open
(db_path
)
36 # Header on pages served by this `Action`
38 # Keywords to `Text::replace`:
39 # * `%app_path%` is the main URL to reach this `Action`
40 # * `%nav_right%` is the pulled right part of the header, used for login form
42 <nav class="navbar navbar-default" role="navigation">
43 <div class="container-fluid">
44 <div class="navbar-header">
45 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
46 <span class="sr-only">Toggle navigation</span>
47 <span class="icon-bar"></span>
48 <span class="icon-bar"></span>
49 <span class="icon-bar"></span>
51 <a class="navbar-brand" href="%app_path%">Tnitter</a>
54 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
55 <ul class="nav navbar-nav">
56 <li><a href="https://github.com/privat/nit/">Nit repository</a></li>
59 <ul class="nav navbar-nav pull-right">
66 # Template of the pages served by this `Action`
68 # Keywords to `Text::replace`:
69 # * The `%header%`, first thing in the `<body>`
70 # * The main page `%content%` within a `<div class="container">`
75 <title>Tnitter</title>
76 <meta charset="utf-8">
77 <meta http-equiv="X-UA-Compatible" content="IE=edge">
78 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
80 <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
81 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
82 <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
88 <div class="container">
94 redef fun answer
(request
, turi
)
96 # Get existing session
97 var session
= request
.session
99 # Error to display on page as a dismissable panel
103 if turi
== "/login" and request
.post_args
.keys
.has
("user") and
104 request
.post_args
.keys
.has
("pass") then
106 var user
= request
.post_args
["user"].trim
107 var pass
= request
.post_args
["pass"]
109 var original_user
= db
.check_login
(user
, pass
)
110 if original_user
!= null then
112 if session
== null then session
= new Session
113 session
.user
= original_user
115 # Check for basic requirements
116 if user
.is_empty
then
117 error
= "Username must have at least 1 character"
118 else if user
.chars
.has
(' ') or user
.chars
.has
('\n') then
119 error
= "Username cannot contain white spaces"
120 else if db
.sign_up
(user
, pass
) then
122 if session
== null then session
= new Session
126 error
= "Invalid combination of username and password"
130 else if turi
== "/logout" then
133 else if turi
== "/post" and request
.post_args
.keys
.has
("text") and session
!= null then
134 var user
= session
.user
137 var text
= request
.post_args
["text"]
140 # Redirect the user to avoid double posting
141 var response
= new HttpResponse(303)
142 response
.header
["Location"] = request
.uri
149 if session
== null or session
.user
== null then
150 # Log in form in the navbar
153 <form class="navbar-form" role="form" action="login" method="POST">
154 <div class="form-group">
155 <input type="text" placeholder="Username" class="form-control" name="user">
157 <div class="form-group">
158 <input type="password" placeholder="Password" class="form-control" name="pass">
160 <button type="submit" class="btn btn-default">Log in (or sign up)</button>
165 # Cannot post when not logged in
168 # Log out form in the navbar
170 <li><p class="navbar-text">Signed in as @{{{session.user.html_escape}}}</p></li>
172 <form class="navbar-form" role="form" action="logout" method="POST">
173 <button type="submit" class="btn btn-default">Log out</button>
180 <form class="form" role="form" action="post" method="POST">
181 <div class="form-group">
182 <div class="input-group">
183 <div class="input-group-addon">Share your thoughts</div>
184 <input class="form-control" type="text" placeholder="..." name="text">
185 <span class="input-group-btn">
186 <button class="btn btn-default" type="submit">Tnit!</button>
188 </div><!-- /input-group -->
196 if error
!= null then
198 <div class="alert alert-danger alert-dismissible" role="alert">
199 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
205 # Load the last 16 Tnits
206 var posts
= db
.latest_posts
(16)
208 var html_posts
= new Array[String]
210 html_posts
.add
"<tr><td>@{post.user.html_escape}</td><td>{post.text.html_escape}</td></tr>"
214 <div class="panel panel-default">
215 <div class="panel-heading">Latest Tnits</div>
216 <table class="table table-striped">
217 {{{html_posts.join("\n")}}}
223 # Get page from template, we replace the header first so we can replace
224 # everything on the same body afterwards
226 replace
("%header%", header
).
227 replace
("%app_path%", request
.uri
.strip_extension
(turi
) + "/").
228 replace
("%header_right%", login_or_out
).
229 replace
("%content%", error_html
+ content
)
232 var response
= new HttpResponse(200)
234 response
.session
= session