liquid_feedback_frontend
view 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 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)
    18   locale.set{ lang = member.lang or config.default_lang }
    20   io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n")
    22   if not member.notify_email then
    23     return
    24   end
    26   local selector = db:new_selector()
    27     :add_field("*")
    28     :from({ "get_initiatives_for_notification(?)", member_id }, "initiative_for_notification")
    29     :join("initiative", nil, "initiative.id = initiative_for_notification.initiative_id")
    30     :join("issue", nil, "issue.id = initiative.issue_id")
    31     :join("member", nil, "member.id = initiative_for_notification.recipient_id")
    32     :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.area_id)")
    33     :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.id)")
    34   selector._class = self
    36   local initiatives_for_notification = selector:exec()
    38   if #initiatives_for_notification < 1 then
    39     return
    40   end
    42   local initiatives = initiatives_for_notification:load("initiative")
    43   local issues = initiatives:load("issue")
    44   issues:load("area")
    45   issues:load("policy")
    47   local last_area_id
    48   local last_issue_id
    50   local draft_count = 0
    51   local suggestion_count = 0
    53   local ms = {}
    55   for i, entry in ipairs(initiatives_for_notification) do
    56     local initiative = entry.initiative
    57     local issue = initiative.issue
    58     local area = issue.area
    59     local policy = issue.policy
    61     local m = {}
    62     if last_area_id ~= area.id then
    63       m[#m+1] = "*** " .. area.name .. " ***"
    64       m[#m+1] = ""
    65       last_area_id = area.id
    66     end
    67     if last_issue_id ~= issue.id then
    68       local state_time_text
    69       if string.sub(issue.state_time_left, 1, 1) == "-" then
    70         state_time_text = _"Phase ends soon"
    71       else
    72         state_time_text = _( "#{interval} left", {
    73           interval = format.interval_text(issue.state_time_left)
    74         })
    75       end
    76       m[#m+1] = "---"
    77       m[#m+1] = policy.name .. " #" .. issue.id .. " - " .. issue.state_name .. " - " .. state_time_text
    78       m[#m+1] = ""
    79       last_issue_id = issue.id
    80     end
    81     m[#m+1] = initiative.display_name
    82     local source
    83     if entry.supported then
    84       source = _"has my support"
    85       local draft_info
    86       if entry.new_draft then
    87         draft_info = _"draft updated"
    88         draft_count = draft_count + 1
    89       end
    90       if draft_info then
    91         source = source .. ", " .. draft_info
    92       end
    93       local info
    94       if entry.new_suggestion_count then
    95         if entry.new_suggestion_count == 1 then
    96           info = _"new suggestion added"
    97         elseif entry.new_suggestion_count > 1 then
    98           info = _("#{count} suggestions added", { count = entry.new_suggestion_count })
    99         end
   100         suggestion_count = suggestion_count + entry.new_suggestion_count
   101       end
   102       if info then
   103         source = source .. ", " .. info
   104       end
   105     elseif entry.featured then
   106       source = _"featured"
   107     end
   108     if entry.leading then
   109       source = source and source .. ", " or ""
   110       source = source .. "currently leading"
   111     end
   112     m[#m+1] = "(" .. source .. ")"
   113     m[#m+1] = ""
   114     if not initiatives_for_notification[i+1] or initiatives_for_notification[i+1].issue_id ~= issue.id then
   115       m[#m+1] = _("more: #{url}", { url = request.get_absolute_baseurl() .. "issue/show/" .. issue.id .. ".html" })
   116       m[#m+1] = ""
   117     end
   118     ms[#ms+1] = table.concat(m, "\n")
   119   end
   121   local info = {}
   123   if draft_count > 0 then
   124     if draft_count == 1 then
   125       info[#info+1] = _"draft updated for " .. initiatives_for_notification[1].initiative.display_name
   126     else
   127       info[#info+1] = _("drafts of #{draft_count} initiatives updated", { draft_count = draft_count })
   128     end
   129   end
   131   if suggestion_count > 0 then
   132     if suggestion_count == 1 then
   133       info[#info+1] = _"new suggestion for " .. initiatives_for_notification[1].initiative.display_name
   134     else
   135       info[#info+1] = _("#{suggestion_count} suggestions added", { suggestion_count = suggestion_count })
   136     end
   137   end
   139   if draft_count == 0 and suggestion_count == 0 then
   140     info[#info+1] = _"featured initiatives"
   141   end
   143   local subject = _("Digest #{id}: #{info}", {
   144     id = member.notification_counter, info = table.concat(info, ", ")
   145   })
   148   local template = config.notification_digest_template
   150   local message = _(template, {
   151     name = member.name,
   152     digest = table.concat(ms, "\n")
   153   })
   155   local success = net.send_mail{
   156     envelope_from = config.mail_envelope_from,
   157     from          = config.mail_from,
   158     reply_to      = config.mail_reply_to,
   159     to            = member.notify_email,
   160     subject       = subject,
   161     content_type  = "text/plain; charset=UTF-8",
   162     content       = message
   163   }
   165   db:query("COMMIT")
   167 end
   169 function InitiativeForNotification:notify_next_member()
   170   local scheduled_notification_to_send = ScheduledNotificationToSend:get_next()
   171   if not scheduled_notification_to_send then
   172     return false
   173   end
   174   InitiativeForNotification:notify_member_id(scheduled_notification_to_send.recipient_id)
   175   return true
   176 end
   178 ScheduledNotificationToSend:get_next()
