# HG changeset patch # User bsw # Date 1460901746 -7200 # Node ID 659e3eda2fadcffd6c3803cdb553101c0a93ebb1 # Parent e3f7555d4de7b86fdae5d5664fdf5e5dab1e317a Minor fixes to new notification system diff -r e3f7555d4de7 -r 659e3eda2fad model/event.lua --- a/model/event.lua Sun Apr 17 15:04:07 2016 +0200 +++ b/model/event.lua Sun Apr 17 16:02:26 2016 +0200 @@ -52,7 +52,6 @@ local members_to_notify = Member:new_selector() :join("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } ) - --:add_where("member.activated NOTNULL AND member.notify_email NOTNULL") -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval") -- do not notify a member about the events caused by the member @@ -61,14 +60,12 @@ io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n") - local url - for i, member in ipairs(members_to_notify) do local subject local body = "" locale.do_with( - { lang = member.lang or config.default_lang or 'en' }, + { lang = member.lang or config.default_lang }, function() body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n" @@ -77,6 +74,8 @@ body = body .. _("[event mail] Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n" body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n" + local url + if self.initiative_id then url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html" else @@ -96,7 +95,7 @@ :count() local initiatives = Initiative:new_selector() :add_where{ "initiative.issue_id = ?", self.issue_id } - :add_order_by("initiative.supporter_count DESC") + :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id") :limit(3) :exec() for i, initiative in ipairs(initiatives) do @@ -114,7 +113,7 @@ subject = config.mail_subject_prefix if self.event == "issue_state_changed" then - 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 }) + 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 }) elseif self.event == "initiative_revoked" then subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name }) end diff -r e3f7555d4de7 -r 659e3eda2fad model/initiative_for_notification.lua --- a/model/initiative_for_notification.lua Sun Apr 17 15:04:07 2016 +0200 +++ b/model/initiative_for_notification.lua Sun Apr 17 16:02:26 2016 +0200 @@ -15,14 +15,9 @@ db:query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ") local member = Member:by_id(member_id) + locale.set{ lang = member.lang or config.default_lang } - io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n") - - if not member.notify_email then - return - end - local selector = db:new_selector() :add_field("*") :from({ "get_initiatives_for_notification(?)", member_id }, "initiative_for_notification") @@ -31,14 +26,23 @@ :join("member", nil, "member.id = initiative_for_notification.recipient_id") :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.area_id)") :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.id)") + selector._class = self local initiatives_for_notification = selector:exec() - if #initiatives_for_notification < 1 then + db:query("COMMIT") + + if not member.notify_email then return end + if not #initiatives_for_notification > 0 then + return + end + + io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n") + local initiatives = initiatives_for_notification:load("initiative") local issues = initiatives:load("issue") issues:load("area") @@ -50,6 +54,9 @@ local draft_count = 0 local suggestion_count = 0 + local draft_initiative + local suggestion_initiative + local ms = {} for i, entry in ipairs(initiatives_for_notification) do @@ -82,25 +89,23 @@ local source if entry.supported then source = _"has my support" - local draft_info if entry.new_draft then - draft_info = _"draft updated" + source = source .. ", " .. _"draft updated" draft_count = draft_count + 1 + draft_initiative = initiative end - if draft_info then - source = source .. ", " .. draft_info - end - local info if entry.new_suggestion_count then if entry.new_suggestion_count == 1 then - info = _"new suggestion added" + source = source .. ", " .. _"new suggestion added" elseif entry.new_suggestion_count > 1 then - info = _("#{count} suggestions added", { count = entry.new_suggestion_count }) + source = source .. ", " .. _("#{count} suggestions added", { count = entry.new_suggestion_count }) end suggestion_count = suggestion_count + entry.new_suggestion_count - end - if info then - source = source .. ", " .. info + if suggestion_initiative and suggestion_initiative.id ~= initiative.id then + suggestion_initiative = false + elseif suggestion_initiative ~= false then + suggestion_initiative = initiative + end end elseif entry.featured then source = _"featured" @@ -122,7 +127,7 @@ if draft_count > 0 then if draft_count == 1 then - info[#info+1] = _"draft updated for " .. initiatives_for_notification[1].initiative.display_name + info[#info+1] = _"draft updated for " .. draft_initiative.display_name else info[#info+1] = _("drafts of #{draft_count} initiatives updated", { draft_count = draft_count }) end @@ -130,7 +135,9 @@ if suggestion_count > 0 then if suggestion_count == 1 then - info[#info+1] = _"new suggestion for " .. initiatives_for_notification[1].initiative.display_name + info[#info+1] = _"new suggestion for " .. suggestion_initiative.display_name + elseif suggestion_initiative then + info[#info+1] = _("#{count} suggestions added for #{initiative}", { count = suggestion_count, suggestion_initiative.display_name }) else info[#info+1] = _("#{count} suggestions added", { count = suggestion_count }) end @@ -143,7 +150,6 @@ local subject = _("Digest #{id}: #{info}", { id = member.notification_counter, info = table.concat(info, ", ") }) - local template = config.notification_digest_template @@ -162,8 +168,6 @@ content = message } - db:query("COMMIT") - end function InitiativeForNotification:notify_next_member() diff -r e3f7555d4de7 -r 659e3eda2fad model/newsletter.lua --- a/model/newsletter.lua Sun Apr 17 15:04:07 2016 +0200 +++ b/model/newsletter.lua Sun Apr 17 16:02:26 2016 +0200 @@ -6,36 +6,37 @@ if not newsletter_to_send then return false end + local newsletter = newsletter_to_send.newsletter local newsletter_to_send = NewsletterToSend:by_newsletter_id(newsletter.id) newsletter_to_send:load("member") - + newsletter.sent = "now" newsletter:save() - + io.stderr:write("Sending newsletter " .. newsletter.id .. " to " .. (#newsletter_to_send) .. " members\n") for i, n in ipairs(newsletter_to_send) do local member = newsletter_to_send.member - if not member.notify_email then - return + if member.notify_email then + io.stderr:write("Sending newsletter " .. newsletter.id .. " to " .. member.notify_email .. "\n") + + local success = net.send_mail{ + envelope_from = config.mail_envelope_from, + from = config.mail_from, + reply_to = config.mail_reply_to, + to = member.notify_email, + subject = newsletter.subject, + content_type = "text/plain; charset=UTF-8", + content = newsletter.content + } end - io.stderr:write("Sending newsletter " .. newsletter.id .. " to " .. member.notify_email .. "\n") - - local success = net.send_mail{ - envelope_from = config.mail_envelope_from, - from = config.mail_from, - reply_to = config.mail_reply_to, - to = member.notify_email, - subject = newsletter.subject, - content_type = "text/plain; charset=UTF-8", - content = newsletter.content - } - end + return true + end \ No newline at end of file diff -r e3f7555d4de7 -r 659e3eda2fad model/newsletter_to_send.lua --- a/model/newsletter_to_send.lua Sun Apr 17 15:04:07 2016 +0200 +++ b/model/newsletter_to_send.lua Sun Apr 17 16:02:26 2016 +0200 @@ -20,6 +20,9 @@ function NewsletterToSend:get_next() return NewsletterToSend:new_selector() :set_distinct("newsletter_id") + -- SAFETY FIRST, NEVER send newsletter more then 21 days in past or future + :add_where("now() - published BETWEEN '-21 days'::interval AND '21 days'::interval") + :add_order_by("published") :add_order_by("newsletter_id") :limit(1) :optional_object_mode() diff -r e3f7555d4de7 -r 659e3eda2fad model/scheduled_notification_to_send.lua --- a/model/scheduled_notification_to_send.lua Sun Apr 17 15:04:07 2016 +0200 +++ b/model/scheduled_notification_to_send.lua Sun Apr 17 16:02:26 2016 +0200 @@ -4,6 +4,7 @@ function ScheduledNotificationToSend:get_next() return self:new_selector() :add_order_by("pending DESC") + :add_order_by("random()") :limit(1) :optional_object_mode() :exec()