annotate src/lib/Notify.luan @ 87:246a792adedf

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 12 Mar 2025 14:20:10 -0600
parents e138343b2c76
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local pairs = Luan.pairs or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local stringify = Luan.stringify or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Time = require "luan:Time.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local time_now = Time.now or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Thread = require "luan:Thread.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Chat = require "site:/lib/Chat.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local User = require "site:/lib/User.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local get_user_by_id = User.get_by_id or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Db = require "site:/lib/Db.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local run_in_transaction = Db.run_in_transaction or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Shared = require "site:/lib/Shared.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local send_mail = Shared.send_mail or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Utils = require "site:/lib/Utils.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local shallow_copy = Utils.shallow_copy or error()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local Logging = require "luan:logging/Logging.luan"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local logger = Logging.logger "Notify"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local Notify = {}
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
25 local base_url = Http.domain and "https://"..Http.domain or "http://localhost:8080"
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local wait = Time.period{seconds=10}
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 local function set_notified(user,was_notified)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 run_in_transaction( function()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 user = user.reload()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 user.was_notified = was_notified
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 user.save()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 end )
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 local function init()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 local users = {}
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 local fns = {}
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 function fns.add(user_ids)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 local now = time_now()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 for _, user_id in ipairs(user_ids) do
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 local user = get_user_by_id(user_id)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 if users[user_id] == nil and user.notify_email ~= nil and (user.multi_notify or not user.was_notified) then
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 users[user_id] = now
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
47 -- logger.info("add "..user_id)
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 function fns.remove(user_id)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 users[user_id] = nil
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
54 -- logger.info("remove "..user_id)
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function fns.notify()
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
58 -- logger.info("notify")
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 local now = time_now()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 for user_id, when in pairs(shallow_copy(users)) do
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 if now - when > wait then
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 local user = get_user_by_id(user_id)
87
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
63 local name = user.name
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
64 local you = name and name..", you" or "You"
41
818697418dbe add voice link
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
65 -- logger.info("notify "..user.notify_email.." "..user_id)
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 send_mail {
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 To = user.notify_email
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 Subject = "New Messages"
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 body = `%>
87
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
70 <%=you%> have received new messages.
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
45
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
72 <%= base_url %>/
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
73
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
74 To unsubscribe:
e138343b2c76 unsubscribe and more
Franklin Schmidt <fschmidt@gmail.com>
parents: 41
diff changeset
75 <%= base_url %>/unsubscribe.html?user=<%=user_id%>&password=<%=user.password%>
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 <% `
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 users[user_id] = nil
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 set_notified(user,true)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 return fns
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 local glob = Thread.global_callable("notify",init)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 function Notify.add(chat)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 Thread.run(function()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 glob.add(chat.user_ids)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 end)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 function Notify.remove(user)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 Thread.run(function()
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 glob.remove(user.id)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 if user.was_notified then
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 set_notified(user,false)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 end)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 Thread.schedule( glob.notify, { repeating_delay=Time.period{seconds=10} } )
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return Notify