annotate src/lib/Chat.luan @ 7:41d35b72c774

chat page
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Oct 2024 22:11:40 -0600
parents 2da10ece826f
children 563a5358f2ee
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()
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
4 local Table = require "luan:Table.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
5 local concat = Table.concat or error()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
6 local is_empty = Table.is_empty or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Time = require "luan:Time.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local time_now = Time.now or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Db = require "site:/lib/Db.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local run_in_transaction = Db.run_in_transaction or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Chat = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local function from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 doc.type == "chat" or error "wrong type"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 return Chat.new {
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 id = doc.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 user_ids = doc.chat_user_ids
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 updated = doc.chat_updated
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local function to_doc(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return {
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 type = "chat"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 id = chat.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 chat_user_ids = chat.user_ids or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 chat_updated = chat.updated or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 end
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.new(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 chat.updated = chat.updated or time_now()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 function chat.save()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 local doc = to_doc(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 Db.save(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 chat.id = doc.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 function chat.delete()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 run_in_transaction( function()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 local id = chat.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 -- Db.delete("chat_user_ids:"..id)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 Db.delete("id:"..id)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 end )
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
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
50 function chat.other_users_email(user)
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
51 local User = require "site:/lib/User.luan"
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
52 local get_user_by_id = User.get_by_id or error()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
53
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
54 local my_id = user.id
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
55 local t = {}
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
56 for _, user_id in ipairs(chat.user_ids) do
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
57 if user_id ~= my_id then
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
58 local other_user = get_user_by_id(user_id) or error()
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
59 t[#t+1] = other_user.email
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
60 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
61 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
62 return concat( t, ", " )
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
63 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
64
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 return chat
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 function Chat.search(query,sort,rows)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 rows = rows or 1000000
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 local chats = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 local docs = Db.search(query,1,rows,{sort=sort})
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 for _, doc in ipairs(docs) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 chats[#chats+1] = from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 return chats
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
78 function Chat.get_by_id(id)
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
79 local doc = Db.get_document("id:"..id)
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
80 return doc and doc.type=="chat" and from_doc(doc) or nil
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
81 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
82
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 return Chat