# This file is part of NIT ( http://www.nitlanguage.org ). # # Copyright 2014 Alexis Laferrière # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # View and controller of Tnitter module action import nitcorn import model redef class Session # User logged in var user: nullable String = null end # Main Tnitter Action class Tnitter super Action var db_path = "tnitter.db" var db = new DB.open(db_path) # Header on pages served by this `Action` # # Keywords to `Text::replace`: # * `%app_path%` is the main URL to reach this `Action` # * `%nav_right%` is the pulled right part of the header, used for login form var header = """ """ # Template of the pages served by this `Action` # # Keywords to `Text::replace`: # * The `%header%`, first thing in the `` # * The main page `%content%` within a `
` var template = """ Tnitter %header%
%content%
""" redef fun answer(request, turi) do # Get existing session var session = request.session # Error to display on page as a dismissable panel var error = null # Login/logout if turi == "/login" and request.post_args.keys.has("user") and request.post_args.keys.has("pass") then var user = request.post_args["user"].trim var pass = request.post_args["pass"] var original_user = db.check_login(user, pass) if original_user != null then # Log in successful if session == null then session = new Session session.user = original_user else # Check for basic requirements if user.is_empty then error = "Username must have at least 1 character" else if user.chars.has(' ') or user.chars.has('\n') then error = "Username cannot contain white spaces" else if db.sign_up(user, pass) then # Sign up successful if session == null then session = new Session session.user = user else # Invalid user/pass error = "Invalid combination of username and password" session = null end end else if turi == "/logout" then # Logging out session = null else if turi == "/post" and request.post_args.keys.has("text") and session != null then var user = session.user if user != null then # Post a Tnit! var text = request.post_args["text"] db.post(user, text) # Redirect the user to avoid double posting var response = new HttpResponse(303) response.header["Location"] = request.uri return response end end var login_or_out var content if session == null or session.user == null then # Log in form in the navbar login_or_out = """
  • """ # Cannot post when not logged in content = "" else # Log out form in the navbar login_or_out = """
  • """ # Post form content = """
    Share your thoughts
    """ end # Show error if any var error_html if error != null then error_html = """ """ else error_html = "" # Load the last 16 Tnits var posts = db.latest_posts(16) var html_posts = new Array[String] for post in posts do html_posts.add "@{post.user.html_escape}{post.text.html_escape}" end content += """
    Latest Tnits
    {{{html_posts.join("\n")}}}
    """ # Get page from template, we replace the header first so we can replace # everything on the same body afterwards var body = template. replace("%header%", header). replace("%app_path%", request.uri.strip_extension(turi) + "/"). replace("%header_right%", login_or_out). replace("%content%", error_html + content) # Build response var response = new HttpResponse(200) response.body = body response.session = session return response end end