combine.fm/routes/search.js

42 lines
997 B
JavaScript
Raw Normal View History

2015-08-20 23:22:57 +01:00
import { parse } from 'url';
2015-06-03 21:45:54 -07:00
import lookup from '../lib/lookup';
import services from '../lib/services';
import { find, create, findMatchesAsync } from '../lib/share';
2015-05-17 19:10:30 +01:00
2015-08-21 18:33:50 +01:00
export default function* () {
2015-08-20 23:22:57 +01:00
const url = parse(this.request.body.url);
2015-06-03 21:45:54 -07:00
this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
2015-06-03 21:45:54 -07:00
const music = yield lookup(this.request.body.url);
2015-06-03 21:45:54 -07:00
this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
2015-05-17 19:10:30 +01:00
let share = yield find(music);
2015-05-17 19:10:30 +01:00
if (!share) {
share = yield create(music);
findMatchesAsync(share);
2015-06-03 21:45:54 -07:00
}
share = share.toJSON();
2017-05-07 22:55:42 +01:00
share.id = share.externalId;
const unmatched = services.filter(service =>
!share.matches.find(match => match.service === service.id));
share.matches = share.matches.concat(unmatched.map((service) => {
return {
service: service.id,
matching: true,
};
}));
share.matches = share.matches.sort(a => !!a.externalId);
this.body = share;
}