liquid_feedback_frontend

view model/initiative_for_notification.lua @ 1304:0bae319805ff

Added tag v3.2.1 for changeset 0160d9a01d0a
author jbe
date Fri May 06 09:44:16 2016 +0200 (2016-05-06)
parents fb62b2c24fb5
children fd50bdd36a4b
line source
1 InitiativeForNotification = mondelefant.new_class()
2 InitiativeForNotification.table = 'initiative_for_notification'
4 InitiativeForNotification:add_reference{
5 mode = 'm1',
6 to = "Initiative",
7 this_key = 'initiative_id',
8 that_key = 'id',
9 ref = 'initiative',
10 }
13 function InitiativeForNotification:notify_member_id(member_id)
15 db:query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ")
17 local member = Member:by_id(member_id)
19 locale.set{ lang = member.lang or config.default_lang }
21 local selector = db:new_selector()
22 :add_field("*")
23 :from({ "get_initiatives_for_notification(?)", member_id }, "initiative_for_notification")
24 :join("initiative", nil, "initiative.id = initiative_for_notification.initiative_id")
25 :join("issue", nil, "issue.id = initiative.issue_id")
26 :join("member", nil, "member.id = initiative_for_notification.recipient_id")
27 :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.area_id)")
28 :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.id)")
30 selector._class = self
32 local initiatives_for_notification = selector:exec()
34 db:query("COMMIT")
36 if not member.notify_email then
37 return
38 end
40 if #initiatives_for_notification < 1 then
41 return
42 end
44 io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n")
46 local initiatives = initiatives_for_notification:load("initiative")
47 local issues = initiatives:load("issue")
48 issues:load("area")
49 issues:load("policy")
51 local last_area_id
52 local last_issue_id
54 local draft_count = 0
55 local suggestion_count = 0
57 local draft_initiative
58 local suggestion_initiative
60 local ms = {}
62 for i, entry in ipairs(initiatives_for_notification) do
63 local initiative = entry.initiative
64 local issue = initiative.issue
65 local area = issue.area
66 local policy = issue.policy
68 local m = {}
69 if last_area_id ~= area.id then
70 m[#m+1] = "*** " .. area.name .. " ***"
71 m[#m+1] = ""
72 last_area_id = area.id
73 end
74 if last_issue_id ~= issue.id then
75 local state_time_text
76 if string.sub(issue.state_time_left, 1, 1) == "-" then
77 state_time_text = _"Phase ends soon"
78 else
79 state_time_text = _( "#{interval} left", {
80 interval = format.interval_text(issue.state_time_left)
81 })
82 end
83 m[#m+1] = "---"
84 m[#m+1] = policy.name .. " #" .. issue.id .. " - " .. issue.state_name .. " - " .. state_time_text
85 m[#m+1] = ""
86 last_issue_id = issue.id
87 end
88 m[#m+1] = initiative.display_name
89 local source
90 if entry.supported then
91 source = _"has my support"
92 if entry.new_draft then
93 source = source .. ", " .. _"draft updated"
94 draft_count = draft_count + 1
95 draft_initiative = initiative
96 end
97 if entry.new_suggestion_count then
98 if entry.new_suggestion_count == 1 then
99 source = source .. ", " .. _"new suggestion added"
100 elseif entry.new_suggestion_count > 1 then
101 source = source .. ", " .. _("#{count} suggestions added", { count = entry.new_suggestion_count })
102 end
103 suggestion_count = suggestion_count + entry.new_suggestion_count
104 if suggestion_initiative and suggestion_initiative.id ~= initiative.id then
105 suggestion_initiative = false
106 elseif suggestion_initiative ~= false then
107 suggestion_initiative = initiative
108 end
109 end
110 elseif entry.featured then
111 source = _"featured"
112 end
113 if entry.leading then
114 source = source and source .. ", " or ""
115 source = source .. "currently leading"
116 end
117 m[#m+1] = "(" .. source .. ")"
118 m[#m+1] = ""
119 if not initiatives_for_notification[i+1] or initiatives_for_notification[i+1].issue_id ~= issue.id then
120 m[#m+1] = _("more: #{url}", { url = request.get_absolute_baseurl() .. "issue/show/" .. issue.id .. ".html" })
121 m[#m+1] = ""
122 end
123 ms[#ms+1] = table.concat(m, "\n")
124 end
126 local info = {}
128 if draft_count > 0 then
129 if draft_count == 1 then
130 info[#info+1] = _"draft updated for " .. draft_initiative.display_name
131 else
132 info[#info+1] = _("drafts of #{draft_count} initiatives updated", { draft_count = draft_count })
133 end
134 end
136 if suggestion_count > 0 then
137 if suggestion_count == 1 then
138 info[#info+1] = _"new suggestion for " .. suggestion_initiative.display_name
139 elseif suggestion_initiative then
140 info[#info+1] = _("#{count} suggestions added for #{initiative}", { count = suggestion_count, suggestion_initiative.display_name })
141 else
142 info[#info+1] = _("#{count} suggestions added", { count = suggestion_count })
143 end
144 end
146 if draft_count == 0 and suggestion_count == 0 then
147 info[#info+1] = _"featured initiatives"
148 end
150 local subject = _("Digest #{id}: #{info}", {
151 id = member.notification_counter, info = table.concat(info, ", ")
152 })
154 local template = config.notification_digest_template
156 local message = _(template, {
157 name = member.name,
158 digest = table.concat(ms, "\n")
159 })
161 local success = net.send_mail{
162 envelope_from = config.mail_envelope_from,
163 from = config.mail_from,
164 reply_to = config.mail_reply_to,
165 to = member.notify_email,
166 subject = subject,
167 content_type = "text/plain; charset=UTF-8",
168 content = message
169 }
171 end
173 function InitiativeForNotification:notify_next_member()
174 local scheduled_notification_to_send = ScheduledNotificationToSend:get_next()
175 if not scheduled_notification_to_send then
176 return false
177 end
178 InitiativeForNotification:notify_member_id(scheduled_notification_to_send.recipient_id)
179 return true
180 end

Impressum / About Us