Mercurial Hosting > chat
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 3:2c63b10781e1 | 4:2da10ece826f |
|---|---|
| 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() | |
| 6 local Db = require "site:/lib/Db.luan" | |
| 7 local run_in_transaction = Db.run_in_transaction or error() | |
| 8 | |
| 9 | |
| 10 local Chat = {} | |
| 11 | |
| 12 local function from_doc(doc) | |
| 13 doc.type == "chat" or error "wrong type" | |
| 14 return Chat.new { | |
| 15 id = doc.id | |
| 16 user_ids = doc.chat_user_ids | |
| 17 updated = doc.chat_updated | |
| 18 } | |
| 19 end | |
| 20 | |
| 21 local function to_doc(chat) | |
| 22 return { | |
| 23 type = "chat" | |
| 24 id = chat.id | |
| 25 chat_user_ids = chat.user_ids or error() | |
| 26 chat_updated = chat.updated or error() | |
| 27 } | |
| 28 end | |
| 29 | |
| 30 function Chat.new(chat) | |
| 31 chat.updated = chat.updated or time_now() | |
| 32 | |
| 33 function chat.save() | |
| 34 local doc = to_doc(chat) | |
| 35 Db.save(doc) | |
| 36 chat.id = doc.id | |
| 37 end | |
| 38 | |
| 39 function chat.delete() | |
| 40 run_in_transaction( function() | |
| 41 local id = chat.id | |
| 42 -- Db.delete("chat_user_ids:"..id) | |
| 43 Db.delete("id:"..id) | |
| 44 end ) | |
| 45 end | |
| 46 | |
| 47 return chat | |
| 48 end | |
| 49 | |
| 50 function Chat.search(query,sort,rows) | |
| 51 rows = rows or 1000000 | |
| 52 local chats = {} | |
| 53 local docs = Db.search(query,1,rows,{sort=sort}) | |
| 54 for _, doc in ipairs(docs) do | |
| 55 chats[#chats+1] = from_doc(doc) | |
| 56 end | |
| 57 return chats | |
| 58 end | |
| 59 | |
| 60 return Chat |
