| 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@287 | 12 function Event.object:send_notification() | 
| bsw@287 | 13 | 
| bsw@363 | 14   local members_to_notify = Member:new_selector() | 
| bsw@363 | 15     :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 | 16     :add_where("member.activated NOTNULL AND member.notify_email NOTNULL") | 
| bsw@363 | 17     :exec() | 
| bsw@363 | 18 | 
| bsw@363 | 19   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify })) | 
| bsw@363 | 20 | 
| bsw@363 | 21 | 
| bsw@287 | 22   local url | 
| bsw@287 | 23 | 
| bsw@287 | 24   local body = "" | 
| bsw@287 | 25 | 
| bsw@287 | 26   body = body .. _("      Unit: #{name}\n", { name = self.issue.area.unit.name }) | 
| bsw@287 | 27   body = body .. _("      Area: #{name}\n", { name = self.issue.area.name }) | 
| bsw@287 | 28   body = body .. _("     Issue: ##{id}\n", { id = self.issue_id }) | 
| bsw@287 | 29   body = body .. _("    Policy: #{phase}\n", { phase = self.issue.policy.name }) | 
| bsw@287 | 30   body = body .. _("     Phase: #{phase}\n\n", { phase = self.state }) | 
| bsw@287 | 31   body = body .. _("     Event: #{event}\n\n", { event = self.event }) | 
| bsw@287 | 32 | 
| bsw@287 | 33   if self.initiative_id then | 
| bsw@287 | 34     url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html" | 
| bsw@287 | 35   elseif self.suggestion_id then | 
| bsw@287 | 36     url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html" | 
| bsw@287 | 37   else | 
| bsw@287 | 38     url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html" | 
| bsw@287 | 39   end | 
| bsw@287 | 40 | 
| bsw@287 | 41   body = body .. _("       URL: #{url}\n\n", { url = url }) | 
| bsw@287 | 42 | 
| bsw@287 | 43   if self.initiative_id then | 
| bsw@287 | 44     local initiative = Initiative:by_id(self.initiative_id) | 
| bsw@287 | 45     body = body .. _("i#{id}: #{name}\n\n", { id = initiative.id, name = initiative.name }) | 
| bsw@287 | 46   else | 
| bsw@287 | 47     local initiative_count = Initiative:new_selector() | 
| bsw@287 | 48       :add_where{ "initiative.issue_id = ?", self.issue_id } | 
| bsw@287 | 49       :count() | 
| bsw@287 | 50     local initiatives = Initiative:new_selector() | 
| bsw@287 | 51       :add_where{ "initiative.issue_id = ?", self.issue_id } | 
| bsw@287 | 52       :add_order_by("initiative.supporter_count DESC") | 
| bsw@287 | 53       :limit(3) | 
| bsw@287 | 54       :exec() | 
| bsw@287 | 55     for i, initiative in ipairs(initiatives) do | 
| bsw@287 | 56       body = body .. _("i#{id}: #{name}\n", { id = initiative.id, name = initiative.name }) | 
| bsw@287 | 57     end | 
| bsw@287 | 58     if initiative_count - 3 > 0 then | 
| bsw@287 | 59       body = body .. _("and #{count} more initiatives\n", { count = initiative_count }) | 
| bsw@287 | 60     end | 
| bsw@287 | 61     body = body .. "\n" | 
| bsw@287 | 62   end | 
| bsw@287 | 63 | 
| bsw@287 | 64   if self.suggestion_id then | 
| bsw@287 | 65     local suggestion = Suggestion:by_id(self.suggestion_id) | 
| bsw@287 | 66     body = body .. _("#{name}\n\n", { name = suggestion.name }) | 
| bsw@287 | 67   end | 
| bsw@287 | 68 | 
| bsw@363 | 69   for i, member in ipairs(members_to_notify) do | 
| bsw@363 | 70     local success = net.send_mail{ | 
| bsw@363 | 71       envelope_from = config.mail_envelope_from, | 
| bsw@363 | 72       from          = config.mail_from, | 
| bsw@363 | 73       reply_to      = config.mail_reply_to, | 
| bsw@363 | 74       to            = member.notify_email, | 
| bsw@363 | 75       subject       = config.mail_subject_prefix .. _("##{id} #{event}", { id = self.issue_id, event = self.event }),      content_type  = "text/plain; charset=UTF-8", | 
| bsw@363 | 76       content       = body | 
| bsw@363 | 77     } | 
| bsw@363 | 78   end | 
| bsw@363 | 79 | 
| bsw@363 | 80   print(body) | 
| bsw@363 | 81   print("") | 
| bsw@364 | 82 | 
| bsw@287 | 83 end | 
| bsw@287 | 84 | 
| bsw@363 | 85 function Event:send_next_notification() | 
| bsw@363 | 86 | 
| bsw@363 | 87   local notification_sent = NotificationSent:new_selector() | 
| bsw@363 | 88     :optional_object_mode() | 
| bsw@363 | 89     :for_update() | 
| bsw@363 | 90     :exec() | 
| bsw@363 | 91 | 
| bsw@363 | 92   local last_event_id = 0 | 
| bsw@363 | 93   if notification_sent then | 
| bsw@363 | 94     last_event_id = notification_sent.event_id | 
| bsw@363 | 95   end | 
| bsw@363 | 96 | 
| bsw@287 | 97   local event = Event:new_selector() | 
| bsw@363 | 98     :add_where{ "event.id > ?", last_event_id } | 
| bsw@287 | 99     :add_order_by("event.id") | 
| bsw@287 | 100     :limit(1) | 
| bsw@287 | 101     :optional_object_mode() | 
| bsw@287 | 102     :exec() | 
| bsw@287 | 103 | 
| bsw@287 | 104   if event then | 
| bsw@363 | 105     if last_event_id == 0 then | 
| bsw@363 | 106       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id } | 
| bsw@363 | 107     else | 
| bsw@363 | 108       db:query{ "UPDATE notification_sent SET event_id = ?", event.id } | 
| bsw@363 | 109     end | 
| bsw@287 | 110 | 
| bsw@287 | 111     event:send_notification() | 
| bsw@364 | 112 | 
| bsw@364 | 113     return true | 
| bsw@287 | 114 | 
| bsw@287 | 115   end | 
| bsw@287 | 116 | 
| bsw@364 | 117 end | 
| bsw@364 | 118 | 
| bsw@364 | 119 function Event:send_notifications_loop() | 
| bsw@364 | 120 | 
| bsw@364 | 121   while true do | 
| bsw@364 | 122     local did_work = Event:send_next_notification() | 
| bsw@364 | 123     if not did_work then | 
| bsw@364 | 124       print "Sleeping 1 second" | 
| bsw@364 | 125       os.execute("sleep 1") | 
| bsw@364 | 126     end | 
| bsw@364 | 127   end | 
| bsw@364 | 128 | 
| bsw@287 | 129 end |