| 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@414
 | 
    14   to            = "Suggestion",
 | 
| 
bsw@414
 | 
    15   this_key      = 'suggestion_id',
 | 
| 
bsw@414
 | 
    16   that_key      = 'id',
 | 
| 
bsw@414
 | 
    17   ref           = 'suggestion',
 | 
| 
bsw@414
 | 
    18 }
 | 
| 
bsw@414
 | 
    19 
 | 
| 
bsw@414
 | 
    20 Event:add_reference{
 | 
| 
bsw@414
 | 
    21   mode          = 'm1',
 | 
| 
bsw@414
 | 
    22   to            = "Member",
 | 
| 
bsw@414
 | 
    23   this_key      = 'member_id',
 | 
| 
bsw@414
 | 
    24   that_key      = 'id',
 | 
| 
bsw@414
 | 
    25   ref           = 'member',
 | 
| 
bsw@414
 | 
    26 }
 | 
| 
bsw@414
 | 
    27 
 | 
| 
bsw@405
 | 
    28 function Event.object_get:event_name()
 | 
| 
bsw@405
 | 
    29   return ({
 | 
| 
bsw@405
 | 
    30     issue_state_changed = _"Issue reached next phase",
 | 
| 
bsw@405
 | 
    31     initiative_created_in_new_issue = _"New issue",
 | 
| 
bsw@414
 | 
    32     initiative_created_in_existing_issue = _"New initiative",
 | 
| 
bsw@405
 | 
    33     initiative_revoked = _"Initiative revoked",
 | 
| 
bsw@405
 | 
    34     new_draft_created = _"New initiative draft",
 | 
| 
bsw@405
 | 
    35     suggestion_created = _"New suggestion"
 | 
| 
bsw@405
 | 
    36   })[self.event]
 | 
| 
bsw@405
 | 
    37 end
 | 
| 
bsw@405
 | 
    38   
 | 
| 
bsw@405
 | 
    39 function Event.object_get:state_name()
 | 
| 
bsw@405
 | 
    40   return ({
 | 
| 
bsw@405
 | 
    41     admission = _"New",
 | 
| 
bsw@405
 | 
    42     discussion = _"Discussion",
 | 
| 
bsw@405
 | 
    43     verification = _"Frozen",
 | 
| 
bsw@405
 | 
    44     voting = _"Voting",
 | 
| 
bsw@405
 | 
    45     canceled_revoked_before_accepted = _"Cancelled (before accepted due to revocation)",
 | 
| 
bsw@405
 | 
    46     canceled_issue_not_accepted = _"Cancelled (issue not accepted)",
 | 
| 
bsw@405
 | 
    47     canceled_after_revocation_during_discussion = _"Cancelled (during discussion due to revocation)",
 | 
| 
bsw@405
 | 
    48     canceled_after_revocation_during_verification = _"Cancelled (during verification due to revocation)",
 | 
| 
bsw@405
 | 
    49     calculation = _"Calculation",
 | 
| 
bsw@405
 | 
    50     canceled_no_initiative_admitted = _"Cancelled (no initiative admitted)",
 | 
| 
bsw@405
 | 
    51     finished_without_winner = _"Finished (without winner)",
 | 
| 
bsw@410
 | 
    52     finished_with_winner = _"Finished (with winner)"
 | 
| 
bsw@405
 | 
    53   })[self.state]
 | 
| 
bsw@405
 | 
    54 end
 | 
| 
bsw@405
 | 
    55   
 | 
| 
bsw@287
 | 
    56 function Event.object:send_notification() 
 | 
| 
bsw@287
 | 
    57 
 | 
| 
bsw@363
 | 
    58   local members_to_notify = Member:new_selector()
 | 
| 
bsw@363
 | 
    59     :join("event_seen_by_member", nil, { "event_seen_by_member.seen_by_member_id = member.id AND event_seen_by_member.notify_level <= member.notify_level AND event_seen_by_member.id = ?", self.id } )
 | 
| 
bsw@363
 | 
    60     :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
 | 
| 
bsw@363
 | 
    61     :exec()
 | 
| 
bsw@363
 | 
    62     
 | 
| 
bsw@363
 | 
    63   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
 | 
| 
bsw@363
 | 
    64 
 | 
| 
bsw@363
 | 
    65 
 | 
| 
bsw@287
 | 
    66   local url
 | 
| 
bsw@287
 | 
    67 
 | 
| 
bsw@405
 | 
    68   for i, member in ipairs(members_to_notify) do
 | 
| 
bsw@405
 | 
    69     local subject
 | 
| 
bsw@405
 | 
    70     local body = ""
 | 
| 
bsw@405
 | 
    71     
 | 
| 
bsw@405
 | 
    72     locale.do_with(
 | 
| 
bsw@405
 | 
    73       { lang = member.lang or config.default_lang or 'en' },
 | 
| 
bsw@405
 | 
    74       function()
 | 
| 
bsw@407
 | 
    75         subject = config.mail_subject_prefix .. " " .. self.event_name
 | 
| 
bsw@405
 | 
    76         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
 | 
| 
bsw@405
 | 
    77         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
 | 
| 
bsw@405
 | 
    78         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
 | 
| 
bsw@405
 | 
    79         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    80         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    81         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    82 
 | 
| 
bsw@405
 | 
    83         if self.initiative_id then
 | 
| 
bsw@405
 | 
    84           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
 | 
| 
bsw@405
 | 
    85         elseif self.suggestion_id then
 | 
| 
bsw@405
 | 
    86           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
 | 
| 
bsw@405
 | 
    87         else
 | 
| 
bsw@405
 | 
    88           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
 | 
| 
bsw@405
 | 
    89         end
 | 
| 
bsw@405
 | 
    90         
 | 
| 
bsw@405
 | 
    91         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
 | 
| 
bsw@405
 | 
    92         
 | 
| 
bsw@405
 | 
    93         if self.initiative_id then
 | 
| 
bsw@405
 | 
    94           local initiative = Initiative:by_id(self.initiative_id)
 | 
| 
bsw@405
 | 
    95           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
 | 
| 
bsw@405
 | 
    96         else
 | 
| 
bsw@405
 | 
    97           local initiative_count = Initiative:new_selector()
 | 
| 
bsw@405
 | 
    98             :add_where{ "initiative.issue_id = ?", self.issue_id }
 | 
| 
bsw@405
 | 
    99             :count()
 | 
| 
bsw@405
 | 
   100           local initiatives = Initiative:new_selector()
 | 
| 
bsw@405
 | 
   101             :add_where{ "initiative.issue_id = ?", self.issue_id }
 | 
| 
bsw@405
 | 
   102             :add_order_by("initiative.supporter_count DESC")
 | 
| 
bsw@405
 | 
   103             :limit(3)
 | 
| 
bsw@405
 | 
   104             :exec()
 | 
| 
bsw@405
 | 
   105           for i, initiative in ipairs(initiatives) do
 | 
| 
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@405
 | 
   109             body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
 | 
| 
bsw@405
 | 
   110           end
 | 
| 
bsw@405
 | 
   111           body = body .. "\n"
 | 
| 
bsw@405
 | 
   112         end
 | 
| 
bsw@405
 | 
   113         
 | 
| 
bsw@405
 | 
   114         if self.suggestion_id then
 | 
| 
bsw@405
 | 
   115           local suggestion = Suggestion:by_id(self.suggestion_id)
 | 
| 
bsw@405
 | 
   116           body = body .. _("#{name}\n\n", { name = suggestion.name })
 | 
| 
bsw@405
 | 
   117         end
 | 
| 
bsw@287
 | 
   118   
 | 
| 
bsw@405
 | 
   119         local success = net.send_mail{
 | 
| 
bsw@405
 | 
   120           envelope_from = config.mail_envelope_from,
 | 
| 
bsw@405
 | 
   121           from          = config.mail_from,
 | 
| 
bsw@405
 | 
   122           reply_to      = config.mail_reply_to,
 | 
| 
bsw@405
 | 
   123           to            = member.notify_email,
 | 
| 
bsw@406
 | 
   124           subject       = subject,
 | 
| 
bsw@405
 | 
   125           content_type  = "text/plain; charset=UTF-8",
 | 
| 
bsw@405
 | 
   126           content       = body
 | 
| 
bsw@405
 | 
   127         }
 | 
| 
bsw@405
 | 
   128     
 | 
| 
bsw@405
 | 
   129       end
 | 
| 
bsw@405
 | 
   130     )
 | 
| 
bsw@287
 | 
   131   end
 | 
| 
bsw@287
 | 
   132   
 | 
| 
bsw@287
 | 
   133 end
 | 
| 
bsw@287
 | 
   134 
 | 
| 
bsw@363
 | 
   135 function Event:send_next_notification()
 | 
| 
bsw@363
 | 
   136   
 | 
| 
bsw@363
 | 
   137   local notification_sent = NotificationSent:new_selector()
 | 
| 
bsw@363
 | 
   138     :optional_object_mode()
 | 
| 
bsw@363
 | 
   139     :for_update()
 | 
| 
bsw@363
 | 
   140     :exec()
 | 
| 
bsw@363
 | 
   141     
 | 
| 
bsw@363
 | 
   142   local last_event_id = 0
 | 
| 
bsw@363
 | 
   143   if notification_sent then
 | 
| 
bsw@363
 | 
   144     last_event_id = notification_sent.event_id
 | 
| 
bsw@363
 | 
   145   end
 | 
| 
bsw@363
 | 
   146   
 | 
| 
bsw@287
 | 
   147   local event = Event:new_selector()
 | 
| 
bsw@363
 | 
   148     :add_where{ "event.id > ?", last_event_id }
 | 
| 
bsw@287
 | 
   149     :add_order_by("event.id")
 | 
| 
bsw@287
 | 
   150     :limit(1)
 | 
| 
bsw@287
 | 
   151     :optional_object_mode()
 | 
| 
bsw@287
 | 
   152     :exec()
 | 
| 
bsw@287
 | 
   153 
 | 
| 
bsw@287
 | 
   154   if event then
 | 
| 
bsw@363
 | 
   155     if last_event_id == 0 then
 | 
| 
bsw@363
 | 
   156       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
 | 
| 
bsw@363
 | 
   157     else
 | 
| 
bsw@363
 | 
   158       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
 | 
| 
bsw@363
 | 
   159     end
 | 
| 
bsw@287
 | 
   160     
 | 
| 
bsw@287
 | 
   161     event:send_notification()
 | 
| 
bsw@364
 | 
   162     
 | 
| 
bsw@364
 | 
   163     return true
 | 
| 
bsw@287
 | 
   164 
 | 
| 
bsw@287
 | 
   165   end
 | 
| 
bsw@287
 | 
   166 
 | 
| 
bsw@364
 | 
   167 end
 | 
| 
bsw@364
 | 
   168 
 | 
| 
bsw@364
 | 
   169 function Event:send_notifications_loop()
 | 
| 
bsw@364
 | 
   170 
 | 
| 
bsw@364
 | 
   171   while true do
 | 
| 
bsw@364
 | 
   172     local did_work = Event:send_next_notification()
 | 
| 
bsw@364
 | 
   173     if not did_work then
 | 
| 
bsw@364
 | 
   174       print "Sleeping 1 second"
 | 
| 
bsw@364
 | 
   175       os.execute("sleep 1")
 | 
| 
bsw@364
 | 
   176     end
 | 
| 
bsw@364
 | 
   177   end
 | 
| 
bsw@364
 | 
   178   
 | 
| 
bsw@287
 | 
   179 end |