annotate src/choose_server.html.luan @ 14:80105b716a62

start bot
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 Oct 2023 23:56:48 -0600
parents af2b95854072
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
3 local stringify = Luan.stringify or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
4 local ipairs = Luan.ipairs or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
5 local Parsers = require "luan:Parsers.luan"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
6 local json_parse = Parsers.json_parse or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
7 local Html = require "luan:Html.luan"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
8 local html_encode = Html.encode or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
9 local url_encode = Html.url_encode or error()
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Io = require "luan:Io.luan"
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
11 local uri = Io.uri or error()
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Http = require "luan:http/Http.luan"
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Shared = require "site:/lib/Shared.luan"
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local head = Shared.head or error()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local header = Shared.header or error()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local footer = Shared.footer or error()
3
43814e9f5802 add config
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
17 local Config = require "site:/lib/Config.luan"
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
18 local Utils = require "site:/lib/Utils.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
19 local list_to_set = Utils.list_to_set or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
20 local Server = require "site:/lib/Server.luan"
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
21 local Logging = require "luan:logging/Logging.luan"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
22 local logger = Logging.logger "choose_server.html"
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
25 local function get_access_token()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
26 local url = "https://discord.com/api/oauth2/token"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
27 local config = Config.get()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
28 local options = {
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
29 method = "POST"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
30 headers = {
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
31 ["User-Agent"] = "fuck you" -- for retarded Cloudflare
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
32 }
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
33 authorization = {
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
34 username = config.discord.client_id
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
35 password = config.discord.client_secret
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
36 type = "basic"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
37 }
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
38 parameters = {
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
39 grant_type = "refresh_token"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
40 refresh_token = Http.request.cookies.refresh_token or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
41 }
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
42 }
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
43 -- logger.info(stringify(options))
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
44 local result = uri(url,options).read_text()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
45 -- logger.info(result)
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
46 result = json_parse(result)
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
47 local access_token = result.access_token or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
48 local refresh_token = result.refresh_token or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
49 -- logger.info("access_token = "..access_token)
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
50 Http.response.set_persistent_cookie("refresh_token",refresh_token)
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
51 return access_token
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
52 end
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
53
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 return function()
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
55 local access_token = get_access_token()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
56 local url = "https://discord.com/api/users/@me/guilds"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
57 local options = {
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
58 headers = {
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
59 ["User-Agent"] = "fuck you" -- for retarded Cloudflare
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
60 Authorization = "Bearer "..access_token
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
61 }
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
62 }
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
63 local result = uri(url,options).read_text()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
64 -- logger.info(result)
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
65 result = json_parse(result)
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
66 -- logger.info(stringify(result))
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
67
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
68 local set = list_to_set{}
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
69 for _, server in ipairs(Server.get_user_servers()) do
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
70 set[server.discord_id] = true
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
71 end
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
72
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
73 local servers = {}
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
74 for _, server in ipairs(result) do
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
75 if server.owner and not set[server.id] then
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
76 servers[#servers+1] = server
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
77 end
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
78 end
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
79 logger.info(stringify(servers))
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
80
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 Io.stdout = Http.response.text_writer()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 <!doctype html>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 <html>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 <head>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 <% head() %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 <title>Disearch</title>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 </head>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 <body>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 <% header() %>
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
91
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
92 <h1>Choose server to add</h1>
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
93 <%
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
94 for _, server in ipairs(servers) do
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
95 local id = server.id or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
96 local name = server.name or error()
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
97 local icon = server.icon
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
98 if icon ~= nil then
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
99 local url = "add_server.html?id="..id.."&name="..url_encode(name).."&icon="..icon
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
100 %>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
101 <p><a href="<%=url%>"><%=html_encode(name)%></a></p>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
102 <%
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
103 else
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
104 %>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
105 <p><%=html_encode(name)%> (missing icon)</p>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
106 <%
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
107 end
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
108 end
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
109 %>
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 <% footer() %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 </body>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 </html>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 <%
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 end