comparison src/save_account.js.luan @ 0:8f4df159f06b

start public repo
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Jul 2025 20:57:49 -0600
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8f4df159f06b
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local String = require "luan:String.luan"
4 local to_lower = String.lower or error()
5 local trim = String.trim or error()
6 local Parsers = require "luan:Parsers.luan"
7 local json_string = Parsers.json_string or error()
8 local Io = require "luan:Io.luan"
9 local Http = require "luan:http/Http.luan"
10 local Shared = require "site:/lib/Shared.luan"
11 local js_error = Shared.js_error or error()
12 local User = require "site:/lib/User.luan"
13 local name_regex = User.name_regex
14 local Db = require "site:/lib/Db.luan"
15 local run_in_transaction = Db.run_in_transaction or error()
16
17
18 return function()
19 Io.stdout = Http.response.text_writer()
20 local title = Http.request.parameters.title or error()
21 local bio = Http.request.parameters.bio or error()
22 local username = Http.request.parameters.username or error()
23 local password = Http.request.parameters.password or error()
24 local pic_uuid = Http.request.parameters.pic_uuid or error()
25 local pic_filename = Http.request.parameters.pic_filename or error()
26
27 if not name_regex.matches(username) then
28 js_error("username",[[Usernames may only contain letters, numbers, underscores ("_") and hyphens ("-")]])
29 return
30 end
31 local pic_changed = false
32 local user = User.current() or error()
33 local err_fld, err_msg = run_in_transaction( function()
34 user = user.reload()
35 if to_lower(user.name) ~= to_lower(username) and User.get_by_name(username) ~= nil then
36 return "username", "This username is already taken"
37 end
38 if title=="" or title==username or title==user.name then
39 title = nil
40 end
41 user.title = title
42 bio = trim(bio)
43 if bio == "" then
44 bio = nil
45 end
46 user.bio = bio
47 user.name = username
48 user.password = password
49 if pic_uuid == "" then
50 -- no change
51 elseif pic_uuid == "remove" then
52 user.pic_uuid = nil
53 user.pic_filename = nil
54 pic_changed = true
55 else
56 user.pic_uuid = pic_uuid
57 user.pic_filename = pic_filename
58 pic_changed = true
59 end
60 user.save()
61 user.login()
62 end )
63 if err_fld ~= nil then
64 js_error(err_fld,err_msg)
65 return
66 end
67 js_error( "success", "Your information has been updated successfully." )
68 if pic_changed then
69 local url = user.get_pic_url() or "/images/user.png"
70 %>
71 let img = document.querySelector('div[header] > span[right] img');
72 if(img)
73 img.src = <%=json_string(url)%>;
74 <%
75 end
76 end