| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var pkg = require('./../../package.json'), | 
					
						
							|  |  |  | 	meta = require('./../meta'), | 
					
						
							|  |  |  | 	user = require('./../user'), | 
					
						
							| 
									
										
										
										
											2014-07-09 15:41:03 -04:00
										 |  |  | 	plugins = require('./../plugins'), | 
					
						
							| 
									
										
										
										
											2014-11-29 23:38:36 -05:00
										 |  |  | 	widgets = require('../widgets'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-04 11:37:20 -05:00
										 |  |  | 	validator = require('validator'), | 
					
						
							| 
									
										
										
										
											2014-11-29 23:38:36 -05:00
										 |  |  | 	nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | var apiController = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-03 12:16:46 -05:00
										 |  |  | apiController.getConfig = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-03-19 02:07:14 -04:00
										 |  |  | 	var config = {}; | 
					
						
							| 
									
										
										
										
											2014-11-29 23:38:36 -05:00
										 |  |  | 	config.relative_path = nconf.get('relative_path'); | 
					
						
							| 
									
										
										
										
											2014-12-25 14:13:54 -05:00
										 |  |  | 	config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket']; | 
					
						
							| 
									
										
										
										
											2014-12-19 18:58:28 -05:00
										 |  |  | 	config.websocketAddress = nconf.get('socket.io:address') || ''; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.version = pkg.version; | 
					
						
							| 
									
										
										
										
											2015-01-04 11:37:20 -05:00
										 |  |  | 	config.siteTitle = validator.escape(meta.config.title || meta.config.browserTitle || 'NodeBB'); | 
					
						
							| 
									
										
										
										
											2014-10-22 17:22:47 -04:00
										 |  |  | 	config.showSiteTitle = parseInt(meta.config.showSiteTitle, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.postDelay = meta.config.postDelay; | 
					
						
							|  |  |  | 	config.minimumTitleLength = meta.config.minimumTitleLength; | 
					
						
							|  |  |  | 	config.maximumTitleLength = meta.config.maximumTitleLength; | 
					
						
							|  |  |  | 	config.minimumPostLength = meta.config.minimumPostLength; | 
					
						
							|  |  |  | 	config.hasImageUploadPlugin = plugins.hasListeners('filter:uploadImage'); | 
					
						
							|  |  |  | 	config.maximumProfileImageSize = meta.config.maximumProfileImageSize; | 
					
						
							|  |  |  | 	config.minimumUsernameLength = meta.config.minimumUsernameLength; | 
					
						
							|  |  |  | 	config.maximumUsernameLength = meta.config.maximumUsernameLength; | 
					
						
							|  |  |  | 	config.minimumPasswordLength = meta.config.minimumPasswordLength; | 
					
						
							|  |  |  | 	config.maximumSignatureLength = meta.config.maximumSignatureLength; | 
					
						
							|  |  |  | 	config.useOutgoingLinksPage = parseInt(meta.config.useOutgoingLinksPage, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-05-16 16:28:35 -04:00
										 |  |  | 	config.allowGuestSearching = parseInt(meta.config.allowGuestSearching, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-12-31 12:36:25 -05:00
										 |  |  | 	config.allowGuestHandles = parseInt(meta.config.allowGuestHandles, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1; | 
					
						
							|  |  |  | 	config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-09-17 17:16:03 -04:00
										 |  |  | 	config.allowAccountDelete = parseInt(meta.config.allowAccountDelete, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-03-26 11:57:42 -04:00
										 |  |  | 	config.privateUserInfo = parseInt(meta.config.privateUserInfo, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.usePagination = parseInt(meta.config.usePagination, 10) === 1; | 
					
						
							|  |  |  | 	config.disableSocialButtons = parseInt(meta.config.disableSocialButtons, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-11-13 15:47:25 -05:00
										 |  |  | 	config.disableChat = parseInt(meta.config.disableChat, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-04-03 17:36:27 -04:00
										 |  |  | 	config.maxReconnectionAttempts = meta.config.maxReconnectionAttempts || 5; | 
					
						
							|  |  |  | 	config.reconnectionDelay = meta.config.reconnectionDelay || 200; | 
					
						
							| 
									
										
										
										
											2014-05-22 14:41:11 -04:00
										 |  |  | 	config.tagsPerTopic = meta.config.tagsPerTopic || 5; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.topicsPerPage = meta.config.topicsPerPage || 20; | 
					
						
							|  |  |  | 	config.postsPerPage = meta.config.postsPerPage || 20; | 
					
						
							|  |  |  | 	config.maximumFileSize = meta.config.maximumFileSize; | 
					
						
							| 
									
										
										
										
											2014-03-09 19:19:13 -04:00
										 |  |  | 	config['theme:id'] = meta.config['theme:id']; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.defaultLang = meta.config.defaultLang || 'en_GB'; | 
					
						
							| 
									
										
										
										
											2014-09-01 14:09:47 -04:00
										 |  |  | 	config.userLang = config.defaultLang; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	config.environment = process.env.NODE_ENV; | 
					
						
							| 
									
										
										
										
											2014-07-24 21:11:46 -04:00
										 |  |  | 	config.loggedIn = !!req.user; | 
					
						
							| 
									
										
										
										
											2014-04-16 20:19:26 -04:00
										 |  |  | 	config['cache-buster'] = meta.config['cache-buster'] || ''; | 
					
						
							| 
									
										
										
										
											2014-07-28 15:25:04 -04:00
										 |  |  | 	config['script-buster'] = meta.js.hash; | 
					
						
							|  |  |  | 	config['css-buster'] = meta.css.hash; | 
					
						
							| 
									
										
										
										
											2014-06-03 18:39:54 -04:00
										 |  |  | 	config.requireEmailConfirmation = parseInt(meta.config.requireEmailConfirmation, 10) === 1; | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 	config.topicPostSort = meta.config.topicPostSort || 'oldest_to_newest'; | 
					
						
							| 
									
										
										
										
											2015-01-08 13:47:15 -05:00
										 |  |  | 	config.categoryTopicSort = meta.config.categoryTopicSort || 'newest_to_oldest'; | 
					
						
							| 
									
										
										
										
											2014-11-18 14:54:54 -05:00
										 |  |  | 	config.csrf_token = req.csrfToken(); | 
					
						
							| 
									
										
										
										
											2014-11-24 12:38:44 -05:00
										 |  |  | 	config.searchEnabled = plugins.hasListeners('filter:search.query'); | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!req.user) { | 
					
						
							| 
									
										
										
										
											2014-03-03 12:16:46 -05:00
										 |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2014-10-04 18:47:56 -04:00
										 |  |  | 			res.status(200).json(config); | 
					
						
							| 
									
										
										
										
											2014-03-03 12:16:46 -05:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			next(null, config); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-26 11:57:42 -04:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-26 11:57:42 -04:00
										 |  |  | 	user.getSettings(req.user.uid, function(err, settings) { | 
					
						
							| 
									
										
										
										
											2014-04-13 15:05:17 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-26 11:57:42 -04:00
										 |  |  | 		config.usePagination = settings.usePagination; | 
					
						
							|  |  |  | 		config.topicsPerPage = settings.topicsPerPage; | 
					
						
							|  |  |  | 		config.postsPerPage = settings.postsPerPage; | 
					
						
							|  |  |  | 		config.notificationSounds = settings.notificationSounds; | 
					
						
							| 
									
										
										
										
											2014-09-01 14:09:47 -04:00
										 |  |  | 		config.userLang = settings.language || config.defaultLang; | 
					
						
							| 
									
										
										
										
											2014-05-08 14:16:40 -04:00
										 |  |  | 		config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab; | 
					
						
							| 
									
										
										
										
											2014-06-06 22:12:14 -04:00
										 |  |  | 		config.topicPostSort = settings.topicPostSort || config.topicPostSort; | 
					
						
							| 
									
										
										
										
											2015-01-08 13:47:15 -05:00
										 |  |  | 		config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort; | 
					
						
							| 
									
										
										
										
											2014-11-24 12:48:21 -05:00
										 |  |  | 		config.topicSearchEnabled = settings.topicSearchEnabled || false; | 
					
						
							| 
									
										
										
										
											2014-03-26 11:57:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2014-10-04 18:47:56 -04:00
										 |  |  | 			res.status(200).json(config); | 
					
						
							| 
									
										
										
										
											2014-03-26 11:57:42 -04:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			next(err, config); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-03-03 11:45:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-09 15:41:03 -04:00
										 |  |  | apiController.renderWidgets = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-09-16 10:12:12 -04:00
										 |  |  | 	var async = require('async'), | 
					
						
							|  |  |  | 		uid = req.user ? req.user.uid : 0, | 
					
						
							|  |  |  | 		areas = { | 
					
						
							| 
									
										
										
										
											2014-07-09 19:23:03 -04:00
										 |  |  | 			template: req.query.template, | 
					
						
							| 
									
										
										
										
											2014-09-16 10:12:12 -04:00
										 |  |  | 			locations: req.query.locations, | 
					
						
							| 
									
										
										
										
											2014-07-09 19:23:03 -04:00
										 |  |  | 			url: req.query.url | 
					
						
							| 
									
										
										
										
											2014-09-16 10:12:12 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		renderedWidgets = []; | 
					
						
							| 
									
										
										
										
											2014-07-09 15:41:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 10:12:12 -04:00
										 |  |  | 	if (!areas.template || !areas.locations) { | 
					
						
							| 
									
										
										
										
											2014-10-04 18:47:56 -04:00
										 |  |  | 		return res.status(200).json({}); | 
					
						
							| 
									
										
										
										
											2014-07-09 19:23:03 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 18:56:36 -04:00
										 |  |  | 	widgets.render(uid, { | 
					
						
							|  |  |  | 		template: areas.template, | 
					
						
							|  |  |  | 		url: areas.url, | 
					
						
							|  |  |  | 		locations: areas.locations | 
					
						
							|  |  |  | 	}, function(err, widgets) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-04 18:47:56 -04:00
										 |  |  | 		res.status(200).json(widgets); | 
					
						
							| 
									
										
										
										
											2014-07-09 15:41:03 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = apiController; |