diff --git a/package.json b/package.json index 72e3b906..1b5a8d5c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.22.1", + "version": "3.22.2", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/channel-storage/channelstore.js b/src/channel-storage/channelstore.js index 278dab46..6ad9d6e0 100644 --- a/src/channel-storage/channelstore.js +++ b/src/channel-storage/channelstore.js @@ -9,20 +9,20 @@ export function init() { CHANNEL_STORE = loadChannelStore(); } -export function load(channelName) { +export function load(id, channelName) { if (CHANNEL_STORE === null) { return Promise.reject(new Error('ChannelStore not initialized yet')); } - return CHANNEL_STORE.load(channelName); + return CHANNEL_STORE.load(id, channelName); } -export function save(channelName, data) { +export function save(id, channelName, data) { if (CHANNEL_STORE === null) { return Promise.reject(new Error('ChannelStore not initialized yet')); } - return CHANNEL_STORE.save(channelName, data); + return CHANNEL_STORE.save(id, channelName, data); } function loadChannelStore() { diff --git a/src/channel-storage/dbstore.js b/src/channel-storage/dbstore.js index c53d7a62..fc2cedaf 100644 --- a/src/channel-storage/dbstore.js +++ b/src/channel-storage/dbstore.js @@ -34,14 +34,13 @@ function buildUpdateQuery(numEntries) { } export class DatabaseStore { - load(channelName) { - return queryAsync(QUERY_CHANNEL_ID_FOR_NAME, [channelName]).then((rows) => { - if (rows.length === 0) { - throw new ChannelNotFoundError(`Channel does not exist: "${channelName}"`); - } + load(id, channelName) { + if (!id || id === 0) { + return Promise.reject(new Error(`Cannot load state for [${channelName}]: ` + + `id was passed as [${id}]`)); + } - return queryAsync(QUERY_CHANNEL_DATA, [rows[0].id]); - }).then(rows => { + return queryAsync(QUERY_CHANNEL_DATA, [id]).then(rows => { const data = {}; rows.forEach(row => { try { @@ -56,36 +55,34 @@ export class DatabaseStore { }); } - save(channelName, data) { - return queryAsync(QUERY_CHANNEL_ID_FOR_NAME, [channelName]).then((rows) => { - if (rows.length === 0) { - throw new ChannelNotFoundError(`Channel does not exist: "${channelName}"`); - } + save(id, channelName, data) { + if (!id || id === 0) { + return Promise.reject(new Error(`Cannot save state for [${channelName}]: ` + + `id was passed as [${id}]`)); + } - let totalSize = 0; - let rowCount = 0; - const id = rows[0].id; - const substitutions = []; - for (const key in data) { - if (typeof data[key] === 'undefined') { - continue; - } - rowCount++; - const value = JSON.stringify(data[key]); - totalSize += value.length; - substitutions.push(id); - substitutions.push(key); - substitutions.push(value); + let totalSize = 0; + let rowCount = 0; + const substitutions = []; + for (const key in data) { + if (typeof data[key] === 'undefined') { + continue; } + rowCount++; + const value = JSON.stringify(data[key]); + totalSize += value.length; + substitutions.push(id); + substitutions.push(key); + substitutions.push(value); + } - if (totalSize > SIZE_LIMIT) { - throw new ChannelStateSizeError('Channel state size is too large', { - limit: SIZE_LIMIT, - actual: totalSize - }); - } + if (totalSize > SIZE_LIMIT) { + throw new ChannelStateSizeError('Channel state size is too large', { + limit: SIZE_LIMIT, + actual: totalSize + }); + } - return queryAsync(buildUpdateQuery(rowCount), substitutions); - }); + return queryAsync(buildUpdateQuery(rowCount), substitutions); } } diff --git a/src/channel-storage/filestore.js b/src/channel-storage/filestore.js index dc0f292c..070112e6 100644 --- a/src/channel-storage/filestore.js +++ b/src/channel-storage/filestore.js @@ -16,7 +16,7 @@ export class FileStore { return path.join(CHANDUMP_DIR, channelName); } - load(channelName) { + load(id, channelName) { const filename = this.filenameForChannel(channelName); return statAsync(filename).then(stats => { if (stats.size > SIZE_LIMIT) { @@ -36,7 +36,7 @@ export class FileStore { }); } - save(channelName, data) { + save(id, channelName, data) { const filename = this.filenameForChannel(channelName); const fileContents = new Buffer(JSON.stringify(data), 'utf8'); if (fileContents.length > SIZE_LIMIT) { diff --git a/src/channel/channel.js b/src/channel/channel.js index a5629a0f..073df3d0 100644 --- a/src/channel/channel.js +++ b/src/channel/channel.js @@ -84,6 +84,7 @@ function Channel(name) { this.users = []; this.refCounter = new ReferenceCounter(this); this.flags = 0; + this.id = 0; this.broadcastUsercount = throttle(() => { this.broadcastAll("usercount", this.users.length); }, USERCOUNT_THROTTLE); @@ -206,7 +207,7 @@ Channel.prototype.loadState = function () { self.setFlag(Flags.C_READY | Flags.C_ERROR); } - ChannelStore.load(this.uniqueName).then(data => { + ChannelStore.load(this.id, this.uniqueName).then(data => { Object.keys(this.modules).forEach(m => { try { this.modules[m].load(data); @@ -260,7 +261,8 @@ Channel.prototype.saveState = function () { this.modules[m].save(data); }); - return ChannelStore.save(this.uniqueName, data).catch(ChannelStateSizeError, err => { + return ChannelStore.save(this.id, this.uniqueName, data) + .catch(ChannelStateSizeError, err => { this.users.forEach(u => { if (u.account.effectiveRank >= 2) { u.socket.emit("warnLargeChandump", { diff --git a/src/database.js b/src/database.js index 008e47f6..3906f453 100644 --- a/src/database.js +++ b/src/database.js @@ -80,6 +80,10 @@ module.exports.query = function (query, sub, callback) { Metrics.stopTimer(timer); } + if (process.env.SHOW_SQL) { + console.log(query); + } + if (sub) { conn.query(query, sub, cback); } else { diff --git a/src/database/accounts.js b/src/database/accounts.js index 3c0e4846..469a71c7 100644 --- a/src/database/accounts.js +++ b/src/database/accounts.js @@ -232,48 +232,6 @@ module.exports = { }); }, - /** - * Verify an auth string of the form name:hash - */ - verifyAuth: function (auth, callback) { - if (typeof callback !== "function") { - return; - } - - if (typeof auth !== "string") { - callback("Invalid auth string", null); - return; - } - - var split = auth.split(":"); - if (split.length !== 2) { - callback("Invalid auth string", null); - return; - } - - var name = split[0]; - var hash = split[1]; - db.query("SELECT name,password,global_rank FROM `users` WHERE " + - "name=? and password=?", [name, hash], - function (err, rows) { - if (err) { - callback(err, null); - return; - } - - if (rows.length === 0) { - callback("Auth string does not match an existing user", null); - return; - } - - callback(null, { - name: rows[0].name, - hash: rows[0].password, - global_rank: rows[0].global_rank - }); - }); - }, - /** * Change a user's password */ diff --git a/src/database/channels.js b/src/database/channels.js index 228322eb..f7dce3d2 100644 --- a/src/database/channels.js +++ b/src/database/channels.js @@ -273,6 +273,7 @@ module.exports = { // than the database has stored. Update accordingly. chan.name = res[0].name; chan.uniqueName = chan.name.toLowerCase(); + chan.id = res[0].id; chan.setFlag(Flags.C_REGISTERED); chan.logger.log("[init] Loaded channel from database"); callback(null, true);