view src/lib/User.luan @ 93:d0566cc4a2ac default tip

uploa fix
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Apr 2025 19:47:48 -0600
parents 7b339b1ccd11
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local range = Luan.range or error()
local to_string = Luan.to_string or error()
local get_local_only = Luan.get_local_only or error()
local set_local_only = Luan.set_local_only or error()
local String = require "luan:String.luan"
local sub_string = String.sub or error()
local Math = require "luan:Math.luan"
local random = Math.random or error()
local Table = require "luan:Table.luan"
local concat = Table.concat or error()
local Html = require "luan:Html.luan"
local html_encode = Html.encode or error()
local Lucene = require "luan:lucene/Lucene.luan"
local lucene_quote = Lucene.quote or error()
local Http = require "luan:http/Http.luan"
local Db = require "site:/lib/Db.luan"
local run_in_transaction = Db.run_in_transaction or error()
local Chat = require "site:/lib/Chat.luan"
local chat_search = Chat.search or error()
local Utils = require "site:/lib/Utils.luan"
local list_to_set = Utils.list_to_set or error()
local set_to_list = Utils.set_to_list or error()
local base_url = Utils.base_url or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "User"


local User = {}

local function from_doc(doc)
	doc.type == "user" or error "wrong type"
	return User.new {
		id = doc.id
		email = doc.user_email
		password = doc.password
		was_notified = doc.was_notified=="true"
		notify_email = doc.notify_email
		multi_notify = doc.multi_notify=="true"
		voice_url = doc.voice_url
		name = doc.name
	}
end

local function to_doc(user)
	return {
		type = "user"
		id = user.id
		user_email = user.email
		password = user.password
		was_notified = user.was_notified and "true" or nil
		notify_email = user.notify_email
		multi_notify = user.multi_notify and "true" or nil
		voice_url = user.voice_url
		name = user.name
	}
end

function User.new(user)

	function user.save()
		local doc = to_doc(user)
		Db.save(doc)
		user.id = doc.id
	end

	function user.reload()
		return User.get_by_id(user.id) or error(user.id)
	end

	function user.delete()
		run_in_transaction( function()
			local id = user.id
			local chats = chat_search("chat_user_ids:"..id)
			for _, chat in ipairs(chats) do
				chat.delete()
			end
			Db.delete("id:"..id)
		end )
	end

	function user.login()
		local id = to_string(user.id)
		Http.response.set_persistent_cookie("user",id)
		Http.response.set_persistent_cookie("password",user.password)
		Http.request.cookies.user = id
		Http.request.cookies.password = user.password or error()
	end

	function user.last_update()
		local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc", 1 )
		local n = #chats
		if n == 0 then
			return 0
		elseif n == 1 then
			return chats[1].updated
		else error() end
	end

	function user.chatting_with_ids()
		local my_id = user.id
		local user_ids = list_to_set{}
		local chats = chat_search( "chat_user_ids:"..my_id )
		for _, chat in ipairs(chats) do
			for _, user_id in ipairs(chat.user_ids) do
				user_ids[user_id] = true
			end
		end
		user_ids[my_id] = false
		return set_to_list(user_ids)
	end

	function user.name_html()
		return html_encode(user.name or user.email)
	end

	function user.login_url()
		return base_url().."/do_login.html?user="..user.id.."&password="..user.password
	end

	return user
end

local function get_by_id(id)
	local doc = Db.get_document("id:"..id)
	return doc and doc.type=="user" and from_doc(doc) or nil
end
User.get_by_id = get_by_id

local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
do
	local t = {}
	for i in range(1,#password_chars) do
		t[#t+1] = sub_string(password_chars,i,i)
	end
	password_chars = t
end	

local function new_password()
	local n = #password_chars
	local t = {}
	for _ in range(1,10) do
		t[#t+1] = password_chars[random(n)]
	end
	return concat(t)
end

local function get_by_email(email)
	local doc = Db.get_document("user_email:"..lucene_quote(email))
	return doc and from_doc(doc)
end
User.get_by_email = get_by_email

function User.get_or_create_by_email(email)
	return run_in_transaction( function()
		local user = get_by_email(email)
		if user == nil then
			user = User.new{
				email = email
				password = new_password()
				notify_email = email
			}
			user.save()
		end
		return user
	end )
end

function User.search(query,sort,rows)
	rows = rows or 1000000
	local users = {}
	local docs = Db.search(query,1,rows,{sort=sort})
	for _, doc in ipairs(docs) do
		users[#users+1] = from_doc(doc)
	end
	return users
end

local function current()
	local user = get_local_only(User,"current")
	if user == nil then
		local id = Http.request.cookies.user
		local password = Http.request.cookies.password
		if id == nil or password == nil then
			user = "nil"
		else
			user = get_by_id(id)
			if user == nil or user.password ~= password then
				user = "nil"
			end
		end
		set_local_only(User,"current",user)
	end
	return user ~= "nil" and user or nil
end
User.current = current

function User.current_required()
	local user = current()
	user or Http.response.send_redirect "/login.html"
	return user
end

return User