combine.fm/routes/share.js

77 lines
2.8 KiB
JavaScript
Raw Normal View History

"use strict";
2014-12-15 12:49:17 +00:00
2015-05-17 19:10:30 +01:00
var React = require("react");
var Router = require("react-router");
require("node-jsx").install();
var routes = require("../views/app.jsx").routes;
2014-12-15 12:49:17 +00:00
2015-05-17 19:10:30 +01:00
var services = require("../lib/services");
var formatAndSort = function(matches, serviceId) {
2015-05-17 19:10:30 +01:00
matches = Object.keys(matches).map(function (key) {return matches[key]; });
matches.sort(function(a, b) {
return a.id && !b.id;
2015-05-17 19:10:30 +01:00
}).sort(function(a) {
return a.service !== serviceId;
});
return matches;
2015-05-17 19:10:30 +01:00
};
module.exports = function(req, res, next) {
var serviceId = req.params.service;
var type = req.params.type;
var itemId = req.params.id;
2015-01-06 12:58:57 +00:00
var matchedService;
services.some(function(service) {
2015-05-17 19:10:30 +01:00
matchedService = serviceId === service.id ? service : null;
2015-01-06 12:58:57 +00:00
return matchedService;
});
2014-12-05 17:40:07 +00:00
2015-05-17 19:10:30 +01:00
if (!matchedService || (type !== "album" && type !== "track")) {
2015-01-06 12:58:57 +00:00
return next();
}
2015-05-17 19:10:30 +01:00
return req.db.matches.findOne({_id: serviceId + "$$" + itemId}).then(function(doc) {
if (!doc) {
return matchedService.lookupId(itemId, type).then(function(item) {
var matches = {};
2015-05-17 19:10:30 +01:00
item.matched_at = new Date(); // eslint-disable-line camelcase
matches[item.service] = item;
services.forEach(function(service) {
2015-05-17 19:10:30 +01:00
if (service.id === item.service) {
return;
}
matches[service.id] = {service: service.id};
service.search(item).then(function(match) {
2015-05-17 19:10:30 +01:00
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});
});
});
2015-05-17 19:10:30 +01:00
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);
2015-05-17 19:10:30 +01:00
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>"));
});
});
2015-05-17 19:10:30 +01:00
});
}
var shares = formatAndSort(doc.services, serviceId);
2015-05-17 19:10:30 +01:00
if (req.params.format === "json") {
return res.json({shares: shares});
}
2015-01-06 12:58:57 +00:00
Router.run(routes, req.url, function (Handler) {
var App = React.createFactory(Handler);
var content = React.renderToString(new App({shares: shares}));
2015-05-17 19:10:30 +01:00
res.send("<!doctype html>\n" + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
});
2015-01-06 12:58:57 +00:00
}).catch(function (error) {
return next(error);
});
2015-01-06 12:58:57 +00:00
};