Mercurial Hosting > freedit
comparison src/change_name.html.luan @ 56:7ce54f6d93f2
add change name
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 28 Nov 2022 22:00:43 -0700 |
parents | src/set_name.html.luan@260abd8f8565 |
children | 169ac5fdb320 |
comparison
equal
deleted
inserted
replaced
55:c57b84f461ae | 56:7ce54f6d93f2 |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 local String = require "luan:String.luan" | |
4 local trim = String.trim or error() | |
5 local Io = require "luan:Io.luan" | |
6 local Http = require "luan:http/Http.luan" | |
7 local Shared = require "site:/lib/Shared.luan" | |
8 local head = Shared.head or error() | |
9 local header = Shared.header or error() | |
10 local footer = Shared.footer or error() | |
11 local Forum = require "site:/lib/Forum.luan" | |
12 local forum_title = Forum.title or error() | |
13 local User = require "site:/lib/User.luan" | |
14 local name_regex = User.name_regex | |
15 local Db = require "site:/lib/Db.luan" | |
16 local run_in_transaction = Db.run_in_transaction or error() | |
17 | |
18 | |
19 local function page(contents) | |
20 Io.stdout = Http.response.text_writer() | |
21 %> | |
22 <!doctype html> | |
23 <html> | |
24 <head> | |
25 <% head() %> | |
26 <title><%=forum_title%> - Change Name</title> | |
27 </head> | |
28 <body> | |
29 <% header() %> | |
30 <div content> | |
31 <h1>Change Name</h1> | |
32 <% | |
33 contents() | |
34 %> | |
35 </div> | |
36 <% footer() %> | |
37 </body> | |
38 </html> | |
39 <% | |
40 end | |
41 | |
42 return function() | |
43 local user = User.current_required() | |
44 local name = user.name or error() | |
45 local error_message = nil | |
46 if Http.request.method == "POST" then | |
47 name = Http.request.parameters.name or error() | |
48 name = trim(name) | |
49 name_regex.matches(name) or error "invalid name" | |
50 run_in_transaction( function() | |
51 user = user.reload() | |
52 if user.name ~= name and User.get_by_name(name) ~= nil then | |
53 error_message = "Name already in use" | |
54 else | |
55 user.name = name | |
56 user.save() | |
57 end | |
58 end ) | |
59 if error_message == nil then | |
60 page(function() | |
61 %> | |
62 <p>Your name has been changed.</p> | |
63 <% | |
64 end) | |
65 return | |
66 end | |
67 end | |
68 page(function() | |
69 if error_message ~= nil then %> | |
70 <p error>Error: <%= error_message %></p> | |
71 <% end %> | |
72 <form method=post> | |
73 <label>User name for <%= user.email %></label> | |
74 <input type="text" name="name" value="<%= name or "" %>" autofocus required pattern="[a-zA-Z0-9_-]+"> | |
75 <input type="submit"> | |
76 </form> | |
77 <% | |
78 end) | |
79 end |