Mercurial Hosting > disearch
view src/lib/Server.luan @ 16:552d6f944acb
add bump
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 01 Nov 2023 17:34:15 -0600 |
parents | 80105b716a62 |
children | 74277b56ac3c |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local Number = require "luan:Number.luan" local long = Number.long 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 discord_id = doc.server_discord_id owner_discord_id = doc.server_owner_discord_id bumped = doc.server_bumped name = doc.name icon = doc.icon invite = doc.invite } end local function to_doc(server) return { type = "server" id = server.id server_discord_id = server.discord_id or error() server_owner_discord_id = server.owner_discord_id or error() server_bumped = long(server.bumped) 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 function Server.get_by_discord_id(discord_id) local doc = Db.get_document("server_discord_id:"..discord_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 function Server.bumped_sort(s1,s2) return s1.bumped > s2.bumped end return Server