Add onPreChangeMedia and improve refreshing

This commit is contained in:
Calvin Montgomery 2014-07-09 21:46:45 -07:00
parent 3f959087af
commit f36d2b0258
5 changed files with 61 additions and 28 deletions

View File

@ -131,7 +131,7 @@ ChatModule.prototype.handleChatMsg = function (user, data) {
} }
data.meta = meta; data.meta = meta;
this.channel.checkModules("onUserChat", [user, data], function (err, result) { this.channel.checkModules("onUserPreChat", [user, data], function (err, result) {
if (result === ChannelModule.PASSTHROUGH) { if (result === ChannelModule.PASSTHROUGH) {
self.processChatMsg(user, data); self.processChatMsg(user, data);
} }

View File

@ -11,7 +11,7 @@ DrinkModule.prototype.onUserPostJoin = function (user) {
user.socket.emit("drinkCount", this.drinks); user.socket.emit("drinkCount", this.drinks);
}; };
DrinkModule.prototype.onUserChat = function (user, data, cb) { DrinkModule.prototype.onUserPreChat = function (user, data, cb) {
var msg = data.msg; var msg = data.msg;
var perms = this.channel.modules.permissions; var perms = this.channel.modules.permissions;
if (msg.match(/^\/d-?[0-9]*/) && perms.canCallDrink(user)) { if (msg.match(/^\/d-?[0-9]*/) && perms.canCallDrink(user)) {

View File

@ -1,6 +1,7 @@
var ChannelModule = require("./module"); var ChannelModule = require("./module");
var Config = require("../config"); var Config = require("../config");
var InfoGetter = require("../get-info"); var InfoGetter = require("../get-info");
var Logger = require("../logger");
function MediaRefresherModule(channel) { function MediaRefresherModule(channel) {
ChannelModule.apply(this, arguments); ChannelModule.apply(this, arguments);
@ -10,33 +11,39 @@ function MediaRefresherModule(channel) {
MediaRefresherModule.prototype = Object.create(ChannelModule.prototype); MediaRefresherModule.prototype = Object.create(ChannelModule.prototype);
MediaRefresherModule.prototype.onMediaChange = function (data) { MediaRefresherModule.prototype.onPreMediaChange = function (data, cb) {
if (this._interval) clearInterval(this._interval); if (this._interval) clearInterval(this._interval);
this._media = data; this._media = data;
switch (data.type) { switch (data.type) {
case "gd": case "gd":
return this.initGoogleDocs(data); return this.initGoogleDocs(data, function () {
cb(null, ChannelModule.PASSTHROUGH);
});
case "vi": case "vi":
return this.initVimeo(data); return this.initVimeo(data, function () {
cb(null, ChannelModule.PASSTHROUGH);
});
default:
return cb(null, ChannelModule.PASSTHROUGH);
} }
}; };
MediaRefresherModule.prototype.initGoogleDocs = function (data) { MediaRefresherModule.prototype.initGoogleDocs = function (data, cb) {
var self = this; var self = this;
self.refreshGoogleDocs(data, true); self.refreshGoogleDocs(data, cb);
/* /*
* Refresh every 55 minutes. * Refresh every 55 minutes.
* The expiration is 1 hour, but refresh 5 minutes early to be safe * The expiration is 1 hour, but refresh 5 minutes early to be safe
*/ */
self._interval = setInterval(function () { self._interval = setInterval(function () {
self.refreshGoogleDocs(data, false); self.refreshGoogleDocs(data);
}, 55 * 60 * 1000); }, 55 * 60 * 1000);
}; };
MediaRefresherModule.prototype.initVimeo = function (data) { MediaRefresherModule.prototype.initVimeo = function (data, cb) {
if (!Config.get("vimeo-workaround")) { if (!Config.get("vimeo-workaround")) {
return; return;
} }
@ -48,13 +55,14 @@ MediaRefresherModule.prototype.initVimeo = function (data) {
self.channel.logger.log("[mediarefresher] Refreshed vimeo video with ID " + self.channel.logger.log("[mediarefresher] Refreshed vimeo video with ID " +
data.id); data.id);
data.meta.direct = hack; data.meta.direct = hack;
self.channel.broadcastAll("changeMedia", data.getFullUpdate());
} }
self.channel.activeLock.release(); self.channel.activeLock.release();
if (cb) cb();
}); });
}; };
MediaRefresherModule.prototype.refreshGoogleDocs = function (media, update) { MediaRefresherModule.prototype.refreshGoogleDocs = function (media, cb) {
var self = this; var self = this;
if (self.dead || self.channel.dead) { if (self.dead || self.channel.dead) {
@ -72,21 +80,23 @@ MediaRefresherModule.prototype.refreshGoogleDocs = function (media, update) {
if (err) { if (err) {
Logger.errlog.log("Google Docs refresh failed for ID " + media.id + Logger.errlog.log("Google Docs refresh failed for ID " + media.id +
": " + err); ": " + err);
return self.channel.activeLock.release(); self.channel.activeLock.release();
if (cb) cb();
return;
} }
} }
if (media !== self._media) { if (media !== self._media) {
return self.channel.activeLock.release(); self.channel.activeLock.release();
if (cb) cb();
return;
} }
self.channel.logger.log("[mediarefresher] Refreshed Google Docs video with ID " + self.channel.logger.log("[mediarefresher] Refreshed Google Docs video with ID " +
media.id); media.id);
media.meta = data.meta; media.meta = data.meta;
if (update) {
self.channel.broadcastAll("changeMedia", data.getFullUpdate());
}
self.channel.activeLock.release(); self.channel.activeLock.release();
if (cb) cb();
}); });
}; };

View File

@ -54,7 +54,14 @@ ChannelModule.prototype = {
/** /**
* Called when a chatMsg event is received * Called when a chatMsg event is received
*/ */
onUserChat: function (user, data, cb) { onUserPreChat: function (user, data, cb) {
cb(null, ChannelModule.PASSTHROUGH);
},
/**
* Called before a new video begins playing
*/
onPreMediaChange: function (data, cb) {
cb(null, ChannelModule.PASSTHROUGH); cb(null, ChannelModule.PASSTHROUGH);
}, },

View File

@ -980,8 +980,16 @@ PlaylistModule.prototype.startPlayback = function (time) {
if (self.leader != null) { if (self.leader != null) {
media.paused = false; media.paused = false;
media.currentTime = time || 0; media.currentTime = time || 0;
self.sendChangeMedia(self.channel.users); self.channel.checkModules("onPreMediaChange", [self.current.media],
self.channel.notifyModules("onMediaChange", [self.current.media]); function () {
/*
* onPreMediaChange doesn't care about the callback result.
* Its purpose is to allow modification of playback data before
* users are sent a changeMedia
*/
self.sendChangeMedia(self.channel.users);
}
);
return; return;
} }
@ -996,16 +1004,24 @@ PlaylistModule.prototype.startPlayback = function (time) {
self._leadInterval = false; self._leadInterval = false;
} }
self.sendChangeMedia(self.channel.users); self.channel.checkModules("onPreMediaChange", [self.current.media],
self.channel.notifyModules("onMediaChange", [self.current.media]); function () {
/*
* onPreMediaChange currently doesn't care about the callback result.
* Its purpose is to allow modification of playback data before
* users are sent a changeMedia
*/
self.sendChangeMedia(self.channel.users);
/* Only start the timer if the media item is not live, i.e. has a duration */ /* Only start the timer if the media item is not live, i.e. has a duration */
if (media.seconds > 0) { if (media.seconds > 0) {
self._lastUpdate = Date.now(); self._lastUpdate = Date.now();
self._leadInterval = setInterval(function() { self._leadInterval = setInterval(function() {
self._leadLoop(); self._leadLoop();
}, 1000); }, 1000);
} }
}
);
} }
const UPDATE_INTERVAL = Config.get("playlist.update-interval"); const UPDATE_INTERVAL = Config.get("playlist.update-interval");