comparison src/lib/Chat.luan @ 104:46418395c860 default tip

add mute chat
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Jul 2025 00:49:11 -0600
parents a47036fd0158
children
comparison
equal deleted inserted replaced
103:3ea9783cee39 104:46418395c860
9 local Http = require "luan:http/Http.luan" 9 local Http = require "luan:http/Http.luan"
10 local Db = require "site:/lib/Db.luan" 10 local Db = require "site:/lib/Db.luan"
11 local run_in_transaction = Db.run_in_transaction or error() 11 local run_in_transaction = Db.run_in_transaction or error()
12 local Utils = require "site:/lib/Utils.luan" 12 local Utils = require "site:/lib/Utils.luan"
13 local base_url = Utils.base_url or error() 13 local base_url = Utils.base_url or error()
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()
14 17
15 18
16 local Chat = {} 19 local Chat = {}
17 20
18 local function from_doc(doc) 21 local function from_doc(doc)
20 return Chat.new { 23 return Chat.new {
21 id = doc.id 24 id = doc.id
22 user_ids = doc.chat_user_ids 25 user_ids = doc.chat_user_ids
23 updated = doc.chat_updated 26 updated = doc.chat_updated
24 key = doc.chat_key 27 key = doc.chat_key
28 mute_ids = list_to_set(to_list(doc.mute_ids))
25 } 29 }
26 end 30 end
27 31
28 local function to_doc(chat) 32 local function to_doc(chat)
29 return { 33 return {
30 type = "chat" 34 type = "chat"
31 id = chat.id 35 id = chat.id
32 chat_user_ids = chat.user_ids or error() 36 chat_user_ids = chat.user_ids or error()
33 chat_updated = chat.updated or error() 37 chat_updated = chat.updated or error()
34 chat_key = chat.key or error() 38 chat_key = chat.key or error()
39 mute_ids = set_to_list(chat.mute_ids)
35 } 40 }
36 end 41 end
37 42
38 local function get_chat_key(user_ids) 43 local function get_chat_key(user_ids)
39 #user_ids > 1 or error "You can't chat with yourself" 44 #user_ids > 1 or error "You can't chat with yourself"
42 end 47 end
43 48
44 function Chat.new(chat) 49 function Chat.new(chat)
45 chat.updated = chat.updated or time_now() 50 chat.updated = chat.updated or time_now()
46 chat.key = chat.key or get_chat_key(chat.user_ids) 51 chat.key = chat.key or get_chat_key(chat.user_ids)
52 chat.mute_ids = chat.mute_ids or list_to_set{}
47 53
48 function chat.save() 54 function chat.save()
49 local doc = to_doc(chat) 55 local doc = to_doc(chat)
50 Db.save(doc) 56 Db.save(doc)
51 chat.id = doc.id 57 chat.id = doc.id
58 end
59
60 function chat.reload()
61 return Chat.get_by_id(chat.id) or error(chat.id)
52 end 62 end
53 63
54 function chat.delete() 64 function chat.delete()
55 run_in_transaction( function() 65 run_in_transaction( function()
56 local id = chat.id 66 local id = chat.id