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