| rev | 
   line source | 
| 
bsw@286
 | 
     1 Event = mondelefant.new_class()
 | 
| 
bsw@286
 | 
     2 Event.table = 'event'
 | 
| 
bsw@286
 | 
     3 
 | 
| 
bsw@286
 | 
     4 Event:add_reference{
 | 
| 
bsw@286
 | 
     5   mode          = 'm1',
 | 
| 
bsw@286
 | 
     6   to            = "Issue",
 | 
| 
bsw@286
 | 
     7   this_key      = 'issue_id',
 | 
| 
bsw@286
 | 
     8   that_key      = 'id',
 | 
| 
bsw@286
 | 
     9   ref           = 'issue',
 | 
| 
bsw@287
 | 
    10 }
 | 
| 
bsw@287
 | 
    11 
 | 
| 
bsw@414
 | 
    12 Event:add_reference{
 | 
| 
bsw@414
 | 
    13   mode          = 'm1',
 | 
| 
bsw@712
 | 
    14   to            = "Initiative",
 | 
| 
bsw@712
 | 
    15   this_key      = 'initiative_id',
 | 
| 
bsw@712
 | 
    16   that_key      = 'id',
 | 
| 
bsw@712
 | 
    17   ref           = 'initiative',
 | 
| 
bsw@712
 | 
    18 }
 | 
| 
bsw@712
 | 
    19 
 | 
| 
bsw@712
 | 
    20 Event:add_reference{
 | 
| 
bsw@712
 | 
    21   mode          = 'm1',
 | 
| 
bsw@414
 | 
    22   to            = "Suggestion",
 | 
| 
bsw@414
 | 
    23   this_key      = 'suggestion_id',
 | 
| 
bsw@414
 | 
    24   that_key      = 'id',
 | 
| 
bsw@414
 | 
    25   ref           = 'suggestion',
 | 
| 
bsw@414
 | 
    26 }
 | 
| 
bsw@414
 | 
    27 
 | 
| 
bsw@414
 | 
    28 Event:add_reference{
 | 
| 
bsw@414
 | 
    29   mode          = 'm1',
 | 
| 
bsw@414
 | 
    30   to            = "Member",
 | 
| 
bsw@414
 | 
    31   this_key      = 'member_id',
 | 
| 
bsw@414
 | 
    32   that_key      = 'id',
 | 
| 
bsw@414
 | 
    33   ref           = 'member',
 | 
| 
bsw@414
 | 
    34 }
 | 
| 
bsw@414
 | 
    35 
 | 
| 
bsw@405
 | 
    36 function Event.object_get:event_name()
 | 
| 
bsw@405
 | 
    37   return ({
 | 
| 
bsw@405
 | 
    38     issue_state_changed = _"Issue reached next phase",
 | 
| 
bsw@405
 | 
    39     initiative_created_in_new_issue = _"New issue",
 | 
| 
bsw@414
 | 
    40     initiative_created_in_existing_issue = _"New initiative",
 | 
| 
bsw@405
 | 
    41     initiative_revoked = _"Initiative revoked",
 | 
| 
bsw@405
 | 
    42     new_draft_created = _"New initiative draft",
 | 
| 
bsw@405
 | 
    43     suggestion_created = _"New suggestion"
 | 
| 
bsw@405
 | 
    44   })[self.event]
 | 
| 
bsw@405
 | 
    45 end
 | 
| 
bsw@405
 | 
    46   
 | 
| 
bsw@405
 | 
    47 function Event.object_get:state_name()
 | 
| 
bsw@972
 | 
    48   return Issue:get_state_name_for_state(self.state)
 | 
| 
bsw@405
 | 
    49 end
 | 
| 
bsw@405
 | 
    50   
 | 
| 
bsw@592
 | 
    51 function Event.object:send_notification()
 | 
| 
bsw@592
 | 
    52   
 | 
| 
bsw@363
 | 
    53   local members_to_notify = Member:new_selector()
 | 
| 
bsw@1248
 | 
    54     :join("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } )
 | 
| 
bsw@1248
 | 
    55     --:add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
 | 
| 
bsw@726
 | 
    56     -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
 | 
| 
bsw@1248
 | 
    57     :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
 | 
| 
bsw@866
 | 
    58     -- do not notify a member about the events caused by the member
 | 
| 
bsw@1248
 | 
    59     :add_where("event_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id")
 | 
| 
bsw@363
 | 
    60     :exec()
 | 
| 
bsw@363
 | 
    61     
 | 
| 
bsw@1159
 | 
    62   io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
 | 
| 
bsw@363
 | 
    63 
 | 
| 
bsw@287
 | 
    64   local url
 | 
| 
bsw@287
 | 
    65 
 | 
| 
bsw@405
 | 
    66   for i, member in ipairs(members_to_notify) do
 | 
| 
bsw@405
 | 
    67     local subject
 | 
| 
bsw@405
 | 
    68     local body = ""
 | 
| 
bsw@405
 | 
    69     
 | 
| 
bsw@405
 | 
    70     locale.do_with(
 | 
| 
bsw@405
 | 
    71       { lang = member.lang or config.default_lang or 'en' },
 | 
| 
bsw@405
 | 
    72       function()
 | 
| 
bsw@1104
 | 
    73 
 | 
| 
bsw@405
 | 
    74         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
 | 
| 
bsw@405
 | 
    75         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
 | 
| 
bsw@405
 | 
    76         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
 | 
| 
bsw@405
 | 
    77         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    78         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    79 
 | 
| 
bsw@405
 | 
    80         if self.initiative_id then
 | 
| 
bsw@405
 | 
    81           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
 | 
| 
bsw@405
 | 
    82         else
 | 
| 
bsw@405
 | 
    83           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
 | 
| 
bsw@405
 | 
    84         end
 | 
| 
bsw@405
 | 
    85         
 | 
| 
bsw@405
 | 
    86         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
 | 
| 
bsw@405
 | 
    87         
 | 
| 
bsw@1248
 | 
    88         local leading_initiative
 | 
| 
bsw@1248
 | 
    89         
 | 
| 
bsw@405
 | 
    90         if self.initiative_id then
 | 
| 
bsw@405
 | 
    91           local initiative = Initiative:by_id(self.initiative_id)
 | 
| 
bsw@405
 | 
    92           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    93         else
 | 
| 
bsw@405
 | 
    94           local initiative_count = Initiative:new_selector()
 | 
| 
bsw@405
 | 
    95             :add_where{ "initiative.issue_id = ?", self.issue_id }
 | 
| 
bsw@405
 | 
    96             :count()
 | 
| 
bsw@405
 | 
    97           local initiatives = Initiative:new_selector()
 | 
| 
bsw@405
 | 
    98             :add_where{ "initiative.issue_id = ?", self.issue_id }
 | 
| 
bsw@405
 | 
    99             :add_order_by("initiative.supporter_count DESC")
 | 
| 
bsw@405
 | 
   100             :limit(3)
 | 
| 
bsw@405
 | 
   101             :exec()
 | 
| 
bsw@405
 | 
   102           for i, initiative in ipairs(initiatives) do
 | 
| 
bsw@1248
 | 
   103             if i == 1 then
 | 
| 
bsw@1248
 | 
   104               leading_initiative = initiative
 | 
| 
bsw@1248
 | 
   105             end
 | 
| 
bsw@405
 | 
   106             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
 | 
| 
bsw@405
 | 
   107           end
 | 
| 
bsw@405
 | 
   108           if initiative_count - 3 > 0 then
 | 
| 
bsw@764
 | 
   109             body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
 | 
| 
bsw@405
 | 
   110           end
 | 
| 
bsw@405
 | 
   111           body = body .. "\n"
 | 
| 
bsw@405
 | 
   112         end
 | 
| 
bsw@405
 | 
   113         
 | 
| 
bsw@1248
 | 
   114         subject = config.mail_subject_prefix
 | 
| 
bsw@1248
 | 
   115         
 | 
| 
bsw@1248
 | 
   116         if self.event == "issue_state_changed" then
 | 
| 
bsw@1248
 | 
   117           subject = subject .. _("State of #{issue} changed to #{state} / Leading: #{initiative}", { issue = self.issue.name, state = Issue:get_state_name_for_state(self.state), initiative = leading_initiative.display_name })
 | 
| 
bsw@1248
 | 
   118         elseif self.event == "initiative_revoked" then
 | 
| 
bsw@1248
 | 
   119           subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name })
 | 
| 
bsw@405
 | 
   120         end
 | 
| 
bsw@1248
 | 
   121       
 | 
| 
bsw@405
 | 
   122         local success = net.send_mail{
 | 
| 
bsw@405
 | 
   123           envelope_from = config.mail_envelope_from,
 | 
| 
bsw@405
 | 
   124           from          = config.mail_from,
 | 
| 
bsw@405
 | 
   125           reply_to      = config.mail_reply_to,
 | 
| 
bsw@405
 | 
   126           to            = member.notify_email,
 | 
| 
bsw@406
 | 
   127           subject       = subject,
 | 
| 
bsw@405
 | 
   128           content_type  = "text/plain; charset=UTF-8",
 | 
| 
bsw@405
 | 
   129           content       = body
 | 
| 
bsw@405
 | 
   130         }
 | 
| 
bsw@405
 | 
   131     
 | 
| 
bsw@405
 | 
   132       end
 | 
| 
bsw@405
 | 
   133     )
 | 
| 
bsw@287
 | 
   134   end
 | 
| 
bsw@287
 | 
   135   
 | 
| 
bsw@287
 | 
   136 end
 | 
| 
bsw@287
 | 
   137 
 | 
| 
bsw@363
 | 
   138 function Event:send_next_notification()
 | 
| 
bsw@363
 | 
   139   
 | 
| 
bsw@1250
 | 
   140   local notification_event_sent = NotificationEventSent:new_selector()
 | 
| 
bsw@363
 | 
   141     :optional_object_mode()
 | 
| 
bsw@363
 | 
   142     :for_update()
 | 
| 
bsw@363
 | 
   143     :exec()
 | 
| 
bsw@363
 | 
   144     
 | 
| 
bsw@363
 | 
   145   local last_event_id = 0
 | 
| 
bsw@1250
 | 
   146   if notification_event_sent then
 | 
| 
bsw@1250
 | 
   147     last_event_id = notification_event_sent.event_id
 | 
| 
bsw@363
 | 
   148   end
 | 
| 
bsw@363
 | 
   149   
 | 
| 
bsw@287
 | 
   150   local event = Event:new_selector()
 | 
| 
bsw@363
 | 
   151     :add_where{ "event.id > ?", last_event_id }
 | 
| 
bsw@287
 | 
   152     :add_order_by("event.id")
 | 
| 
bsw@287
 | 
   153     :limit(1)
 | 
| 
bsw@287
 | 
   154     :optional_object_mode()
 | 
| 
bsw@287
 | 
   155     :exec()
 | 
| 
bsw@287
 | 
   156 
 | 
| 
bsw@287
 | 
   157   if event then
 | 
| 
bsw@363
 | 
   158     if last_event_id == 0 then
 | 
| 
bsw@1250
 | 
   159       db:query{ "INSERT INTO notification_event_sent (event_id) VALUES (?)", event.id }
 | 
| 
bsw@363
 | 
   160     else
 | 
| 
bsw@1250
 | 
   161       db:query{ "UPDATE notification_event_sent SET event_id = ?", event.id }
 | 
| 
bsw@363
 | 
   162     end
 | 
| 
bsw@287
 | 
   163     
 | 
| 
bsw@287
 | 
   164     event:send_notification()
 | 
| 
bsw@364
 | 
   165     
 | 
| 
bsw@1158
 | 
   166     if config.notification_handler_func then
 | 
| 
bsw@1158
 | 
   167       config.notification_handler_func(event)
 | 
| 
bsw@1158
 | 
   168     end
 | 
| 
bsw@1158
 | 
   169     
 | 
| 
bsw@364
 | 
   170     return true
 | 
| 
bsw@287
 | 
   171 
 | 
| 
bsw@287
 | 
   172   end
 | 
| 
bsw@287
 | 
   173 
 | 
| 
bsw@364
 | 
   174 end
 |