liquid_feedback_frontend

view model/event.lua @ 1157:591b78770f66

merge
author jbe
date Mon Mar 23 23:18:43 2015 +0100 (2015-03-23)
parents c037558f6610
children e9f6ae14bc72
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 Event:add_reference{
13 mode = 'm1',
14 to = "Initiative",
15 this_key = 'initiative_id',
16 that_key = 'id',
17 ref = 'initiative',
18 }
20 Event:add_reference{
21 mode = 'm1',
22 to = "Suggestion",
23 this_key = 'suggestion_id',
24 that_key = 'id',
25 ref = 'suggestion',
26 }
28 Event:add_reference{
29 mode = 'm1',
30 to = "Member",
31 this_key = 'member_id',
32 that_key = 'id',
33 ref = 'member',
34 }
36 function Event.object_get:event_name()
37 return ({
38 issue_state_changed = _"Issue reached next phase",
39 initiative_created_in_new_issue = _"New issue",
40 initiative_created_in_existing_issue = _"New initiative",
41 initiative_revoked = _"Initiative revoked",
42 new_draft_created = _"New initiative draft",
43 suggestion_created = _"New suggestion"
44 })[self.event]
45 end
47 function Event.object_get:state_name()
48 return Issue:get_state_name_for_state(self.state)
49 end
51 function Event.object:send_notification()
53 local members_to_notify = Member:new_selector()
54 :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 } )
55 :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
56 -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
57 :add_where("now() - event_seen_by_member.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
58 -- do not notify a member about the events caused by the member
59 :add_where("event_seen_by_member.member_id ISNULL OR event_seen_by_member.member_id != member.id")
60 :exec()
62 print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
65 local url
67 for i, member in ipairs(members_to_notify) do
68 local subject
69 local body = ""
71 locale.do_with(
72 { lang = member.lang or config.default_lang or 'en' },
73 function()
75 local suggestion
76 if self.suggestion_id then
77 suggestion = Suggestion:by_id(self.suggestion_id)
78 if not suggestion then
79 return
80 end
81 end
83 subject = config.mail_subject_prefix .. " " .. self.event_name
84 body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
85 body = body .. _("[event mail] Area: #{name}", { name = self.issue.area.name }) .. "\n"
86 body = body .. _("[event mail] Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
87 body = body .. _("[event mail] Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
88 body = body .. _("[event mail] Event: #{event}", { event = self.event_name }) .. "\n\n"
89 body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
91 if self.initiative_id then
92 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
93 elseif self.suggestion_id then
94 url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
95 else
96 url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
97 end
99 body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n"
101 if self.initiative_id then
102 local initiative = Initiative:by_id(self.initiative_id)
103 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
104 else
105 local initiative_count = Initiative:new_selector()
106 :add_where{ "initiative.issue_id = ?", self.issue_id }
107 :count()
108 local initiatives = Initiative:new_selector()
109 :add_where{ "initiative.issue_id = ?", self.issue_id }
110 :add_order_by("initiative.supporter_count DESC")
111 :limit(3)
112 :exec()
113 for i, initiative in ipairs(initiatives) do
114 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
115 end
116 if initiative_count - 3 > 0 then
117 body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
118 end
119 body = body .. "\n"
120 end
122 if suggestion then
123 body = body .. _("#{name}\n\n", { name = suggestion.name })
124 end
126 local success = net.send_mail{
127 envelope_from = config.mail_envelope_from,
128 from = config.mail_from,
129 reply_to = config.mail_reply_to,
130 to = member.notify_email,
131 subject = subject,
132 content_type = "text/plain; charset=UTF-8",
133 content = body
134 }
136 end
137 )
138 end
140 end
142 function Event:send_next_notification()
144 local notification_sent = NotificationSent:new_selector()
145 :optional_object_mode()
146 :for_update()
147 :exec()
149 local last_event_id = 0
150 if notification_sent then
151 last_event_id = notification_sent.event_id
152 end
154 local event = Event:new_selector()
155 :add_where{ "event.id > ?", last_event_id }
156 :add_order_by("event.id")
157 :limit(1)
158 :optional_object_mode()
159 :exec()
161 if event then
162 if last_event_id == 0 then
163 db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
164 else
165 db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
166 end
168 event:send_notification()
170 return true
172 end
174 end
176 function Event:send_notifications_loop()
178 while true do
179 local did_work = Event:send_next_notification()
180 if not did_work then
181 print "Sleeping 1 second"
182 os.execute("sleep 1")
183 end
184 end
186 end

Impressum / About Us