liquid_feedback_frontend
changeset 363:a80e74881caf
More work at the notification system
author | bsw |
---|---|
date | Thu Mar 01 00:08:06 2012 +0100 (2012-03-01) |
parents | f0a59bbf659a |
children | e1d60ee19e52 |
files | model/event.lua model/notification_sent.lua |
line diff
1.1 --- a/model/event.lua Thu Mar 01 00:07:46 2012 +0100 1.2 +++ b/model/event.lua Thu Mar 01 00:08:06 2012 +0100 1.3 @@ -11,6 +11,14 @@ 1.4 1.5 function Event.object:send_notification() 1.6 1.7 + local members_to_notify = Member:new_selector() 1.8 + :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 } ) 1.9 + :add_where("member.activated NOTNULL AND member.notify_email NOTNULL") 1.10 + :exec() 1.11 + 1.12 + print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify })) 1.13 + 1.14 + 1.15 local url 1.16 1.17 local body = "" 1.18 @@ -58,32 +66,48 @@ 1.19 body = body .. _("#{name}\n\n", { name = suggestion.name }) 1.20 end 1.21 1.22 - slot.put("<pre>", encode.html_newlines(body), "</pre>") 1.23 - slot.put("<hr />") 1.24 + for i, member in ipairs(members_to_notify) do 1.25 + local success = net.send_mail{ 1.26 + envelope_from = config.mail_envelope_from, 1.27 + from = config.mail_from, 1.28 + reply_to = config.mail_reply_to, 1.29 + to = member.notify_email, 1.30 + subject = config.mail_subject_prefix .. _("##{id} #{event}", { id = self.issue_id, event = self.event }), content_type = "text/plain; charset=UTF-8", 1.31 + content = body 1.32 + } 1.33 + end 1.34 + 1.35 + print(body) 1.36 + print("") 1.37 end 1.38 1.39 -function Event:send_next_notification(last_event_id) 1.40 - 1.41 +function Event:send_next_notification() 1.42 + 1.43 + local notification_sent = NotificationSent:new_selector() 1.44 + :optional_object_mode() 1.45 + :for_update() 1.46 + :exec() 1.47 + 1.48 + local last_event_id = 0 1.49 + if notification_sent then 1.50 + last_event_id = notification_sent.event_id 1.51 + end 1.52 + 1.53 local event = Event:new_selector() 1.54 - :add_where{ "event.id > ?", last_id } 1.55 + :add_where{ "event.id > ?", last_event_id } 1.56 :add_order_by("event.id") 1.57 :limit(1) 1.58 :optional_object_mode() 1.59 :exec() 1.60 1.61 - last_id = nil 1.62 if event then 1.63 - last_id = event.id 1.64 - local members_to_notify = Member:new_selector() 1.65 - :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 = ?", event.id } ) 1.66 - :add_where("member.activated NOTNULL AND member.notify_email NOTNULL") 1.67 - :exec() 1.68 - 1.69 - ui.container{ content = _("Event #{id} -> #{num} members", { id = event.id, num = #members_to_notify }) } 1.70 + if last_event_id == 0 then 1.71 + db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id } 1.72 + else 1.73 + db:query{ "UPDATE notification_sent SET event_id = ?", event.id } 1.74 + end 1.75 1.76 event:send_notification() 1.77 - 1.78 - return event.id 1.79 1.80 else 1.81 return last_event_id