annotate src/add_server.js.luan @ 13:af2b95854072

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