From 6b5646f70b1c14eadeb9778f93e078338f45f3f4 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 8 May 2012 14:21:45 +0100 Subject: Changed destroy! to destroy everywhere to ensure Histories are deleted correctly. Added test. --- lib/mauve/alert_changed.rb | 2 +- lib/mauve/notifiers/xmpp.rb | 2 +- lib/mauve/web_interface.rb | 2 +- test/tc_mauve_alert.rb | 31 +++++++++++++++++++++++++++---- 4 files changed, 30 insertions(+), 7 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/notifiers/xmpp.rb b/lib/mauve/notifiers/xmpp.rb index 323f89d..dc44f1c 100644 --- a/lib/mauve/notifiers/xmpp.rb +++ b/lib/mauve/notifiers/xmpp.rb @@ -677,7 +677,7 @@ EOF next end - if alert.destroy! + if alert.destroy msg << "#{alert.to_s} destroyed" else msg << "#{alert.to_s}: destruction failed." 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/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 -- cgit v1.2.1 From a6c5615b84fed58588b7a9acf36b3084ab58ad44 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 8 May 2012 14:22:09 +0100 Subject: Updated JS to sanity check ack arguments. --- static/javascript/mauve_utils.js | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) 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 // -- cgit v1.2.1 From 90d4fe602abc04e7665735a1466e2df174dcb476 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 8 May 2012 14:22:45 +0100 Subject: Added dummy xmpp method to PeopleList to make sure that it doesn't cause the XMPP stuff to barf. --- lib/mauve/people_list.rb | 5 +++++ 1 file changed, 5 insertions(+) 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 -- cgit v1.2.1 From bebac3756dd535bf754752a34ab8061098d7b91d Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Wed, 9 May 2012 16:38:17 +0100 Subject: Remnoved destroy command and replaced with clear. --- lib/mauve/notifiers/xmpp.rb | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/mauve/notifiers/xmpp.rb b/lib/mauve/notifiers/xmpp.rb index dc44f1c..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 -- cgit v1.2.1 From 196b337a02f959a431a4bb7c443ed116e4e291f3 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Wed, 9 May 2012 17:04:39 +0100 Subject: DuringRunner#no_one_in now is only evaluated once per instance per people_list to prevent the calendar getting thrashed. --- lib/mauve/notification.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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 -- cgit v1.2.1