annotate src/add_server.js.luan @ 16:552d6f944acb

add bump
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 01 Nov 2023 17:34:15 -0600
parents af2b95854072
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local String = require "luan:String.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local regex = String.regex or error()
16
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
5 local Time = require "luan:Time.luan"
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
6 local time_now = Time.now or error()
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Http = require "luan:http/Http.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Shared = require "site:/lib/Shared.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local js_error = Shared.js_error or error()
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
11 local Server = require "site:/lib/Server.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
12 local Utils = require "site:/lib/Utils.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
13 local get_user = Utils.get_user or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
14 local Db = require "site:/lib/Db.luan"
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
15 local run_in_transaction = Db.run_in_transaction or error()
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Logging = require "luan:logging/Logging.luan"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local logger = Logging.logger "add_server.js"
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local invite_regex = regex[[^https://discord.gg/(\w+)$]]
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return function()
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 Io.stdout = Http.response.text_writer()
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local parameters = Http.request.parameters
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local id = parameters.id or error()
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local name = parameters.name or error()
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local icon = parameters.icon or error()
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local invite = parameters.invite or error()
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 invite = invite_regex.match(invite)
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 logger.info("invite = "..invite)
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 if invite==nil then
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 js_error( "invite", [[Invalid Discord invite URL]] )
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 return
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 end
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
35 run_in_transaction( function()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
36 local server = Server.new{
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
37 owner_discord_id = get_user() or error()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
38 discord_id = id
16
552d6f944acb add bump
Franklin Schmidt <fschmidt@gmail.com>
parents: 13
diff changeset
39 bumped = time_now()
13
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
40 name = name
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
41 icon = icon
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
42 invite = invite
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
43 }
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
44 server.save()
af2b95854072 add server records
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
45 end )
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 %>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 location = '/servers.html';
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 <%
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 end