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
|
110
|
41 function fns.notify(user_ids,mute_ids,user_name,post_content,chat_id)
|
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 send_mail {
|
|
51 To = user.notify_email
|
110
|
52 Subject = "Message from "..user_name
|
40
|
53 body = `%>
|
110
|
54 <%= post_content %>
|
40
|
55
|
110
|
56 <%= base_url %>/?chat=<%= chat_id %>
|
45
|
57
|
|
58 To unsubscribe:
|
|
59 <%= base_url %>/unsubscribe.html?user=<%=user_id%>&password=<%=user.password%>
|
40
|
60 <% `
|
|
61 }
|
110
|
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
|
40
|
79 users[user_id] = nil
|
|
80 end
|
|
81 end
|
|
82 end
|
|
83
|
|
84 return fns
|
|
85 end
|
|
86
|
|
87 local glob = Thread.global_callable("notify",init)
|
|
88
|
110
|
89 function Notify.notify(chat,post,user)
|
40
|
90 Thread.run(function()
|
110
|
91 local user_name = user.name or user.email
|
|
92 glob.notify(chat.user_ids,chat.mute_ids,user_name,post.content,chat.id)
|
40
|
93 end)
|
|
94 end
|
|
95
|
110
|
96 function Notify.active(user)
|
40
|
97 Thread.run(function()
|
110
|
98 glob.active(user.id)
|
40
|
99 if user.was_notified then
|
|
100 set_notified(user,false)
|
|
101 end
|
|
102 end)
|
|
103 end
|
|
104
|
110
|
105 Thread.schedule( glob.remove, { repeating_delay=Time.period{seconds=10} } )
|
40
|
106
|
|
107 return Notify
|