liquid_feedback_frontend
view model/event.lua @ 729:f52f918b38b8
Cleanup of config and orphaned code
| author | bsw | 
|---|---|
| date | Thu Jun 28 17:07:00 2012 +0200 (2012-06-28) | 
| parents | d3d7a01624f6 | 
| children | a68c02f4f7a6 | 
 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     :exec()
    73   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
    76   local url
    78   for i, member in ipairs(members_to_notify) do
    79     local subject
    80     local body = ""
    82     locale.do_with(
    83       { lang = member.lang or config.default_lang or 'en' },
    84       function()
    85         subject = config.mail_subject_prefix .. " " .. self.event_name
    86         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    87         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    88         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    89         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    90         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
    91         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    93         if self.initiative_id then
    94           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    95         elseif self.suggestion_id then
    96           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
    97         else
    98           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    99         end
   101         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
   103         if self.initiative_id then
   104           local initiative = Initiative:by_id(self.initiative_id)
   105           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
   106         else
   107           local initiative_count = Initiative:new_selector()
   108             :add_where{ "initiative.issue_id = ?", self.issue_id }
   109             :count()
   110           local initiatives = Initiative:new_selector()
   111             :add_where{ "initiative.issue_id = ?", self.issue_id }
   112             :add_order_by("initiative.supporter_count DESC")
   113             :limit(3)
   114             :exec()
   115           for i, initiative in ipairs(initiatives) do
   116             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   117           end
   118           if initiative_count - 3 > 0 then
   119             body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
   120           end
   121           body = body .. "\n"
   122         end
   124         if self.suggestion_id then
   125           local suggestion = Suggestion:by_id(self.suggestion_id)
   126           body = body .. _("#{name}\n\n", { name = suggestion.name })
   127         end
   129         local success = net.send_mail{
   130           envelope_from = config.mail_envelope_from,
   131           from          = config.mail_from,
   132           reply_to      = config.mail_reply_to,
   133           to            = member.notify_email,
   134           subject       = subject,
   135           content_type  = "text/plain; charset=UTF-8",
   136           content       = body
   137         }
   139       end
   140     )
   141   end
   143 end
   145 function Event:send_next_notification()
   147   local notification_sent = NotificationSent:new_selector()
   148     :optional_object_mode()
   149     :for_update()
   150     :exec()
   152   local last_event_id = 0
   153   if notification_sent then
   154     last_event_id = notification_sent.event_id
   155   end
   157   local event = Event:new_selector()
   158     :add_where{ "event.id > ?", last_event_id }
   159     :add_order_by("event.id")
   160     :limit(1)
   161     :optional_object_mode()
   162     :exec()
   164   if event then
   165     if last_event_id == 0 then
   166       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
   167     else
   168       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
   169     end
   171     event:send_notification()
   173     return true
   175   end
   177 end
   179 function Event:send_notifications_loop()
   181   while true do
   182     local did_work = Event:send_next_notification()
   183     if not did_work then
   184       print "Sleeping 1 second"
   185       os.execute("sleep 1")
   186     end
   187   end
   189 end
