Mercurial Hosting > disearch
comparison src/lib/Server.luan @ 13:af2b95854072
add server records
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 30 Oct 2023 21:32:42 -0600 |
parents | |
children | 80105b716a62 |
comparison
equal
deleted
inserted
replaced
12:3e2cb946d511 | 13:af2b95854072 |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 local ipairs = Luan.ipairs or error() | |
4 local Db = require "site:/lib/Db.luan" | |
5 local Utils = require "site:/lib/Utils.luan" | |
6 local get_user = Utils.get_user or error() | |
7 | |
8 | |
9 local Server = {} | |
10 | |
11 local function from_doc(doc) | |
12 doc.type == "server" or error "wrong type" | |
13 return Server.new { | |
14 id = doc.id | |
15 owner_discord_id = doc.server_owner_discord_id | |
16 discord_id = doc.discord_id | |
17 name = doc.name | |
18 icon = doc.icon | |
19 invite = doc.invite | |
20 } | |
21 end | |
22 | |
23 local function to_doc(server) | |
24 return { | |
25 type = "server" | |
26 id = server.id | |
27 server_owner_discord_id = server.owner_discord_id or error() | |
28 discord_id = server.discord_id or error() | |
29 name = server.name or error() | |
30 icon = server.icon or error() | |
31 invite = server.invite or error() | |
32 } | |
33 end | |
34 | |
35 function Server.new(server) | |
36 | |
37 function server.save() | |
38 local doc = to_doc(server) | |
39 Db.save(doc) | |
40 server.id = doc.id | |
41 end | |
42 | |
43 function server.reload() | |
44 return Server.get_by_id(server.id) or error(server.id) | |
45 end | |
46 | |
47 function server.delete() | |
48 Db.run_in_transaction( function() | |
49 Db.delete("id:"..server.id) | |
50 end ) | |
51 end | |
52 | |
53 function server.write_img_html() | |
54 %><img icon src="https://cdn.discordapp.com/icons/<%=server.discord_id%>/<%=server.icon%>.png"><% | |
55 end | |
56 | |
57 function server.invite_url() | |
58 return "https://discord.gg/"..server.invite | |
59 end | |
60 | |
61 return server | |
62 end | |
63 | |
64 function Server.get_by_id(id) | |
65 local doc = Db.get_document("id:"..id) | |
66 return doc and from_doc(doc) | |
67 end | |
68 | |
69 local function search(query,sort,rows) | |
70 rows = rows or 1000000 | |
71 local docs = Db.search(query,1,rows,{sort=sort}) | |
72 local servers = {} | |
73 for _, doc in ipairs(docs) do | |
74 local server = from_doc(doc) | |
75 servers[#servers+1] = server | |
76 end | |
77 return servers | |
78 end | |
79 | |
80 function Server.get_user_servers() | |
81 return search( "server_owner_discord_id:"..get_user() ) | |
82 end | |
83 | |
84 return Server |