bsw@286: Event = mondelefant.new_class() bsw@286: Event.table = 'event' bsw@286: bsw@286: Event:add_reference{ bsw@286: 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@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@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@405: return ({ bsw@405: admission = _"New", bsw@405: discussion = _"Discussion", bsw@405: verification = _"Frozen", bsw@405: voting = _"Voting", bsw@405: canceled_revoked_before_accepted = _"Cancelled (before accepted due to revocation)", bsw@405: canceled_issue_not_accepted = _"Cancelled (issue not accepted)", bsw@405: canceled_after_revocation_during_discussion = _"Cancelled (during discussion due to revocation)", bsw@405: canceled_after_revocation_during_verification = _"Cancelled (during verification due to revocation)", bsw@405: calculation = _"Calculation", bsw@405: canceled_no_initiative_admitted = _"Cancelled (no initiative admitted)", bsw@405: finished_without_winner = _"Finished (without winner)", bsw@410: finished_with_winner = _"Finished (with winner)" bsw@405: })[self.state] bsw@405: end bsw@405: bsw@287: function Event.object:send_notification() bsw@287: bsw@363: local members_to_notify = Member:new_selector() bsw@363: :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 } ) bsw@363: :add_where("member.activated NOTNULL AND member.notify_email NOTNULL") bsw@363: :exec() bsw@363: bsw@363: print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify })) bsw@363: bsw@363: bsw@287: local url bsw@287: 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@405: { lang = member.lang or config.default_lang or 'en' }, bsw@405: function() bsw@407: subject = config.mail_subject_prefix .. " " .. self.event_name 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] Event: #{event}", { event = self.event_name }) .. "\n\n" bsw@405: body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n" bsw@405: bsw@405: if self.initiative_id then bsw@405: url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html" bsw@405: elseif self.suggestion_id then bsw@405: url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_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@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@405: :add_order_by("initiative.supporter_count DESC") bsw@405: :limit(3) bsw@405: :exec() bsw@405: for i, initiative in ipairs(initiatives) do 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@405: body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n" bsw@405: end bsw@405: body = body .. "\n" bsw@405: end bsw@405: bsw@405: if self.suggestion_id then bsw@405: local suggestion = Suggestion:by_id(self.suggestion_id) bsw@405: body = body .. _("#{name}\n\n", { name = suggestion.name }) bsw@405: end bsw@287: 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@405: to = 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@363: function Event:send_next_notification() bsw@363: bsw@363: local notification_sent = NotificationSent:new_selector() bsw@363: :optional_object_mode() bsw@363: :for_update() bsw@363: :exec() bsw@363: bsw@363: local last_event_id = 0 bsw@363: if notification_sent then bsw@363: last_event_id = notification_sent.event_id bsw@363: end bsw@363: bsw@287: local event = Event:new_selector() bsw@363: :add_where{ "event.id > ?", last_event_id } bsw@287: :add_order_by("event.id") bsw@287: :limit(1) bsw@287: :optional_object_mode() bsw@287: :exec() bsw@287: bsw@287: if event then bsw@363: if last_event_id == 0 then bsw@363: db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id } bsw@363: else bsw@363: db:query{ "UPDATE notification_sent SET event_id = ?", event.id } bsw@363: end bsw@287: bsw@287: event:send_notification() bsw@364: bsw@364: return true bsw@287: bsw@287: end bsw@287: bsw@364: end bsw@364: bsw@364: function Event:send_notifications_loop() bsw@364: bsw@364: while true do bsw@364: local did_work = Event:send_next_notification() bsw@364: if not did_work then bsw@364: print "Sleeping 1 second" bsw@364: os.execute("sleep 1") bsw@364: end bsw@364: end bsw@364: bsw@287: end