annotate src/lib/Chat.luan @ 83:a47036fd0158

group chat
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Mar 2025 13:56:59 -0600
parents bc9f452ee168
children
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()
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
6 local Table = require "luan:Table.luan"
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
7 local sort = Table.sort or error()
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
8 local concat = Table.concat or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
9 local Http = require "luan:http/Http.luan"
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Db = require "site:/lib/Db.luan"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local run_in_transaction = Db.run_in_transaction or error()
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
12 local Utils = require "site:/lib/Utils.luan"
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
13 local base_url = Utils.base_url or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local Chat = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local function from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 doc.type == "chat" or error "wrong type"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 return Chat.new {
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 id = doc.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 user_ids = doc.chat_user_ids
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 updated = doc.chat_updated
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
24 key = doc.chat_key
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local function to_doc(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return {
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 type = "chat"
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 id = chat.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 chat_user_ids = chat.user_ids or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 chat_updated = chat.updated or error()
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
34 chat_key = chat.key or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
38 local function get_chat_key(user_ids)
64
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
39 #user_ids > 1 or error "You can't chat with yourself"
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
40 sort(user_ids)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
41 return concat(user_ids,"~")
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
42 end
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
43
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 function Chat.new(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 chat.updated = chat.updated or time_now()
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
46 chat.key = chat.key or get_chat_key(chat.user_ids)
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 function chat.save()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 local doc = to_doc(chat)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 Db.save(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 chat.id = doc.id
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 function chat.delete()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 run_in_transaction( function()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 local id = chat.id
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 7
diff changeset
57 Db.delete("post_chat_id:"..id)
55
d21ae4920aac minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
58 Db.delete("chatuser_chat_id:"..id)
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 Db.delete("id:"..id)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 end )
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
63 function chat.http_push(message)
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
64 local url = base_url().."/chat/"..chat.id
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
65 Http.push(url,message)
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
66 end
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
67
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 64
diff changeset
68 function chat.other_user_ids(my_id)
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 64
diff changeset
69 local t = {nil}
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
70 for _, user_id in ipairs(chat.user_ids) do
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
71 if user_id ~= my_id then
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 64
diff changeset
72 t[#t+1] = user_id
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
73 end
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
74 end
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 64
diff changeset
75 return t
46
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
76 end
42b741a1d5c6 add username
Franklin Schmidt <fschmidt@gmail.com>
parents: 33
diff changeset
77
57
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
78 local function get_chatuser_key(user)
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
79 return chat.id.."~"..user.id
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
80 end
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
81
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
82 function chat.unread(user)
57
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
83 local doc = Db.get_document("chatuser_key:"..get_chatuser_key(user))
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
84 local read_date = doc and doc.read_date or 0
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
85 return Db.count("+post_chat_id:"..chat.id.." +post_date:["..read_date.." TO *]")
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
86 end
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
87
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
88 function chat.read(user)
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
89 run_in_transaction( function()
57
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
90 local chatuser_key = get_chatuser_key(user)
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
91 local doc = Db.get_document("chatuser_key:"..chatuser_key) or {
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
92 type = "chatuser"
57
c420f39eb474 chatuser_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 55
diff changeset
93 chatuser_key = chatuser_key
55
d21ae4920aac minor fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
94 chatuser_chat_id = chat.id or error()
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
95 }
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
96 doc.read_date = time_now()
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
97 Db.save(doc)
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
98 end )
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
99 end
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
100
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 return chat
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 function Chat.search(query,sort,rows)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 rows = rows or 1000000
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 local chats = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 local docs = Db.search(query,1,rows,{sort=sort})
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 for _, doc in ipairs(docs) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 chats[#chats+1] = from_doc(doc)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return chats
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113
7
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
114 function Chat.get_by_id(id)
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
115 local doc = Db.get_document("id:"..id)
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
116 return doc and doc.type=="chat" and from_doc(doc) or nil
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
117 end
41d35b72c774 chat page
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
118
58
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
119 function Chat.get_by_user_ids(user_ids)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
120 local key = get_chat_key(user_ids)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
121 local doc = Db.get_document("chat_key:"..key)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
122 return doc and from_doc(doc)
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
123 end
7b6691bd65c3 chat_key
Franklin Schmidt <fschmidt@gmail.com>
parents: 57
diff changeset
124
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 return Chat