diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/save_account.js.luan	Fri Jul 11 20:57:49 2025 -0600
@@ -0,0 +1,76 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local String = require "luan:String.luan"
+local to_lower = String.lower or error()
+local trim = String.trim or error()
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Shared = require "site:/lib/Shared.luan"
+local js_error = Shared.js_error or error()
+local User = require "site:/lib/User.luan"
+local name_regex = User.name_regex
+local Db = require "site:/lib/Db.luan"
+local run_in_transaction = Db.run_in_transaction or error()
+
+
+return function()
+	Io.stdout = Http.response.text_writer()
+	local title = Http.request.parameters.title or error()
+	local bio = Http.request.parameters.bio or error()
+	local username = Http.request.parameters.username or error()
+	local password = Http.request.parameters.password or error()
+	local pic_uuid = Http.request.parameters.pic_uuid or error()
+	local pic_filename = Http.request.parameters.pic_filename or error()
+
+	if not name_regex.matches(username) then
+		js_error("username",[[Usernames may only contain letters, numbers, underscores ("_") and hyphens ("-")]])
+		return
+	end
+	local pic_changed = false
+	local user = User.current() or error()
+	local err_fld, err_msg = run_in_transaction( function()
+		user = user.reload()
+		if to_lower(user.name) ~= to_lower(username) and User.get_by_name(username) ~= nil then
+			return "username", "This username is already taken"
+		end
+		if title=="" or title==username or title==user.name then
+			title = nil
+		end
+		user.title = title
+		bio = trim(bio)
+		if bio == "" then
+			bio = nil
+		end
+		user.bio = bio
+		user.name = username
+		user.password = password
+		if pic_uuid == "" then
+			-- no change
+		elseif pic_uuid == "remove" then
+			user.pic_uuid = nil
+			user.pic_filename = nil
+			pic_changed = true
+		else
+			user.pic_uuid = pic_uuid
+			user.pic_filename = pic_filename
+			pic_changed = true
+		end
+		user.save()
+		user.login()
+	end )
+	if err_fld ~= nil then
+		js_error(err_fld,err_msg)
+		return
+	end
+	js_error( "success", "Your information has been updated successfully." )
+	if pic_changed then
+		local url = user.get_pic_url() or "/images/user.png"
+%>
+		let img = document.querySelector('div[header] > span[right] img');
+		if(img)
+			img.src = <%=json_string(url)%>;
+<%
+	end
+end