Skip to content
This repository was archived by the owner on Nov 7, 2020. It is now read-only.

Commit

Permalink
Add hangout URL invalidation if no one joins
Browse files Browse the repository at this point in the history
After setting a new Hangout URL in a session, start a timer (currently
20 seconds).  If no one has joined yet when the timer has finished,
invalidate the hangout URL so a new one will be created.

This addresses issue #242.
  • Loading branch information
yourcelf committed Jan 9, 2014
1 parent d1bd5ed commit 795f2cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
24 changes: 23 additions & 1 deletion lib/server-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ var client_models = require('../public/js/models.js'),
sanitize = require('validator').sanitize,
crypto = require('crypto');

// How long do we wait for the first participant to join before we consider the
// hangout URL to be a failure?
var HANGOUT_CONNECTION_TIMEOUT = 20000;
// If a participant is creating a non-farmed hangout URL, how long do we give
// them before assuming they've skipped out?
var HANGOUT_CREATION_TIMEOUT = 20000;

// This file contains extensions to the core models defined in
// public/js/models.js.
//
Expand Down Expand Up @@ -299,7 +306,7 @@ exports.ServerSession = client_models.Session.extend({
// obj has (optionally) "userId" and definitely has "time"
var obj = this.get("hangout-pending");

if((new Date().getTime() - obj.time) > 15000) {
if((new Date().getTime() - obj.time) > HANGOUT_CREATION_TIMEOUT) {
logger.debug("Hangout pending was too old, returning false and resetting hangout-pending to null.");
this.set("hangout-pending", null);
return false;
Expand All @@ -323,6 +330,16 @@ exports.ServerSession = client_models.Session.extend({
// let anyone who was waiting for a hangout url from this session know
// that we have a valid one now.
this.trigger("hangout-url", url);

// If no one is in this hangout, set a timeout to invalidate our
// hangout URL if no one joins.
if (this.getNumConnectedParticipants() == 0) {
this._hangoutConnectionTimeout = setTimeout(_.bind(function() {
if (this.getNumConnectedParticipants() == 0) {
this.set("hangout-url", null);
}
}, this), HANGOUT_CONNECTION_TIMEOUT);
}
} else {
// If someone tries to overwrite an active hangout URL with a
// different one, return false, so we can warn them what the
Expand All @@ -347,6 +364,11 @@ exports.ServerSession = client_models.Session.extend({
id: this.id, participants: this.get("connectedParticipants")
});
}
// Clear the timeout that would invalidate our hangout URL if no one
// joined.
if (this._hangoutConnectionTimeout) {
clearTimeout(this._hangoutConnectionTimeout);
}

logger.debug("setting connected participants to: " + _.pluck(participants, "id"));
},
Expand Down
46 changes: 42 additions & 4 deletions test/test.hangout-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ describe("HANGOUT REDIRECTS", function() {

var url = "http://example.com/farmed" + suffix;
checkRedirect(url, "regular1", function() {
farming.getNextHangoutUrl(function(url) {
farming.getNextHangoutUrl(function(err, url) {
expect(err).to.be(null);
expect(url).to.be(null);
done();
});
Expand All @@ -90,7 +91,8 @@ describe("HANGOUT REDIRECTS", function() {
});
it("Uses button URL when farmed hangout links are unavailable", function(done) {
// Ensure we have nothing farmed...
farming.getNextHangoutUrl(function(url) {
farming.getNextHangoutUrl(function(err, url) {
expect(err).to.be(null);
expect(url).to.be(null);
var url = "https://plus.google.com/hangouts/_?gid=fun&gd=sessionId:" + session.id;
checkRedirect(url, "regular1", function() {
Expand All @@ -100,7 +102,7 @@ describe("HANGOUT REDIRECTS", function() {
});
});
it("Lets a 2nd user be the pending creator if the 1st times out", function(done) {
this.timeout(20000); // We're testing long timeouts. :(
this.timeout(30000); // We're testing long timeouts. :(
var u1 = common.server.db.users.findWhere({"sock-key": "regular1"});
var u2 = common.server.db.users.findWhere({"sock-key": "regular2"});
var url = "https://plus.google.com/hangouts/_?gid=fun&gd=sessionId:" + session.id;
Expand All @@ -113,6 +115,42 @@ describe("HANGOUT REDIRECTS", function() {
expect(session.get("hangout-pending").userId).to.be(u2.id);
done();
});
}, 15000);
}, 20000);
});
it("Retains a farmed url after adding it to a session if someone joins", function(done) {
this.timeout(30000); // We're testing long timeouts. :(
var user = common.server.db.users.findWhere({"sock-key": "regular1"});
var url = "http://example.com/good-url";
farming.reuseUrl(url, function(err) {
expect(err).to.be(null);
checkRedirect(url + suffix, "regular1", function() {
session.setConnectedParticipants([{id: user.id}]);
setTimeout(function() {
expect(session.getHangoutUrl()).to.be(url);
done();
}, 25000);
});
});
});
it("Times-out a farmed url (without re-using it) after adding it to a session if no one joins", function(done) {
this.timeout(30000); // We're testing long timeouts. :(
var url = "http://example.com/poison-url";
expect(farming.getNumHangoutsAvailable()).to.be(0);
farming.reuseUrl(url, function(err) {
expect(err).to.be(null);
// We should get the farmed (poison) url...
checkRedirect(url + suffix, "regular1", function() {
// ... but we don't enter the session, and never start it.
setTimeout(function() {
expect(session.getHangoutUrl()).to.be(null);
// Ensure the URL wasn't reused..
farming.getNextHangoutUrl(function(err, url) {
expect(err).to.be(null);
expect(url).to.be(null);
done();
});
}, 20000);
});
});
});
});

0 comments on commit 795f2cf

Please sign in to comment.