view src/add_server.js.luan @ 13:af2b95854072

add server records
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 30 Oct 2023 21:32:42 -0600
parents 3e2cb946d511
children 552d6f944acb
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local String = require "luan:String.luan"
local regex = String.regex 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 Server = require "site:/lib/Server.luan"
local Utils = require "site:/lib/Utils.luan"
local get_user = Utils.get_user or error()
local Db = require "site:/lib/Db.luan"
local run_in_transaction = Db.run_in_transaction or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "add_server.js"


local invite_regex = regex[[^https://discord.gg/(\w+)$]]

return function()
	Io.stdout = Http.response.text_writer()
	local parameters = Http.request.parameters
	local id = parameters.id or error()
	local name = parameters.name or error()
	local icon = parameters.icon or error()
	local invite = parameters.invite or error()
	invite = invite_regex.match(invite)
	logger.info("invite = "..invite)
	if invite==nil then
		js_error( "invite", [[Invalid Discord invite URL]] )
		return
	end
	run_in_transaction( function()
		local server = Server.new{
			owner_discord_id = get_user() or error()
			discord_id = id
			name = name
			icon = icon
			invite = invite
		}
		server.save()
	end )
%>
	location = '/servers.html';
<%
end