annotate src/change_name.html.luan @ 59:02d8876dc41d

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