liquid_feedback_frontend
view model/event.lua @ 1200:8e36f08eaac6
Added Moonbridge to about page
| author | bsw | 
|---|---|
| date | Tue Jun 23 19:57:10 2015 +0200 (2015-06-23) | 
| parents | dafce7add549 | 
| children | c0fd12b97d65 | 
 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   io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
    64   local url
    66   for i, member in ipairs(members_to_notify) do
    67     local subject
    68     local body = ""
    70     locale.do_with(
    71       { lang = member.lang or config.default_lang or 'en' },
    72       function()
    74 	local suggestion
    75         if self.suggestion_id then
    76 	  suggestion = Suggestion:by_id(self.suggestion_id)
    77 	  if not suggestion then
    78 	    return
    79 	  end
    80 	end
    82 	subject = config.mail_subject_prefix .. " " .. self.event_name
    83         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    84         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    85         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    86         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    87         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
    88         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    90         if self.initiative_id then
    91           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    92         elseif self.suggestion_id then
    93           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
    94         else
    95           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    96         end
    98         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
   100         if self.initiative_id then
   101           local initiative = Initiative:by_id(self.initiative_id)
   102           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
   103         else
   104           local initiative_count = Initiative:new_selector()
   105             :add_where{ "initiative.issue_id = ?", self.issue_id }
   106             :count()
   107           local initiatives = Initiative:new_selector()
   108             :add_where{ "initiative.issue_id = ?", self.issue_id }
   109             :add_order_by("initiative.supporter_count DESC")
   110             :limit(3)
   111             :exec()
   112           for i, initiative in ipairs(initiatives) do
   113             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   114           end
   115           if initiative_count - 3 > 0 then
   116             body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
   117           end
   118           body = body .. "\n"
   119         end
   121         if suggestion then
   122           body = body .. _("#{name}\n\n", { name = suggestion.name })
   123         end
   125         local success = net.send_mail{
   126           envelope_from = config.mail_envelope_from,
   127           from          = config.mail_from,
   128           reply_to      = config.mail_reply_to,
   129           to            = member.notify_email,
   130           subject       = subject,
   131           content_type  = "text/plain; charset=UTF-8",
   132           content       = body
   133         }
   135       end
   136     )
   137   end
   139 end
   141 function Event:send_next_notification()
   143   local notification_sent = NotificationSent:new_selector()
   144     :optional_object_mode()
   145     :for_update()
   146     :exec()
   148   local last_event_id = 0
   149   if notification_sent then
   150     last_event_id = notification_sent.event_id
   151   end
   153   local event = Event:new_selector()
   154     :add_where{ "event.id > ?", last_event_id }
   155     :add_order_by("event.id")
   156     :limit(1)
   157     :optional_object_mode()
   158     :exec()
   160   if event then
   161     if last_event_id == 0 then
   162       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
   163     else
   164       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
   165     end
   167     event:send_notification()
   169     if config.notification_handler_func then
   170       config.notification_handler_func(event)
   171     end
   173     return true
   175   end
   177 end
   179 function Event:send_pending_notifications()
   180   while true do
   181     if not Event:send_next_notification() then
   182       break 
   183     end
   184   end
   185 end
   187 function Event:send_notifications_loop()
   189   while true do
   190     local did_work = Event:send_next_notification()
   191     if not did_work then
   192       print "Sleeping 5 second"
   193       os.execute("sleep 5")
   194     end
   195   end
   197 end
