From 65f9c98f18a27ecac5db04ba9086fbe93b941fed Mon Sep 17 00:00:00 2001 From: Christopher De Cairos Date: Wed, 15 May 2013 15:20:35 -0400 Subject: [PATCH] Fix Bug 871700 - Basic tag support for Popcorn Maker projects --- lib/project.js | 50 +++++++++++++------------- package.json | 3 +- public/src/core/project.js | 22 +++++++++++- public/src/editor/project-editor.js | 3 ++ public/src/layouts/project-editor.html | 2 ++ routes/index.js | 15 +++++--- server.js | 13 +++---- 7 files changed, 71 insertions(+), 37 deletions(-) diff --git a/lib/project.js b/lib/project.js index 0486f21e..675b515e 100644 --- a/lib/project.js +++ b/lib/project.js @@ -7,7 +7,7 @@ function defaultDBReadyFunction( err ) { } } -module.exports = function( config, makeapi, utils, dbReadyFn ) { +module.exports = function( config, makeapiConfig, utils, dbReadyFn ) { config = config || {}; dbReadyFn = dbReadyFn || defaultDBReadyFunction; @@ -16,7 +16,7 @@ module.exports = function( config, makeapi, utils, dbReadyFn ) { password = config.password || "", Sequelize = require( "sequelize" ), sequelize, - makeClient = require( "makeapi" ).makeAPI( makeapi ); + makeClient = require( "makeapi" ).makeAPI( makeapiConfig ); try { sequelize = new Sequelize( config.database, username, password, config.options ); @@ -139,7 +139,7 @@ module.exports = function( config, makeapi, utils, dbReadyFn ) { return callback( err ); } - makeClient.remove( result[ 0 ]._id, function( error ) { + makeClient.remove( result[ 0 ].id, function( error ) { if ( error ) { return callback( error ); } @@ -234,8 +234,8 @@ module.exports = function( config, makeapi, utils, dbReadyFn ) { return; } - var projectDataJSON = data.data; - var projectDataString = JSON.stringify( projectDataJSON ); + var projectDataJSON = data.data, + projectDataString = JSON.stringify( projectDataJSON ); project.updateAttributes({ data: projectDataString, @@ -251,30 +251,32 @@ module.exports = function( config, makeapi, utils, dbReadyFn ) { .error( function( err ) { callback( err ); }) - .success( function( projectUpdateResult ) { - makeClient.url( utils.generatePublishUrl( projectUpdateResult.id ) ).then(function( err, result ) { + .success( function( project ) { + makeClient.url( utils.generatePublishUrl( project.id ) ) + .then( function( err, result ) { if ( err ) { - return callback( err, projectUpdateResult ); + callback( err, project ); + return; } - makeClient.update( result[ 0 ]._id, { - maker: email, - make: { - title: projectUpdateResult.name, - author: projectUpdateResult.author, - email: email, - contentType: "application/x-popcorn", - url: utils.generatePublishUrl( projectUpdateResult.id ), - thumbnail: projectUpdateResult.thumbnail, - description: projectUpdateResult.description, - remixedFrom: projectUpdateResult.remixedFrom - } - }, function( error ) { - if ( error ) { - return callback( error ); + var make = result[ 0 ]; + + make.title = project.name; + make.author = project.author; + make.url = utils.generatePublishUrl( project.id ); + make.contentType = "application/x-popcorn"; + make.thumbnail = project.thumbnail; + make.description = project.description; + make.email = email; + make.tags = data.tags; + + make.update( email, function( err ) { + if ( err ) { + callback( err ); + return; } - callback( null, projectUpdateResult ); + callback( null, project ); }); }); }); diff --git a/package.json b/package.json index 3d43da69..43fcca2b 100644 --- a/package.json +++ b/package.json @@ -91,9 +91,8 @@ "knox": "0.7.0", "less-middleware": "0.1.9", "less": "1.3.3", - "makeapi": "0.1.5", + "makeapi": "0.1.10", "mysql": "2.0.0-alpha8", - "makeapi": "0.1.5", "nconf": "0.6.7", "newrelic": "0.9.20", "node-uuid": "1.4.0", diff --git a/public/src/core/project.js b/public/src/core/project.js index c38736b0..4b4d855d 100644 --- a/public/src/core/project.js +++ b/public/src/core/project.js @@ -13,6 +13,8 @@ define( [ "core/eventmanager", "core/media", "util/sanitizer" ], _id, _name, _template, _author, _description, _dataObject, _publishUrl, _iframeUrl, _remixedFrom, _remixedFromUrl, + _tags = [], + // Whether or not a save to server is required (project data has changed) _isDirty = false, @@ -124,6 +126,19 @@ define( [ "core/eventmanager", "core/media", "util/sanitizer" ], enumerable: true }, + "tags": { + set: function( val ) { + _tags = val.split( "," ).map(function( v ) { + return v.trim(); + }); + invalidate(); + }, + get: function() { + return _tags; + }, + enumerable: true + }, + "data": { get: function() { // Memoize value, since it doesn't always change @@ -209,7 +224,6 @@ define( [ "core/eventmanager", "core/media", "util/sanitizer" ], _this.import = function( json ) { var oldTarget, targets, targetData, mediaData, media, m, i, l; - // If JSON, convert to Object if ( typeof json === "string" ) { try { @@ -243,6 +257,10 @@ define( [ "core/eventmanager", "core/media", "util/sanitizer" ], _description = json.description; } + if ( json.tags ) { + _tags = json.tags; + } + if ( json.thumbnail ) { _thumbnail = json.thumbnail; } @@ -322,6 +340,7 @@ define( [ "core/eventmanager", "core/media", "util/sanitizer" ], data.template = _template; data.author = _author; data.description = _description; + data.tags = _tags.join( "," ); data.thumbnail = _thumbnail; data.backupDate = Date.now(); try { @@ -363,6 +382,7 @@ define( [ "core/eventmanager", "core/media", "util/sanitizer" ], description: _description, thumbnail: _thumbnail, data: _this.data, + tags: _this.tags, remixedFrom: _remixedFrom }; diff --git a/public/src/editor/project-editor.js b/public/src/editor/project-editor.js index 2daeaef1..f34814db 100644 --- a/public/src/editor/project-editor.js +++ b/public/src/editor/project-editor.js @@ -13,6 +13,7 @@ define([ "editor/editor", "editor/base-editor", _socialMedia = new SocialMedia(), _projectURL = _rootElement.querySelector( ".butter-project-url" ), _authorInput = _rootElement.querySelector( ".butter-project-author" ), + _tagInput = _rootElement.querySelector( ".butter-project-tags" ), _descriptionInput = _rootElement.querySelector( ".butter-project-description" ), _dropArea = _rootElement.querySelector( ".image-droparea" ), _thumbnailInput = _rootElement.querySelector( ".butter-project-thumbnail" ), @@ -131,6 +132,7 @@ define([ "editor/editor", "editor/base-editor", applyInputListeners( _authorInput, "author" ); applyInputListeners( _thumbnailInput, "thumbnail" ); + applyInputListeners( _tagInput, "tags" ); applyInputListeners( _descriptionInput, "description" ); _descriptionInput.addEventListener( "keyup", checkDescription, false ); @@ -171,6 +173,7 @@ define([ "editor/editor", "editor/base-editor", _previewBtn.href = _projectURL.value = _project.publishUrl; _viewSourceBtn.href = "view-source:" + _project.iframeUrl; _thumbnailInput.value = _project.thumbnail; + _tagInput.value = _project.tags; updateEmbed( _project.iframeUrl ); _previewBtn.onclick = function() { diff --git a/public/src/layouts/project-editor.html b/public/src/layouts/project-editor.html index 8a1d8aa9..9a8a4f31 100644 --- a/public/src/layouts/project-editor.html +++ b/public/src/layouts/project-editor.html @@ -31,6 +31,8 @@
Or drag an image here from your desktop
+ + diff --git a/routes/index.js b/routes/index.js index a3ab813a..c1057a8a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,12 +1,13 @@ 'use strict'; module.exports = function routesCtor( app, Project, filter, sanitizer, - stores, utils, metrics ) { + stores, utils, metrics, makeapiConfig ) { var uuid = require( "node-uuid" ), // Keep track of whether this is production or development deploymentType = app.settings.env === "production" ? "production" : "development", - api = require( "./api" )( metrics, utils, stores ); + api = require( "./api" )( metrics, utils, stores ), + makeClient = require( "makeapi" ).makeAPI( makeapiConfig ); app.get( '/healthcheck', api.healthcheck ); @@ -112,7 +113,14 @@ module.exports = function routesCtor( app, Project, filter, sanitizer, projectJSON.remixedFrom = doc.remixedFrom; projectJSON.remixedFromUrl = utils.generateIframeUrl( doc.remixedFrom ); } - res.json( projectJSON ); + + makeClient.url( projectJSON.publishUrl ).then(function( err, make ) { + if ( err ) { + res.json( 500, { error: err } ); + } + projectJSON.tags = make[ 0 ].tags; + res.json( projectJSON ); + }); }); }); @@ -120,7 +128,6 @@ module.exports = function routesCtor( app, Project, filter, sanitizer, app.get( '/api/remix/:id', filter.isStorageAvailable, function( req, res ) { - Project.find( { id: req.params.id }, function( err, project ) { if ( err ) { res.json( { error: err }, 500 ); diff --git a/server.js b/server.js index f6cad2f8..0c308754 100644 --- a/server.js +++ b/server.js @@ -22,7 +22,11 @@ var express = require('express'), APP_HOSTNAME = config.hostname, WWW_ROOT = path.resolve( __dirname, config.dirs.wwwRoot ), VALID_TEMPLATES = config.templates, - port = config.PORT; + port = config.PORT, + makeapiConfig= { + apiURL: config.MAKE_ENDPOINT, + auth: config.MAKE_USERNAME + ":" + config.MAKE_PASSWORD + }; var templateConfigs = {}; @@ -160,10 +164,7 @@ app.configure( function() { EMBED_SUFFIX: '_' }, stores ); - Project = require( './lib/project' )( config.database, { - apiURL: config.MAKE_ENDPOINT, - auth: config.MAKE_USERNAME + ":" + config.MAKE_PASSWORD - }, utils ); + Project = require( './lib/project' )( config.database, makeapiConfig, utils ); filter = require( './lib/filter' )( Project.isDBOnline ); }); @@ -172,7 +173,7 @@ require( 'express-persona' )( app, { }); var routes = require('./routes'); -routes( app, Project, filter, sanitizer, stores, utils, metrics ); +routes( app, Project, filter, sanitizer, stores, utils, metrics, makeapiConfig ); function writeEmbedShell( embedPath, url, data, callback ) { if( !writeEmbedShell.templateFn ) {