aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/albumsGET.js
blob: bbaa51873ab99b381c619d2eed66b142e6a1b194 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');

class albumsGET extends Route {
	constructor() {
		super('/albums/mini', 'get');
	}

	async run(req, res, db, user) {
		/*
			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
			for anyone consuming the API outside of the lolisafe frontend.
		*/
		const albums = await db.table('albums')
			.where('albums.userId', user.id)
			.select('id', 'name', 'editedAt');

		for (const album of albums) {
			// TODO: Optimize the shit out of this. Ideally a JOIN that grabs all the needed stuff in 1 query instead of 3

			// Fetch every public link the album has
			// const links = await db.table('links').where('albumId', album.id); // eslint-disable-line no-await-in-loop

			// Fetch the total amount of files each album has.
			const fileCount = await db.table('albumsFiles') // eslint-disable-line no-await-in-loop
				.where('albumId', album.id)
				.count({ count: 'id' });

			// Fetch the file list from each album but limit it to 5 per album
			const filesToFetch = await db.table('albumsFiles') // eslint-disable-line no-await-in-loop
				.where('albumId', album.id)
				.select('fileId')
				.orderBy('id', 'desc')
				.limit(5);

			// Fetch the actual files
			const files = await db.table('files') // eslint-disable-line no-await-in-loop
				.whereIn('id', filesToFetch.map(el => el.fileId))
				.select('id', 'name');

			// Fetch thumbnails and stuff
			for (let file of files) {
				file = Util.constructFilePublicLink(file);
			}

			album.fileCount = fileCount[0].count;
			album.files = files;
		}

		return res.json({
			message: 'Successfully retrieved albums',
			albums
		});
	}
}

class albumsDropdownGET extends Route {
	constructor() {
		super('/albums/dropdown', 'get', { canApiKey: true });
	}

	async run(req, res, db, user) {
		const albums = await db.table('albums')
			.where('userId', user.id)
			.select('id', 'name');
		return res.json({
			message: 'Successfully retrieved albums',
			albums
		});
	}
}

module.exports = [albumsGET, albumsDropdownGET];