liquid_feedback_frontend
view model/event.lua @ 1261:0f4925c2843a
Fixes on notification system
| author | bsw | 
|---|---|
| date | Sun Apr 17 17:00:56 2016 +0200 (2016-04-17) | 
| parents | 659e3eda2fad | 
| children | 5aecbbb04a42 | 
 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("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } )
    55     -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
    56     :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
    57     -- do not notify a member about the events caused by the member
    58     :add_where("event_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id")
    59     :exec()
    61   io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
    63   for i, member in ipairs(members_to_notify) do
    64     local subject
    65     local body = ""
    67     locale.do_with(
    68       { lang = member.lang or config.default_lang },
    69       function()
    71         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    72         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    73         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    74         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    75         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    77         local url
    79         if self.initiative_id then
    80           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    81         else
    82           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    83         end
    85         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
    87         local leading_initiative
    89         if self.initiative_id then
    90           local initiative = Initiative:by_id(self.initiative_id)
    91           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
    92         else
    93           local initiative_count = Initiative:new_selector()
    94             :add_where{ "initiative.issue_id = ?", self.issue_id }
    95             :count()
    96           local initiatives = Initiative:new_selector()
    97             :add_where{ "initiative.issue_id = ?", self.issue_id }
    98             :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id")
    99             :limit(3)
   100             :exec()
   101           for i, initiative in ipairs(initiatives) do
   102             if i == 1 then
   103               leading_initiative = initiative
   104             end
   105             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   106           end
   107           if initiative_count - 3 > 0 then
   108             body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
   109           end
   110           body = body .. "\n"
   111         end
   113         subject = config.mail_subject_prefix
   115         if self.event == "issue_state_changed" then
   116           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 })
   117         elseif self.event == "initiative_revoked" then
   118           subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name })
   119         end
   121         local success = net.send_mail{
   122           envelope_from = config.mail_envelope_from,
   123           from          = config.mail_from,
   124           reply_to      = config.mail_reply_to,
   125           to            = member.notify_email,
   126           subject       = subject,
   127           content_type  = "text/plain; charset=UTF-8",
   128           content       = body
   129         }
   131       end
   132     )
   133   end
   135 end
   137 function Event:send_next_notification()
   139   local notification_event_sent = NotificationEventSent:new_selector()
   140     :optional_object_mode()
   141     :for_update()
   142     :exec()
   144   local last_event_id = 0
   145   if notification_event_sent then
   146     last_event_id = notification_event_sent.event_id
   147   end
   149   local event = Event:new_selector()
   150     :add_where{ "event.id > ?", last_event_id }
   151     :add_order_by("event.id")
   152     :limit(1)
   153     :optional_object_mode()
   154     :exec()
   156   if event then
   157     if last_event_id == 0 then
   158       db:query{ "INSERT INTO notification_event_sent (event_id) VALUES (?)", event.id }
   159     else
   160       db:query{ "UPDATE notification_event_sent SET event_id = ?", event.id }
   161     end
   163     event:send_notification()
   165     if config.notification_handler_func then
   166       config.notification_handler_func(event)
   167     end
   169     return true
   171   end
   173 end
