liquid_feedback_frontend
view model/event.lua @ 937:c4e4be01285c
Correct formatting of timestamps and interval in issue and initiatve details added
| author | bsw | 
|---|---|
| date | Sun Nov 04 18:51:07 2012 +0100 (2012-11-04) | 
| parents | 3f0e41887551 | 
| children | 9fbbd04ea889 | 
 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 ({
    49     admission = _"New",
    50     discussion = _"Discussion",
    51     verification = _"Frozen",
    52     voting = _"Voting",
    53     canceled_revoked_before_accepted = _"Cancelled (before accepted due to revocation)",
    54     canceled_issue_not_accepted = _"Cancelled (issue not accepted)",
    55     canceled_after_revocation_during_discussion = _"Cancelled (during discussion due to revocation)",
    56     canceled_after_revocation_during_verification = _"Cancelled (during verification due to revocation)",
    57     calculation = _"Calculation",
    58     canceled_no_initiative_admitted = _"Cancelled (no initiative admitted)",
    59     finished_without_winner = _"Finished (without winner)",
    60     finished_with_winner = _"Finished (with winner)"
    61   })[self.state]
    62 end
    64 function Event.object:send_notification()
    66   local members_to_notify = Member:new_selector()
    67     :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 } )
    68     :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
    69     -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
    70     :add_where("now() - event_seen_by_member.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
    71     -- do not notify a member about the events caused by the member
    72     :add_where("event_seen_by_member.member_id ISNULL OR event_seen_by_member.member_id != member.id")
    73     :exec()
    75   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
    78   local url
    80   for i, member in ipairs(members_to_notify) do
    81     local subject
    82     local body = ""
    84     locale.do_with(
    85       { lang = member.lang or config.default_lang or 'en' },
    86       function()
    87         subject = config.mail_subject_prefix .. " " .. self.event_name
    88         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    89         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    90         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    91         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    92         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
    93         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    95         if self.initiative_id then
    96           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    97         elseif self.suggestion_id then
    98           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
    99         else
   100           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
   101         end
   103         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
   105         if self.initiative_id then
   106           local initiative = Initiative:by_id(self.initiative_id)
   107           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
   108         else
   109           local initiative_count = Initiative:new_selector()
   110             :add_where{ "initiative.issue_id = ?", self.issue_id }
   111             :count()
   112           local initiatives = Initiative:new_selector()
   113             :add_where{ "initiative.issue_id = ?", self.issue_id }
   114             :add_order_by("initiative.supporter_count DESC")
   115             :limit(3)
   116             :exec()
   117           for i, initiative in ipairs(initiatives) do
   118             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   119           end
   120           if initiative_count - 3 > 0 then
   121             body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
   122           end
   123           body = body .. "\n"
   124         end
   126         if self.suggestion_id then
   127           local suggestion = Suggestion:by_id(self.suggestion_id)
   128           body = body .. _("#{name}\n\n", { name = suggestion.name })
   129         end
   131         local success = net.send_mail{
   132           envelope_from = config.mail_envelope_from,
   133           from          = config.mail_from,
   134           reply_to      = config.mail_reply_to,
   135           to            = member.notify_email,
   136           subject       = subject,
   137           content_type  = "text/plain; charset=UTF-8",
   138           content       = body
   139         }
   141       end
   142     )
   143   end
   145 end
   147 function Event:send_next_notification()
   149   local notification_sent = NotificationSent:new_selector()
   150     :optional_object_mode()
   151     :for_update()
   152     :exec()
   154   local last_event_id = 0
   155   if notification_sent then
   156     last_event_id = notification_sent.event_id
   157   end
   159   local event = Event:new_selector()
   160     :add_where{ "event.id > ?", last_event_id }
   161     :add_order_by("event.id")
   162     :limit(1)
   163     :optional_object_mode()
   164     :exec()
   166   if event then
   167     if last_event_id == 0 then
   168       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
   169     else
   170       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
   171     end
   173     event:send_notification()
   175     return true
   177   end
   179 end
   181 function Event:send_notifications_loop()
   183   while true do
   184     local did_work = Event:send_next_notification()
   185     if not did_work then
   186       print "Sleeping 1 second"
   187       os.execute("sleep 1")
   188     end
   189   end
   191 end
