Code cleanup
This commit is contained in:
parent
a74ccfa575
commit
be00516521
18 changed files with 209 additions and 202 deletions
|
@ -1,18 +1,18 @@
|
|||
"use strict";
|
||||
|
||||
var React = require('react');
|
||||
var nodejsx = require('node-jsx').install({extension: '.jsx'});
|
||||
var Router = require('react-router');
|
||||
var routes = require('../views/app.jsx').routes;
|
||||
var React = require("react");
|
||||
var Router = require("react-router");
|
||||
require("node-jsx").install({extension: ".jsx"});
|
||||
var routes = require("../views/app.jsx").routes;
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
|
||||
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
||||
req.db.matches.find().sort({"created_at": -1}).limit(6).toArray().then(function(docs){
|
||||
var recents = [];
|
||||
docs.forEach(function(doc) {
|
||||
var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]});
|
||||
var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]; });
|
||||
shares.some(function(item) {
|
||||
if (item.service == doc._id.split("$$")[0]) {
|
||||
if (item.service === doc._id.split("$$")[0]) { // eslint-disable-line no-underscore-dangle
|
||||
recents.push(item);
|
||||
return false;
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ module.exports = function(req, res, next) {
|
|||
Router.run(routes, req.url, function (Handler) {
|
||||
var App = React.createFactory(Handler);
|
||||
var content = React.renderToString(new App({recents: recents}));
|
||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var recents = " + JSON.stringify(recents) + "</script></body></html>"));
|
||||
res.send("<!doctype html>\n" + content.replace("</body></html>", "<script>var recents = " + JSON.stringify(recents) + "</script></body></html>"));
|
||||
});
|
||||
}).catch(function(error) {
|
||||
next(error);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var request = require('superagent');
|
||||
var parse = require("url").parse;
|
||||
var request = require("superagent");
|
||||
|
||||
module.exports = function(req, res) {
|
||||
var url = "http://" + req.url.substr(8);
|
||||
|
|
|
@ -1,41 +1,40 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var path = require('path');
|
||||
var lookup = require('../lib/lookup');
|
||||
var services = require('../lib/services');
|
||||
var parse = require("url").parse;
|
||||
var lookup = require("../lib/lookup");
|
||||
var services = require("../lib/services");
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
module.exports = function(req, res) {
|
||||
var url = parse(req.body.url);
|
||||
if (!url.host) {
|
||||
return res.json({error:{message:"You need to submit a url."}});
|
||||
return res.json({error: {message: "You need to submit a url."}});
|
||||
}
|
||||
|
||||
|
||||
var promise = lookup(req.body.url);
|
||||
|
||||
|
||||
if (!promise) {
|
||||
return res.json({error: {message: "No supported music found at that link :("}});
|
||||
}
|
||||
|
||||
|
||||
promise.then(function(item) {
|
||||
if (!item) {
|
||||
return res.json({error: {message: "No supported music found at that link :("}});
|
||||
}
|
||||
item.matched_at = new Date();
|
||||
item.matched_at = new Date(); // eslint-disable-line camelcase
|
||||
var matches = {};
|
||||
matches[item.service] = item;
|
||||
services.forEach(function(service) {
|
||||
if (service.id == item.service) {
|
||||
if (service.id === item.service) {
|
||||
return;
|
||||
}
|
||||
matches[service.id] = {service: service.id};
|
||||
service.search(item).then(function(match) {
|
||||
match.matched_at = new Date();
|
||||
match.matched_at = new Date(); // eslint-disable-line camelcase
|
||||
var update = {};
|
||||
update["services." + match.service] = match;
|
||||
req.db.matches.update({_id: item.service + "$$" + item.id}, {"$set": update});
|
||||
});
|
||||
});
|
||||
return req.db.matches.save({_id: item.service + "$$" + item.id, created_at: new Date(), services:matches}).then(function() {
|
||||
return req.db.matches.save({_id: item.service + "$$" + item.id, "created_at": new Date(), services: matches}).then(function() {
|
||||
res.json(item);
|
||||
});
|
||||
}, function(error) {
|
||||
|
|
|
@ -1,25 +1,21 @@
|
|||
"use strict";
|
||||
var path = require('path');
|
||||
var Promise = require('bluebird');
|
||||
var util = require('util');
|
||||
|
||||
var browserify = require('connect-browserify');
|
||||
var React = require('react');
|
||||
var Router = require('react-router');
|
||||
var nodejsx = require('node-jsx').install();
|
||||
var routes = require('../views/app.jsx').routes;
|
||||
var React = require("react");
|
||||
var Router = require("react-router");
|
||||
require("node-jsx").install();
|
||||
var routes = require("../views/app.jsx").routes;
|
||||
|
||||
var services = require('../lib/services');
|
||||
var services = require("../lib/services");
|
||||
|
||||
var formatAndSort = function(matches, serviceId) {
|
||||
matches = Object.keys(matches).map(function (key) {return matches[key]});
|
||||
matches = Object.keys(matches).map(function (key) {return matches[key]; });
|
||||
matches.sort(function(a, b) {
|
||||
return a.id && !b.id;
|
||||
}).sort(function(a, b) {
|
||||
return a.service != serviceId;
|
||||
}).sort(function(a) {
|
||||
return a.service !== serviceId;
|
||||
});
|
||||
return matches;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
var serviceId = req.params.service;
|
||||
|
@ -28,50 +24,50 @@ module.exports = function(req, res, next) {
|
|||
|
||||
var matchedService;
|
||||
services.some(function(service) {
|
||||
matchedService = serviceId == service.id ? service : null;
|
||||
matchedService = serviceId === service.id ? service : null;
|
||||
return matchedService;
|
||||
});
|
||||
|
||||
if (!matchedService || (type != "album" && type != "track")) {
|
||||
if (!matchedService || (type !== "album" && type !== "track")) {
|
||||
return next();
|
||||
}
|
||||
|
||||
return req.db.matches.findOne({_id:serviceId + "$$" + itemId}).then(function(doc) {
|
||||
return req.db.matches.findOne({_id: serviceId + "$$" + itemId}).then(function(doc) {
|
||||
if (!doc) {
|
||||
return matchedService.lookupId(itemId, type).then(function(item) {
|
||||
var matches = {};
|
||||
item.matched_at = new Date();
|
||||
item.matched_at = new Date(); // eslint-disable-line camelcase
|
||||
matches[item.service] = item;
|
||||
services.forEach(function(service) {
|
||||
if (service.id == item.service) {
|
||||
if (service.id === item.service) {
|
||||
return;
|
||||
}
|
||||
matches[service.id] = {service: service.id};
|
||||
service.search(item).then(function(match) {
|
||||
match.matched_at = new Date();
|
||||
match.matched_at = new Date(); // eslint-disable-line camelcase
|
||||
var update = {};
|
||||
update["services." + match.service] = match;
|
||||
req.db.matches.update({_id: item.service + "$$" + item.id}, {"$set": update});
|
||||
});
|
||||
});
|
||||
return req.db.matches.save({_id: item.service + "$$" + item.id, created_at: new Date(), services:matches}).then(function() {
|
||||
var shares = formatAndSort(matches, serviceId);
|
||||
return req.db.matches.save({_id: item.service + "$$" + item.id, "created_at": new Date(), services: matches}).then(function() {
|
||||
var newShares = formatAndSort(matches, serviceId);
|
||||
Router.run(routes, req.url, function (Handler) {
|
||||
var App = React.createFactory(Handler);
|
||||
var content = React.renderToString(new App({shares: shares}));
|
||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
|
||||
var content = React.renderToString(new App({shares: newShares}));
|
||||
res.send("<!doctype html>\n" + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(newShares) + "</script></body></html>"));
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
var shares = formatAndSort(doc.services, serviceId);
|
||||
if (req.params.format == "json") {
|
||||
return res.json({shares:shares});
|
||||
if (req.params.format === "json") {
|
||||
return res.json({shares: shares});
|
||||
}
|
||||
Router.run(routes, req.url, function (Handler) {
|
||||
var App = React.createFactory(Handler);
|
||||
var content = React.renderToString(new App({shares: shares}));
|
||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
|
||||
res.send("<!doctype html>\n" + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
|
||||
});
|
||||
}).catch(function (error) {
|
||||
return next(error);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue