diff --git a/db/manifest b/db/manifest index e933ef5e8..396ce5b48 100755 --- a/db/manifest +++ b/db/manifest @@ -231,3 +231,4 @@ shopping_carts.sql recurly.sql add_track_resource_id.sql user_genres.sql +user_online.sql diff --git a/db/up/user_online.sql b/db/up/user_online.sql new file mode 100644 index 000000000..80b15be81 --- /dev/null +++ b/db/up/user_online.sql @@ -0,0 +1,25 @@ +-- this was patched on the server without a proper migration, so the migration script here takes care to make sure it can run 2x +ALTER TABLE users DROP COLUMN IF EXISTS online; +DROP TRIGGER IF EXISTS connection_up_down ON connections; + +ALTER TABLE users ADD COLUMN online BOOL DEFAULT FALSE NOT NULL; + +CREATE OR REPLACE FUNCTION manage_user_online() RETURNS TRIGGER +STRICT VOLATILE AS $$ + BEGIN + IF (TG_OP = 'DELETE') THEN + UPDATE users set ONLINE = (SELECT COUNT(conn.id) FROM connections conn WHERE conn.user_id = OLD.user_id) > 0 WHERE users.id = OLD.user_id; + RETURN NULL; + ELSIF (TG_OP = 'INSERT') THEN + UPDATE users set ONLINE = (SELECT COUNT(conn.id) FROM connections conn WHERE conn.user_id = NEW.user_id) > 0 WHERE users.id = NEW.user_id; + RETURN NULL; + END IF; + RETURN NULL; + END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER connection_up_down AFTER INSERT OR DELETE +ON connections FOR EACH ROW EXECUTE PROCEDURE manage_user_online(user_id); + +-- ONE time bootstrap the new online field +UPDATE users set online = (SELECT COUNT(conn.id) FROM connections conn WHERE conn.user_id = users.id) > 0; \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index f66d9e8ef..da0ff9881 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -267,7 +267,7 @@ module JamRuby notification_msg = format_msg(notification.description, {:user => user}) - if friend.online + if friend.online? msg = @@message_factory.friend_request( friend.id, friend_request_id, diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index ba874eb73..832a03c72 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -253,7 +253,7 @@ module JamRuby end def online - @online ||= !self.connections.nil? && self.connections.size > 0 + online? end def name diff --git a/ruby/spec/jam_ruby/models/connection_spec.rb b/ruby/spec/jam_ruby/models/connection_spec.rb index dc7d8cdba..8596eeaa5 100644 --- a/ruby/spec/jam_ruby/models/connection_spec.rb +++ b/ruby/spec/jam_ruby/models/connection_spec.rb @@ -103,4 +103,79 @@ describe JamRuby::Connection do end end + describe "triggers" do + describe "manage_user_online" do + it "offline for new user" do + user.online.should be_false + end + + it "online/offline based on connection up/down" do + conn.touch # make a connection + user.reload + user.online.should be_true + + conn.delete + user.reload + user.online.should be_false + end + + it "verify that online up/down is only for affected user (no spillover)" do + user2 = FactoryGirl.create(:user) + user2.online.should be_false + + conn.touch + user.reload + user2.reload + + user.online.should be_true + user2.online.should be_false # make sure there is no spillover to unrelated users + + conn2 = FactoryGirl.create(:connection, + :user => user2, + :music_session => music_session, + :ip_address => "1.1.1.1", + :client_id => "2") + + user.reload + user2.reload + + user.online.should be_true + user2.online.should be_true + + # whack one of the connections + conn.delete + user.reload + user2.reload + user.online.should be_false + user2.online.should be_true + + conn2.delete + user.reload + user2.reload + user.online.should be_false + user2.online.should be_false + end + + it "n connections" do + conn.touch + conn2 = FactoryGirl.create(:connection, + :user => user, + :music_session => music_session, + :ip_address => "1.1.1.1", + :client_id => "2") + + user.reload + user.online.should be_true + + conn.delete + user.reload + user.online.should be_true + + conn2.delete + user.reload + user.online.should be_false + end + + end + end end diff --git a/ruby/spec/jam_ruby/models/notification_spec.rb b/ruby/spec/jam_ruby/models/notification_spec.rb index 2a5865004..1f201c142 100644 --- a/ruby/spec/jam_ruby/models/notification_spec.rb +++ b/ruby/spec/jam_ruby/models/notification_spec.rb @@ -779,6 +779,7 @@ describe Notification do it "success when online" do receiver_connection = FactoryGirl.create(:connection, user: @receiver) + @receiver.reload message = "Just a test message!" calls = count_publish_to_user_calls notification = Notification.send_text_message(message, @sender, @receiver) @@ -796,6 +797,7 @@ describe Notification do it "success when online with long message" do receiver_connection = FactoryGirl.create(:connection, user: @receiver) + @receiver.reload message = "0" * 203 # 200 is clip size calls = count_publish_to_user_calls notification = Notification.send_text_message(message, @sender, @receiver)