4
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local Number = require "luan:Number.luan"
|
|
5 local long = Number.long or error()
|
|
6 local Time = require "luan:Time.luan"
|
|
7 local time_now = Time.now or error()
|
|
8 local Html = require "luan:Html.luan"
|
|
9 local html_encode = Html.encode or error()
|
|
10 local Db = require "site:/lib/Db.luan"
|
|
11
|
|
12
|
|
13 local Chat = {}
|
|
14
|
|
15 local function from_doc(doc)
|
|
16 doc.type == "chat" or error "wrong type"
|
|
17 return Chat.new {
|
|
18 id = doc.id
|
|
19 user_id = doc.chat_user_id
|
|
20 updated = doc.chat_updated
|
|
21 name = doc.name
|
|
22 ai_key = doc.ai_key
|
|
23 }
|
|
24 end
|
|
25
|
|
26 local function to_doc(chat)
|
|
27 return {
|
|
28 type = "chat"
|
|
29 id = chat.id
|
|
30 chat_user_id = long(chat.user_id)
|
|
31 chat_updated = long(chat.updated)
|
|
32 name = chat.name or error()
|
|
33 ai_key = chat.ai_key -- or error()
|
|
34 }
|
|
35 end
|
|
36
|
|
37 function Chat.new(chat)
|
|
38 chat.updated = chat.updated or time_now()
|
|
39
|
|
40 function chat.save()
|
|
41 local doc = to_doc(chat)
|
|
42 Db.save(doc)
|
|
43 chat.id = doc.id
|
|
44 end
|
|
45
|
|
46 function chat.delete()
|
|
47 Db.delete("id:"..chat.id)
|
|
48 end
|
|
49
|
|
50 function chat.name_html()
|
|
51 return html_encode(chat.name)
|
|
52 end
|
|
53
|
|
54 return chat
|
|
55 end
|
|
56
|
|
57 function Chat.search(query,sort,rows)
|
|
58 rows = rows or 1000000
|
|
59 local chats = {}
|
|
60 local docs = Db.search(query,1,rows,{sort=sort})
|
|
61 for _, doc in ipairs(docs) do
|
|
62 chats[#chats+1] = from_doc(doc)
|
|
63 end
|
|
64 return chats
|
|
65 end
|
|
66
|
|
67 function Chat.get_by_id(id)
|
|
68 local doc = Db.get_document("id:"..id)
|
|
69 return doc and doc.type=="chat" and from_doc(doc) or nil
|
|
70 end
|
|
71
|
|
72 return Chat
|