view src/bump.html.luan @ 20:914a37651a38

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Nov 2023 11:31:16 -0700
parents 552d6f944acb
children
line wrap: on
line source

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