From 96fe6d288f06e20f31b9696afdb51f2e3be48d5b Mon Sep 17 00:00:00 2001
From: Julian Lam <julian@designcreateplay.com>
Date: Mon, 11 Jan 2016 16:23:37 -0500
Subject: [PATCH] Allowing theme to be re-ordered like plugins.

---
 src/meta/templates.js |  4 ----
 src/meta/themes.js    | 12 +++++++++++-
 src/plugins.js        |  2 --
 src/upgrade.js        | 25 ++++++++++++++++++++++++-
 4 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/src/meta/templates.js b/src/meta/templates.js
index 7f84daf5d2..d7e86591ec 100644
--- a/src/meta/templates.js
+++ b/src/meta/templates.js
@@ -72,7 +72,6 @@ Templates.compile = function(callback) {
 
 			coreTpls = !coreTpls ? [] : coreTpls.map(function(tpl) { return tpl.replace(coreTemplatesPath, ''); });
 			baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
-			themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
 
 			coreTpls.forEach(function(el, i) {
 				paths[coreTpls[i]] = path.join(coreTemplatesPath, coreTpls[i]);
@@ -82,9 +81,6 @@ Templates.compile = function(callback) {
 				paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
 			});
 
-			themeTpls.forEach(function(el, i) {
-				paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]);
-			});
 
 			for (var tpl in pluginTemplates) {
 				if (pluginTemplates.hasOwnProperty(tpl)) {
diff --git a/src/meta/themes.js b/src/meta/themes.js
index fedb1ff0d9..c3d912a222 100644
--- a/src/meta/themes.js
+++ b/src/meta/themes.js
@@ -8,7 +8,8 @@ var nconf = require('nconf'),
 	async = require('async'),
 
 	file = require('../file'),
-	db = require('../database');
+	db = require('../database'),
+	meta = require('../meta');
 
 module.exports = function(Meta) {
 	Meta.themes = {};
@@ -76,6 +77,15 @@ module.exports = function(Meta) {
 		switch(data.type) {
 		case 'local':
 			async.waterfall([
+				async.apply(meta.configs.get, 'theme:id'),
+				function(current, next) {
+					async.series([
+						async.apply(db.sortedSetRemove, 'plugins:active', current),
+						async.apply(db.sortedSetAdd, 'plugins:active', 0, data.id)
+					], function(err) {
+						next(err);
+					});
+				},
 				function(next) {
 					fs.readFile(path.join(nconf.get('themes_path'), data.id, 'theme.json'), function(err, config) {
 						if (!err) {
diff --git a/src/plugins.js b/src/plugins.js
index 85516ba406..f7ef80c984 100644
--- a/src/plugins.js
+++ b/src/plugins.js
@@ -96,8 +96,6 @@ var fs = require('fs'),
 					return next();
 				}
 
-				plugins.push(meta.config['theme:id']);
-
 				plugins = plugins.filter(function(plugin){
 					return plugin && typeof plugin === 'string';
 				}).map(function(plugin){
diff --git a/src/upgrade.js b/src/upgrade.js
index 94aeb1ea55..c82db21a06 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -10,7 +10,7 @@ var db = require('./database'),
 	schemaDate, thisSchemaDate,
 
 	// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
-	latestSchema = Date.UTC(2015, 11, 23);
+	latestSchema = Date.UTC(2016, 0, 11);
 
 Upgrade.check = function(callback) {
 	db.get('schemaDate', function(err, value) {
@@ -278,6 +278,29 @@ Upgrade.upgrade = function(callback) {
 				winston.info('[2015/12/23] Chats room hashes upgrade skipped!');
 				next();
 			}
+		},
+		function(next) {
+			thisSchemaDate = Date.UTC(2016, 0, 11);
+
+			if (schemaDate < thisSchemaDate) {
+				updatesMade = true;
+				winston.info('[2015/12/23] Adding theme to active plugins sorted set');
+
+				async.waterfall([
+					async.apply(db.getObjectField, 'config', 'theme:id'),
+					async.apply(db.sortedSetAdd, 'plugins:active', 0)
+				], function(err) {
+					if (err) {
+						return next(err);
+					}
+
+					winston.info('[2015/12/23] Adding theme to active plugins sorted set done!');
+					Upgrade.update(thisSchemaDate, next);
+				})
+			} else {
+				winston.info('[2015/12/23] Adding theme to active plugins sorted set skipped!');
+				next();
+			}
 		}
 		// Add new schema updates here
 		// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!