changeset 11:62dd23f0b549

start add server
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 29 Oct 2023 22:39:31 -0600
parents ae9ebeb1c246
children 3e2cb946d511
files src/add_server.html.luan src/choose_server.html.luan src/index.html.luan src/lib/Shared.luan src/login1.red.luan src/login2.html.luan src/servers.html.luan src/site.css src/site.js
diffstat 9 files changed, 182 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/add_server.html.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -0,0 +1,40 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Io = require "luan:Io.luan"
+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()
+
+
+return function()
+	local parameters = Http.request.parameters
+	local id = parameters.id or error()
+	local name = parameters.name or error()
+	local icon = parameters.icon
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Disearch</title>
+	</head>
+	<body>
+<%		header() %>
+
+		<h1>Add server</h1>
+
+		<p><%=html_encode(name)%></p>
+<%	if icon ~= nil then %>
+		<p><img src="https://cdn.discordapp.com/icons/<%=id%>/<%=icon%>.png"></p>
+<%	end %>
+
+<%		footer() %>
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/choose_server.html.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -0,0 +1,102 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local stringify = Luan.stringify or error()
+local ipairs = Luan.ipairs or error()
+local Parsers = require "luan:Parsers.luan"
+local json_parse = Parsers.json_parse or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local url_encode = Html.url_encode 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 Config = require "site:/lib/Config.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "choose_server.html"
+
+
+local function get_access_token()
+	local url = "https://discord.com/api/oauth2/token"
+	local config = Config.get()
+	local options = {
+		method = "POST"
+		headers = {
+			["User-Agent"] = "fuck you"  -- for retarded Cloudflare
+		}
+		authorization = {
+			username = config.discord.client_id
+			password = config.discord.client_secret
+			type = "basic"
+		}
+		parameters = {
+			grant_type = "refresh_token"
+			refresh_token = Http.request.cookies.refresh_token or error()
+		}
+	}
+	-- logger.info(stringify(options))
+	local result = uri(url,options).read_text()
+	-- logger.info(result)
+	result = json_parse(result)
+	local access_token = result.access_token or error()
+	local refresh_token = result.refresh_token or error()
+	-- logger.info("access_token = "..access_token)
+	Http.response.set_persistent_cookie("refresh_token",refresh_token)
+	return access_token
+end
+
+return function()
+	local access_token = get_access_token()
+	local url = "https://discord.com/api/users/@me/guilds"
+	local options = {
+		headers = {
+			["User-Agent"] = "fuck you"  -- for retarded Cloudflare
+			Authorization = "Bearer "..access_token
+		}
+	}
+	local result = uri(url,options).read_text()
+	-- logger.info(result)
+	result = json_parse(result)
+	-- logger.info(stringify(result))
+	local servers = {}
+	for _, server in ipairs(result) do
+		if server.owner then
+			servers[#servers+1] = server
+		end
+	end
+	logger.info(stringify(servers))
+
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Disearch</title>
+	</head>
+	<body>
+<%		header() %>
+
+		<h1>Choose server to add</h1>
+<%
+	for _, server in ipairs(servers) do
+		local id = server.id or error()
+		local name = server.name or error()
+		local icon = server.icon
+		local url = "add_server.html?id="..id.."&name="..url_encode(name)
+		if icon ~= nil then
+			url = url.."&icon="..icon
+		end
+%>
+		<p><a href="<%=url%>"><%=html_encode(name)%></a></p>
+<%
+	end
+%>
+<%		footer() %>
+	</body>
+</html>
+<%
+end
--- a/src/index.html.luan	Fri Oct 27 16:20:45 2023 -0600
+++ b/src/index.html.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -6,7 +6,6 @@
 local head = Shared.head or error()
 local header = Shared.header or error()
 local footer = Shared.footer or error()
-local Config = require "site:/lib/Config.luan"
 
 
 return function()
--- a/src/lib/Shared.luan	Fri Oct 27 16:20:45 2023 -0600
+++ b/src/lib/Shared.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -34,6 +34,7 @@
 				<script>document.write(`<img src="https://cdn.discordapp.com/avatars/${localStorage.user_id}/${localStorage.user_avatar}.png" onclick="clickMenu(this)">`)</script>
 				<div pulldown_menu>
 					<span username><script>document.write(localStorage.user_name)</script></span>
+					<a href="/servers.html">Your Servers</a>
 					<a href="javascript:logout()">Log out</a>
 				</div>
 			</span>
--- a/src/login1.red.luan	Fri Oct 27 16:20:45 2023 -0600
+++ b/src/login1.red.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -13,7 +13,7 @@
 return function()
 	local config = Config.get()
 	local redirect_uri = discord_redirect_uri()
-	local scope = "identify"
+	local scope = "identify guilds"
 	local url = "https://discord.com/api/oauth2/authorize?client_id="..config.discord.client_id.."&redirect_uri="..url_encode(redirect_uri).."&response_type=code&scope="..url_encode(scope)
 	-- logger.info(url)
 	Http.response.send_redirect(url)
--- a/src/login2.html.luan	Fri Oct 27 16:20:45 2023 -0600
+++ b/src/login2.html.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -10,7 +10,7 @@
 local Shared = require "site:/lib/Shared.luan"
 local discord_redirect_uri = Shared.discord_redirect_uri or error()
 local Logging = require "luan:logging/Logging.luan"
-local logger = Logging.logger "login2.red"
+local logger = Logging.logger "login2.html"
 
 
 return function()
@@ -40,7 +40,9 @@
 	-- logger.info(result)
 	result = json_parse(result)
 	local access_token = result.access_token or error()
+	local refresh_token = result.refresh_token or error()
 	logger.info("access_token = "..access_token)
+	Http.response.set_persistent_cookie("refresh_token",refresh_token)
 
 	local url = "https://discord.com/api/users/@me"
 	local options = {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/servers.html.luan	Sun Oct 29 22:39:31 2023 -0600
@@ -0,0 +1,31 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Io = require "luan:Io.luan"
+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()
+
+
+return function()
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Disearch</title>
+	</head>
+	<body>
+<%		header() %>
+
+		<h1>Your servers</h1>
+
+		<p><a href="choose_server.html">add server</a></p>
+
+<%		footer() %>
+	</body>
+</html>
+<%
+end
--- a/src/site.css	Fri Oct 27 16:20:45 2023 -0600
+++ b/src/site.css	Sun Oct 29 22:39:31 2023 -0600
@@ -21,13 +21,15 @@
 	position: relative;
 }
 [pulldown_menu] {
-	display: none;
+	display: none;  /* set to flex */
+	flex-direction: column;
 	z-index: 20;
 	position: absolute;
 	top: 40px;
 	border: 1px solid #DDDDDD;
 	text-align: left;
 	background-color: #ffffff;
+	white-space: nowrap;
 }
 
 div[header] {
--- a/src/site.js	Fri Oct 27 16:20:45 2023 -0600
+++ b/src/site.js	Sun Oct 29 22:39:31 2023 -0600
@@ -50,7 +50,7 @@
 	//console.log("clickMenu");
 	let pulldown = clicked.parentNode.querySelector('[pulldown_menu]');
 	if( pulldown !== currentPulldown ) {
-		pulldown.style.display = display || "block";
+		pulldown.style.display = display || "flex";
 		newPulldown = pulldown;
 		window.onclick = function() {
 			if( currentPulldown ) {