Update to Koa 2
This commit is contained in:
parent
42b6cd5a32
commit
87459b2acc
15 changed files with 467 additions and 419 deletions
|
@ -17,20 +17,18 @@ const recentQuery = {
|
|||
],
|
||||
};
|
||||
|
||||
export default function* () {
|
||||
const recentAlbums = yield models.album.findAll(recentQuery);
|
||||
const recentTracks = yield models.track.findAll(recentQuery);
|
||||
export default async function (ctx) {
|
||||
const recentAlbums = await models.album.findAll(recentQuery);
|
||||
const recentTracks = await models.track.findAll(recentQuery);
|
||||
|
||||
const initialState = {
|
||||
recents: recentAlbums.map(album => album.toJSON())
|
||||
const serviceList = services.map(service => service.id);
|
||||
const recents = recentAlbums.map(album => album.toJSON())
|
||||
.concat(recentTracks.map(track => track.toJSON()))
|
||||
.sort((a, b) => a.createdAt < b.createdAt).slice(0, 9),
|
||||
services: services.map(service => service.id),
|
||||
};
|
||||
.sort((a, b) => a.createdAt < b.createdAt).slice(0, 9);
|
||||
|
||||
const url = '/';
|
||||
|
||||
const html = yield render(url, initialState);
|
||||
const html = await render(url, { manifest: ctx.manifest, services: serviceList, recents });
|
||||
|
||||
const head = {
|
||||
title: 'Share Music',
|
||||
|
@ -39,10 +37,8 @@ export default function* () {
|
|||
share: false,
|
||||
};
|
||||
|
||||
this.set('Cache-Control', 'no-cache');
|
||||
|
||||
yield this.render('index', {
|
||||
initialState,
|
||||
await ctx.render('index', {
|
||||
initialState: { services: serviceList, recents },
|
||||
head,
|
||||
html,
|
||||
});
|
||||
|
|
|
@ -11,11 +11,11 @@ const recentQuery = {
|
|||
],
|
||||
};
|
||||
|
||||
export default function* () {
|
||||
const recentAlbums = yield models.album.findAll(recentQuery);
|
||||
const recentTracks = yield models.track.findAll(recentQuery);
|
||||
export default async function (ctx) {
|
||||
const recentAlbums = await models.album.findAll(recentQuery);
|
||||
const recentTracks = await models.track.findAll(recentQuery);
|
||||
|
||||
this.body = {
|
||||
ctx.body = {
|
||||
recents: recentAlbums.map(album => album.toJSON())
|
||||
.concat(recentTracks.map(track => track.toJSON()))
|
||||
.sort((a, b) => a.createdAt < b.createdAt).slice(0, 9),
|
||||
|
|
|
@ -13,20 +13,20 @@ const queue = kue.createQueue({
|
|||
redis: process.env.REDIS_URL,
|
||||
});
|
||||
|
||||
export default function* () {
|
||||
export default async function (ctx) {
|
||||
try {
|
||||
const url = parse(this.request.body.url);
|
||||
debug(`URL ${url.href}`);
|
||||
this.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
|
||||
const url = parse(ctx.request.body.url);
|
||||
|
||||
const music = yield lookup(this.request.body.url);
|
||||
ctx.assert(url.host, 400, { error: { message: 'You need to submit a url.' } });
|
||||
|
||||
this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
|
||||
const music = await lookup(ctx.request.body.url);
|
||||
|
||||
let share = yield find(music);
|
||||
ctx.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
|
||||
|
||||
let share = await find(music);
|
||||
|
||||
if (!share) {
|
||||
share = yield create(music);
|
||||
share = await create(music);
|
||||
|
||||
services.forEach((service) => {
|
||||
if (service.id !== share.service) {
|
||||
|
@ -56,10 +56,12 @@ export default function* () {
|
|||
|
||||
share.matches = share.matches.sort(a => !!a.externalId);
|
||||
|
||||
this.body = share;
|
||||
} catch (e) {
|
||||
debug(inspect(e, {showHidden: false, depth: null}));
|
||||
this.throw(400, { error: { message: 'Unexpected error looking up music. Please try again later.' } });
|
||||
throw e;
|
||||
ctx.body = share;
|
||||
} catch (err) {
|
||||
if (err.name === 'BadRequestError') {
|
||||
throw err;
|
||||
}
|
||||
debug(inspect(err, {showHidden: false, depth: null}));
|
||||
ctx.throw(400, { error: { message: 'Unexpected error looking up music. Please try again later.' } });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,10 +12,10 @@ const queue = kue.createQueue({
|
|||
redis: process.env.REDIS_URL,
|
||||
});
|
||||
|
||||
export default function* (serviceId, type, itemId, format) {
|
||||
this.assert(type === 'album' || type === 'track', 400, { error: 'Invalid type' });
|
||||
export default async function (ctx, serviceId, type, itemId, format) {
|
||||
ctx.assert(type === 'album' || type === 'track', 400, { error: 'Invalid type' });
|
||||
|
||||
let share = yield models[type].findOne({
|
||||
let share = await models[type].findOne({
|
||||
where: {
|
||||
externalId: itemId,
|
||||
},
|
||||
|
@ -27,14 +27,14 @@ export default function* (serviceId, type, itemId, format) {
|
|||
|
||||
if (!share) {
|
||||
const matchedService = services.find(service => serviceId === service.id);
|
||||
const music = yield matchedService.lookupId(itemId, type);
|
||||
const music = await matchedService.lookupId(itemId, type);
|
||||
|
||||
this.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
|
||||
ctx.assert(music, 400, { error: { message: 'No supported music found at that link :(' } });
|
||||
|
||||
share = yield find(music);
|
||||
share = await find(music);
|
||||
|
||||
if (!share) {
|
||||
share = yield create(music);
|
||||
share = await create(music);
|
||||
|
||||
services.forEach((service) => {
|
||||
if (service.id !== share.service) {
|
||||
|
@ -49,7 +49,7 @@ export default function* (serviceId, type, itemId, format) {
|
|||
}
|
||||
}
|
||||
|
||||
this.assert(share, 404);
|
||||
ctx.assert(share, 404);
|
||||
|
||||
const unmatched = services.filter(service =>
|
||||
!share.matches.find(match => match.service === service.id));
|
||||
|
@ -64,7 +64,7 @@ export default function* (serviceId, type, itemId, format) {
|
|||
share.matches = share.matches.sort(a => !a.externalId);
|
||||
|
||||
if (format === 'json') {
|
||||
this.body = share;
|
||||
ctx.body = share;
|
||||
} else {
|
||||
const initialState = {
|
||||
item: share,
|
||||
|
@ -74,16 +74,16 @@ export default function* (serviceId, type, itemId, format) {
|
|||
|
||||
const url = `/${serviceId}/${type}/${itemId}`;
|
||||
|
||||
const html = yield render(url, initialState);
|
||||
const html = await render(url, initialState);
|
||||
|
||||
const head = {
|
||||
share,
|
||||
title: `${share.name} by ${share.artist.name}`,
|
||||
shareUrl: `${this.request.origin}${url}`,
|
||||
shareUrl: `${ctx.request.origin}${url}`,
|
||||
image: share.matches.find(el => el.service === share.service).artworkLarge,
|
||||
};
|
||||
|
||||
yield this.render('index', {
|
||||
await ctx.render('index', {
|
||||
initialState,
|
||||
share,
|
||||
head,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue