Mercurial Hosting > disearch
changeset 13:af2b95854072
add server records
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 30 Oct 2023 21:32:42 -0600 |
parents | 3e2cb946d511 |
children | 80105b716a62 |
files | src/add_server.js.luan src/choose_server.html.luan src/delete_server.js.luan src/lib/Db.luan src/lib/Server.luan src/lib/Shared.luan src/lib/Utils.luan src/servers.html.luan |
diffstat | 8 files changed, 177 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
diff -r 3e2cb946d511 -r af2b95854072 src/add_server.js.luan --- a/src/add_server.js.luan Mon Oct 30 14:59:45 2023 -0600 +++ b/src/add_server.js.luan Mon Oct 30 21:32:42 2023 -0600 @@ -6,6 +6,11 @@ local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" local js_error = Shared.js_error or error() +local Server = require "site:/lib/Server.luan" +local Utils = require "site:/lib/Utils.luan" +local get_user = Utils.get_user or error() +local Db = require "site:/lib/Db.luan" +local run_in_transaction = Db.run_in_transaction or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "add_server.js" @@ -25,6 +30,16 @@ js_error( "invite", [[Invalid Discord invite URL]] ) return end + run_in_transaction( function() + local server = Server.new{ + owner_discord_id = get_user() or error() + discord_id = id + name = name + icon = icon + invite = invite + } + server.save() + end ) %> location = '/servers.html'; <%
diff -r 3e2cb946d511 -r af2b95854072 src/choose_server.html.luan --- a/src/choose_server.html.luan Mon Oct 30 14:59:45 2023 -0600 +++ b/src/choose_server.html.luan Mon Oct 30 21:32:42 2023 -0600 @@ -15,6 +15,9 @@ local header = Shared.header or error() local footer = Shared.footer or error() local Config = require "site:/lib/Config.luan" +local Utils = require "site:/lib/Utils.luan" +local list_to_set = Utils.list_to_set or error() +local Server = require "site:/lib/Server.luan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "choose_server.html" @@ -61,9 +64,15 @@ -- logger.info(result) result = json_parse(result) -- logger.info(stringify(result)) + + local set = list_to_set{} + for _, server in ipairs(Server.get_user_servers()) do + set[server.discord_id] = true + end + local servers = {} for _, server in ipairs(result) do - if server.owner then + if server.owner and not set[server.id] then servers[#servers+1] = server end end
diff -r 3e2cb946d511 -r af2b95854072 src/delete_server.js.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/delete_server.js.luan Mon Oct 30 21:32:42 2023 -0600 @@ -0,0 +1,19 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" +local Server = require "site:/lib/Server.luan" +local Utils = require "site:/lib/Utils.luan" +local get_user = Utils.get_user or error() + + +return function() + local server_id = Http.request.parameters.server or error() + local server = Server.get_by_id(server_id) + server.owner_discord_id == get_user() or error() + server.delete() + Io.stdout = Http.response.text_writer() +%> + location.reload(); +<% +end
diff -r 3e2cb946d511 -r af2b95854072 src/lib/Db.luan --- a/src/lib/Db.luan Mon Oct 30 14:59:45 2023 -0600 +++ b/src/lib/Db.luan Mon Oct 30 21:32:42 2023 -0600 @@ -18,6 +18,8 @@ name = "lucene" } ) +Db.indexed_fields.server_owner_discord_id = Lucene.type.string + function Db.not_in_transaction() logger.error(new_error("not in transaction")) end
diff -r 3e2cb946d511 -r af2b95854072 src/lib/Server.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/Server.luan Mon Oct 30 21:32:42 2023 -0600 @@ -0,0 +1,84 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local ipairs = Luan.ipairs or error() +local Db = require "site:/lib/Db.luan" +local Utils = require "site:/lib/Utils.luan" +local get_user = Utils.get_user or error() + + +local Server = {} + +local function from_doc(doc) + doc.type == "server" or error "wrong type" + return Server.new { + id = doc.id + owner_discord_id = doc.server_owner_discord_id + discord_id = doc.discord_id + name = doc.name + icon = doc.icon + invite = doc.invite + } +end + +local function to_doc(server) + return { + type = "server" + id = server.id + server_owner_discord_id = server.owner_discord_id or error() + discord_id = server.discord_id or error() + name = server.name or error() + icon = server.icon or error() + invite = server.invite or error() + } +end + +function Server.new(server) + + function server.save() + local doc = to_doc(server) + Db.save(doc) + server.id = doc.id + end + + function server.reload() + return Server.get_by_id(server.id) or error(server.id) + end + + function server.delete() + Db.run_in_transaction( function() + Db.delete("id:"..server.id) + end ) + end + + function server.write_img_html() + %><img icon src="https://cdn.discordapp.com/icons/<%=server.discord_id%>/<%=server.icon%>.png"><% + end + + function server.invite_url() + return "https://discord.gg/"..server.invite + end + + return server +end + +function Server.get_by_id(id) + local doc = Db.get_document("id:"..id) + return doc and from_doc(doc) +end + +local function search(query,sort,rows) + rows = rows or 1000000 + local docs = Db.search(query,1,rows,{sort=sort}) + local servers = {} + for _, doc in ipairs(docs) do + local server = from_doc(doc) + servers[#servers+1] = server + end + return servers +end + +function Server.get_user_servers() + return search( "server_owner_discord_id:"..get_user() ) +end + +return Server
diff -r 3e2cb946d511 -r af2b95854072 src/lib/Shared.luan --- a/src/lib/Shared.luan Mon Oct 30 14:59:45 2023 -0600 +++ b/src/lib/Shared.luan Mon Oct 30 21:32:42 2023 -0600 @@ -4,17 +4,14 @@ local Parsers = require "luan:Parsers.luan" local json_string = Parsers.json_string or error() local Http = require "luan:http/Http.luan" +local Utils = require "site:/lib/Utils.luan" +local get_user = Utils.get_user or error() local Shared = {} local started = Time.now() -local function get_user() - return Http.request.cookies.user -end -Shared.get_user = get_user - function Shared.head() %> <meta name="viewport" content="width=device-width, initial-scale=1">
diff -r 3e2cb946d511 -r af2b95854072 src/lib/Utils.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/Utils.luan Mon Oct 30 21:32:42 2023 -0600 @@ -0,0 +1,28 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local ipairs = Luan.ipairs or error() +local set_metatable = Luan.set_metatable or error() +local Http = require "luan:http/Http.luan" + + +local Utils = {} + +function Utils.get_user() + return Http.request.cookies.user +end + +local set_mt = {} +function set_mt.__index(table,key) + return false +end + +function Utils.list_to_set(list) + local set = {} + for _, v in ipairs(list) do + set[v] = true + end + set_metatable(set,set_mt) + return set +end + +return Utils
diff -r 3e2cb946d511 -r af2b95854072 src/servers.html.luan --- a/src/servers.html.luan Mon Oct 30 14:59:45 2023 -0600 +++ b/src/servers.html.luan Mon Oct 30 21:32:42 2023 -0600 @@ -1,14 +1,19 @@ local Luan = require "luan:Luan.luan" local error = Luan.error +local ipairs = Luan.ipairs or error() +local Html = require "luan:Html.luan" +local html_encode = Html.encode or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local Shared = require "site:/lib/Shared.luan" local head = Shared.head or error() local header = Shared.header or error() local footer = Shared.footer or error() +local Server = require "site:/lib/Server.luan" return function() + local servers = Server.get_user_servers() Io.stdout = Http.response.text_writer() %> <!doctype html> @@ -21,7 +26,18 @@ <% header() %> <h1>Your servers</h1> - +<% + for _, server in ipairs(servers) do +%> + <p> + <%=html_encode(server.name)%> + <% server.write_img_html() %> + <a href="<%=server.invite_url()%>">invite</a> + <a href="javascript:ajax('/delete_server.js?server=<%=server.id%>')">delete</a> + </p> +<% + end +%> <p><a href="choose_server.html">add server</a></p> <% footer() %>