Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Fix Bug 880569 - Publish Unpublished makes to the Make API
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher De Cairos committed Jun 15, 2013
1 parent 137998b commit 5fcadba
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions migrations/PublishToMakeAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* This Script is designed to upgrade existing popcorn projects
* generated by Popcorn Maker PRE Webmaker V2. It will publish
* each project the does not contain a MakeID to the MakeAPI
* and add the newly generated MakeID to the record.
**/

var async = require( "async" ),
path = require( "path" ),
config = require( "../lib/config" ),

// Database information
dbConfig = config.database,
sequelize = new (require( "sequelize" ))(
dbConfig.database,
dbConfig.username,
dbConfig.password,
dbConfig.options
),

// MakeAPI lib
makeapi = require( "makeapi" ).makeAPI({
apiURL: config.MAKE_ENDPOINT,
auth: config.MAKE_USERNAME + ":" + config.MAKE_PASSWORD
}),

// Project model
Project = sequelize.import( path.normalize( __dirname + "/../lib/models/project" ) ),

// helper vars
limit = 50,
completed = 0
hostname = config.dirs.embedHostname ? config.dirs.embedHostname : config.hostname,
prefix = config.publishStore.namePrefix || "",
suffix = config.publishStore.nameSuffix || "";

function expand( name ) {
name = name + '';
return path.join( prefix, name ) + suffix;
}

function generatePublishUrl( id ) {
return ( hostname + '/' + expand( id.toString( 36 ) ) ).replace( /\\/g, '/' );
}

// Add the newly generated Make ID to the project
function updateWithMakeID( project, makeid, callback ) {
project.makeid = makeid;
project.save().error(function( error ){
callback(error);
}).success(function() {
callback();
});
}

// Build the make object, submit to MakeAPI
function createMake( project, remixID, callback ) {

var makeObj = {
title: project.name,
author: project.author || "",
url: generatePublishUrl( project.id ),
email: project.email,
contentType: "application/x-popcorn",
thumbnail: project.thumbnail,
description: project.description || "",
remixedFrom: remixID || null
};

makeapi.create({
maker: "migrationScript",
make: makeObj
}, function( err, make ) {
if ( err ) {
console.log( err );
callback( err );
}
updateWithMakeID( project, make.id, callback );
});
}

// Resolve remix dependencies
function resolveMake( project, callback ) {
if ( project.remixedFrom ) {
Project.find( project.remixedFrom ).success(function( remixProject ) {
if ( remixProject ) {
createMake( project, remixProject.makeid, callback );
return;
}
// deleted remix
createMake( project, null, callback );
});
} else {
createMake( project, null, callback );
}
}

function getNextSet( callback ) {
Project.findAll({
where: {
makeid: null
},
limit: limit,
order: "id ASC"
}).complete(function( err, projects ) {
async.eachSeries( projects, resolveMake, function() {
callback();
});
});
}

function migrateProjects( err ) {
if ( err ) {
console.log( "There was an error syncing the database: ", JSON.stringify( err ) );
return;
}
Project.count({
where: {
makeid: null
}
}).success(function( count ) {
console.log( "Found " + count + " unpublished projects..." );
async.doWhilst( getNextSet, function() {
completed += limit;
if ( completed < count ) {
return true
}
return false;
}, function( err ) {
if ( err ) {
console.log( "Error: ", err );
}
console.log( count + " Projects successfully added to the MakeAPI!" );
});
});
}

sequelize.sync().complete( migrateProjects );

0 comments on commit 5fcadba

Please sign in to comment.