Mercurial Hosting > chat
comparison src/lib/Chat.luan @ 58:7b6691bd65c3
chat_key
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 04 Mar 2025 07:38:43 -0700 |
| parents | c420f39eb474 |
| children | afd5ab5b02a2 |
comparison
equal
deleted
inserted
replaced
| 57:c420f39eb474 | 58:7b6691bd65c3 |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | 2 local error = Luan.error |
| 3 local ipairs = Luan.ipairs or error() | 3 local ipairs = Luan.ipairs or error() |
| 4 local Time = require "luan:Time.luan" | 4 local Time = require "luan:Time.luan" |
| 5 local time_now = Time.now or error() | 5 local time_now = Time.now or error() |
| 6 local Table = require "luan:Table.luan" | |
| 7 local sort = Table.sort or error() | |
| 8 local concat = Table.concat or error() | |
| 6 local Http = require "luan:http/Http.luan" | 9 local Http = require "luan:http/Http.luan" |
| 7 local Db = require "site:/lib/Db.luan" | 10 local Db = require "site:/lib/Db.luan" |
| 8 local run_in_transaction = Db.run_in_transaction or error() | 11 local run_in_transaction = Db.run_in_transaction or error() |
| 9 local Utils = require "site:/lib/Utils.luan" | 12 local Utils = require "site:/lib/Utils.luan" |
| 10 local base_url = Utils.base_url or error() | 13 local base_url = Utils.base_url or error() |
| 16 doc.type == "chat" or error "wrong type" | 19 doc.type == "chat" or error "wrong type" |
| 17 return Chat.new { | 20 return Chat.new { |
| 18 id = doc.id | 21 id = doc.id |
| 19 user_ids = doc.chat_user_ids | 22 user_ids = doc.chat_user_ids |
| 20 updated = doc.chat_updated | 23 updated = doc.chat_updated |
| 24 key = doc.chat_key | |
| 21 } | 25 } |
| 22 end | 26 end |
| 23 | 27 |
| 24 local function to_doc(chat) | 28 local function to_doc(chat) |
| 25 return { | 29 return { |
| 26 type = "chat" | 30 type = "chat" |
| 27 id = chat.id | 31 id = chat.id |
| 28 chat_user_ids = chat.user_ids or error() | 32 chat_user_ids = chat.user_ids or error() |
| 29 chat_updated = chat.updated or error() | 33 chat_updated = chat.updated or error() |
| 34 chat_key = chat.key or error() | |
| 30 } | 35 } |
| 36 end | |
| 37 | |
| 38 local function get_chat_key(user_ids) | |
| 39 sort(user_ids) | |
| 40 return concat(user_ids,"~") | |
| 31 end | 41 end |
| 32 | 42 |
| 33 function Chat.new(chat) | 43 function Chat.new(chat) |
| 34 chat.updated = chat.updated or time_now() | 44 chat.updated = chat.updated or time_now() |
| 45 chat.key = chat.key or get_chat_key(chat.user_ids) | |
| 35 | 46 |
| 36 function chat.save() | 47 function chat.save() |
| 37 local doc = to_doc(chat) | 48 local doc = to_doc(chat) |
| 38 Db.save(doc) | 49 Db.save(doc) |
| 39 chat.id = doc.id | 50 chat.id = doc.id |
| 101 function Chat.get_by_id(id) | 112 function Chat.get_by_id(id) |
| 102 local doc = Db.get_document("id:"..id) | 113 local doc = Db.get_document("id:"..id) |
| 103 return doc and doc.type=="chat" and from_doc(doc) or nil | 114 return doc and doc.type=="chat" and from_doc(doc) or nil |
| 104 end | 115 end |
| 105 | 116 |
| 117 function Chat.get_by_user_ids(user_ids) | |
| 118 local key = get_chat_key(user_ids) | |
| 119 local doc = Db.get_document("chat_key:"..key) | |
| 120 return doc and from_doc(doc) | |
| 121 end | |
| 122 | |
| 106 return Chat | 123 return Chat |
