aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2012-05-09 17:05:15 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2012-05-09 17:05:15 +0100
commit39cf220fcd2955faae798623164efc48a10b8417 (patch)
tree2128acd6141dee68e7247299c86ce481da0dadb2
parent8d60bca4c43e386f74e44206fa6eb369e5fe5aae (diff)
parent196b337a02f959a431a4bb7c443ed116e4e291f3 (diff)
merge
-rw-r--r--lib/mauve/alert_changed.rb2
-rw-r--r--lib/mauve/notification.rb15
-rw-r--r--lib/mauve/notifiers/xmpp.rb39
-rw-r--r--lib/mauve/people_list.rb5
-rw-r--r--lib/mauve/web_interface.rb2
-rw-r--r--static/javascript/mauve_utils.js40
-rw-r--r--test/tc_mauve_alert.rb31
7 files changed, 108 insertions, 26 deletions
diff --git a/lib/mauve/alert_changed.rb b/lib/mauve/alert_changed.rb
index 0e0d257..2e3ac7d 100644
--- a/lib/mauve/alert_changed.rb
+++ b/lib/mauve/alert_changed.rb
@@ -71,7 +71,7 @@ module Mauve
def remind
unless alert.is_a?(Alert)
logger.info "#{self.inspect} lost alert #{alert_id}. Killing self."
- destroy!
+ destroy
return false
end
diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb
index 82da2e8..57d82d2 100644
--- a/lib/mauve/notification.rb
+++ b/lib/mauve/notification.rb
@@ -115,16 +115,23 @@ module Mauve
protected
- # Test to see if a people_list is empty.
+ # Test to see if a people_list is empty. NB this is just evaluated at the
+ # time that the DuringRunner is set up with.
#
# @param [String] people_list People list to query
# @return [Boolean]
#
def no_one_in(people_list)
return true unless Configuration.current.people[people_list].respond_to?(:people)
+
+ #
+ # Cache the results to prevent hitting the calendar too many times.
+ #
+ @no_one_in_cache ||= Hash.new
- @test_time = @time if @test_time.nil?
- return Configuration.current.people[people_list].people(@test_time).empty?
+ return @no_one_in_cache[people_list] if @no_one_in_cache.has_key?(people_list)
+
+ @no_one_in_cache[people_list] = Configuration.current.people[people_list].people(@time).empty?
end
# Returns true if the current hour is in the list of hours given.
@@ -347,6 +354,4 @@ module Mauve
end
- class NotificationDummy < Struct.new(:during, :every) ; end
-
end
diff --git a/lib/mauve/notifiers/xmpp.rb b/lib/mauve/notifiers/xmpp.rb
index 323f89d..f2c5503 100644
--- a/lib/mauve/notifiers/xmpp.rb
+++ b/lib/mauve/notifiers/xmpp.rb
@@ -474,8 +474,10 @@ module Mauve
do_parse_show(msg)
when /ack/i
do_parse_ack(msg)
+ when /clear/i
+ do_parse_clear(msg)
when /destroy\s?/i
- do_parse_destroy(msg)
+ "Sorry -- destroy has been disabled. Try \"clear\" instead."
else
File.executable?('/usr/games/fortune') ? `/usr/games/fortune -s -n 60`.chomp : "I'd love to stay and chat, but I'm really quite busy"
end
@@ -577,7 +579,7 @@ EOF
def do_parse_ack(msg)
return "Sorry -- I don't understand your acknowledge command." unless
- msg.body =~ /ack(?:nowledge)?\s+([\d\D]+)\s+for\s+(\d+(?:\.\d+)?)\s+(work(?:ing)?|day(?:time)?|wall(?:-?clock)?)?\s*(day|hour|min(?:ute)?|sec(?:ond))s?(?:\s+because\s+(.*))?/i
+ msg.body =~ /ack(?:nowledge)?\s+([\d\D]+)\s+for\s+(\d+(?:\.\d+)?)\s+(work(?:ing)?|day(?:time)?|wall(?:-?clock)?)?\s*(day|hour|min(?:ute)?|sec(?:ond))s?(?:\s+(?:cos|cause|as|because)?\s*(.*))?/i
alerts, n_hours, type_hours, dhms, note = [$1,$2, $3, $4, $5]
@@ -654,35 +656,50 @@ EOF
return msg.join("\n")
end
- def do_parse_destroy(msg)
- return "Sorry -- I don't understand your destroy command." unless
- msg.body =~ /destroy\s+([\d\D]+)$/i
+ def do_parse_clear(msg)
+ return "Sorry -- I don't understand your clear command." unless
+ msg.body =~ /clear\s+([\d\D]+)(?:\s+(?:coz|cause|cos|because|as)?\s*(.*))?/i
alerts = $1.split(/\D+/)
+ note = $2
username = get_username_for(msg.from)
if is_muc?(Configuration.current.people[username].xmpp)
- return "I'm sorry -- if you want to destroy alerts, please do it from a private chat"
+ return "I'm sorry -- if you want to clear alerts, please do it from a private chat"
end
msg = []
- msg << "Results of your destruction:" if alerts.length > 1
+ msg << "Clearing results:" if alerts.length > 1
alerts.each do |alert_id|
alert = Alert.get(alert_id)
if alert.nil?
- msg << "#{alert_id}: alert not found"
+ msg << "#{alert_id}: alert not found."
next
end
- if alert.destroy!
- msg << "#{alert.to_s} destroyed"
+ if alert.cleared?
+ msg << "#{alert_id}: alert already cleared."
+ next
+ end
+
+ if alert.clear!
+ msg << "#{alert.to_s} cleared."
else
- msg << "#{alert.to_s}: destruction failed."
+ msg << "#{alert.to_s}: clearing failed."
end
end
+
+ #
+ # Add the note.
+ #
+ unless note.to_s.empty?
+ note = Alert.remove_html(note)
+ h = History.new(:alerts => succeeded, :type => "note", :event => note.to_s, :user => username)
+ logger.debug h.errors unless h.save
+ end
return msg.join("\n")
end
diff --git a/lib/mauve/people_list.rb b/lib/mauve/people_list.rb
index 8178635..7124241 100644
--- a/lib/mauve/people_list.rb
+++ b/lib/mauve/people_list.rb
@@ -25,6 +25,11 @@ module Mauve
alias username label
+ #
+ # A dummy XMPP method.
+ #
+ def xmpp; nil ; end
+
# Append an Array or String to a list
#
# @param [Array or String] arr
diff --git a/lib/mauve/web_interface.rb b/lib/mauve/web_interface.rb
index 9a46403..b5c0442 100644
--- a/lib/mauve/web_interface.rb
+++ b/lib/mauve/web_interface.rb
@@ -409,7 +409,7 @@ EOF
post '/alert/:id/destroy' do
alert = Alert.get(params[:id])
- alert.destroy!
+ alert.destroy
flash['notice'] = "Successfully destroyed alert #{alert.alert_id} from source #{alert.source}."
redirect "/"
end
diff --git a/static/javascript/mauve_utils.js b/static/javascript/mauve_utils.js
index 1330635..5522c08 100644
--- a/static/javascript/mauve_utils.js
+++ b/static/javascript/mauve_utils.js
@@ -1,22 +1,53 @@
function updateDate() {
+ var n_hours;
+ var type_hours;
+
+ //
+ // Make sure the number of hours is sane.
+ //
+ n_hours = new Number( $( '#n_hours' ).val() );
+
+ if (isNaN(n_hours)) {
+ n_hours = new Number(2);
+ }
+
+ //
+ // Make sure the type is one of three things:
+ //
+ // type_hours = new String( $( '#type_hours' ).val() );
+
+ switch( $( '#type_hours' ).val() ) {
+ case "working":
+ type_hours = "working"
+ break;
+
+ case "daytime":
+ type_hours = "daytime"
+ break;
+
+ default:
+ type_hours = "wallclock";
+ break;
+ }
+
//
// Use a asynchronous ajax convert a date to a human string. NB Date.getTime()
// returns *milliseconds*
//
$.ajax( {
- url: '/ajax/time_in_x_hours/'+$( '#n_hours' ).val()+'/'+$( '#type_hours' ).val(),
+ url: '/ajax/time_in_x_hours/'+n_hours.toString()+'/'+type_hours,
timeout: 2000,
success: function( data ) {
- $( '#ack_until' ).val( data['time'] );
- $( '#ack_until_text' ).html( "( until "+data['string']+" )" );
+ $( '#ack_until' ).val( data['time'].toString() );
+ $( '#ack_until_text' ).html( "( until "+data['string'].toString()+" )" );
},
error: function( a,b,c ) {
//
// Date.getTime() returns *milliseconds*
//
- var this_date = workoutDate( $( '#n_hours' ).val(), $( '#type_hours' ).val() );
+ var this_date = workoutDate( n_hours, type_hours );
$( '#ack_until' ).val( this_date.getTime()/1000 );
$( '#ack_until_text' ).html( "( until "+this_date.toString()+" )" );
}
@@ -40,6 +71,7 @@ function workoutDate( h, type ) {
//
var d = new Date();
var t = d.getTime();
+
//
// Can't ack longer than a week
//
diff --git a/test/tc_mauve_alert.rb b/test/tc_mauve_alert.rb
index 3af9075..269bf4c 100644
--- a/test/tc_mauve_alert.rb
+++ b/test/tc_mauve_alert.rb
@@ -38,10 +38,6 @@ EOF
super
end
- def test_alert_group
-
- end
-
#
# This is also the test for in_source_list?
@@ -292,4 +288,31 @@ EOF
end
+ def test_destroy_history_on_destroy
+ Configuration.current = ConfigurationBuilder.parse(@test_config)
+ Server.instance.setup
+
+ alert = Alert.new(
+ :alert_id => "test_no_notification_for_old_alerts",
+ :source => "test",
+ :subject => "test"
+ )
+ alert.save
+ alert.raise!
+ alert.reload
+ assert_equal(1, History.all.length)
+
+
+ Timecop.freeze(Time.now + 5.minutes)
+ alert.clear!
+ assert_equal(2, History.all.length)
+
+ #
+ # OK now we destroy the alert. Destory the histories too.
+ #
+ alert.destroy
+ assert_equal(0, History.all.length)
+
+ end
+
end