fdb718b50f338b0ad5757be7c9c633c7a479e474
[nit.git] / contrib / tnitter / src / action.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # View and controller of Tnitter
18 module action
19
20 import nitcorn
21
22 import model
23 import database
24
25 redef class Session
26 # User logged in
27 var user: nullable String = null
28 end
29
30 # Main Tnitter Action
31 class Tnitter
32 super Action
33
34 var db_path = "tnitter.db"
35 var db = new DB.open(db_path)
36
37 # Header on pages served by this `Action`
38 #
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
42 var header = """
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>
51 </button>
52 <a class="navbar-brand" href="%app_path%">Tnitter</a>
53 </div>
54
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>
58 </ul>
59
60 <ul class="nav navbar-nav pull-right">
61 %header_right%
62 </ul>
63 </div>
64 </div>
65 </nav>"""
66
67 # Template of the pages served by this `Action`
68 #
69 # Keywords to `Text::replace`:
70 # * The `%header%`, first thing in the `<body>`
71 # * The main page `%content%` within a `<div class="container">`
72 var template = """
73 <!DOCTYPE html>
74 <html>
75 <head>
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">
80
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>
84 </head>
85 <body>
86
87 %header%
88
89 <div class="container">
90 %content%
91 </div>
92 </body>
93 </html>"""
94
95 redef fun answer(request, turi)
96 do
97 # Get existing session
98 var session = request.session
99
100 # Error to display on page as a dismissable panel
101 var error = null
102
103 # Login/logout
104 if turi == "/login" and request.post_args.keys.has("user") and
105 request.post_args.keys.has("pass") then
106
107 var user = request.post_args["user"].trim
108 var pass = request.post_args["pass"]
109
110 var original_user = db.check_login(user, pass)
111 if original_user != null then
112 # Log in successful
113 if session == null then session = new Session
114 session.user = original_user
115 else
116 # Check for basic requirements
117 if user.is_empty then
118 error = "Username must have at least 1 character"
119 else if user.chars.has(' ') or user.chars.has('\n') then
120 error = "Username cannot contain white spaces"
121 else if db.sign_up(user, pass) then
122 # Sign up successful
123 if session == null then session = new Session
124 session.user = user
125 else
126 # Invalid user/pass
127 error = "Invalid combination of username and password"
128 session = null
129 end
130 end
131 else if turi == "/logout" then
132 # Logging out
133 session = null
134 else if turi == "/post" and request.post_args.keys.has("text") and session != null then
135 var user = session.user
136 if user != null then
137 # Post a Tnit!
138 var text = request.post_args["text"]
139 db.post(user, text)
140
141 # Redirect the user to avoid double posting
142 var response = new HttpResponse(303)
143 response.header["Location"] = request.uri
144 return response
145 end
146 end
147
148 var login_or_out
149 var content
150 if session == null or session.user == null then
151 # Log in form in the navbar
152 login_or_out = """
153 <li>
154 <form class="navbar-form" role="form" action="login" method="POST">
155 <div class="form-group">
156 <input type="text" placeholder="Username" class="form-control" name="user">
157 </div>
158 <div class="form-group">
159 <input type="password" placeholder="Password" class="form-control" name="pass">
160 </div>
161 <button type="submit" class="btn btn-default">Log in (or sign up)</button>
162 </form>
163 </li>
164 """
165
166 # Cannot post when not logged in
167 content = ""
168 else
169 # Log out form in the navbar
170 login_or_out = """
171 <li><p class="navbar-text">Signed in as @{{{session.user.html_escape}}}</p></li>
172 <li>
173 <form class="navbar-form" role="form" action="logout" method="POST">
174 <button type="submit" class="btn btn-default">Log out</button>
175 </form>
176 </li>
177 """
178
179 # Post form
180 content = """
181 <form class="form" role="form" action="post" method="POST">
182 <div class="form-group">
183 <div class="input-group">
184 <div class="input-group-addon">Share your thoughts</div>
185 <input class="form-control" type="text" placeholder="..." name="text">
186 <span class="input-group-btn">
187 <button class="btn btn-default" type="submit">Tnit!</button>
188 </span>
189 </div><!-- /input-group -->
190 </div>
191 </form>
192 """
193 end
194
195 # Show error if any
196 var error_html
197 if error != null then
198 error_html = """
199 <div class="alert alert-danger alert-dismissible" role="alert">
200 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
201 {{{error}}}
202 </div>
203 """
204 else error_html = ""
205
206 # Load the last 16 Tnits
207 var posts = db.latest_posts(16)
208
209 var html_posts = new Array[String]
210 for post in posts do
211 html_posts.add "<tr><td>@{post.user.html_escape}</td><td>{post.text.html_escape}</td></tr>"
212 end
213
214 content += """
215 <div class="panel panel-default">
216 <div class="panel-heading">Latest Tnits</div>
217 <table class="table table-striped">
218 {{{html_posts.join("\n")}}}
219 </table>
220 </div>
221 </div>
222 """
223
224 # Get page from template, we replace the header first so we can replace
225 # everything on the same body afterwards
226 var body = template.
227 replace("%header%", header).
228 replace("%app_path%", request.uri.strip_extension(turi) + "/").
229 replace("%header_right%", login_or_out).
230 replace("%content%", error_html + content)
231
232 # Build response
233 var response = new HttpResponse(200)
234 response.body = body
235 response.session = session
236 return response
237 end
238 end