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