liquid_feedback_frontend

view model/event.lua @ 1248:c0fd12b97d65

Changes on notifications system, newsletter support added
author bsw
date Tue Apr 05 20:40:37 2016 +0200 (2016-04-05)
parents dafce7add549
children 84f6e17c7ceb
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 --: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_for_notification.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_for_notification.member_id ISNULL OR event_for_notification.member_id != member.id")
60 :exec()
62 io.stderr:write("Sending notifications for event " .. self.id .. " to " .. (#members_to_notify) .. " members\n")
64 local url
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 or 'en' },
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 if self.initiative_id then
81 url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
82 else
83 url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
84 end
86 body = body .. _("[event mail] URL: #{url}", { url = url }) .. "\n\n"
88 local leading_initiative
90 if self.initiative_id then
91 local initiative = Initiative:by_id(self.initiative_id)
92 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
93 else
94 local initiative_count = Initiative:new_selector()
95 :add_where{ "initiative.issue_id = ?", self.issue_id }
96 :count()
97 local initiatives = Initiative:new_selector()
98 :add_where{ "initiative.issue_id = ?", self.issue_id }
99 :add_order_by("initiative.supporter_count DESC")
100 :limit(3)
101 :exec()
102 for i, initiative in ipairs(initiatives) do
103 if i == 1 then
104 leading_initiative = initiative
105 end
106 body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
107 end
108 if initiative_count - 3 > 0 then
109 body = body .. _("and #{count} more initiatives", { count = initiative_count - 3 }) .. "\n"
110 end
111 body = body .. "\n"
112 end
114 subject = config.mail_subject_prefix
116 if self.event == "issue_state_changed" then
117 subject = subject .. _("State of #{issue} changed to #{state} / Leading: #{initiative}", { issue = self.issue.name, state = Issue:get_state_name_for_state(self.state), initiative = leading_initiative.display_name })
118 elseif self.event == "initiative_revoked" then
119 subject = subject .. _("Initiative revoked: #{initiative_name}", { initiative_name = self.initiative.display_name })
120 end
122 local success = net.send_mail{
123 envelope_from = config.mail_envelope_from,
124 from = config.mail_from,
125 reply_to = config.mail_reply_to,
126 to = member.notify_email,
127 subject = subject,
128 content_type = "text/plain; charset=UTF-8",
129 content = body
130 }
132 end
133 )
134 end
136 end
138 function Event:send_next_notification()
140 local notification_sent = NotificationSent:new_selector()
141 :optional_object_mode()
142 :for_update()
143 :exec()
145 local last_event_id = 0
146 if notification_sent then
147 last_event_id = notification_sent.event_id
148 end
150 local event = Event:new_selector()
151 :add_where{ "event.id > ?", last_event_id }
152 :add_order_by("event.id")
153 :limit(1)
154 :optional_object_mode()
155 :exec()
157 if event then
158 if last_event_id == 0 then
159 db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
160 else
161 db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
162 end
164 event:send_notification()
166 if config.notification_handler_func then
167 config.notification_handler_func(event)
168 end
170 return true
172 end
174 end

Impressum / About Us