liquid_feedback_frontend
view model/event.lua @ 1305:fd50bdd36a4b
Set name for To: field of notification mails, added missing translation function call
| author | bsw | 
|---|---|
| date | Sat May 07 19:27:58 2016 +0200 (2016-05-07) | 
| parents | 9ff5b868760d | 
| children | 32cc544d5a5b | 
 line source
     1 Event = mondelefant.new_class()
     2 Event.table = 'event'
     4 Event:add_reference{
     5   mode          = 'm1',
     6   to            = "Issue",
     7   this_key      = 'issue_id',
     8   that_key      = 'id',
     9   ref           = 'issue',
    10 }
    12 Event:add_reference{
    13   mode          = 'm1',
    14   to            = "Initiative",
    15   this_key      = 'initiative_id',
    16   that_key      = 'id',
    17   ref           = 'initiative',
    18 }
    20 Event:add_reference{
    21   mode          = 'm1',
    22   to            = "Suggestion",
    23   this_key      = 'suggestion_id',
    24   that_key      = 'id',
    25   ref           = 'suggestion',
    26 }
    28 Event:add_reference{
    29   mode          = 'm1',
    30   to            = "Member",
    31   this_key      = 'member_id',
    32   that_key      = 'id',
    33   ref           = 'member',
    34 }
    36 function Event.object_get:event_name()
    37   return ({
    38     issue_state_changed = _"Issue reached next phase",
    39     initiative_created_in_new_issue = _"New issue",
    40     initiative_created_in_existing_issue = _"New initiative",
    41     initiative_revoked = _"Initiative revoked",
    42     new_draft_created = _"New initiative draft",
    43     suggestion_created = _"New suggestion"
    44   })[self.event]
    45 end
    47 function Event.object_get:state_name()
    48   return Issue:get_state_name_for_state(self.state)
    49 end
    51 function Event.object:send_notification()
    53   local members_to_notify = Member:new_selector()
    54     :join("member_to_notify", nil, "member_to_notify.id = member.id")
    55     :join("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } )
    56     -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
    57     :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
    58     -- do not notify a member about the events caused by the member
    59     :add_where("event_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id")
    60     :add_where("member.notify_email NOTNULL")
    61     :exec()
    63   io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
    65   for i, member in ipairs(members_to_notify) do
    66     local subject
    67     local body = ""
    69     locale.do_with(
    70       { lang = member.lang or config.default_lang },
    71       function()
    73         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    74         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    75         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    76         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    77         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    79         local url
    81         if self.initiative_id then
    82           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    83         else
    84           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    85         end
    87         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
    89         local leading_initiative
    91         if self.initiative_id then
    92           local initiative = Initiative:by_id(self.initiative_id)
    93           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
    94         else
    95           local initiative_count = Initiative:new_selector()
    96             :add_where{ "initiative.issue_id = ?", self.issue_id }
    97             :count()
    98           local initiatives = Initiative:new_selector()
    99             :add_where{ "initiative.issue_id = ?", self.issue_id }
   100             :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id")
   101             :limit(3)
   102             :exec()
   103           for i, initiative in ipairs(initiatives) do
   104             if i == 1 then
   105               leading_initiative = initiative
   106             end
   107             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   108           end
   109           if initiative_count - 3 > 0 then
   110             body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
   111           end
   112           body = body .. "\n"
   113         end
   115         subject = config.mail_subject_prefix
   117         if self.event == "issue_state_changed" then
   118           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 })
   119         elseif self.event == "initiative_revoked" then
   120           subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name })
   121         end
   123         local success = net.send_mail{
   124           envelope_from = config.mail_envelope_from,
   125           from          = config.mail_from,
   126           reply_to      = config.mail_reply_to,
   127           to            = { name = member.name, address = member.notify_email },
   128           subject       = subject,
   129           content_type  = "text/plain; charset=UTF-8",
   130           content       = body
   131         }
   133       end
   134     )
   135   end
   137 end
   139 function Event:send_next_notification()
   141   local notification_event_sent = NotificationEventSent:new_selector()
   142     :optional_object_mode()
   143     :for_update()
   144     :exec()
   146   local last_event_id = 0
   147   if notification_event_sent then
   148     last_event_id = notification_event_sent.event_id
   149   end
   151   local event = Event:new_selector()
   152     :add_where{ "event.id > ?", last_event_id }
   153     :add_order_by("event.id")
   154     :limit(1)
   155     :optional_object_mode()
   156     :exec()
   158   if event then
   159     if last_event_id == 0 then
   160       db:query{ "INSERT INTO notification_event_sent (event_id) VALUES (?)", event.id }
   161     else
   162       db:query{ "UPDATE notification_event_sent SET event_id = ?", event.id }
   163     end
   165     event:send_notification()
   167     if config.notification_handler_func then
   168       config.notification_handler_func(event)
   169     end
   171     return true
   173   end
   175 end
