liquid_feedback_frontend
changeset 1252:659e3eda2fad
Minor fixes to new notification system
author | bsw |
---|---|
date | Sun Apr 17 16:02:26 2016 +0200 (2016-04-17) |
parents | e3f7555d4de7 |
children | 235a36a08da1 |
files | model/event.lua model/initiative_for_notification.lua model/newsletter.lua model/newsletter_to_send.lua model/scheduled_notification_to_send.lua |
line diff
1.1 --- a/model/event.lua Sun Apr 17 15:04:07 2016 +0200 1.2 +++ b/model/event.lua Sun Apr 17 16:02:26 2016 +0200 1.3 @@ -52,7 +52,6 @@ 1.4 1.5 local members_to_notify = Member:new_selector() 1.6 :join("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } ) 1.7 - --:add_where("member.activated NOTNULL AND member.notify_email NOTNULL") 1.8 -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future 1.9 :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval") 1.10 -- do not notify a member about the events caused by the member 1.11 @@ -61,14 +60,12 @@ 1.12 1.13 io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n") 1.14 1.15 - local url 1.16 - 1.17 for i, member in ipairs(members_to_notify) do 1.18 local subject 1.19 local body = "" 1.20 1.21 locale.do_with( 1.22 - { lang = member.lang or config.default_lang or 'en' }, 1.23 + { lang = member.lang or config.default_lang }, 1.24 function() 1.25 1.26 body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n" 1.27 @@ -77,6 +74,8 @@ 1.28 body = body .. _("[event mail] Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n" 1.29 body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n" 1.30 1.31 + local url 1.32 + 1.33 if self.initiative_id then 1.34 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html" 1.35 else 1.36 @@ -96,7 +95,7 @@ 1.37 :count() 1.38 local initiatives = Initiative:new_selector() 1.39 :add_where{ "initiative.issue_id = ?", self.issue_id } 1.40 - :add_order_by("initiative.supporter_count DESC") 1.41 + :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id") 1.42 :limit(3) 1.43 :exec() 1.44 for i, initiative in ipairs(initiatives) do 1.45 @@ -114,7 +113,7 @@ 1.46 subject = config.mail_subject_prefix 1.47 1.48 if self.event == "issue_state_changed" then 1.49 - subject = subject .. _("State of #{issue} changed to #{state} / Leading: #{initiative}", { issue = self.issue.name, state = Issue:get_state_name_for_state(self.state), initiative = leading_initiative.display_name }) 1.50 + subject = subject .. _("State of #{issue} changed to #{state} / #{initiative}", { issue = self.issue.name, state = Issue:get_state_name_for_state(self.state), initiative = leading_initiative.display_name }) 1.51 elseif self.event == "initiative_revoked" then 1.52 subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name }) 1.53 end
2.1 --- a/model/initiative_for_notification.lua Sun Apr 17 15:04:07 2016 +0200 2.2 +++ b/model/initiative_for_notification.lua Sun Apr 17 16:02:26 2016 +0200 2.3 @@ -15,14 +15,9 @@ 2.4 db:query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ") 2.5 2.6 local member = Member:by_id(member_id) 2.7 + 2.8 locale.set{ lang = member.lang or config.default_lang } 2.9 2.10 - io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n") 2.11 - 2.12 - if not member.notify_email then 2.13 - return 2.14 - end 2.15 - 2.16 local selector = db:new_selector() 2.17 :add_field("*") 2.18 :from({ "get_initiatives_for_notification(?)", member_id }, "initiative_for_notification") 2.19 @@ -31,14 +26,23 @@ 2.20 :join("member", nil, "member.id = initiative_for_notification.recipient_id") 2.21 :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.area_id)") 2.22 :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.id)") 2.23 + 2.24 selector._class = self 2.25 2.26 local initiatives_for_notification = selector:exec() 2.27 2.28 - if #initiatives_for_notification < 1 then 2.29 + db:query("COMMIT") 2.30 + 2.31 + if not member.notify_email then 2.32 return 2.33 end 2.34 2.35 + if not #initiatives_for_notification > 0 then 2.36 + return 2.37 + end 2.38 + 2.39 + io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n") 2.40 + 2.41 local initiatives = initiatives_for_notification:load("initiative") 2.42 local issues = initiatives:load("issue") 2.43 issues:load("area") 2.44 @@ -50,6 +54,9 @@ 2.45 local draft_count = 0 2.46 local suggestion_count = 0 2.47 2.48 + local draft_initiative 2.49 + local suggestion_initiative 2.50 + 2.51 local ms = {} 2.52 2.53 for i, entry in ipairs(initiatives_for_notification) do 2.54 @@ -82,25 +89,23 @@ 2.55 local source 2.56 if entry.supported then 2.57 source = _"has my support" 2.58 - local draft_info 2.59 if entry.new_draft then 2.60 - draft_info = _"draft updated" 2.61 + source = source .. ", " .. _"draft updated" 2.62 draft_count = draft_count + 1 2.63 + draft_initiative = initiative 2.64 end 2.65 - if draft_info then 2.66 - source = source .. ", " .. draft_info 2.67 - end 2.68 - local info 2.69 if entry.new_suggestion_count then 2.70 if entry.new_suggestion_count == 1 then 2.71 - info = _"new suggestion added" 2.72 + source = source .. ", " .. _"new suggestion added" 2.73 elseif entry.new_suggestion_count > 1 then 2.74 - info = _("#{count} suggestions added", { count = entry.new_suggestion_count }) 2.75 + source = source .. ", " .. _("#{count} suggestions added", { count = entry.new_suggestion_count }) 2.76 end 2.77 suggestion_count = suggestion_count + entry.new_suggestion_count 2.78 - end 2.79 - if info then 2.80 - source = source .. ", " .. info 2.81 + if suggestion_initiative and suggestion_initiative.id ~= initiative.id then 2.82 + suggestion_initiative = false 2.83 + elseif suggestion_initiative ~= false then 2.84 + suggestion_initiative = initiative 2.85 + end 2.86 end 2.87 elseif entry.featured then 2.88 source = _"featured" 2.89 @@ -122,7 +127,7 @@ 2.90 2.91 if draft_count > 0 then 2.92 if draft_count == 1 then 2.93 - info[#info+1] = _"draft updated for " .. initiatives_for_notification[1].initiative.display_name 2.94 + info[#info+1] = _"draft updated for " .. draft_initiative.display_name 2.95 else 2.96 info[#info+1] = _("drafts of #{draft_count} initiatives updated", { draft_count = draft_count }) 2.97 end 2.98 @@ -130,7 +135,9 @@ 2.99 2.100 if suggestion_count > 0 then 2.101 if suggestion_count == 1 then 2.102 - info[#info+1] = _"new suggestion for " .. initiatives_for_notification[1].initiative.display_name 2.103 + info[#info+1] = _"new suggestion for " .. suggestion_initiative.display_name 2.104 + elseif suggestion_initiative then 2.105 + info[#info+1] = _("#{count} suggestions added for #{initiative}", { count = suggestion_count, suggestion_initiative.display_name }) 2.106 else 2.107 info[#info+1] = _("#{count} suggestions added", { count = suggestion_count }) 2.108 end 2.109 @@ -143,7 +150,6 @@ 2.110 local subject = _("Digest #{id}: #{info}", { 2.111 id = member.notification_counter, info = table.concat(info, ", ") 2.112 }) 2.113 - 2.114 2.115 local template = config.notification_digest_template 2.116 2.117 @@ -162,8 +168,6 @@ 2.118 content = message 2.119 } 2.120 2.121 - db:query("COMMIT") 2.122 - 2.123 end 2.124 2.125 function InitiativeForNotification:notify_next_member()
3.1 --- a/model/newsletter.lua Sun Apr 17 15:04:07 2016 +0200 3.2 +++ b/model/newsletter.lua Sun Apr 17 16:02:26 2016 +0200 3.3 @@ -6,36 +6,37 @@ 3.4 if not newsletter_to_send then 3.5 return false 3.6 end 3.7 + 3.8 local newsletter = newsletter_to_send.newsletter 3.9 3.10 local newsletter_to_send = NewsletterToSend:by_newsletter_id(newsletter.id) 3.11 newsletter_to_send:load("member") 3.12 - 3.13 + 3.14 newsletter.sent = "now" 3.15 newsletter:save() 3.16 - 3.17 + 3.18 io.stderr:write("Sending newsletter " .. newsletter.id .. " to " .. (#newsletter_to_send) .. " members\n") 3.19 3.20 for i, n in ipairs(newsletter_to_send) do 3.21 3.22 local member = newsletter_to_send.member 3.23 3.24 - if not member.notify_email then 3.25 - return 3.26 + if member.notify_email then 3.27 + io.stderr:write("Sending newsletter " .. newsletter.id .. " to " .. member.notify_email .. "\n") 3.28 + 3.29 + local success = net.send_mail{ 3.30 + envelope_from = config.mail_envelope_from, 3.31 + from = config.mail_from, 3.32 + reply_to = config.mail_reply_to, 3.33 + to = member.notify_email, 3.34 + subject = newsletter.subject, 3.35 + content_type = "text/plain; charset=UTF-8", 3.36 + content = newsletter.content 3.37 + } 3.38 end 3.39 3.40 - io.stderr:write("Sending newsletter " .. newsletter.id .. " to " .. member.notify_email .. "\n") 3.41 - 3.42 - local success = net.send_mail{ 3.43 - envelope_from = config.mail_envelope_from, 3.44 - from = config.mail_from, 3.45 - reply_to = config.mail_reply_to, 3.46 - to = member.notify_email, 3.47 - subject = newsletter.subject, 3.48 - content_type = "text/plain; charset=UTF-8", 3.49 - content = newsletter.content 3.50 - } 3.51 - 3.52 end 3.53 3.54 + return true 3.55 + 3.56 end 3.57 \ No newline at end of file
4.1 --- a/model/newsletter_to_send.lua Sun Apr 17 15:04:07 2016 +0200 4.2 +++ b/model/newsletter_to_send.lua Sun Apr 17 16:02:26 2016 +0200 4.3 @@ -20,6 +20,9 @@ 4.4 function NewsletterToSend:get_next() 4.5 return NewsletterToSend:new_selector() 4.6 :set_distinct("newsletter_id") 4.7 + -- SAFETY FIRST, NEVER send newsletter more then 21 days in past or future 4.8 + :add_where("now() - published BETWEEN '-21 days'::interval AND '21 days'::interval") 4.9 + :add_order_by("published") 4.10 :add_order_by("newsletter_id") 4.11 :limit(1) 4.12 :optional_object_mode()
5.1 --- a/model/scheduled_notification_to_send.lua Sun Apr 17 15:04:07 2016 +0200 5.2 +++ b/model/scheduled_notification_to_send.lua Sun Apr 17 16:02:26 2016 +0200 5.3 @@ -4,6 +4,7 @@ 5.4 function ScheduledNotificationToSend:get_next() 5.5 return self:new_selector() 5.6 :add_order_by("pending DESC") 5.7 + :add_order_by("random()") 5.8 :limit(1) 5.9 :optional_object_mode() 5.10 :exec()