From d6938f48189d1910cb553b6569262629fccfa03d Mon Sep 17 00:00:00 2001
From: Julian Lam <julian.lam@gmail.com>
Date: Wed, 24 Jul 2013 15:19:26 -0400
Subject: [PATCH] first pass at a custom title function (not ready for prod)

---
 src/login.js                 |  9 ++++--
 src/meta.js                  | 14 +++++++++
 src/routes/authentication.js |  6 ++--
 src/user.js                  |  3 ++
 src/webserver.js             | 55 +++++++++++++++++++++++-------------
 5 files changed, 61 insertions(+), 26 deletions(-)

diff --git a/src/login.js b/src/login.js
index 17dc94a10e..b0c8716660 100644
--- a/src/login.js
+++ b/src/login.js
@@ -1,7 +1,8 @@
 
 var user = require('./user.js'),
 	bcrypt = require('bcrypt'),
-	RDB = require('./redis.js');
+	RDB = require('./redis.js'),
+	path = require('path');
 
 (function(Login){
 
@@ -72,8 +73,10 @@ var user = require('./user.js'),
 
 						// Save their photo, if present
 						if (photos && photos.length > 0) {
-							user.setUserField(uid, 'uploadedpicture', photos[0].value);
-							user.setUserField(uid, 'picture', photos[0].value);
+							var	photoUrl = photos[0].value;
+							photoUrl = path.dirname(photoUrl) + '/' + path.basename(photoUrl, path.extname(photoUrl)).slice(0, -6) + 'bigger' + path.extname(photoUrl);
+							user.setUserField(uid, 'uploadedpicture', photoUrl);
+							user.setUserField(uid, 'picture', photoUrl);
 						}
 
 						callback(null, {
diff --git a/src/meta.js b/src/meta.js
index 130edb312e..6786fb90e9 100644
--- a/src/meta.js
+++ b/src/meta.js
@@ -79,4 +79,18 @@ var utils = require('./../public/src/utils.js'),
 			// ...
 		}
 	}
+
+	Meta.build_title = function(title, current_user, callback) {
+		var user = require('./user');
+
+		if (!title) title = global.config.title || 'NodeBB';
+		else title += ' | ' + global.config.title || 'NodeBB';
+
+		// Grab the number of unread notifications
+		user.notifications.getUnreadCount(current_user, function(err, count) {
+			if (!err && count > 0) title = '(' + count + ') ' + title;
+
+			callback(err, title);
+		});
+	}
 }(exports));
\ No newline at end of file
diff --git a/src/routes/authentication.js b/src/routes/authentication.js
index ab9db73226..b03bce015e 100644
--- a/src/routes/authentication.js
+++ b/src/routes/authentication.js
@@ -87,7 +87,7 @@
 			console.log('info: [Auth] Session ' + req.sessionID + ' logout (uid: ' + global.uid + ')');
 			login_module.logout(req.sessionID, function(logout) {
 				req.logout();
-				res.send(app.build_header(res) + templates['logout'] + templates['footer']);
+				res.send(app.build_header({ req: req, res: res }) + templates['logout'] + templates['footer']);
 			});
 		});
 
@@ -121,11 +121,11 @@
 
 
 		app.get('/reset/:code', function(req, res) {
-			res.send(app.build_header(res) + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']);
+			res.send(app.build_header({ req: req, res: res }) + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']);
 		});
 
 		app.get('/reset', function(req, res) {
-			res.send(app.build_header(res) + templates['reset'] + templates['footer']);
+			res.send(app.build_header({ req: req, res: res }) + templates['reset'] + templates['footer']);
 		});
 
 
diff --git a/src/user.js b/src/user.js
index 2c56c4a607..29e249b0ef 100644
--- a/src/user.js
+++ b/src/user.js
@@ -1061,6 +1061,9 @@ var utils = require('./../public/src/utils.js'),
 				callback(notifications);
 			});
 		},
+		getUnreadCount: function(uid, callback) {
+			RDB.zcount('uid:' + uid + ':notifications:unread', 0, 10, callback);
+		},
 		hasFlag: function(uid, callback) {
 			RDB.get('uid:1:notifications:flag', function(err, flag) {
 				if (err) {
diff --git a/src/webserver.js b/src/webserver.js
index c2555b1400..9ad69d6fcf 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -26,21 +26,26 @@ var express = require('express'),
 (function(app) {
 	var templates = null;
 	
-	app.build_header = function(res, metaTags) {
+	app.build_header = function(options) {
 		var	defaultMetaTags = [
 				{ name: 'viewport', content: 'width=device-width, initial-scale=1.0' },
 				{ name: 'content-type', content: 'text/html; charset=UTF-8' },
 				{ name: 'apple-mobile-web-app-capable', content: 'yes' }
 			],
-			metaString = utils.buildMetaTags(defaultMetaTags.concat(metaTags || []));
-
-		return templates['header'].parse({
-			cssSrc: global.config['theme:src'] || global.nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
-			title: global.config['title'] || 'NodeBB',
-			csrf:res.locals.csrf_token,
-			relative_path: global.nconf.get('relative_path'),
-			meta_tags: metaString
-		});
+			metaString = utils.buildMetaTags(defaultMetaTags.concat(options.metaTags || [])),
+			templateValues = {
+				cssSrc: global.config['theme:src'] || global.nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
+				title: global.config['title'] || 'NodeBB',
+				csrf: options.res.locals.csrf_token,
+				relative_path: global.nconf.get('relative_path'),
+				meta_tags: metaString
+			};
+
+		// meta.build_title(options.title, (options.req.user ? options.req.user.uid : 0), function(err, title) {
+		// 	if (!err) templateValues.title = title;
+		// });
+
+		return templates['header'].parse(templateValues);
 	};
 
 	// Middlewares
@@ -153,8 +158,8 @@ var express = require('express'),
 							});
 							return;
 						}
-						
-						res.send(app.build_header(res) + app.create_route(route) + templates['footer']);
+
+						res.send(app.build_header({ req: req, res: res }) + app.create_route(route) + templates['footer']);
 					});
 				}(routes[i]));
 			}
@@ -164,7 +169,7 @@ var express = require('express'),
 		app.get('/', function(req, res) {
 			categories.getAllCategories(function(returnData) {
 				res.send(
-					app.build_header(res) +
+					app.build_header({ req: req, res: res }) +
 					'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/home'].parse(returnData) + '\n\t</noscript>' +
 					app.create_route('') +
 					templates['footer']
@@ -192,7 +197,13 @@ var express = require('express'),
 				if (err) return res.redirect('404');
 
 				res.send(
-					app.build_header(res) +
+					app.build_header({
+						req: req,
+						res: res,
+						metaTags: [
+							{ name: "title", content: topic.topic_name }
+						]
+					}) +
 					'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/topic'].parse(topic) + '\n\t</noscript>' +
 					'\n\t<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' +
 					templates['footer']
@@ -220,10 +231,14 @@ var express = require('express'),
 				if(err) return res.redirect('404');
 
 				res.send(
-					app.build_header(res, [
-						{ name: 'title', content: returnData.category_name },
-						{ name: 'description', content: returnData.category_description }
-					]) +
+					app.build_header({
+						req: req,
+						res: res,
+						metaTags: [
+							{ name: 'title', content: returnData.category_name },
+							{ name: 'description', content: returnData.category_description }
+						]
+					}) +
 					'\n\t<noscript>\n' + templates['noscript/header'] + templates['noscript/category'].parse(returnData) + '\n\t</noscript>' +
 					'\n\t<script>templates.ready(function(){ajaxify.go("category/' + category_url + '");});</script>' +
 					templates['footer']
@@ -232,7 +247,7 @@ var express = require('express'),
 		});
 
 		app.get('/confirm/:code', function(req, res) {
-			res.send(app.build_header(res) + '<script>templates.ready(function(){ajaxify.go("confirm/' + req.params.code + '");});</script>' + templates['footer']);
+			res.send(app.build_header({ req: req, res: res }) + '<script>templates.ready(function(){ajaxify.go("confirm/' + req.params.code + '");});</script>' + templates['footer']);
 		});
 
 		app.get('/sitemap.xml', function(req, res) {
@@ -288,7 +303,7 @@ var express = require('express'),
 			var url = req.url.split('?');
 
 			if (url[1]) {
-				res.send(app.build_header(res) + templates['outgoing'].parse({
+				res.send(app.build_header({ req: req, res: res }) + templates['outgoing'].parse({
 					url: url[1],
 					home: global.nconf.get('url')
 				}) + templates['footer']);