2020-07-08 02:13:51 +02:00
|
|
|
/* eslint-disable max-classes-per-file */
|
2018-09-16 05:56:13 +02:00
|
|
|
const Route = require('../../structures/Route');
|
|
|
|
const Util = require('../../utils/Util');
|
|
|
|
|
|
|
|
class albumsGET extends Route {
|
|
|
|
constructor() {
|
|
|
|
super('/albums/mini', 'get');
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:52:24 +01:00
|
|
|
async run(req, res, db, user) {
|
2018-09-16 05:56:13 +02:00
|
|
|
/*
|
|
|
|
Let's fetch the albums. This route will only return a small portion
|
|
|
|
of the album files for displaying on the dashboard. It's probably useless
|
2020-12-25 12:45:22 +01:00
|
|
|
for anyone consuming the API outside of the chibisafe frontend.
|
2018-09-16 05:56:13 +02:00
|
|
|
*/
|
2020-07-04 02:25:04 +02:00
|
|
|
const albums = await db
|
|
|
|
.table('albums')
|
2020-06-24 18:35:52 +02:00
|
|
|
.where('albums.userId', user.id)
|
2020-07-04 02:25:04 +02:00
|
|
|
.select('id', 'name', 'createdAt', 'editedAt')
|
|
|
|
.orderBy('createdAt', 'desc');
|
2018-09-16 05:56:13 +02:00
|
|
|
|
|
|
|
for (const album of albums) {
|
2020-06-24 18:35:52 +02:00
|
|
|
// Fetch the total amount of files each album has.
|
2020-07-08 02:13:51 +02:00
|
|
|
const fileCount = await db // eslint-disable-line no-await-in-loop
|
|
|
|
.table('albumsFiles')
|
2018-09-16 05:56:13 +02:00
|
|
|
.where('albumId', album.id)
|
|
|
|
.count({ count: 'id' });
|
|
|
|
|
2020-06-24 18:35:52 +02:00
|
|
|
// Fetch the file list from each album but limit it to 5 per album
|
2020-07-08 02:13:51 +02:00
|
|
|
const files = await db // eslint-disable-line no-await-in-loop
|
|
|
|
.table('albumsFiles')
|
2020-07-04 02:25:04 +02:00
|
|
|
.join('files', { 'files.id': 'albumsFiles.fileId' })
|
2018-09-16 05:56:13 +02:00
|
|
|
.where('albumId', album.id)
|
2020-07-04 02:25:04 +02:00
|
|
|
.select('files.id', 'files.name')
|
|
|
|
.orderBy('albumsFiles.id', 'desc')
|
2018-09-16 05:56:13 +02:00
|
|
|
.limit(5);
|
|
|
|
|
2020-06-24 18:35:52 +02:00
|
|
|
// Fetch thumbnails and stuff
|
2018-09-16 05:56:13 +02:00
|
|
|
for (let file of files) {
|
|
|
|
file = Util.constructFilePublicLink(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
album.fileCount = fileCount[0].count;
|
|
|
|
album.files = files;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
message: 'Successfully retrieved albums',
|
2020-12-24 09:40:50 +01:00
|
|
|
albums
|
2018-09-16 05:56:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class albumsDropdownGET extends Route {
|
|
|
|
constructor() {
|
2020-05-10 17:57:56 +02:00
|
|
|
super('/albums/dropdown', 'get', { canApiKey: true });
|
2018-09-16 05:56:13 +02:00
|
|
|
}
|
|
|
|
|
2019-02-19 15:52:24 +01:00
|
|
|
async run(req, res, db, user) {
|
2020-07-04 02:25:04 +02:00
|
|
|
const albums = await db
|
|
|
|
.table('albums')
|
2018-09-16 05:56:13 +02:00
|
|
|
.where('userId', user.id)
|
|
|
|
.select('id', 'name');
|
|
|
|
return res.json({
|
|
|
|
message: 'Successfully retrieved albums',
|
2020-12-24 09:40:50 +01:00
|
|
|
albums
|
2018-09-16 05:56:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = [albumsGET, albumsDropdownGET];
|