aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/albumsGET.js
blob: 98cc82eecf63ef9d6d40312b992ba8d0d6baf608 (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
/* eslint-disable max-classes-per-file */
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 chibisafe frontend.
		*/
		const albums = await db
			.table('albums')
			.where('albums.userId', user.id)
			.select('id', 'name', 'nsfw', 'createdAt', 'editedAt')
			.orderBy('createdAt', 'desc');

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

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

			// Fetch thumbnails and stuff
			for (let file of files) {
				file = Util.constructFilePublicLink(req, 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];