4
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local Time = require "luan:Time.luan"
|
|
5 local time_now = Time.now or error()
|
58
|
6 local Table = require "luan:Table.luan"
|
|
7 local sort = Table.sort or error()
|
|
8 local concat = Table.concat or error()
|
12
|
9 local Http = require "luan:http/Http.luan"
|
4
|
10 local Db = require "site:/lib/Db.luan"
|
|
11 local run_in_transaction = Db.run_in_transaction or error()
|
12
|
12 local Utils = require "site:/lib/Utils.luan"
|
|
13 local base_url = Utils.base_url or error()
|
104
|
14 local set_to_list = Utils.set_to_list or error()
|
|
15 local list_to_set = Utils.list_to_set or error()
|
|
16 local to_list = Utils.to_list or error()
|
4
|
17
|
|
18
|
|
19 local Chat = {}
|
|
20
|
|
21 local function from_doc(doc)
|
|
22 doc.type == "chat" or error "wrong type"
|
|
23 return Chat.new {
|
|
24 id = doc.id
|
|
25 user_ids = doc.chat_user_ids
|
|
26 updated = doc.chat_updated
|
58
|
27 key = doc.chat_key
|
104
|
28 mute_ids = list_to_set(to_list(doc.mute_ids))
|
4
|
29 }
|
|
30 end
|
|
31
|
|
32 local function to_doc(chat)
|
|
33 return {
|
|
34 type = "chat"
|
|
35 id = chat.id
|
|
36 chat_user_ids = chat.user_ids or error()
|
|
37 chat_updated = chat.updated or error()
|
58
|
38 chat_key = chat.key or error()
|
104
|
39 mute_ids = set_to_list(chat.mute_ids)
|
4
|
40 }
|
|
41 end
|
|
42
|
58
|
43 local function get_chat_key(user_ids)
|
64
|
44 #user_ids > 1 or error "You can't chat with yourself"
|
58
|
45 sort(user_ids)
|
|
46 return concat(user_ids,"~")
|
|
47 end
|
|
48
|
4
|
49 function Chat.new(chat)
|
|
50 chat.updated = chat.updated or time_now()
|
58
|
51 chat.key = chat.key or get_chat_key(chat.user_ids)
|
104
|
52 chat.mute_ids = chat.mute_ids or list_to_set{}
|
4
|
53
|
|
54 function chat.save()
|
|
55 local doc = to_doc(chat)
|
|
56 Db.save(doc)
|
|
57 chat.id = doc.id
|
|
58 end
|
|
59
|
104
|
60 function chat.reload()
|
|
61 return Chat.get_by_id(chat.id) or error(chat.id)
|
|
62 end
|
|
63
|
4
|
64 function chat.delete()
|
|
65 run_in_transaction( function()
|
|
66 local id = chat.id
|
11
|
67 Db.delete("post_chat_id:"..id)
|
55
|
68 Db.delete("chatuser_chat_id:"..id)
|
4
|
69 Db.delete("id:"..id)
|
|
70 end )
|
|
71 end
|
|
72
|
12
|
73 function chat.http_push(message)
|
|
74 local url = base_url().."/chat/"..chat.id
|
|
75 Http.push(url,message)
|
|
76 end
|
|
77
|
83
|
78 function chat.other_user_ids(my_id)
|
|
79 local t = {nil}
|
46
|
80 for _, user_id in ipairs(chat.user_ids) do
|
|
81 if user_id ~= my_id then
|
83
|
82 t[#t+1] = user_id
|
46
|
83 end
|
|
84 end
|
83
|
85 return t
|
46
|
86 end
|
|
87
|
57
|
88 local function get_chatuser_key(user)
|
53
|
89 return chat.id.."~"..user.id
|
|
90 end
|
|
91
|
|
92 function chat.unread(user)
|
57
|
93 local doc = Db.get_document("chatuser_key:"..get_chatuser_key(user))
|
53
|
94 local read_date = doc and doc.read_date or 0
|
|
95 return Db.count("+post_chat_id:"..chat.id.." +post_date:["..read_date.." TO *]")
|
|
96 end
|
|
97
|
|
98 function chat.read(user)
|
|
99 run_in_transaction( function()
|
57
|
100 local chatuser_key = get_chatuser_key(user)
|
|
101 local doc = Db.get_document("chatuser_key:"..chatuser_key) or {
|
53
|
102 type = "chatuser"
|
57
|
103 chatuser_key = chatuser_key
|
55
|
104 chatuser_chat_id = chat.id or error()
|
53
|
105 }
|
|
106 doc.read_date = time_now()
|
|
107 Db.save(doc)
|
|
108 end )
|
|
109 end
|
|
110
|
4
|
111 return chat
|
|
112 end
|
|
113
|
|
114 function Chat.search(query,sort,rows)
|
|
115 rows = rows or 1000000
|
|
116 local chats = {}
|
|
117 local docs = Db.search(query,1,rows,{sort=sort})
|
|
118 for _, doc in ipairs(docs) do
|
|
119 chats[#chats+1] = from_doc(doc)
|
|
120 end
|
|
121 return chats
|
|
122 end
|
|
123
|
7
|
124 function Chat.get_by_id(id)
|
|
125 local doc = Db.get_document("id:"..id)
|
|
126 return doc and doc.type=="chat" and from_doc(doc) or nil
|
|
127 end
|
|
128
|
58
|
129 function Chat.get_by_user_ids(user_ids)
|
|
130 local key = get_chat_key(user_ids)
|
|
131 local doc = Db.get_document("chat_key:"..key)
|
|
132 return doc and from_doc(doc)
|
|
133 end
|
|
134
|
4
|
135 return Chat
|