annotate src/lib/Chat.luan @ 4:2da10ece826f

add Chat
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 27 Oct 2024 20:39:18 -0600
parents
children 41d35b72c774
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local Time = require "luan:Time.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local time_now = Time.now or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Db = require "site:/lib/Db.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local run_in_transaction = Db.run_in_transaction or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Chat = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local function from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 doc.type == "chat" or error "wrong type"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 return Chat.new {
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 id = doc.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 user_ids = doc.chat_user_ids
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 updated = doc.chat_updated
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local function to_doc(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return {
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 type = "chat"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 id = chat.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 chat_user_ids = chat.user_ids or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 chat_updated = chat.updated or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 function Chat.new(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 chat.updated = chat.updated or time_now()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 function chat.save()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local doc = to_doc(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 Db.save(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 chat.id = doc.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 function chat.delete()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 run_in_transaction( function()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 local id = chat.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 -- Db.delete("chat_user_ids:"..id)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 Db.delete("id:"..id)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 end )
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return chat
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 function Chat.search(query,sort,rows)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 rows = rows or 1000000
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 local chats = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 local docs = Db.search(query,1,rows,{sort=sort})
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 for _, doc in ipairs(docs) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 chats[#chats+1] = from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 return chats
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 return Chat