From 0ed2894ca7371199788c202b61f53a3f73c4b03b Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Sun, 17 Mar 2013 12:44:33 +0100 Subject: Reduce the push load. --- app/controllers/trigger_controller.rb | 29 ++++++++++++++++++----------- app/models/sip_account.rb | 5 ----- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/controllers/trigger_controller.rb b/app/controllers/trigger_controller.rb index a858e33..6b58d6a 100644 --- a/app/controllers/trigger_controller.rb +++ b/app/controllers/trigger_controller.rb @@ -92,19 +92,26 @@ class TriggerController < ApplicationController def sip_account_update sip_account = SipAccount.find(params[:id]) - # TO-DO: Only update the neccessary entries. - # - SwitchboardEntry.all.each do |switchboard_entry| - new_html = ActionController::Base.helpers.escape_javascript(render_to_string("switchboard_entries/_switchboard_entry", :layout => false, :locals => {:switchboard_entry => switchboard_entry})) - PrivatePub.publish_to("/switchboards/#{switchboard_entry.switchboard.id}", "$('#switchboard_entry_id_" + switchboard_entry.id.to_s + "').replaceWith('#{new_html}');") - end + if sip_account.updated_at < Time.now - Switchboard.all.each do |switchboard| - if sip_account.call_legs.where(:sip_account_id => switchboard.user.sip_account_ids).any? || - sip_account.b_call_legs.where(:sip_account_id => switchboard.user.sip_account_ids).any? - new_html = ActionController::Base.helpers.escape_javascript(render_to_string("switchboards/_current_user_dashboard", :layout => false, :locals => {:current_user => switchboard.user})) - PrivatePub.publish_to("/switchboards/#{switchboard.id}", "$('.dashboard').replaceWith('#{new_html}');") + # Push an update to sip_account.switchboard_entries + # + sip_account.switchboard_entries.each do |switchboard_entry| + escaped_switchboard_entry_partial = ActionController::Base.helpers.escape_javascript(render_to_string("switchboard_entries/_switchboard_entry", :layout => false, :locals => {:switchboard_entry => switchboard_entry})) + PrivatePub.publish_to("/switchboards/#{switchboard_entry.switchboard.id}", "$('#switchboard_entry_id_" + switchboard_entry.id.to_s + "').replaceWith('#{escaped_switchboard_entry_partial}');") end + + # Push an update to the needed switchboards + # + Switchboard.where(:user_id => sip_account.sip_accountable.id).each do |switchboard| + if sip_account.call_legs.where(:sip_account_id => switchboard.user.sip_account_ids).any? || + sip_account.b_call_legs.where(:sip_account_id => switchboard.user.sip_account_ids).any? + escaped_switchboard_partial = ActionController::Base.helpers.escape_javascript(render_to_string("switchboards/_current_user_dashboard", :layout => false, :locals => {:current_user => switchboard.user})) + PrivatePub.publish_to("/switchboards/#{switchboard.id}", "$('.dashboard').replaceWith('#{escaped_switchboard_partial}');") + end + end + + sip_account.touch end render( diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb index cea5f0e..930069d 100644 --- a/app/models/sip_account.rb +++ b/app/models/sip_account.rb @@ -213,11 +213,6 @@ class SipAccount < ActiveRecord::Base return states end - def non_e164_phone_numbers - array_of_phone_numbers_as_strings = self.phone_numbers.map{ |phone_number| phone_number.number.to_s }.sort - self.phone_numbers.where(:number => array_of_phone_numbers_as_strings.select { |phone_number| phone_number[0] != '+' }) - end - private def save_value_of_to_s -- cgit v1.2.3