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