From f36d2b0258aefca091e53bc129bf0124c0c28bdd Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Wed, 9 Jul 2014 21:46:45 -0700 Subject: [PATCH] Add onPreChangeMedia and improve refreshing --- lib/channel/chat.js | 2 +- lib/channel/drink.js | 2 +- lib/channel/mediarefresher.js | 38 ++++++++++++++++++++++------------- lib/channel/module.js | 9 ++++++++- lib/channel/playlist.js | 38 +++++++++++++++++++++++++---------- 5 files changed, 61 insertions(+), 28 deletions(-) diff --git a/lib/channel/chat.js b/lib/channel/chat.js index 1775012b..fc67e9e7 100644 --- a/lib/channel/chat.js +++ b/lib/channel/chat.js @@ -131,7 +131,7 @@ ChatModule.prototype.handleChatMsg = function (user, data) { } 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) { self.processChatMsg(user, data); } diff --git a/lib/channel/drink.js b/lib/channel/drink.js index bb702f43..91959725 100644 --- a/lib/channel/drink.js +++ b/lib/channel/drink.js @@ -11,7 +11,7 @@ DrinkModule.prototype.onUserPostJoin = function (user) { 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 perms = this.channel.modules.permissions; if (msg.match(/^\/d-?[0-9]*/) && perms.canCallDrink(user)) { diff --git a/lib/channel/mediarefresher.js b/lib/channel/mediarefresher.js index ec758c7a..ca1fd022 100644 --- a/lib/channel/mediarefresher.js +++ b/lib/channel/mediarefresher.js @@ -1,6 +1,7 @@ var ChannelModule = require("./module"); var Config = require("../config"); var InfoGetter = require("../get-info"); +var Logger = require("../logger"); function MediaRefresherModule(channel) { ChannelModule.apply(this, arguments); @@ -10,33 +11,39 @@ function MediaRefresherModule(channel) { MediaRefresherModule.prototype = Object.create(ChannelModule.prototype); -MediaRefresherModule.prototype.onMediaChange = function (data) { +MediaRefresherModule.prototype.onPreMediaChange = function (data, cb) { if (this._interval) clearInterval(this._interval); this._media = data; switch (data.type) { case "gd": - return this.initGoogleDocs(data); + return this.initGoogleDocs(data, function () { + cb(null, ChannelModule.PASSTHROUGH); + }); 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; - self.refreshGoogleDocs(data, true); + self.refreshGoogleDocs(data, cb); /* * Refresh every 55 minutes. * The expiration is 1 hour, but refresh 5 minutes early to be safe */ self._interval = setInterval(function () { - self.refreshGoogleDocs(data, false); + self.refreshGoogleDocs(data); }, 55 * 60 * 1000); }; -MediaRefresherModule.prototype.initVimeo = function (data) { +MediaRefresherModule.prototype.initVimeo = function (data, cb) { if (!Config.get("vimeo-workaround")) { return; } @@ -48,13 +55,14 @@ MediaRefresherModule.prototype.initVimeo = function (data) { self.channel.logger.log("[mediarefresher] Refreshed vimeo video with ID " + data.id); data.meta.direct = hack; - self.channel.broadcastAll("changeMedia", data.getFullUpdate()); } self.channel.activeLock.release(); + + if (cb) cb(); }); }; -MediaRefresherModule.prototype.refreshGoogleDocs = function (media, update) { +MediaRefresherModule.prototype.refreshGoogleDocs = function (media, cb) { var self = this; if (self.dead || self.channel.dead) { @@ -72,21 +80,23 @@ MediaRefresherModule.prototype.refreshGoogleDocs = function (media, update) { if (err) { Logger.errlog.log("Google Docs refresh failed for ID " + media.id + ": " + err); - return self.channel.activeLock.release(); + self.channel.activeLock.release(); + if (cb) cb(); + return; } } 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 " + media.id); media.meta = data.meta; - if (update) { - self.channel.broadcastAll("changeMedia", data.getFullUpdate()); - } self.channel.activeLock.release(); + if (cb) cb(); }); }; diff --git a/lib/channel/module.js b/lib/channel/module.js index 430efab3..c8c19ec0 100644 --- a/lib/channel/module.js +++ b/lib/channel/module.js @@ -54,7 +54,14 @@ ChannelModule.prototype = { /** * 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); }, diff --git a/lib/channel/playlist.js b/lib/channel/playlist.js index 24b22431..4dcd9197 100644 --- a/lib/channel/playlist.js +++ b/lib/channel/playlist.js @@ -980,8 +980,16 @@ PlaylistModule.prototype.startPlayback = function (time) { if (self.leader != null) { media.paused = false; media.currentTime = time || 0; - self.sendChangeMedia(self.channel.users); - self.channel.notifyModules("onMediaChange", [self.current.media]); + self.channel.checkModules("onPreMediaChange", [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; } @@ -996,16 +1004,24 @@ PlaylistModule.prototype.startPlayback = function (time) { self._leadInterval = false; } - self.sendChangeMedia(self.channel.users); - self.channel.notifyModules("onMediaChange", [self.current.media]); + self.channel.checkModules("onPreMediaChange", [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 */ - if (media.seconds > 0) { - self._lastUpdate = Date.now(); - self._leadInterval = setInterval(function() { - self._leadLoop(); - }, 1000); - } + /* Only start the timer if the media item is not live, i.e. has a duration */ + if (media.seconds > 0) { + self._lastUpdate = Date.now(); + self._leadInterval = setInterval(function() { + self._leadLoop(); + }, 1000); + } + } + ); } const UPDATE_INTERVAL = Config.get("playlist.update-interval");