liquid_feedback_frontend
view model/event.lua @ 1825:7b04f14c6d0a
Check area configuration only for new issues
| author | bsw | 
|---|---|
| date | Thu Feb 03 11:47:41 2022 +0100 (2022-02-03) | 
| parents | 32cc544d5a5b | 
| children | 
 line source
     1 Event = mondelefant.new_class()
     2 Event.table = 'event'
     4 Event:add_reference{
     5   mode          = 'm1',
     6   to            = "Unit",
     7   this_key      = 'unit_id',
     8   that_key      = 'id',
     9   ref           = 'unit',
    10 }
    12 Event:add_reference{
    13   mode          = 'm1',
    14   to            = "Area",
    15   this_key      = 'area_id',
    16   that_key      = 'id',
    17   ref           = 'area',
    18 }
    20 Event:add_reference{
    21   mode          = 'm1',
    22   to            = "Policy",
    23   this_key      = 'policy_id',
    24   that_key      = 'id',
    25   ref           = 'policy',
    26 }
    28 Event:add_reference{
    29   mode          = 'm1',
    30   to            = "Issue",
    31   this_key      = 'issue_id',
    32   that_key      = 'id',
    33   ref           = 'issue',
    34 }
    36 Event:add_reference{
    37   mode          = 'm1',
    38   to            = "Initiative",
    39   this_key      = 'initiative_id',
    40   that_key      = 'id',
    41   ref           = 'initiative',
    42 }
    44 Event:add_reference{
    45   mode          = 'm1',
    46   to            = "Suggestion",
    47   this_key      = 'suggestion_id',
    48   that_key      = 'id',
    49   ref           = 'suggestion',
    50 }
    52 Event:add_reference{
    53   mode          = 'm1',
    54   to            = "Member",
    55   this_key      = 'member_id',
    56   that_key      = 'id',
    57   ref           = 'member',
    58 }
    60 function Event:by_member_id(member_id)
    61   return Event:new_selector()
    62     :add_where{ "member_id = ?", member_id }
    63     :add_order_by("id DESC")
    64     :exec()
    65 end
    67 function Event.object_get:event_name()
    68   return ({
    69     issue_state_changed = _"Issue reached next phase",
    70     initiative_created_in_new_issue = _"New issue",
    71     initiative_created_in_existing_issue = _"New initiative",
    72     initiative_revoked = _"Initiative revoked",
    73     new_draft_created = _"New initiative draft",
    74     suggestion_created = _"New suggestion"
    75   })[self.event]
    76 end
    78 function Event.object_get:state_name()
    79   return Issue:get_state_name_for_state(self.state)
    80 end
    82 function Event.object:send_notification()
    84   local members_to_notify = Member:new_selector()
    85     :join("member_to_notify", nil, "member_to_notify.id = member.id")
    86     :join("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } )
    87     -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
    88     :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
    89     -- do not notify a member about the events caused by the member
    90     :add_where("event_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id")
    91     :add_where("member.notify_email NOTNULL")
    92     :exec()
    94   io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
    96   for i, member in ipairs(members_to_notify) do
    97     local subject
    98     local body = ""
   100     locale.do_with(
   101       { lang = member.lang or config.default_lang },
   102       function()
   104         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
   105         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
   106         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
   107         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
   108         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
   110         local url
   112         if self.initiative_id then
   113           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
   114         else
   115           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
   116         end
   118         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
   120         local leading_initiative
   122         if self.initiative_id then
   123           local initiative = Initiative:by_id(self.initiative_id)
   124           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
   125         else
   126           local initiative_count = Initiative:new_selector()
   127             :add_where{ "initiative.issue_id = ?", self.issue_id }
   128             :count()
   129           local initiatives = Initiative:new_selector()
   130             :add_where{ "initiative.issue_id = ?", self.issue_id }
   131             :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id")
   132             :limit(3)
   133             :exec()
   134           for i, initiative in ipairs(initiatives) do
   135             if i == 1 then
   136               leading_initiative = initiative
   137             end
   138             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   139           end
   140           if initiative_count - 3 > 0 then
   141             body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
   142           end
   143           body = body .. "\n"
   144         end
   146         subject = config.mail_subject_prefix
   148         if self.event == "issue_state_changed" then
   149           subject = subject .. _("State of #{issue} changed to #{state} / #{initiative}", { issue = self.issue.name, state = Issue:get_state_name_for_state(self.state), initiative = leading_initiative.display_name })
   150         elseif self.event == "initiative_revoked" then
   151           subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name })
   152         end
   154         local success = net.send_mail{
   155           envelope_from = config.mail_envelope_from,
   156           from          = config.mail_from,
   157           reply_to      = config.mail_reply_to,
   158           to            = { name = member.name, address = member.notify_email },
   159           subject       = subject,
   160           content_type  = "text/plain; charset=UTF-8",
   161           content       = body
   162         }
   164       end
   165     )
   166   end
   168 end
   170 function Event:get_events_after_id(id)
   171   return (
   172     Event:new_selector()
   173       :add_where{ "event.id > ?", id }
   174       :add_order_by("event.id")
   175       :exec()
   176   )
   177 end
   181 Event.handlers = {}
   183 function Event:add_handler(func)
   184   table.insert(Event.handlers, func)
   185 end
   187 function Event:process_stream(poll)
   189   db:query('LISTEN "event"')
   191   local last_event_id = EventProcessed:get_last_id()
   193   while true do
   195     local events = Event:get_events_after_id(last_event_id)
   197     for i_event, event in ipairs(events) do
   198       last_event_id = event.id
   199       EventProcessed:set_last_id(last_event_id)
   200       for i_handler, event_handler in ipairs(Event.handlers) do
   201         event_handler(event)
   202       end
   203       event:send_notification()
   204     end
   206     if poll then
   207       while not db:wait(0) do
   208         if not poll({[db.fd]=true}, nil, nil, true) then
   209           goto exit
   210         end
   211       end
   212     else
   213       db:wait()
   214     end
   215     while db:wait(0) do end  -- discard multiple events
   217   end
   219   ::exit::
   221   db:query('UNLISTEN "event"')
   223 end
