13
|
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
|
14
|
15 discord_id = doc.server_discord_id
|
13
|
16 owner_discord_id = doc.server_owner_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
|
14
|
27 server_discord_id = server.discord_id or error()
|
13
|
28 server_owner_discord_id = server.owner_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
|
14
|
69 function Server.get_by_discord_id(discord_id)
|
|
70 local doc = Db.get_document("server_discord_id:"..discord_id)
|
|
71 return doc and from_doc(doc)
|
|
72 end
|
|
73
|
13
|
74 local function search(query,sort,rows)
|
|
75 rows = rows or 1000000
|
|
76 local docs = Db.search(query,1,rows,{sort=sort})
|
|
77 local servers = {}
|
|
78 for _, doc in ipairs(docs) do
|
|
79 local server = from_doc(doc)
|
|
80 servers[#servers+1] = server
|
|
81 end
|
|
82 return servers
|
|
83 end
|
|
84
|
|
85 function Server.get_user_servers()
|
|
86 return search( "server_owner_discord_id:"..get_user() )
|
|
87 end
|
|
88
|
|
89 return Server
|