changeset 16:552d6f944acb

add bump
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 01 Nov 2023 17:34:15 -0600
parents d0b8e1a8fc69
children 74277b56ac3c
files src/add_server.js.luan src/bump.html.luan src/bump.js.luan src/choose_server.html.luan src/lib/Server.luan src/lib/Shared.luan src/login2.html.luan src/private/tools/config_save.txt.luan
diffstat 8 files changed, 220 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
--- a/src/add_server.js.luan	Wed Nov 01 00:12:08 2023 -0600
+++ b/src/add_server.js.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -2,6 +2,8 @@
 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"
@@ -34,6 +36,7 @@
 		local server = Server.new{
 			owner_discord_id = get_user() or error()
 			discord_id = id
+			bumped = time_now()
 			name = name
 			icon = icon
 			invite = invite
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/bump.html.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -0,0 +1,110 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local stringify = Luan.stringify or error()
+local ipairs = Luan.ipairs or error()
+local Table = require "luan:Table.luan"
+local sort = Table.sort or error()
+local Time = require "luan:Time.luan"
+local time_now = Time.now 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 ago = Shared.ago or error()
+local Config = require "site:/lib/Config.luan"
+local Server = require "site:/lib/Server.luan"
+local get_server = Server.get_by_discord_id or error()
+local bumped_sort = Server.bumped_sort or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "bump.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"] = "Disboard"  -- 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"] = "Disboard"  -- 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
+		servers[#servers+1] = get_server(server.id)
+	end
+	sort(servers,bumped_sort)
+	-- logger.info(stringify(servers))
+
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Disearch</title>
+	</head>
+	<body>
+<%		header() %>
+
+		<h1>Bump Servers</h1>
+<%
+	local now = time_now()
+	for _, server in ipairs(servers) do
+%>
+		<p>
+			<%=html_encode(server.name)%>
+			<% server.write_img_html() %>
+			<a href="<%=server.invite_url()%>">invite</a>
+			<a href="javascript:ajax('/bump.js?server=<%=server.id%>')">bump</a>
+			bumped <% ago( now - server.bumped ) %>
+		</p>
+<%
+	end
+%>
+<%		footer() %>
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/bump.js.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -0,0 +1,26 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.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 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()
+
+
+return function()
+	local server_id = Http.request.parameters.server or error()
+	run_in_transaction( function()
+		local server = Server.get_by_id(server_id) or error()
+		server.owner_discord_id == get_user() or error()
+		server.bumped = time_now()
+		server.save()
+	end )
+	Io.stdout = Http.response.text_writer()
+%>
+	location.reload();
+<%
+end
--- a/src/choose_server.html.luan	Wed Nov 01 00:12:08 2023 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-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 Utils = require "site:/lib/Utils.luan"
-local list_to_set = Utils.list_to_set or error()
-local Server = require "site:/lib/Server.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 set = list_to_set{}
-	for _, server in ipairs(Server.get_user_servers()) do
-		set[server.discord_id] = true
-	end
-
-	local servers = {}
-	for _, server in ipairs(result) do
-		if server.owner and not set[server.id] 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
-		if icon ~= nil then
-			local url = "add_server.html?id="..id.."&name="..url_encode(name).."&icon="..icon
-%>
-			<p><a href="<%=url%>"><%=html_encode(name)%></a></p>
-<%
-		else
-%>
-			<p><%=html_encode(name)%> (missing icon)</p>
-<%
-		end
-	end
-%>
-<%		footer() %>
-	</body>
-</html>
-<%
-end
--- a/src/lib/Server.luan	Wed Nov 01 00:12:08 2023 -0600
+++ b/src/lib/Server.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -1,6 +1,8 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
+local Number = require "luan:Number.luan"
+local long = Number.long or error()
 local Db = require "site:/lib/Db.luan"
 local Utils = require "site:/lib/Utils.luan"
 local get_user = Utils.get_user or error()
@@ -14,6 +16,7 @@
 		id = doc.id
 		discord_id = doc.server_discord_id
 		owner_discord_id = doc.server_owner_discord_id
+		bumped = doc.server_bumped
 		name = doc.name
 		icon = doc.icon
 		invite = doc.invite
@@ -26,6 +29,7 @@
 		id = server.id
 		server_discord_id = server.discord_id or error()
 		server_owner_discord_id = server.owner_discord_id or error()
+		server_bumped = long(server.bumped)
 		name = server.name or error()
 		icon = server.icon or error()
 		invite = server.invite or error()
@@ -86,4 +90,8 @@
 	return search( "server_owner_discord_id:"..get_user() )
 end
 
+function Server.bumped_sort(s1,s2)
+	return s1.bumped > s2.bumped
+end
+
 return Server
--- a/src/lib/Shared.luan	Wed Nov 01 00:12:08 2023 -0600
+++ b/src/lib/Shared.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -1,5 +1,6 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local ipairs = Luan.ipairs or error()
 local Time = require "luan:Time.luan"
 local Parsers = require "luan:Parsers.luan"
 local json_string = Parsers.json_string or error()
@@ -33,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="/bump.html">Bump Servers</a>
 					<a href="/servers.html">Your Servers</a>
 					<a href="javascript:logout()">Log out</a>
 				</div>
@@ -68,4 +70,42 @@
 <%
 end
 
+local times = {
+	{
+		time = 1000*60*60*24*365
+		unit = "year"
+	}
+	{
+		time = 1000*60*60*24*7
+		unit = "week"
+	}
+	{
+		time = 1000*60*60*24
+		unit = "day"
+	}
+	{
+		time = 1000*60*60
+		unit = "hour"
+	}
+	{
+		time = 1000*60
+		unit = "minute"
+	}
+}
+
+function Shared.ago(time)
+	for _, t in ipairs(times) do
+		local n = time // t.time
+		if n > 0 then
+			%><%=n%> <%=t.unit%><%
+			if n > 1 then
+				%>s<%
+			end
+			%> ago<%
+			return
+		end
+	end
+	%>just now<%
+end
+
 return Shared
--- a/src/login2.html.luan	Wed Nov 01 00:12:08 2023 -0600
+++ b/src/login2.html.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -14,6 +14,10 @@
 
 
 return function()
+	if Http.request.parameters.error == "access_denied" then
+		Http.response.send_redirect("/")
+		return
+	end
 	local code = Http.request.parameters.code or error()
 	logger.info("code = "..code)
 
@@ -40,7 +44,9 @@
 	-- logger.info(result)
 	result = json_parse(result)
 	local access_token = result.access_token or error()
-	logger.info("access_token = "..access_token)
+	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 = {
--- a/src/private/tools/config_save.txt.luan	Wed Nov 01 00:12:08 2023 -0600
+++ b/src/private/tools/config_save.txt.luan	Wed Nov 01 17:34:15 2023 -0600
@@ -1,14 +1,40 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local stringify = Luan.stringify or error()
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
 local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
 local Http = require "luan:http/Http.luan"
 local Config = require "site:/lib/Config.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "config_save.txt"
 
 
+local function set_cmd()
+	local config = Config.get()
+	local url = "https://discord.com/api/applications/"..config.discord.client_id.."/commands"
+	local options = {
+		method = "POST"
+		headers = {
+			["User-Agent"] = "Disearch"  -- for retarded Cloudflare
+			Authorization = "Bot "..config.discord.bot_token
+			["Content-Type"] = "application/json"
+		}
+		content = json_string{
+			type = 1
+			name = "dbump"
+			description = "Bump this server in Disearch."
+		}
+	}
+	local result = uri(url,options).read_text()
+	logger.info(result)
+end
+
 return function()
 	local config = Http.request.parameters.config or error()
 	Config.set(config)
+	-- set_cmd()  don't do this
 	Io.stdout = Http.response.text_writer()
 	%>saved<%
 end