liquid_feedback_frontend

view model/event.lua @ 1300:5aecbbb04a42

Added missing conditions to events for notification selector
author bsw
date Wed May 04 21:38:02 2016 +0200 (2016-05-04)
parents 659e3eda2fad
children 9ff5b868760d
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_for_notification", nil, { "event_for_notification.recipient_id = member.id AND event_for_notification.id = ?", self.id } )
55 -- SAFETY FIRST, NEVER send notifications for events more then 3 days in past or future
56 :add_where("now() - event_for_notification.occurrence BETWEEN '-3 days'::interval AND '3 days'::interval")
57 -- do not notify a member about the events caused by the member
58 :add_where("event_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id")
59 :add_where("member.notify_email NOTNULL")
60 :add_where("NOT member.locked")
61 :add_where("NOT member.disable_notifications")
62 :exec()
64 io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
66 for i, member in ipairs(members_to_notify) do
67 local subject
68 local body = ""
70 locale.do_with(
71 { lang = member.lang or config.default_lang },
72 function()
74 body = body .. _("[event mail] Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
75 body = body .. _("[event mail] Area: #{name}", { name = self.issue.area.name }) .. "\n"
76 body = body .. _("[event mail] Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
77 body = body .. _("[event mail] Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
78 body = body .. _("[event mail] Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
80 local url
82 if self.initiative_id then
83 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
84 else
85 url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
86 end
88 body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n"
90 local leading_initiative
92 if self.initiative_id then
93 local initiative = Initiative:by_id(self.initiative_id)
94 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
95 else
96 local initiative_count = Initiative:new_selector()
97 :add_where{ "initiative.issue_id = ?", self.issue_id }
98 :count()
99 local initiatives = Initiative:new_selector()
100 :add_where{ "initiative.issue_id = ?", self.issue_id }
101 :add_order_by("initiative.admitted DESC NULLS LAST, initiative.rank NULLS LAST, initiative.harmonic_weight DESC NULLS LAST, id")
102 :limit(3)
103 :exec()
104 for i, initiative in ipairs(initiatives) do
105 if i == 1 then
106 leading_initiative = initiative
107 end
108 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
109 end
110 if initiative_count - 3 > 0 then
111 body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
112 end
113 body = body .. "\n"
114 end
116 subject = config.mail_subject_prefix
118 if self.event == "issue_state_changed" then
119 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 })
120 elseif self.event == "initiative_revoked" then
121 subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name })
122 end
124 local success = net.send_mail{
125 envelope_from = config.mail_envelope_from,
126 from = config.mail_from,
127 reply_to = config.mail_reply_to,
128 to = member.notify_email,
129 subject = subject,
130 content_type = "text/plain; charset=UTF-8",
131 content = body
132 }
134 end
135 )
136 end
138 end
140 function Event:send_next_notification()
142 local notification_event_sent = NotificationEventSent:new_selector()
143 :optional_object_mode()
144 :for_update()
145 :exec()
147 local last_event_id = 0
148 if notification_event_sent then
149 last_event_id = notification_event_sent.event_id
150 end
152 local event = Event:new_selector()
153 :add_where{ "event.id > ?", last_event_id }
154 :add_order_by("event.id")
155 :limit(1)
156 :optional_object_mode()
157 :exec()
159 if event then
160 if last_event_id == 0 then
161 db:query{ "INSERT INTO notification_event_sent (event_id) VALUES (?)", event.id }
162 else
163 db:query{ "UPDATE notification_event_sent SET event_id = ?", event.id }
164 end
166 event:send_notification()
168 if config.notification_handler_func then
169 config.notification_handler_func(event)
170 end
172 return true
174 end
176 end

Impressum / About Us