liquid_feedback_frontend
view 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 |
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 function Event.object:send_notification()
14 local members_to_notify = Member:new_selector()
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 } )
16 :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
17 :exec()
19 print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
22 local url
24 local body = ""
26 body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
27 body = body .. _("[event mail] Area: #{name}", { name = self.issue.area.name }) .. "\n"
28 body = body .. _("[event mail] Issue: ##{id}", { id = self.issue_id }) .. "\n"
29 body = body .. _("[event mail] Policy: #{policy}", { phase = self.issue.policy.name }) .. "\n"
30 body = body .. _("[event mail] Phase: #{phase}", { phase = self.state }) .. "\n\n"
31 body = body .. _("[event mail] Event: #{event}", { event = self.event }) .. "\n\n"
33 if self.initiative_id then
34 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
35 elseif self.suggestion_id then
36 url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
37 else
38 url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
39 end
41 body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n"
43 if self.initiative_id then
44 local initiative = Initiative:by_id(self.initiative_id)
45 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
46 else
47 local initiative_count = Initiative:new_selector()
48 :add_where{ "initiative.issue_id = ?", self.issue_id }
49 :count()
50 local initiatives = Initiative:new_selector()
51 :add_where{ "initiative.issue_id = ?", self.issue_id }
52 :add_order_by("initiative.supporter_count DESC")
53 :limit(3)
54 :exec()
55 for i, initiative in ipairs(initiatives) do
56 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
57 end
58 if initiative_count - 3 > 0 then
59 body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
60 end
61 body = body .. "\n"
62 end
64 if self.suggestion_id then
65 local suggestion = Suggestion:by_id(self.suggestion_id)
66 body = body .. _("#{name}\n\n", { name = suggestion.name })
67 end
69 for i, member in ipairs(members_to_notify) do
70 local success = net.send_mail{
71 envelope_from = config.mail_envelope_from,
72 from = config.mail_from,
73 reply_to = config.mail_reply_to,
74 to = member.notify_email,
75 subject = config.mail_subject_prefix .. _("##{id} #{event}", { id = self.issue_id, event = self.event }), content_type = "text/plain; charset=UTF-8",
76 content = body
77 }
78 end
80 end
82 function Event:send_next_notification()
84 local notification_sent = NotificationSent:new_selector()
85 :optional_object_mode()
86 :for_update()
87 :exec()
89 local last_event_id = 0
90 if notification_sent then
91 last_event_id = notification_sent.event_id
92 end
94 local event = Event:new_selector()
95 :add_where{ "event.id > ?", last_event_id }
96 :add_order_by("event.id")
97 :limit(1)
98 :optional_object_mode()
99 :exec()
101 if event then
102 if last_event_id == 0 then
103 db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
104 else
105 db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
106 end
108 event:send_notification()
110 return true
112 end
114 end
116 function Event:send_notifications_loop()
118 while true do
119 local did_work = Event:send_next_notification()
120 if not did_work then
121 print "Sleeping 1 second"
122 os.execute("sleep 1")
123 end
124 end
126 end