13
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
16
|
4 local Number = require "luan:Number.luan"
|
|
5 local long = Number.long or error()
|
13
|
6 local Db = require "site:/lib/Db.luan"
|
|
7 local Utils = require "site:/lib/Utils.luan"
|
|
8 local get_user = Utils.get_user or error()
|
|
9
|
|
10
|
|
11 local Server = {}
|
|
12
|
|
13 local function from_doc(doc)
|
|
14 doc.type == "server" or error "wrong type"
|
|
15 return Server.new {
|
|
16 id = doc.id
|
14
|
17 discord_id = doc.server_discord_id
|
13
|
18 owner_discord_id = doc.server_owner_discord_id
|
16
|
19 bumped = doc.server_bumped
|
13
|
20 name = doc.name
|
|
21 icon = doc.icon
|
|
22 invite = doc.invite
|
|
23 }
|
|
24 end
|
|
25
|
|
26 local function to_doc(server)
|
|
27 return {
|
|
28 type = "server"
|
|
29 id = server.id
|
14
|
30 server_discord_id = server.discord_id or error()
|
13
|
31 server_owner_discord_id = server.owner_discord_id or error()
|
16
|
32 server_bumped = long(server.bumped)
|
13
|
33 name = server.name or error()
|
|
34 icon = server.icon or error()
|
|
35 invite = server.invite or error()
|
|
36 }
|
|
37 end
|
|
38
|
|
39 function Server.new(server)
|
|
40
|
|
41 function server.save()
|
|
42 local doc = to_doc(server)
|
|
43 Db.save(doc)
|
|
44 server.id = doc.id
|
|
45 end
|
|
46
|
|
47 function server.reload()
|
|
48 return Server.get_by_id(server.id) or error(server.id)
|
|
49 end
|
|
50
|
|
51 function server.delete()
|
|
52 Db.run_in_transaction( function()
|
|
53 Db.delete("id:"..server.id)
|
|
54 end )
|
|
55 end
|
|
56
|
|
57 function server.write_img_html()
|
|
58 %><img icon src="https://cdn.discordapp.com/icons/<%=server.discord_id%>/<%=server.icon%>.png"><%
|
|
59 end
|
|
60
|
|
61 function server.invite_url()
|
|
62 return "https://discord.gg/"..server.invite
|
|
63 end
|
|
64
|
|
65 return server
|
|
66 end
|
|
67
|
|
68 function Server.get_by_id(id)
|
|
69 local doc = Db.get_document("id:"..id)
|
|
70 return doc and from_doc(doc)
|
|
71 end
|
|
72
|
14
|
73 function Server.get_by_discord_id(discord_id)
|
|
74 local doc = Db.get_document("server_discord_id:"..discord_id)
|
|
75 return doc and from_doc(doc)
|
|
76 end
|
|
77
|
13
|
78 local function search(query,sort,rows)
|
|
79 rows = rows or 1000000
|
|
80 local docs = Db.search(query,1,rows,{sort=sort})
|
|
81 local servers = {}
|
|
82 for _, doc in ipairs(docs) do
|
|
83 local server = from_doc(doc)
|
|
84 servers[#servers+1] = server
|
|
85 end
|
|
86 return servers
|
|
87 end
|
17
|
88 Server.search = search
|
13
|
89
|
|
90 function Server.get_user_servers()
|
|
91 return search( "server_owner_discord_id:"..get_user() )
|
|
92 end
|
|
93
|
16
|
94 function Server.bumped_sort(s1,s2)
|
|
95 return s1.bumped > s2.bumped
|
|
96 end
|
|
97
|
13
|
98 return Server
|