view src/add_server.js.luan @ 19:b386303be994

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 02 Nov 2023 21:00:37 -0600
parents 552d6f944acb
children
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 Time = require "luan:Time.luan"
local time_now = Time.now 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
			bumped = time_now()
			name = name
			icon = icon
			invite = invite
		}
		server.save()
	end )
%>
	location = '/servers.html';
<%
end