bsw@286: Event = mondelefant.new_class() bsw@286: Event.table = 'event' bsw@286: bsw@286: Event:add_reference{ bsw@286: mode = 'm1', bsw/jbe@1309: to = "Unit", bsw/jbe@1309: this_key = 'unit_id', bsw/jbe@1309: that_key = 'id', bsw/jbe@1309: ref = 'unit', bsw/jbe@1309: } bsw/jbe@1309: bsw/jbe@1309: Event:add_reference{ bsw/jbe@1309: mode = 'm1', bsw/jbe@1309: to = "Area", bsw/jbe@1309: this_key = 'area_id', bsw/jbe@1309: that_key = 'id', bsw/jbe@1309: ref = 'area', bsw/jbe@1309: } bsw/jbe@1309: bsw/jbe@1309: Event:add_reference{ bsw/jbe@1309: mode = 'm1', bsw/jbe@1309: to = "Policy", bsw/jbe@1309: this_key = 'policy_id', bsw/jbe@1309: that_key = 'id', bsw/jbe@1309: ref = 'policy', bsw/jbe@1309: } bsw/jbe@1309: bsw/jbe@1309: Event:add_reference{ bsw/jbe@1309: mode = 'm1', bsw@286: to = "Issue", bsw@286: this_key = 'issue_id', bsw@286: that_key = 'id', bsw@286: ref = 'issue', bsw@287: } bsw@287: bsw@414: Event:add_reference{ bsw@414: mode = 'm1', bsw@712: to = "Initiative", bsw@712: this_key = 'initiative_id', bsw@712: that_key = 'id', bsw@712: ref = 'initiative', bsw@712: } bsw@712: bsw@712: Event:add_reference{ bsw@712: mode = 'm1', bsw@414: to = "Suggestion", bsw@414: this_key = 'suggestion_id', bsw@414: that_key = 'id', bsw@414: ref = 'suggestion', bsw@414: } bsw@414: bsw@414: Event:add_reference{ bsw@414: mode = 'm1', bsw@414: to = "Member", bsw@414: this_key = 'member_id', bsw@414: that_key = 'id', bsw@414: ref = 'member', bsw@414: } bsw@414: bsw/jbe@1309: function Event:by_member_id(member_id) bsw/jbe@1309: return Event:new_selector() bsw/jbe@1309: :add_where{ "member_id = ?", member_id } bsw/jbe@1309: :add_order_by("id DESC") bsw/jbe@1309: :exec() bsw/jbe@1309: end bsw/jbe@1309: bsw@405: function Event.object_get:event_name() bsw@405: return ({ bsw@405: issue_state_changed = _"Issue reached next phase", bsw@405: initiative_created_in_new_issue = _"New issue", bsw@414: initiative_created_in_existing_issue = _"New initiative", bsw@405: initiative_revoked = _"Initiative revoked", bsw@405: new_draft_created = _"New initiative draft", bsw@405: suggestion_created = _"New suggestion" bsw@405: })[self.event] bsw@405: end bsw@405: bsw@405: function Event.object_get:state_name() bsw@972: return Issue:get_state_name_for_state(self.state) bsw@405: end bsw@405: bsw@592: function Event.object:send_notification() bsw@592: bsw@363: local members_to_notify = Member:new_selector() bsw@1302: :join("member_to_notify", nil, "member_to_notify.id = member.id") bsw@1248: :join("event_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } ) bsw@726: -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future bsw@1248: :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval") bsw@866: -- do not notify a member about the events caused by the member bsw@1248: :add_where("event_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id") bsw@1300: :add_where("member.notify_email NOTNULL") bsw@363: :exec() bsw@363: bsw@1159: io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n") bsw@363: bsw@405: for i, member in ipairs(members_to_notify) do bsw@405: local subject bsw@405: local body = "" bsw@405: bsw@405: locale.do_with( bsw@1252: { lang = member.lang or config.default_lang }, bsw@405: function() bsw@1104: bsw@405: body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n" bsw@405: body = body .. _("[event mail] Area: #{name}", { name = self.issue.area.name }) .. "\n" bsw@405: body = body .. _("[event mail] Issue: ##{id}", { id = self.issue_id }) .. "\n\n" bsw@405: body = body .. _("[event mail] Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n" bsw@405: body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n" bsw@405: bsw@1252: local url bsw@1252: bsw@405: if self.initiative_id then bsw@405: url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html" bsw@405: else bsw@405: url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html" bsw@405: end bsw@405: bsw@405: body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n" bsw@405: bsw@1248: local leading_initiative bsw@1248: bsw@405: if self.initiative_id then bsw@405: local initiative = Initiative:by_id(self.initiative_id) bsw@405: body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n" bsw@405: else bsw@405: local initiative_count = Initiative:new_selector() bsw@405: :add_where{ "initiative.issue_id = ?", self.issue_id } bsw@405: :count() bsw@405: local initiatives = Initiative:new_selector() bsw@405: :add_where{ "initiative.issue_id = ?", self.issue_id } bsw@1252: :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id") bsw@405: :limit(3) bsw@405: :exec() bsw@405: for i, initiative in ipairs(initiatives) do bsw@1248: if i == 1 then bsw@1248: leading_initiative = initiative bsw@1248: end bsw@405: body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n" bsw@405: end bsw@405: if initiative_count - 3 > 0 then bsw@764: body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n" bsw@405: end bsw@405: body = body .. "\n" bsw@405: end bsw@405: bsw@1248: subject = config.mail_subject_prefix bsw@1248: bsw@1248: if self.event == "issue_state_changed" then bsw@1252: 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 }) bsw@1248: elseif self.event == "initiative_revoked" then bsw@1248: subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name }) bsw@405: end bsw@1248: bsw@405: local success = net.send_mail{ bsw@405: envelope_from = config.mail_envelope_from, bsw@405: from = config.mail_from, bsw@405: reply_to = config.mail_reply_to, bsw@1305: to = { name = member.name, address = member.notify_email }, bsw@406: subject = subject, bsw@405: content_type = "text/plain; charset=UTF-8", bsw@405: content = body bsw@405: } bsw@405: bsw@405: end bsw@405: ) bsw@287: end bsw@287: bsw@287: end bsw@287: bsw/jbe@1309: function Event:get_events_after_id(id) bsw/jbe@1309: return ( bsw/jbe@1309: Event:new_selector() bsw/jbe@1309: :add_where{ "event.id > ?", id } bsw/jbe@1309: :add_order_by("event.id") bsw/jbe@1309: :exec() bsw/jbe@1309: ) bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: Event.handlers = {} bsw/jbe@1309: bsw/jbe@1309: function Event:add_handler(func) bsw/jbe@1309: table.insert(Event.handlers, func) bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: function Event:process_stream(poll) bsw/jbe@1309: bsw/jbe@1309: db:query('LISTEN "event"') bsw/jbe@1309: bsw/jbe@1309: local last_event_id = EventProcessed:get_last_id() bsw@363: bsw/jbe@1309: while true do bsw/jbe@1309: bsw/jbe@1309: local events = Event:get_events_after_id(last_event_id) bsw/jbe@1309: bsw/jbe@1309: for i_event, event in ipairs(events) do bsw/jbe@1309: last_event_id = event.id bsw/jbe@1309: EventProcessed:set_last_id(last_event_id) bsw/jbe@1309: for i_handler, event_handler in ipairs(Event.handlers) do bsw/jbe@1309: event_handler(event) bsw/jbe@1309: end bsw/jbe@1309: event:send_notification() bsw/jbe@1309: end bsw@287: bsw/jbe@1309: if poll then bsw/jbe@1309: while not db:wait(0) do bsw/jbe@1309: if not poll({[db.fd]=true}, nil, nil, true) then bsw/jbe@1309: goto exit bsw/jbe@1309: end bsw/jbe@1309: end bsw@363: else bsw/jbe@1309: db:wait() bsw@363: end bsw/jbe@1309: while db:wait(0) do end -- discard multiple events bsw@364: bsw@287: end bsw@287: bsw/jbe@1309: ::exit:: bsw/jbe@1309: bsw/jbe@1309: db:query('UNLISTEN "event"') bsw/jbe@1309: bsw@364: end