comparison src/lib/Notify.luan @ 110:d2637760cd00

better notification
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 15 Sep 2025 13:29:10 -0600
parents 46418395c860
children
comparison
equal deleted inserted replaced
109:b86bb25fb416 110:d2637760cd00
36 36
37 local function init() 37 local function init()
38 local users = {} 38 local users = {}
39 local fns = {} 39 local fns = {}
40 40
41 function fns.add(user_ids,mute_ids) 41 function fns.notify(user_ids,mute_ids,user_name,post_content,chat_id)
42 local now = time_now() 42 local now = time_now()
43 for _, user_id in ipairs(user_ids) do 43 for _, user_id in ipairs(user_ids) do
44 local user = get_user_by_id(user_id) 44 local user = get_user_by_id(user_id)
45 if users[user_id] == nil \ 45 if users[user_id] == nil \
46 and user.notify_email ~= nil \ 46 and user.notify_email ~= nil \
47 and not mute_ids[user_id] \ 47 and not mute_ids[user_id] \
48 and (user.multi_notify or not user.was_notified) \ 48 and (user.multi_notify or not user.was_notified) \
49 then 49 then
50 users[user_id] = now
51 -- logger.info("add "..user_id)
52 end
53 end
54 end
55
56 function fns.remove(user_id)
57 users[user_id] = nil
58 -- logger.info("remove "..user_id)
59 end
60
61 function fns.notify()
62 -- logger.info("notify")
63 local now = time_now()
64 for user_id, when in pairs(shallow_copy(users)) do
65 if now - when > wait then
66 local user = get_user_by_id(user_id)
67 local name = user.name
68 local you = name and name..", you" or "You"
69 -- logger.info("notify "..user.notify_email.." "..user_id)
70 send_mail { 50 send_mail {
71 To = user.notify_email 51 To = user.notify_email
72 Subject = "New Messages" 52 Subject = "Message from "..user_name
73 body = `%> 53 body = `%>
74 <%=you%> have received new messages. 54 <%= post_content %>
75 55
76 <%= base_url %>/ 56 <%= base_url %>/?chat=<%= chat_id %>
77 57
78 To unsubscribe: 58 To unsubscribe:
79 <%= base_url %>/unsubscribe.html?user=<%=user_id%>&password=<%=user.password%> 59 <%= base_url %>/unsubscribe.html?user=<%=user_id%>&password=<%=user.password%>
80 <% ` 60 <% `
81 } 61 }
62 if not user.multi_notify then
63 set_notified(user,true)
64 end
65 end
66 end
67 end
68
69 function fns.active(user_id)
70 users[user_id] = time_now()
71 -- logger.info("active "..user_id)
72 end
73
74 function fns.remove()
75 -- logger.info("remove")
76 local now = time_now()
77 for user_id, when in pairs(shallow_copy(users)) do
78 if now - when > wait then
82 users[user_id] = nil 79 users[user_id] = nil
83 set_notified(user,true)
84 end 80 end
85 end 81 end
86 end 82 end
87 83
88 return fns 84 return fns
89 end 85 end
90 86
91 local glob = Thread.global_callable("notify",init) 87 local glob = Thread.global_callable("notify",init)
92 88
93 function Notify.add(chat) 89 function Notify.notify(chat,post,user)
94 Thread.run(function() 90 Thread.run(function()
95 glob.add(chat.user_ids,chat.mute_ids) 91 local user_name = user.name or user.email
92 glob.notify(chat.user_ids,chat.mute_ids,user_name,post.content,chat.id)
96 end) 93 end)
97 end 94 end
98 95
99 function Notify.remove(user) 96 function Notify.active(user)
100 Thread.run(function() 97 Thread.run(function()
101 glob.remove(user.id) 98 glob.active(user.id)
102 if user.was_notified then 99 if user.was_notified then
103 set_notified(user,false) 100 set_notified(user,false)
104 end 101 end
105 end) 102 end)
106 end 103 end
107 104
108 Thread.schedule( glob.notify, { repeating_delay=Time.period{seconds=10} } ) 105 Thread.schedule( glob.remove, { repeating_delay=Time.period{seconds=10} } )
109 106
110 return Notify 107 return Notify