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
25 # Path to the Sqlite3 database
26 fun tnitter_db_path
: String do return "tnitter.db"
30 var user
: nullable String = null
37 # Header on pages served by this `Action`
39 # Keywords to `Text::replace`:
40 # * `%app_path%` is the main URL to reach this `Action`
41 # * `%nav_right%` is the pulled right part of the header, used for login form
43 <nav class="navbar navbar-default" role="navigation">
44 <div class="container-fluid">
45 <div class="navbar-header">
46 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
47 <span class="sr-only">Toggle navigation</span>
48 <span class="icon-bar"></span>
49 <span class="icon-bar"></span>
50 <span class="icon-bar"></span>
52 <a class="navbar-brand" href="%app_path%">Tnitter</a>
55 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
56 <ul class="nav navbar-nav">
57 <li><a href="https://github.com/nitlang/nit/">Nit repository</a></li>
60 <ul class="nav navbar-nav pull-right">
67 # Template of the pages served by this `Action`
69 # Keywords to `Text::replace`:
70 # * The `%header%`, first thing in the `<body>`
71 # * The main page `%content%` within a `<div class="container">`
76 <title>Tnitter</title>
77 <meta charset="utf-8">
78 <meta http-equiv="X-UA-Compatible" content="IE=edge">
79 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
81 <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
82 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
83 <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
89 <div class="container">
95 redef fun answer
(request
, turi
)
97 # Get existing session
98 var session
= request
.session
100 # Error to display on page as a dismissable panel
103 var db
= new DB.open
(tnitter_db_path
)
106 if turi
== "/login" and request
.post_args
.keys
.has
("user") and
107 request
.post_args
.keys
.has
("pass") then
109 var user
= request
.post_args
["user"].trim
110 var pass
= request
.post_args
["pass"]
112 var original_user
= db
.check_login
(user
, pass
)
113 if original_user
!= null then
115 if session
== null then session
= new Session
116 session
.user
= original_user
118 # Check for basic requirements
119 if user
.is_empty
then
120 error
= "Username must have at least 1 character"
121 else if user
.chars
.has
(' ') or user
.chars
.has
('\n') then
122 error
= "Username cannot contain white spaces"
123 else if db
.sign_up
(user
, pass
) then
125 if session
== null then session
= new Session
129 error
= "Invalid combination of username and password"
133 else if turi
== "/logout" then
136 else if turi
== "/post" and request
.post_args
.keys
.has
("text") and session
!= null then
137 var user
= session
.user
138 var text
= request
.post_args
["text"].trim
139 if user
!= null and not text
.is_empty
then
144 # Redirect the user to avoid double posting
145 var response
= new HttpResponse(303)
146 response
.header
["Location"] = request
.uri
147 response
.session
= session
154 if session
== null or session
.user
== null then
155 # Log in form in the navbar
158 <form class="navbar-form" role="form" action="login" method="POST">
159 <div class="form-group">
160 <input type="text" placeholder="Username" class="form-control" name="user">
162 <div class="form-group">
163 <input type="password" placeholder="Password" class="form-control" name="pass">
165 <button type="submit" class="btn btn-default">Log in (or sign up)</button>
170 # Cannot post when not logged in
173 # Log out form in the navbar
175 <li><p class="navbar-text">Signed in as @{{{session.user.html_escape}}}</p></li>
177 <form class="navbar-form" role="form" action="logout" method="POST">
178 <button type="submit" class="btn btn-default">Log out</button>
185 <form class="form" role="form" action="post" method="POST">
186 <div class="form-group">
187 <div class="input-group">
188 <div class="input-group-addon">Share your thoughts</div>
189 <input class="form-control" type="text" placeholder="..." name="text">
190 <span class="input-group-btn">
191 <button class="btn btn-default" type="submit">Tnit!</button>
193 </div><!-- /input-group -->
201 if error
!= null then
203 <div class="alert alert-danger alert-dismissible" role="alert">
204 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
210 # Load the last 16 Tnits
211 var posts
= db
.list_posts
(0, 16)
214 var html_posts
= new Array[String]
216 html_posts
.add
"<tr><td>@{post.user.html_escape}</td><td>{post.text.html_escape}</td></tr>"
220 <div class="panel panel-default">
221 <div class="panel-heading">Latest Tnits</div>
222 <table class="table table-striped">
223 {{{html_posts.join("\n")}}}
229 # Get page from template, we replace the header first so we can replace
230 # everything on the same body afterwards
232 replace
("%header%", header
).
233 replace
("%app_path%", request
.uri
.strip_extension
(turi
) + "/").
234 replace
("%header_right%", login_or_out
).
235 replace
("%content%", error_html
+ content
)
238 var response
= new HttpResponse(200)
240 response
.session
= session