annotate src/lib/Server.luan @ 20:914a37651a38

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