annotate src/lib/Shared.luan @ 20:914a37651a38

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Nov 2023 11:31:16 -0700
parents b386303be994
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
16
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
3 local ipairs = Luan.ipairs or error()
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Time = require "luan:Time.luan"
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
5 local Parsers = require "luan:Parsers.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
6 local json_string = Parsers.json_string or error()
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
7 local Http = require "luan:http/Http.luan"
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
8 local Utils = require "site:/lib/Utils.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
9 local get_user = Utils.get_user or error()
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local Shared = {}
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local started = Time.now()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 function Shared.head()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 <meta name="viewport" content="width=device-width, initial-scale=1">
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 <style>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 @import "/site.css?s=<%=started%>";
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 </style>
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
22 <script src="/site.js?s=<%=started%>"></script>
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 end
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 function Shared.header()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 <div header>
2
5ae5fbce0d75 start header
Franklin Schmidt <fschmidt@gmail.com>
parents: 1
diff changeset
29 <h1><a href="/">Disearch</a></h1>
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
30 <% if get_user() == nil then %>
5
aa1920665f98 discord login
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
31 <a href="login1.red">login</a>
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
32 <% else %>
9
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
33 <span right pulldown>
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
34 <script>document.write(`<img src="https://cdn.discordapp.com/avatars/${localStorage.user_id}/${localStorage.user_avatar}.png" onclick="clickMenu(this)">`)</script>
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
35 <div pulldown_menu>
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
36 <span username><script>document.write(localStorage.user_name)</script></span>
16
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
37 <a href="/bump.html">Bump Servers</a>
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
38 <a href="/servers.html">Your Servers</a>
9
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
39 <a href="javascript:logout()">Log out</a>
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
40 </div>
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
41 </span>
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents: 2
diff changeset
42 <% end %>
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 </div>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 <hr>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 <%
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 end
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 function Shared.footer()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 <hr>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 <div footer>
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
52 <a href="/tools/links.html">links</a>
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 </div>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 <%
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 end
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
57 local function base_url()
6
d59ae3e468f1 login work
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
58 local request = Http.request
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
59 return request.scheme.."://"..request.headers["Host"]
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
60 end
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
61 Shared.base_url = base_url
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
62
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
63 function Shared.discord_redirect_uri()
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
64 return base_url().."/login2.html"
6
d59ae3e468f1 login work
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
65 end
d59ae3e468f1 login work
Franklin Schmidt <fschmidt@gmail.com>
parents: 5
diff changeset
66
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
67 function Shared.js_error(field,message)
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
68 %>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
69 showError( context.form, '<%=field%>', <%=json_string(message)%> );
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
70 <%
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
71 end
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
72
16
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
73 local times = {
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
74 {
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
75 time = 1000*60*60*24*365
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
76 unit = "year"
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
77 }
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
78 {
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
79 time = 1000*60*60*24*7
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
80 unit = "week"
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
81 }
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
82 {
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
83 time = 1000*60*60*24
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
84 unit = "day"
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
85 }
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
86 {
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
87 time = 1000*60*60
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
88 unit = "hour"
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
89 }
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
90 {
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
91 time = 1000*60
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
92 unit = "minute"
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
93 }
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
94 }
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
95
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
96 function Shared.ago(time)
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
97 for _, t in ipairs(times) do
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
98 local n = time // t.time
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
99 if n > 0 then
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
100 %><%=n%> <%=t.unit%><%
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
101 if n > 1 then
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
102 %>s<%
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
103 end
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
104 %> ago<%
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
105 return
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
106 end
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
107 end
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
108 %>just now<%
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
109 end
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
110
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return Shared