comparison src/bump.html.luan @ 16:552d6f944acb

add bump
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 01 Nov 2023 17:34:15 -0600
parents src/choose_server.html.luan@af2b95854072
children
comparison
equal deleted inserted replaced
15:d0b8e1a8fc69 16:552d6f944acb
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local stringify = Luan.stringify or error()
4 local ipairs = Luan.ipairs or error()
5 local Table = require "luan:Table.luan"
6 local sort = Table.sort or error()
7 local Time = require "luan:Time.luan"
8 local time_now = Time.now or error()
9 local Parsers = require "luan:Parsers.luan"
10 local json_parse = Parsers.json_parse or error()
11 local Html = require "luan:Html.luan"
12 local html_encode = Html.encode or error()
13 local url_encode = Html.url_encode or error()
14 local Io = require "luan:Io.luan"
15 local uri = Io.uri or error()
16 local Http = require "luan:http/Http.luan"
17 local Shared = require "site:/lib/Shared.luan"
18 local head = Shared.head or error()
19 local header = Shared.header or error()
20 local footer = Shared.footer or error()
21 local ago = Shared.ago or error()
22 local Config = require "site:/lib/Config.luan"
23 local Server = require "site:/lib/Server.luan"
24 local get_server = Server.get_by_discord_id or error()
25 local bumped_sort = Server.bumped_sort or error()
26 local Logging = require "luan:logging/Logging.luan"
27 local logger = Logging.logger "bump.html"
28
29
30 local function get_access_token()
31 local url = "https://discord.com/api/oauth2/token"
32 local config = Config.get()
33 local options = {
34 method = "POST"
35 headers = {
36 ["User-Agent"] = "Disboard" -- for retarded Cloudflare
37 }
38 authorization = {
39 username = config.discord.client_id
40 password = config.discord.client_secret
41 type = "basic"
42 }
43 parameters = {
44 grant_type = "refresh_token"
45 refresh_token = Http.request.cookies.refresh_token or error()
46 }
47 }
48 -- logger.info(stringify(options))
49 local result = uri(url,options).read_text()
50 -- logger.info(result)
51 result = json_parse(result)
52 local access_token = result.access_token or error()
53 local refresh_token = result.refresh_token or error()
54 -- logger.info("access_token = "..access_token)
55 Http.response.set_persistent_cookie("refresh_token",refresh_token)
56 return access_token
57 end
58
59 return function()
60 local access_token = get_access_token()
61 local url = "https://discord.com/api/users/@me/guilds"
62 local options = {
63 headers = {
64 ["User-Agent"] = "Disboard" -- for retarded Cloudflare
65 Authorization = "Bearer "..access_token
66 }
67 }
68 local result = uri(url,options).read_text()
69 -- logger.info(result)
70 result = json_parse(result)
71 -- logger.info(stringify(result))
72
73 local servers = {}
74 for _, server in ipairs(result) do
75 servers[#servers+1] = get_server(server.id)
76 end
77 sort(servers,bumped_sort)
78 -- logger.info(stringify(servers))
79
80 Io.stdout = Http.response.text_writer()
81 %>
82 <!doctype html>
83 <html>
84 <head>
85 <% head() %>
86 <title>Disearch</title>
87 </head>
88 <body>
89 <% header() %>
90
91 <h1>Bump Servers</h1>
92 <%
93 local now = time_now()
94 for _, server in ipairs(servers) do
95 %>
96 <p>
97 <%=html_encode(server.name)%>
98 <% server.write_img_html() %>
99 <a href="<%=server.invite_url()%>">invite</a>
100 <a href="javascript:ajax('/bump.js?server=<%=server.id%>')">bump</a>
101 bumped <% ago( now - server.bumped ) %>
102 </p>
103 <%
104 end
105 %>
106 <% footer() %>
107 </body>
108 </html>
109 <%
110 end