liquid_feedback_frontend
view model/event.lua @ 1092:5b65ea5c24f5
Fixed free timings in poll mode
| author | bsw | 
|---|---|
| date | Fri Oct 17 13:03:31 2014 +0200 (2014-10-17) | 
| parents | 9fbbd04ea889 | 
| children | c037558f6610 | 
 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_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 } )
    55     :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
    56     -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
    57     :add_where("now() - event_seen_by_member.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
    58     -- do not notify a member about the events caused by the member
    59     :add_where("event_seen_by_member.member_id ISNULL OR event_seen_by_member.member_id != member.id")
    60     :exec()
    62   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
    65   local url
    67   for i, member in ipairs(members_to_notify) do
    68     local subject
    69     local body = ""
    71     locale.do_with(
    72       { lang = member.lang or config.default_lang or 'en' },
    73       function()
    74         subject = config.mail_subject_prefix .. " " .. self.event_name
    75         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    76         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    77         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    78         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    79         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
    80         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    82         if self.initiative_id then
    83           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    84         elseif self.suggestion_id then
    85           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
    86         else
    87           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    88         end
    90         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
    92         if self.initiative_id then
    93           local initiative = Initiative:by_id(self.initiative_id)
    94           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
    95         else
    96           local initiative_count = Initiative:new_selector()
    97             :add_where{ "initiative.issue_id = ?", self.issue_id }
    98             :count()
    99           local initiatives = Initiative:new_selector()
   100             :add_where{ "initiative.issue_id = ?", self.issue_id }
   101             :add_order_by("initiative.supporter_count DESC")
   102             :limit(3)
   103             :exec()
   104           for i, initiative in ipairs(initiatives) do
   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         if self.suggestion_id then
   114           local suggestion = Suggestion:by_id(self.suggestion_id)
   115           body = body .. _("#{name}\n\n", { name = suggestion.name })
   116         end
   118         local success = net.send_mail{
   119           envelope_from = config.mail_envelope_from,
   120           from          = config.mail_from,
   121           reply_to      = config.mail_reply_to,
   122           to            = member.notify_email,
   123           subject       = subject,
   124           content_type  = "text/plain; charset=UTF-8",
   125           content       = body
   126         }
   128       end
   129     )
   130   end
   132 end
   134 function Event:send_next_notification()
   136   local notification_sent = NotificationSent:new_selector()
   137     :optional_object_mode()
   138     :for_update()
   139     :exec()
   141   local last_event_id = 0
   142   if notification_sent then
   143     last_event_id = notification_sent.event_id
   144   end
   146   local event = Event:new_selector()
   147     :add_where{ "event.id > ?", last_event_id }
   148     :add_order_by("event.id")
   149     :limit(1)
   150     :optional_object_mode()
   151     :exec()
   153   if event then
   154     if last_event_id == 0 then
   155       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
   156     else
   157       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
   158     end
   160     event:send_notification()
   162     return true
   164   end
   166 end
   168 function Event:send_notifications_loop()
   170   while true do
   171     local did_work = Event:send_next_notification()
   172     if not did_work then
   173       print "Sleeping 1 second"
   174       os.execute("sleep 1")
   175     end
   176   end
   178 end
