view src/add_server.html.luan @ 15:d0b8e1a8fc69

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 01 Nov 2023 00:12:08 -0600
parents 80105b716a62
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local Html = require "luan:Html.luan"
local html_encode = Html.encode or error()
local Parsers = require "luan:Parsers.luan"
local json_parse = Parsers.json_parse or error()
local Io = require "luan:Io.luan"
local uri = Io.uri or error()
local Http = require "luan:http/Http.luan"
local Shared = require "site:/lib/Shared.luan"
local head = Shared.head or error()
local header = Shared.header or error()
local footer = Shared.footer or error()
local Server = require "site:/lib/Server.luan"
local Config = require "site:/lib/Config.luan"


local function error_page(msg)
	Io.stdout = Http.response.text_writer()
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title>Disearch</title>
	</head>
	<body>
<%		header() %>
		<p><%=msg%></p>
<%		footer() %>
	</body>
</html>
<%
end

return function()
	local parameters = Http.request.parameters
	if parameters.error == "access_denied" then
		Http.response.send_redirect("/servers.html")
		return
	end
	local guild_id = parameters.guild_id or error()
	if Server.get_by_discord_id(guild_id) ~= nil then
		error_page("already added")
		return
	end

	local url = "https://discord.com/api/guilds/"..guild_id
	local config = Config.get()
	local options = {
		headers = {
			["User-Agent"] = "Disearch"  -- for retarded Cloudflare
			Authorization = "Bot "..config.discord.bot_token
		}
	}
	local result = uri(url,options).read_text()
	--logger.info(result)
	result = json_parse(result)

	local name = result.name or error()
	name = html_encode(name)
	local icon = result.icon
	if icon == nil then
		error_page("server must have icon")
		return
	end
	Io.stdout = Http.response.text_writer()
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title>Disearch</title>
		<style>
			input[type="url"] {
				width: 100%;
			}
		</style>
	</head>
	<body>
<%		header() %>

		<h1>Add server</h1>

		<form page onsubmit="ajaxForm('/add_server.js',this); return false">
			<input type=hidden name=id value="<%=guild_id%>">
			<input type=hidden name=name value="<%=name%>">
			<input type=hidden name=icon value="<%=icon%>">
			<p><%=name%></p>
			<p><img icon src="https://cdn.discordapp.com/icons/<%=guild_id%>/<%=icon%>.png"></p>
			<p>
				Invite URL:<br>
				<input type=url required name=invite>
				<span error=invite></span>
			</p>
			<p><input type=submit></p>
		</form>

<%		footer() %>
	</body>
</html>
<%
end