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