Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 27 additions & 56 deletions decentralize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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' );
});
Expand All @@ -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();

});

Expand Down
37 changes: 34 additions & 3 deletions lib/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -90,6 +121,6 @@ Engine.prototype.sync = function( cb ) {
});
});
});
}
};

module.exports = Engine;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions resources/Index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
name: 'Index',
routes: { query: '/' },
templates: { query: 'index' },
requires: {
'Show': {
filter: {}
}
},
static: true,
internal: true
};
17 changes: 17 additions & 0 deletions resources/Show.js
Original file line number Diff line number Diff line change
@@ -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'
}