liquid_feedback_frontend
diff model/initiative_for_notification.lua @ 1248:c0fd12b97d65
Changes on notifications system, newsletter support added
author | bsw |
---|---|
date | Tue Apr 05 20:40:37 2016 +0200 (2016-04-05) |
parents | |
children | 84f6e17c7ceb |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/model/initiative_for_notification.lua Tue Apr 05 20:40:37 2016 +0200 1.3 @@ -0,0 +1,178 @@ 1.4 +InitiativeForNotification = mondelefant.new_class() 1.5 +InitiativeForNotification.table = 'initiative_for_notification' 1.6 + 1.7 +InitiativeForNotification:add_reference{ 1.8 + mode = 'm1', 1.9 + to = "Initiative", 1.10 + this_key = 'initiative_id', 1.11 + that_key = 'id', 1.12 + ref = 'initiative', 1.13 +} 1.14 + 1.15 + 1.16 +function InitiativeForNotification:notify_member_id(member_id) 1.17 + 1.18 + db:query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ") 1.19 + 1.20 + local member = Member:by_id(member_id) 1.21 + locale.set{ lang = member.lang or config.default_lang } 1.22 + 1.23 + io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n") 1.24 + 1.25 + if not member.notify_email then 1.26 + return 1.27 + end 1.28 + 1.29 + local selector = db:new_selector() 1.30 + :add_field("*") 1.31 + :from({ "get_initiatives_for_notification(?)", member_id }, "initiative_for_notification") 1.32 + :join("initiative", nil, "initiative.id = initiative_for_notification.initiative_id") 1.33 + :join("issue", nil, "issue.id = initiative.issue_id") 1.34 + :join("member", nil, "member.id = initiative_for_notification.recipient_id") 1.35 + :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.area_id)") 1.36 + :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.id)") 1.37 + selector._class = self 1.38 + 1.39 + local initiatives_for_notification = selector:exec() 1.40 + 1.41 + if #initiatives_for_notification < 1 then 1.42 + return 1.43 + end 1.44 + 1.45 + local initiatives = initiatives_for_notification:load("initiative") 1.46 + local issues = initiatives:load("issue") 1.47 + issues:load("area") 1.48 + issues:load("policy") 1.49 + 1.50 + local last_area_id 1.51 + local last_issue_id 1.52 + 1.53 + local draft_count = 0 1.54 + local suggestion_count = 0 1.55 + 1.56 + local ms = {} 1.57 + 1.58 + for i, entry in ipairs(initiatives_for_notification) do 1.59 + local initiative = entry.initiative 1.60 + local issue = initiative.issue 1.61 + local area = issue.area 1.62 + local policy = issue.policy 1.63 + 1.64 + local m = {} 1.65 + if last_area_id ~= area.id then 1.66 + m[#m+1] = "*** " .. area.name .. " ***" 1.67 + m[#m+1] = "" 1.68 + last_area_id = area.id 1.69 + end 1.70 + if last_issue_id ~= issue.id then 1.71 + local state_time_text 1.72 + if string.sub(issue.state_time_left, 1, 1) == "-" then 1.73 + state_time_text = _"Phase ends soon" 1.74 + else 1.75 + state_time_text = _( "#{interval} left", { 1.76 + interval = format.interval_text(issue.state_time_left) 1.77 + }) 1.78 + end 1.79 + m[#m+1] = "---" 1.80 + m[#m+1] = policy.name .. " #" .. issue.id .. " - " .. issue.state_name .. " - " .. state_time_text 1.81 + m[#m+1] = "" 1.82 + last_issue_id = issue.id 1.83 + end 1.84 + m[#m+1] = initiative.display_name 1.85 + local source 1.86 + if entry.supported then 1.87 + source = _"has my support" 1.88 + local draft_info 1.89 + if entry.new_draft then 1.90 + draft_info = _"draft updated" 1.91 + draft_count = draft_count + 1 1.92 + end 1.93 + if draft_info then 1.94 + source = source .. ", " .. draft_info 1.95 + end 1.96 + local info 1.97 + if entry.new_suggestion_count then 1.98 + if entry.new_suggestion_count == 1 then 1.99 + info = _"new suggestion added" 1.100 + elseif entry.new_suggestion_count > 1 then 1.101 + info = _("#{count} suggestions added", { count = entry.new_suggestion_count }) 1.102 + end 1.103 + suggestion_count = suggestion_count + entry.new_suggestion_count 1.104 + end 1.105 + if info then 1.106 + source = source .. ", " .. info 1.107 + end 1.108 + elseif entry.featured then 1.109 + source = _"featured" 1.110 + end 1.111 + if entry.leading then 1.112 + source = source and source .. ", " or "" 1.113 + source = source .. "currently leading" 1.114 + end 1.115 + m[#m+1] = "(" .. source .. ")" 1.116 + m[#m+1] = "" 1.117 + if not initiatives_for_notification[i+1] or initiatives_for_notification[i+1].issue_id ~= issue.id then 1.118 + m[#m+1] = _("more: #{url}", { url = request.get_absolute_baseurl() .. "issue/show/" .. issue.id .. ".html" }) 1.119 + m[#m+1] = "" 1.120 + end 1.121 + ms[#ms+1] = table.concat(m, "\n") 1.122 + end 1.123 + 1.124 + local info = {} 1.125 + 1.126 + if draft_count > 0 then 1.127 + if draft_count == 1 then 1.128 + info[#info+1] = _"draft updated for " .. initiatives_for_notification[1].initiative.display_name 1.129 + else 1.130 + info[#info+1] = _("drafts of #{draft_count} initiatives updated", { draft_count = draft_count }) 1.131 + end 1.132 + end 1.133 + 1.134 + if suggestion_count > 0 then 1.135 + if suggestion_count == 1 then 1.136 + info[#info+1] = _"new suggestion for " .. initiatives_for_notification[1].initiative.display_name 1.137 + else 1.138 + info[#info+1] = _("#{suggestion_count} suggestions added", { suggestion_count = suggestion_count }) 1.139 + end 1.140 + end 1.141 + 1.142 + if draft_count == 0 and suggestion_count == 0 then 1.143 + info[#info+1] = _"featured initiatives" 1.144 + end 1.145 + 1.146 + local subject = _("Digest #{id}: #{info}", { 1.147 + id = member.notification_counter, info = table.concat(info, ", ") 1.148 + }) 1.149 + 1.150 + 1.151 + local template = config.notification_digest_template 1.152 + 1.153 + local message = _(template, { 1.154 + name = member.name, 1.155 + digest = table.concat(ms, "\n") 1.156 + }) 1.157 + 1.158 + local success = net.send_mail{ 1.159 + envelope_from = config.mail_envelope_from, 1.160 + from = config.mail_from, 1.161 + reply_to = config.mail_reply_to, 1.162 + to = member.notify_email, 1.163 + subject = subject, 1.164 + content_type = "text/plain; charset=UTF-8", 1.165 + content = message 1.166 + } 1.167 + 1.168 + db:query("COMMIT") 1.169 + 1.170 +end 1.171 + 1.172 +function InitiativeForNotification:notify_next_member() 1.173 + local scheduled_notification_to_send = ScheduledNotificationToSend:get_next() 1.174 + if not scheduled_notification_to_send then 1.175 + return false 1.176 + end 1.177 + InitiativeForNotification:notify_member_id(scheduled_notification_to_send.recipient_id) 1.178 + return true 1.179 +end 1.180 + 1.181 +ScheduledNotificationToSend:get_next() 1.182 \ No newline at end of file