ad41a71c25e16a69660a62f9d33af19453356dc2
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
21 import json
::serialization
26 # Path to the Sqlite3 database
27 fun tnitter_db_path
: String do return "tnitter.db"
31 var user
: nullable String = null
38 # Header on pages served by this `Action`
40 # Keywords to `Text::replace`:
41 # * `%app_path%` is the main URL to reach this `Action`
42 # * `%nav_right%` is the pulled right part of the header, used for login form
44 <nav class="navbar navbar-default" role="navigation">
45 <div class="container-fluid">
46 <div class="navbar-header">
47 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
48 <span class="sr-only">Toggle navigation</span>
49 <span class="icon-bar"></span>
50 <span class="icon-bar"></span>
51 <span class="icon-bar"></span>
53 <a class="navbar-brand" href="%app_path%">Tnitter</a>
56 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
57 <ul class="nav navbar-nav">
58 <li><a href="https://github.com/nitlang/nit/">Nit repository</a></li>
61 <ul class="nav navbar-nav pull-right">
68 # Template of the pages served by this `Action`
70 # Keywords to `Text::replace`:
71 # * The `%header%`, first thing in the `<body>`
72 # * The main page `%content%` within a `<div class="container">`
77 <title>Tnitter</title>
78 <meta charset="utf-8">
79 <meta http-equiv="X-UA-Compatible" content="IE=edge">
80 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
82 <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
83 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
84 <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
86 {{{javascript_header or else ""}}}
93 <div class="container">
99 # Custom JavaScript code added within a `<script>` block to each page
100 var javascript_header
: nullable Writable = null is writable
102 redef fun answer
(request
, turi
)
104 # Get existing session
105 var session
= request
.session
107 # Error to display on page as a dismissable panel
110 var db
= new DB.open
(tnitter_db_path
)
113 if turi
== "/login" and request
.post_args
.keys
.has
("user") and
114 request
.post_args
.keys
.has
("pass") then
116 var user
= request
.post_args
["user"].trim
117 var pass
= request
.post_args
["pass"]
119 var original_user
= db
.check_login
(user
, pass
)
120 if original_user
!= null then
122 if session
== null then session
= new Session
123 session
.user
= original_user
125 # Check for basic requirements
126 if user
.is_empty
then
127 error
= "Username must have at least 1 character"
128 else if user
.chars
.has
(' ') or user
.chars
.has
('\n') then
129 error
= "Username cannot contain white spaces"
130 else if db
.sign_up
(user
, pass
) then
132 if session
== null then session
= new Session
136 error
= "Invalid combination of username and password"
140 else if turi
== "/logout" then
143 else if turi
== "/post" and request
.post_args
.keys
.has
("text") and session
!= null then
144 var user
= session
.user
145 var text
= request
.post_args
["text"].trim
146 if user
!= null and not text
.is_empty
then
151 # Redirect the user to avoid double posting
152 var response
= new HttpResponse(303)
153 response
.header
["Location"] = request
.uri
154 response
.session
= session
161 if session
== null or session
.user
== null then
162 # Log in form in the navbar
165 <form class="navbar-form" role="form" action="login" method="POST">
166 <div class="form-group">
167 <input type="text" placeholder="Username" class="form-control" name="user">
169 <div class="form-group">
170 <input type="password" placeholder="Password" class="form-control" name="pass">
172 <button type="submit" class="btn btn-default">Log in (or sign up)</button>
177 # Cannot post when not logged in
180 # Log out form in the navbar
182 <li><p class="navbar-text">Signed in as @{{{session.user.html_escape}}}</p></li>
184 <form class="navbar-form" role="form" action="logout" method="POST">
185 <button type="submit" class="btn btn-default">Log out</button>
192 <form class="form" role="form" action="post" method="POST">
193 <div class="form-group">
194 <div class="input-group">
195 <div class="input-group-addon">Share your thoughts</div>
196 <input class="form-control" type="text" placeholder="..." name="text">
197 <span class="input-group-btn">
198 <button class="btn btn-default" type="submit">Tnit!</button>
200 </div><!-- /input-group -->
208 if error
!= null then
210 <div class="alert alert-danger alert-dismissible" role="alert">
211 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
217 # Load the last 16 Tnits
218 var posts
= db
.list_posts
(0, 16)
221 var html_posts
= new Array[String]
223 html_posts
.add
"<tr><td>@{post.user.html_escape}</td><td>{post.text.html_escape}</td></tr>"
227 <div class="panel panel-default">
228 <div class="panel-heading">Latest Tnits</div>
229 <table class="table table-striped">
230 {{{html_posts.join("\n")}}}
236 # Get page from template, we replace the header first so we can replace
237 # everything on the same body afterwards
239 replace
("%header%", header
).
240 replace
("%app_path%", request
.uri
.strip_extension
(turi
) + "/").
241 replace
("%header_right%", login_or_out
).
242 replace
("%content%", error_html
+ content
)
245 var response
= new HttpResponse(200)
247 response
.session
= session
252 # Tnitter RESTful interface
256 redef fun answer
(request
, turi
)
258 if turi
== "/list" then
259 # list?from=1&count=2 -> Error | Array[Post]
261 var from
= request
.int_arg
("from") or else 0
262 var count
= request
.int_arg
("count") or else 8
264 var db
= new DB.open
(tnitter_db_path
)
265 var posts
= db
.list_posts
(from
, count
)
268 var response
= new HttpResponse(200)
269 response
.body
= posts
.to_json_string
273 # Format not recognized
274 var error
= new Error("Bad Request")
275 var response
= new HttpResponse(400)
276 response
.body
= error
.to_json_string