liquid_feedback_frontend
view model/initiative_for_notification.lua @ 1859:02c34183b6df
Fixed wrong filename in INSTALL file
| author | bsw | 
|---|---|
| date | Tue Nov 28 18:54:51 2023 +0100 (23 months ago) | 
| parents | 3dbfac432576 | 
| children | 
 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)
    19   locale.set{ lang = member.lang or config.default_lang }
    21   local selector = db:new_selector()
    22     :add_field("*")
    23     :from({ "get_initiatives_for_notification(?)", member_id }, "initiative_for_notification")
    24     :left_join("initiative", nil, "initiative.id = initiative_for_notification.initiative_id")
    25     :left_join("issue", nil, "issue.id = initiative.issue_id")
    26     :join("member", nil, "member.id = initiative_for_notification.recipient_id")
    27     :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.area_id)")
    28     :add_order_by("md5(initiative_for_notification.recipient_id || '-' || member.notification_counter || '-' || issue.id)")
    30   selector._class = self
    32   local initiatives_for_notification = selector:exec()
    34   db:query("COMMIT")
    36   if not member.notify_email then
    37     return
    38   end
    40   if #initiatives_for_notification < 1 then
    41     return
    42   end
    44   io.stderr:write("Sending digest #" .. member.notification_counter .. " to " .. member.notify_email .. "\n")
    46   local initiatives = initiatives_for_notification:load("initiative")
    47   local issues = initiatives:load("issue")
    48   issues:load("area")
    49   issues:load("policy")
    51   local last_area_id
    52   local last_issue_id
    54   local draft_count = 0
    55   local suggestion_count = 0
    57   local draft_initiative
    58   local suggestion_initiative
    60   local ms = {}
    62   for i, entry in ipairs(initiatives_for_notification) do
    63     local initiative = entry.initiative
    64     local issue = initiative.issue
    65     local area = issue.area
    66     local policy = issue.policy
    68     local m = {}
    69     if last_area_id ~= area.id then
    70       m[#m+1] = "*** " .. area.name .. " ***"
    71       m[#m+1] = ""
    72       last_area_id = area.id
    73     end
    74     if last_issue_id ~= issue.id then
    75       local state_time_text
    76       if string.sub(issue.state_time_left, 1, 1) == "-" then
    77         state_time_text = _"Phase ends soon"
    78       else
    79         state_time_text = format.interval_text(issue.state_time_left, { mode = "time_left" })
    80       end
    81       m[#m+1] = "---"
    82       m[#m+1] = policy.name .. " #" .. issue.id .. " - " .. issue.state_name .. " - " .. state_time_text
    83       m[#m+1] = ""
    84       last_issue_id = issue.id
    85     end
    86     m[#m+1] = initiative.display_name
    87     local source
    88     if entry.supported then
    89       source = _"has my support"
    90       if entry.new_draft then
    91         source = source .. ", " .. _"draft updated"
    92         draft_count = draft_count + 1
    93         draft_initiative = initiative
    94       end
    95       if entry.new_suggestion_count then
    96         if entry.new_suggestion_count == 1 then
    97           source = source .. ", " .. _"new suggestion added"
    98         elseif entry.new_suggestion_count > 1 then
    99           source = source .. ", " .. _("#{count} suggestions added", { count = entry.new_suggestion_count })
   100         end
   101         suggestion_count = suggestion_count + entry.new_suggestion_count
   102         if suggestion_initiative and suggestion_initiative.id ~= initiative.id then
   103           suggestion_initiative = false
   104         elseif suggestion_initiative ~= false then
   105           suggestion_initiative = initiative
   106         end
   107       end
   108     elseif entry.featured then
   109       source = _"featured"
   110     end
   111     if entry.leading then
   112       source = source and source .. ", " or ""
   113       source = source .. _"currently leading"
   114     end
   115     m[#m+1] = "(" .. source .. ")"
   116     m[#m+1] = ""
   117     if not initiatives_for_notification[i+1] or initiatives_for_notification[i+1].issue_id ~= issue.id then
   118       m[#m+1] = _("more: #{url}", { url = request.get_absolute_baseurl() .. "issue/show/" .. issue.id .. ".html" })
   119       m[#m+1] = ""
   120     end
   121     ms[#ms+1] = table.concat(m, "\n")
   122   end
   124   local info = {}
   126   if draft_count > 0 then
   127     if draft_count == 1 then
   128       info[#info+1] = _"draft updated for " .. draft_initiative.display_name
   129     else
   130       info[#info+1] = _("drafts of #{draft_count} initiatives updated", { draft_count = draft_count })
   131     end
   132   end
   134   if suggestion_count > 0 then
   135     if suggestion_count == 1 then
   136       info[#info+1] = _"new suggestion for " .. suggestion_initiative.display_name
   137     elseif suggestion_initiative then
   138       info[#info+1] = _("#{count} suggestions added for #{initiative}", { count = suggestion_count, suggestion_initiative.display_name })
   139     else
   140       info[#info+1] = _("#{count} suggestions added", { count = suggestion_count })
   141     end
   142   end
   144   if draft_count == 0 and suggestion_count == 0 then
   145     info[#info+1] = _"featured initiatives"
   146   end
   148   local subject = _("Digest #{id}: #{info}", {
   149     id = member.notification_counter, info = table.concat(info, ", ")
   150   })
   152   local template = config.notification_digest_template
   154   local message = _(template, {
   155     name = member.name,
   156     digest = table.concat(ms, "\n")
   157   })
   159   local success = net.send_mail{
   160     envelope_from = config.mail_envelope_from,
   161     from          = config.mail_from,
   162     reply_to      = config.mail_reply_to,
   163     to            = { name = member.name, address = member.notify_email },
   164     subject       = subject,
   165     content_type  = "text/plain; charset=UTF-8",
   166     content       = message
   167   }
   169 end
   171 function InitiativeForNotification:notify_next_member()
   172   local scheduled_notification_to_send = ScheduledNotificationToSend:get_next()
   173   if not scheduled_notification_to_send then
   174     return false
   175   end
   176   InitiativeForNotification:notify_member_id(scheduled_notification_to_send.recipient_id)
   177   return true
   178 end
