diff --git a/decentralize.js b/decentralize.js index 15c98a6..c24a6e8 100644 --- a/decentralize.js +++ b/decentralize.js @@ -6,9 +6,9 @@ var procure = require('procure'); var Maki = require('maki'); var Soundcloud = require('./lib/Soundcloud'); var Engine = require('./lib/Engine'); -var WebSocket = require('ws'); +var Sessions = require('maki-sessions'); -var jsonpatch = require('fast-json-patch'); +var rest = require('restler'); var home = 'https://' + config.service.authority; @@ -18,34 +18,11 @@ source.authority = source.host + (([80, 443].indexOf( parseInt(source.port) ) == var decentralize = new Maki( config ); var soundcloud = new Soundcloud( config.soundcloud ); -Show = decentralize.define('Show', { - attributes: { - title: { type: String , slug: true }, - slug: { type: String }, - recorded: { type: Date }, - released: { type: Date , default: Date.now , required: true }, - description: { type: String }, - audio: { type: String }, - // TODO: replace with a sources list. - youtube: { type: String } - }, - names: { get: 'item' }, - source: source.proto + '://' + source.authority + '/recordings', - icon: 'sound' -}); +var sessions = new Sessions(); +decentralize.use( sessions ); -Index = decentralize.define('Index', { - name: 'Index', - routes: { query: '/' }, - templates: { query: 'index' }, - requires: { - 'Show': { - filter: {} - } - }, - static: true, - internal: true -}); +Show = decentralize.define('Show', require('./resources/Show') ); +Index = decentralize.define('Index', require('./resources/Index') ); procure( source.proto + '://' + source.authority + '/shows/decentralize', function(err, show) { if (err) return console.error(err); @@ -77,6 +54,25 @@ procure( source.proto + '://' + source.authority + '/shows/decentralize', functi return next(); } }); + decentralize.app.get('/shows/:showSlug/edit', function(req, res, next) { + Show.get({ slug: req.param('showSlug') }, function(err, show) { + return res.render('show-edit', { + item: show + }); + }); + }); + decentralize.app.post('/shows/:showSlug', function(req, res, next) { + rest.patch( source.proto + '://' + source.authority + '/recordings/' + req.param('showSlug') , { + headers: { + Accept: 'application/json' + }, + data: req.body + }).on('complete', function(data, request) { + if (request.statusCode !== 200) return res.error('Editing failed'); + req.flash('success', 'Content edited successfully!'); + return res.redirect('/shows/' + req.param('showSlug')); + }); + }); decentralize.app.get('/rss', function(req, res, next) { res.redirect( 301 , '/feed' ); }); @@ -90,38 +86,13 @@ procure( source.proto + '://' + source.authority + '/shows/decentralize', functi }); }); - // subscribe to updates to important things. - // mainly, recordings - var ws = new WebSocket(source.sockets + source.authority + '/recordings'); - ws.on('error', function(err) { - console.error( err ); - }); - ws.on('message', function(data) { - console.log('DATAGRAM:' , data ); - var msg = JSON.parse( data ); - if (msg.method === 'patch') { - if (msg.params.channel === '/recordings') { - jsonpatch.apply( decentralize.resources['Show'].data , msg.params.ops ); - } - - if (msg.params.channel.match(/\/recordings\/(.*)/)) { - var recordings = decentralize.resources['Show'].data; - for (var i = 0; i < recordings.length; i++) { - if (msg.params.channel === '/recordings/' + recordings[ i ].slug ) { - jsonpatch.apply( recordings[ i ] , msg.params.ops ); - } - } - } - - } - }); - - var engine = new Engine( config ); + var engine = new Engine( config , decentralize ); setInterval(function() { engine.sync(); }, /*/ 2500 /*/ 1 * 3600 * 1000 /**/ ); engine.sync(); + engine.subscribe(); }); diff --git a/lib/Engine.js b/lib/Engine.js index a4638c4..64d0cea 100644 --- a/lib/Engine.js +++ b/lib/Engine.js @@ -4,13 +4,44 @@ var request = require('request'); var jsonpatch = require('fast-json-patch'); var contentDisposition = require('content-disposition'); +var WebSocket = require('ws'); var Soundcloud = require('./Soundcloud'); var Form = require('form-data'); -function Engine( config ) { +function Engine( config , decentralize ) { this.config = config; + this.decentralize = decentralize; this.soundcloud = new Soundcloud( config.soundcloud ); -} +}; + +Engine.prototype.subscribe = function( cb ) { + var self = this; + // subscribe to updates to important things. + // mainly, recordings + var ws = new WebSocket( self.config.source.sockets + self.config.source.authority + '/recordings'); + ws.on('error', function(err) { + console.error( err ); + }); + ws.on('message', function(data) { + console.log('DATAGRAM:' , data ); + var msg = JSON.parse( data ); + if (msg.method === 'patch') { + if (msg.params.channel === '/recordings') { + jsonpatch.apply( self.decentralize.resources['Show'].data , msg.params.ops ); + } + + if (msg.params.channel.match(/\/recordings\/(.*)/)) { + var recordings = self.decentralize.resources['Show'].data; + for (var i = 0; i < recordings.length; i++) { + if (msg.params.channel === '/recordings/' + recordings[ i ].slug ) { + jsonpatch.apply( recordings[ i ] , msg.params.ops ); + } + } + } + + } + }); +}; Engine.prototype.sync = function( cb ) { if (!cb) var cb = new Function(); @@ -90,6 +121,6 @@ Engine.prototype.sync = function( cb ) { }); }); }); -} +}; module.exports = Engine; diff --git a/package.json b/package.json index 25aa7e0..47d5fdb 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "form-data": "^0.2.0", "lodash": "^3.3.0", "maki": "git://github.com/martindale/maki", + "maki-sessions": "^0.1.0", "node-torrent-stream": "git://github.com/unusualbob/node-torrent-stream", "procure": "^0.1.3", "request": "^2.53.0", diff --git a/resources/Index.js b/resources/Index.js new file mode 100644 index 0000000..a56349b --- /dev/null +++ b/resources/Index.js @@ -0,0 +1,12 @@ +module.exports = { + name: 'Index', + routes: { query: '/' }, + templates: { query: 'index' }, + requires: { + 'Show': { + filter: {} + } + }, + static: true, + internal: true +}; diff --git a/resources/Show.js b/resources/Show.js new file mode 100644 index 0000000..5996214 --- /dev/null +++ b/resources/Show.js @@ -0,0 +1,17 @@ +var source = require('../config').source; + +module.exports = { + attributes: { + title: { type: String , slug: true }, + slug: { type: String }, + recorded: { type: Date }, + released: { type: Date , default: Date.now , required: true }, + description: { type: String , format: 'markdown' }, + audio: { type: String }, + // TODO: replace with a sources list. + youtube: { type: String } + }, + names: { get: 'item' }, + source: source.proto + '://' + source.authority + '/recordings', + icon: 'sound' +}