Mercurial Hosting > chat
view src/lib/Chat.luan @ 53:9298b04607ae
add unread
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 03 Mar 2025 19:39:30 -0700 |
parents | 42b741a1d5c6 |
children | d21ae4920aac |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local Time = require "luan:Time.luan" local time_now = Time.now or error() local Http = require "luan:http/Http.luan" local Db = require "site:/lib/Db.luan" local run_in_transaction = Db.run_in_transaction or error() local Utils = require "site:/lib/Utils.luan" local base_url = Utils.base_url or error() local Chat = {} local function from_doc(doc) doc.type == "chat" or error "wrong type" return Chat.new { id = doc.id user_ids = doc.chat_user_ids updated = doc.chat_updated } end local function to_doc(chat) return { type = "chat" id = chat.id chat_user_ids = chat.user_ids or error() chat_updated = chat.updated or error() } end function Chat.new(chat) chat.updated = chat.updated or time_now() function chat.save() local doc = to_doc(chat) Db.save(doc) chat.id = doc.id end function chat.delete() run_in_transaction( function() local id = chat.id Db.delete("post_chat_id:"..id) Db.delete("id:"..id) end ) end function chat.http_push(message) local url = base_url().."/chat/"..chat.id Http.push(url,message) end function chat.other_user_id(my_id) for _, user_id in ipairs(chat.user_ids) do if user_id ~= my_id then return user_id end end error() end local function get_chatuser_id(user) return chat.id.."~"..user.id end function chat.unread(user) local doc = Db.get_document("chatuser_id:"..get_chatuser_id(user)) local read_date = doc and doc.read_date or 0 return Db.count("+post_chat_id:"..chat.id.." +post_date:["..read_date.." TO *]") end function chat.read(user) run_in_transaction( function() local chatuser_id = get_chatuser_id(user) local doc = Db.get_document("chatuser_id:"..chatuser_id) or { type = "chatuser" chatuser_id = chatuser_id } doc.read_date = time_now() Db.save(doc) end ) end return chat end function Chat.search(query,sort,rows) rows = rows or 1000000 local chats = {} local docs = Db.search(query,1,rows,{sort=sort}) for _, doc in ipairs(docs) do chats[#chats+1] = from_doc(doc) end return chats end function Chat.get_by_id(id) local doc = Db.get_document("id:"..id) return doc and doc.type=="chat" and from_doc(doc) or nil end return Chat