liquid_feedback_frontend

annotate model/event.lua @ 404:475465da327b

Revoved debug code
author bsw
date Thu Mar 08 14:36:04 2012 +0100 (2012-03-08)
parents 203eac3d267d
children 2f121e50d15c
rev   line source
bsw@286 1 Event = mondelefant.new_class()
bsw@286 2 Event.table = 'event'
bsw@286 3
bsw@286 4 Event:add_reference{
bsw@286 5 mode = 'm1',
bsw@286 6 to = "Issue",
bsw@286 7 this_key = 'issue_id',
bsw@286 8 that_key = 'id',
bsw@286 9 ref = 'issue',
bsw@287 10 }
bsw@287 11
bsw@287 12 function Event.object:send_notification()
bsw@287 13
bsw@363 14 local members_to_notify = Member:new_selector()
bsw@363 15 :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 16 :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
bsw@363 17 :exec()
bsw@363 18
bsw@363 19 print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
bsw@363 20
bsw@363 21
bsw@287 22 local url
bsw@287 23
bsw@287 24 local body = ""
bsw@287 25
bsw@403 26 body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
bsw@403 27 body = body .. _("[event mail] Area: #{name}", { name = self.issue.area.name }) .. "\n"
bsw@403 28 body = body .. _("[event mail] Issue: ##{id}", { id = self.issue_id }) .. "\n"
bsw@403 29 body = body .. _("[event mail] Policy: #{policy}", { phase = self.issue.policy.name }) .. "\n"
bsw@403 30 body = body .. _("[event mail] Phase: #{phase}", { phase = self.state }) .. "\n\n"
bsw@403 31 body = body .. _("[event mail] Event: #{event}", { event = self.event }) .. "\n\n"
bsw@287 32
bsw@287 33 if self.initiative_id then
bsw@287 34 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
bsw@287 35 elseif self.suggestion_id then
bsw@287 36 url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
bsw@287 37 else
bsw@287 38 url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
bsw@287 39 end
bsw@287 40
bsw@403 41 body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n"
bsw@287 42
bsw@287 43 if self.initiative_id then
bsw@287 44 local initiative = Initiative:by_id(self.initiative_id)
bsw@403 45 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
bsw@287 46 else
bsw@287 47 local initiative_count = Initiative:new_selector()
bsw@287 48 :add_where{ "initiative.issue_id = ?", self.issue_id }
bsw@287 49 :count()
bsw@287 50 local initiatives = Initiative:new_selector()
bsw@287 51 :add_where{ "initiative.issue_id = ?", self.issue_id }
bsw@287 52 :add_order_by("initiative.supporter_count DESC")
bsw@287 53 :limit(3)
bsw@287 54 :exec()
bsw@287 55 for i, initiative in ipairs(initiatives) do
bsw@403 56 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
bsw@287 57 end
bsw@287 58 if initiative_count - 3 > 0 then
bsw@403 59 body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
bsw@287 60 end
bsw@287 61 body = body .. "\n"
bsw@287 62 end
bsw@287 63
bsw@287 64 if self.suggestion_id then
bsw@287 65 local suggestion = Suggestion:by_id(self.suggestion_id)
bsw@287 66 body = body .. _("#{name}\n\n", { name = suggestion.name })
bsw@287 67 end
bsw@287 68
bsw@363 69 for i, member in ipairs(members_to_notify) do
bsw@363 70 local success = net.send_mail{
bsw@363 71 envelope_from = config.mail_envelope_from,
bsw@363 72 from = config.mail_from,
bsw@363 73 reply_to = config.mail_reply_to,
bsw@363 74 to = member.notify_email,
bsw@363 75 subject = config.mail_subject_prefix .. _("##{id} #{event}", { id = self.issue_id, event = self.event }), content_type = "text/plain; charset=UTF-8",
bsw@363 76 content = body
bsw@363 77 }
bsw@363 78 end
bsw@363 79
bsw@287 80 end
bsw@287 81
bsw@363 82 function Event:send_next_notification()
bsw@363 83
bsw@363 84 local notification_sent = NotificationSent:new_selector()
bsw@363 85 :optional_object_mode()
bsw@363 86 :for_update()
bsw@363 87 :exec()
bsw@363 88
bsw@363 89 local last_event_id = 0
bsw@363 90 if notification_sent then
bsw@363 91 last_event_id = notification_sent.event_id
bsw@363 92 end
bsw@363 93
bsw@287 94 local event = Event:new_selector()
bsw@363 95 :add_where{ "event.id > ?", last_event_id }
bsw@287 96 :add_order_by("event.id")
bsw@287 97 :limit(1)
bsw@287 98 :optional_object_mode()
bsw@287 99 :exec()
bsw@287 100
bsw@287 101 if event then
bsw@363 102 if last_event_id == 0 then
bsw@363 103 db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
bsw@363 104 else
bsw@363 105 db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
bsw@363 106 end
bsw@287 107
bsw@287 108 event:send_notification()
bsw@364 109
bsw@364 110 return true
bsw@287 111
bsw@287 112 end
bsw@287 113
bsw@364 114 end
bsw@364 115
bsw@364 116 function Event:send_notifications_loop()
bsw@364 117
bsw@364 118 while true do
bsw@364 119 local did_work = Event:send_next_notification()
bsw@364 120 if not did_work then
bsw@364 121 print "Sleeping 1 second"
bsw@364 122 os.execute("sleep 1")
bsw@364 123 end
bsw@364 124 end
bsw@364 125
bsw@287 126 end

Impressum / About Us