# HG changeset patch
# User bsw
# Date 1330167876 -3600
# Node ID ee477a136fd4fde07f0c125ab6dbc12d829dc734
# Parent c587d8762e62a96a5caf7eb480a4dab41c2d184f
Moved notification functions to model
diff -r c587d8762e62 -r ee477a136fd4 app/main/member/settings_notification.lua
--- a/app/main/member/settings_notification.lua Sat Feb 25 11:51:37 2012 +0100
+++ b/app/main/member/settings_notification.lua Sat Feb 25 12:04:36 2012 +0100
@@ -1,58 +1,3 @@
-function send_notification(event)
-
- local url
-
- local body = ""
-
- body = body .. _(" Unit: #{name}\n", { name = event.issue.area.unit.name })
- body = body .. _(" Area: #{name}\n", { name = event.issue.area.name })
- body = body .. _(" Issue: ##{id}\n", { id = event.issue_id })
- body = body .. _(" Policy: #{phase}\n", { phase = event.issue.policy.name })
- body = body .. _(" Phase: #{phase}\n\n", { phase = event.state })
- body = body .. _(" Event: #{event}\n\n", { event = event.event })
-
- if event.initiative_id then
- url = request.get_absolute_baseurl() .. "initiative/show/" .. event.initiative_id .. ".html"
- elseif event.suggestion_id then
- url = request.get_absolute_baseurl() .. "suggestion/show/" .. event.suggestion_id .. ".html"
- else
- url = request.get_absolute_baseurl() .. "issue/show/" .. event.issue_id .. ".html"
- end
-
- body = body .. _(" URL: #{url}\n\n", { url = url })
-
- if event.initiative_id then
- local initiative = Initiative:by_id(event.initiative_id)
- body = body .. _("i#{id}: #{name}\n\n", { id = initiative.id, name = initiative.name })
- else
- local initiative_count = Initiative:new_selector()
- :add_where{ "initiative.issue_id = ?", event.issue_id }
- :count()
- local initiatives = Initiative:new_selector()
- :add_where{ "initiative.issue_id = ?", event.issue_id }
- :add_order_by("initiative.supporter_count DESC")
- :limit(3)
- :exec()
- for i, initiative in ipairs(initiatives) do
- body = body .. _("i#{id}: #{name}\n", { id = initiative.id, name = initiative.name })
- end
- if initiative_count - 3 > 0 then
- body = body .. _("and #{count} more initiatives\n", { count = initiative_count })
- end
- body = body .. "\n"
- end
-
- if event.suggestion_id then
- local suggestion = Suggestion:by_id(event.suggestion_id)
- body = body .. _("#{name}\n\n", { name = suggestion.name })
- end
-
- slot.put("
", encode.html_newlines(body), "
")
- slot.put("
")
-end
-
-
-
slot.put_into("title", _"Notification settings")
slot.select("actions", function()
@@ -155,31 +100,5 @@
ui.submit{ value = _"Change display settings" }
end
}
-
-local last_id = 6000;
-
-while last_id < 6050 do
-
- local event = Event:new_selector()
- :add_where{ "event.id > ?", last_id }
- :add_order_by("event.id")
- :limit(1)
- :optional_object_mode()
- :exec()
-
- last_id = nil
- if event then
- last_id = event.id
- local members_to_notify = Member:new_selector()
- :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 } )
- :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
- :exec()
- ui.container{ content = _("Event #{id} -> #{num} members", { id = event.id, num = #members_to_notify }) }
-
- send_notification(event)
-
- end
-end
-
-
+
-- select event.id, event.occurrence, membership.member_id NOTNULL as membership, interest.member_id NOTNULL as interest, supporter.member_id NOTNULL as supporter, event.event, event.state, issue.id, initiative.name FROM event JOIN issue ON issue.id = event.issue_id LEFT JOIN membership ON membership.area_id = issue.area_id AND membership.member_id = 41 LEFT JOIN interest ON interest.issue_id = issue.id AND interest.member_id = 41 LEFT JOIN initiative ON initiative.id = event.initiative_id LEFT JOIN supporter ON supporter.initiative_id = initiative.id AND supporter.member_id = 41 WHERE (((event.event = 'issue_state_changed' OR event.event = 'initiative_created_in_new_issue') AND membership.member_id NOTNULL OR interest.member_id NOTNULL) OR (event.event = 'initiative_created_in_existing_issue' AND interest.member_id NOTNULL) OR ((event.event = 'initiative_revoked' OR event.event = 'new_draft_created' OR event.event = 'suggestion_created') AND supporter.member_id NOTNULL)) AND event.id > 7000 ORDER by event.id ASC LIMIT 1;
\ No newline at end of file
diff -r c587d8762e62 -r ee477a136fd4 model/event.lua
--- a/model/event.lua Sat Feb 25 11:51:37 2012 +0100
+++ b/model/event.lua Sat Feb 25 12:04:36 2012 +0100
@@ -7,4 +7,87 @@
this_key = 'issue_id',
that_key = 'id',
ref = 'issue',
-}
\ No newline at end of file
+}
+
+function Event.object:send_notification()
+
+ local url
+
+ local body = ""
+
+ body = body .. _(" Unit: #{name}\n", { name = self.issue.area.unit.name })
+ body = body .. _(" Area: #{name}\n", { name = self.issue.area.name })
+ body = body .. _(" Issue: ##{id}\n", { id = self.issue_id })
+ body = body .. _(" Policy: #{phase}\n", { phase = self.issue.policy.name })
+ body = body .. _(" Phase: #{phase}\n\n", { phase = self.state })
+ body = body .. _(" Event: #{event}\n\n", { event = self.event })
+
+ if self.initiative_id then
+ url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
+ elseif self.suggestion_id then
+ url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
+ else
+ url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
+ end
+
+ body = body .. _(" URL: #{url}\n\n", { url = url })
+
+ if self.initiative_id then
+ local initiative = Initiative:by_id(self.initiative_id)
+ body = body .. _("i#{id}: #{name}\n\n", { id = initiative.id, name = initiative.name })
+ else
+ local initiative_count = Initiative:new_selector()
+ :add_where{ "initiative.issue_id = ?", self.issue_id }
+ :count()
+ local initiatives = Initiative:new_selector()
+ :add_where{ "initiative.issue_id = ?", self.issue_id }
+ :add_order_by("initiative.supporter_count DESC")
+ :limit(3)
+ :exec()
+ for i, initiative in ipairs(initiatives) do
+ body = body .. _("i#{id}: #{name}\n", { id = initiative.id, name = initiative.name })
+ end
+ if initiative_count - 3 > 0 then
+ body = body .. _("and #{count} more initiatives\n", { count = initiative_count })
+ end
+ body = body .. "\n"
+ end
+
+ if self.suggestion_id then
+ local suggestion = Suggestion:by_id(self.suggestion_id)
+ body = body .. _("#{name}\n\n", { name = suggestion.name })
+ end
+
+ slot.put("", encode.html_newlines(body), "
")
+ slot.put("
")
+end
+
+function Event:send_next_notification(last_event_id)
+
+ local event = Event:new_selector()
+ :add_where{ "event.id > ?", last_id }
+ :add_order_by("event.id")
+ :limit(1)
+ :optional_object_mode()
+ :exec()
+
+ last_id = nil
+ if event then
+ last_id = event.id
+ local members_to_notify = Member:new_selector()
+ :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 } )
+ :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
+ :exec()
+
+ ui.container{ content = _("Event #{id} -> #{num} members", { id = event.id, num = #members_to_notify }) }
+
+ event:send_notification()
+
+ return event.id
+
+ else
+ return last_event_id
+
+ end
+
+end
\ No newline at end of file